Employee.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Define a Fiche employé
  4. * @author Administrateur PRINCIPAL
  5. * @category Plugin
  6. * @license copyright
  7. */
  8. class Employee extends Entity{
  9. public $id;
  10. public $photo; //Photo (Image)
  11. public $birthName; //Nom de naissance (Texte)
  12. public $name; //Nom marital (Texte)
  13. public $firstname; //Prénom (Texte)
  14. public $job; //Poste (Liste configurable)
  15. public $jobDescription; //Rôle/Missions (Texte enrichis)
  16. public $manager; //Responsable (Employee)
  17. public $account; //Responsable (Utilisateur)
  18. public $workplace; //Lieu de travail (Adresse)
  19. public $hardware; //Mise a disposition de matériel (Liste configurable)
  20. public $comment; //Comment (Wysiwyg)
  21. protected $TABLE_NAME = 'employee';
  22. public $entityLabel = 'Fiche employé';
  23. public $fields = array(
  24. 'id' => array('type'=>'key', 'label' => 'Identifiant'),
  25. 'birthName' => array('type'=>'text', 'label' => 'Nom de naissance'),
  26. 'name' => array('type'=>'text', 'label' => 'Nom marital'),
  27. 'firstname' => array('type'=>'text', 'label' => 'Prénom'),
  28. 'job' => array('type'=>'dictionnary', 'label' => 'Poste'),
  29. 'jobDescription' => array('type'=>'wysiwyg', 'label' => 'Rôle/Missions'),
  30. 'manager' => array('type'=>'integer','label' => 'Responsable','link'=>'plugin/employee/Employee.class.php'),
  31. 'account' => array('type'=>'user','label' => 'Compte lié'),
  32. 'workplace' => array('type'=>'address', 'label' => 'Lieu de travail'),
  33. 'hardware' => array('type'=>'longstring', 'label' => 'Mise a disposition de matériel'),
  34. 'comment' => array('type'=>'wysiwyg', 'label' => 'Information sur le recrutement'),
  35. );
  36. public function picture(){
  37. $pictures = glob(File::dir().'employee/employee/'.$this->id.'/photo.*');
  38. return !empty($pictures) ? $pictures[0] : '';
  39. }
  40. //Surchage de toArray pour prendre en compte le fullName et les initials régulierement utilisé en toArray
  41. public function toArray($decoded=false) {
  42. $fields = parent::toArray($decoded);
  43. $fields['fullName'] = $this->fullName();
  44. if($decoded){
  45. $fields['fullName'] = html_entity_decode($fields['fullName']);
  46. }
  47. return $fields;
  48. }
  49. public function fullName(){
  50. return ucfirst(mb_strtolower($this->firstname)).' '.mb_strtoupper($this->name);
  51. }
  52. //managers récursifs de l'employé ciblé
  53. public static function managers($employee){
  54. $alloweds = array();
  55. $alloweds[] = $employee;
  56. if(!empty($employee->manager) && $employee->manager!=$employee->id){
  57. $alloweds = array_merge($alloweds,self::managers(Employee::getById($employee->manager)));
  58. }
  59. return $alloweds;
  60. }
  61. //suboordonés récursifs de l'employé ciblé
  62. public static function subordinates($employee,$employees = null,$recursive = true){
  63. $subordinates = array();
  64. if(!isset($employee) || empty($employee->id)) return $subordinates;
  65. if(!isset($employees)){
  66. $employees = array();
  67. foreach(self::loadAll() as $item){
  68. if(!isset($employees[$item->manager])) $employees[$item->manager] = array();
  69. $employees[$item->manager][$item->id] = $item;
  70. }
  71. }
  72. $currentSubordinates = isset($employees[$employee->id]) ? $employees[$employee->id] : array();
  73. foreach($currentSubordinates as $subordinate){
  74. $subordinates[$subordinate->id] = $subordinate;
  75. if($recursive) $subordinates = array_merge($subordinates,self::subordinates($subordinate,$employees));
  76. }
  77. return $subordinates;
  78. }
  79. public function can($targetEmployee,$crud){
  80. //Droits de visibilité sur la fiche :
  81. // - L'admin configure dans tous les cas
  82. // - Le créateur de la fiche si pas de manager renseigné
  83. // - Le manager si renseigné
  84. $user = User::byLogin($this->account);
  85. if($user->can('employee','configure')) return true;
  86. if(!$user->can('employee',$crud)) return false;
  87. $hierarchy = array();
  88. foreach(Employee::subordinates($this) as $subordinate){
  89. $subordinatesIds[] = $subordinate->id;
  90. }
  91. $subordinatesIds[] = $this->id;
  92. if(empty($targetEmployee->manager) && $targetEmployee->creator==$user->login) return true;
  93. if(!in_array($targetEmployee->manager,$subordinatesIds)) return false;
  94. return true;
  95. }
  96. public function currentContract(){
  97. require_once(__DIR__.SLASH.'EmployeeContract.class.php');
  98. require_once(__DIR__.SLASH.'EmployeeWorkTime.class.php');
  99. $contract = new EmployeeContract();
  100. $contracts = EmployeeContract::staticQuery('SELECT main.*,'.EmployeeWorkTime::joinString('wt').' FROM {{table}} main LEFT JOIN '.EmployeeWorkTime::tableName().' wt ON main.worktime = wt.id WHERE (main.`end` IS NULL OR main.`end` = "" OR main.`end` > ?) AND main.employee=? LIMIT 1',array(time(),$this->id),true,1);
  101. if(isset($contracts[0])) $contract = $contracts[0];
  102. return $contract;
  103. }
  104. public function photo(){
  105. if(count(glob(File::dir().SLASH.'employee'.SLASH.'employee'.SLASH.$this->id.SLASH.'photo.*')) ==0 ) return 'img/default-avatar.png';
  106. return 'action.php?action=employee_employee_photo&type=download&path='.base64_encode('employee/employee/'.$this->id.'/photo.*');
  107. }
  108. //Colonnes indexées
  109. public $indexes = array();
  110. }
  111. ?>