Employee.class.php 5.2 KB

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