ContactPerson.class.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Define a person contact.
  4. * @author Valentin carrueso
  5. * @category Plugin
  6. * @license MIT
  7. */
  8. class ContactPerson extends Entity{
  9. public $id;
  10. public $name; //Nom (Texte)
  11. public $firstname; //Prénom (Texte)
  12. public $job; //Fonction (Texte)
  13. public $account; //Compte lié (uniqment si référent interne)
  14. public $civility; //Civilité (Texte)
  15. public $state; //Etat (Texte)
  16. public $type; //Type (Texte)
  17. public $phone; //Téléphone (Texte)
  18. public $mobile; //Téléphone Mobile (Texte)
  19. public $mail; //E-mail (Texte)
  20. public $scope; //Scope (Texte)
  21. public $comment; //Commentaire (Texte long)
  22. public $uid; //UID (Entier)
  23. public $meta = array();
  24. public $tag; //Etiquettes (Etiquettes)
  25. const TYPE_EXTERNAL = "external";
  26. const TYPE_CLIENT = "client";
  27. const TYPE_USER = "user";
  28. public $entityLabel = 'Contact';
  29. protected $TABLE_NAME = 'contact_person';
  30. public $fields = array(
  31. 'id' => array('type'=>'key', 'label' => 'Identifiant'),
  32. 'name' => array('type'=>'text', 'label' => 'Nom'),
  33. 'firstname' => array('type'=>'text', 'label' => 'Prénom'),
  34. 'job' => array('type'=>'text', 'label' => 'Métier/Fonction'),
  35. 'account' => array('label'=>'Compte utilisateur lié','type'=>'user'),
  36. 'civility' => array('type'=>'list', 'label' => 'Civilité'),
  37. 'state' => array('type'=>'text', 'label' => 'Etat'),
  38. 'type' => array('type'=>'text', 'label' => 'Type'),
  39. //'phone' => array('type'=>'phone', 'label' => 'Téléphone'),
  40. //'mail' => array('type'=>'mail', 'label' => 'Email'),
  41. 'scope' => array('type'=>'text', 'label' => 'Périmetre'),
  42. 'comment' => array('type'=>'textarea', 'label' => 'Commentaire'),
  43. 'uid' => array('type'=>'integer', 'label' => 'Identifiant de périmetre'),
  44. 'tag' => array('type'=>'tag', 'label' => 'Etiquettes')
  45. );
  46. //Colonnes indexées
  47. public $indexes = array('uid','scope');
  48. //remplis les métas (phones, mail principaux, phone et mails secondaires) par types si spécifiés
  49. public function meta($types = array(),$defaultOnly = false ){
  50. $filters = array('scope'=>'contact_person','uid'=>$this->id);
  51. if(!empty($types)) $filters['type:IN'] = implode(',',$types);
  52. if($defaultOnly) $filters['label'] = '';
  53. foreach(Contact::loadAll($filters) as $contact){
  54. if($contact->type==Contact::PHONE && $contact->label=='') $this->phone = $contact->value;
  55. if($contact->type==Contact::MOBILE && $contact->label=='') $this->mobile = $contact->value;
  56. if($contact->type=="mail" && $contact->label=='') $this->mail = $contact->value;
  57. if(!isset($this->meta[$contact->type])) $this->meta[$contact->type] = array();
  58. $this->meta[$contact->type][] = $contact;
  59. }
  60. }
  61. public function getTags(){
  62. $tags = explode(',', $this->tag);
  63. return array_values(array_filter($tags));
  64. }
  65. public function setTags($tags){
  66. if(is_array($tags)){
  67. $tags = array_filter($tags);
  68. $tags = implode(',', $tags);
  69. }
  70. $this->tag = ','.$tags.',';
  71. }
  72. public function toArray($decoded = false){
  73. $fields = parent::toArray();
  74. $fields['phone'] = $this->phone;
  75. $fields['mobile'] = $this->mobile;
  76. $fields['mail'] = $this->mail;
  77. $fields['meta'] = $this->meta;
  78. return $fields;
  79. }
  80. //récupere la liste d'entité et leurs entitées par défaut liées (téléphone et mail)
  81. public static function getAll($parameters = array()){
  82. $query = '';
  83. $data = array();
  84. $query .= 'SELECT *,';
  85. $query .= '(SELECT value FROM '.Contact::tableName().' c WHERE c.type=? AND c.scope=? AND c.uid={{table}}.id AND (c.label=? OR c.label IS NULL) LIMIT 1) as phone';
  86. $query .= ',(SELECT value FROM '.Contact::tableName().' c WHERE c.type=? AND c.scope=? AND c.uid={{table}}.id AND (c.label=? OR c.label IS NULL) LIMIT 1) as mobile';
  87. $query .= ',(SELECT value FROM '.Contact::tableName().' c WHERE c.type=? AND c.scope=? AND c.uid={{table}}.id AND (c.label=? OR c.label IS NULL) LIMIT 1) as mail';
  88. $query .= ' FROM {{table}}';
  89. $data[] = Contact::PHONE;
  90. $data[] = 'contact_person';
  91. $data[] = '';
  92. $data[] = Contact::MOBILE;
  93. $data[] = 'contact_person';
  94. $data[] = '';
  95. $data[] = 'mail';
  96. $data[] = 'contact_person';
  97. $data[] = '';
  98. $query .= ' WHERE 1 ';
  99. if(!empty($parameters['where'])){
  100. foreach($parameters['where'] as $key=>$value){
  101. $query .= ' AND '.$key.' = ? ';
  102. $data[] = $value;
  103. }
  104. }
  105. $rows = array();
  106. foreach(self::staticQuery($query,$data,true) as $item){
  107. $item->phone = $item->foreign('phone');
  108. $item->mobile = $item->foreign('mobile');
  109. $item->mail = $item->foreign('mail');
  110. $rows[] = $item;
  111. }
  112. return $rows;
  113. }
  114. //liste des Civilité possibles
  115. public static function civilities($key=null){
  116. $items = array(
  117. 'm' => array('label'=>'M.'),
  118. 'mme' => array('label'=>'Mme.')
  119. );
  120. if(!isset($key)) return $items;
  121. return isset($items[$key]) ? $items[$key] : array('label'=>'Non définis');
  122. }
  123. public static function remove($id){
  124. if(!isset($id)) throw new Exception("ID se suppression non spécifié");
  125. self::deleteById($id);
  126. Contact::delete(array('scope'=>'contact_person','uid'=>$id));
  127. }
  128. //Sauvegarde l'entité et ses entitées de contact liées (téléphones, mail...)
  129. public function save_all(){
  130. parent::save();
  131. $currentMeta = array('phone'=>$this->phone,'mobile'=>$this->mobile,'mail'=>$this->mail);
  132. //Enregistrement des mails et téléphones princpaux dans la table de contacts (si renseignés)
  133. if(!isset($this->phone) && !isset($this->mobile) && !isset($this->mail)) return;
  134. $metaContacts = array('phone','mail','mobile');
  135. //récuperation des contacts par defaut existants pour cette personne
  136. $this->meta($metaContacts,true);
  137. foreach ($metaContacts as $metaContact) {
  138. //si le champ est renseigné
  139. if(isset($currentMeta[$metaContact])){
  140. //on récupere le champ existant ou on en créé un nouveau
  141. $contact = !empty($this->meta[$metaContact]) ? $this->meta[$metaContact][0] : new Contact();
  142. //on ne save que si la valeur a changée
  143. if($contact->value != $currentMeta[$metaContact]){
  144. $contact->value = $currentMeta[$metaContact];
  145. $contact->scope = 'contact_person';
  146. $contact->uid = $this->id;
  147. $contact->type = $metaContact;
  148. $contact->label = '';
  149. $contact->save();
  150. }
  151. }
  152. }
  153. }
  154. public function avatar(){
  155. $mapping = array(
  156. '/pdg|directeur|dg/is' => 'job-direction.png',
  157. '/commercial(e)?|vente/is' => 'job-sales.png',
  158. '/communication|marketing|social/is' => 'job-communication.png',
  159. '/ingénieur|engineer|architect(e)?|recherche/is' => 'job-engineer.png',
  160. '/support|secrétaire|hotline/is' => 'job-support.png',
  161. '/ressources humaines|RH|HR/is' => 'job-rh.png',
  162. '/financ(e|ière|ier)|monnaie|comptab(le|bilitée)/is' => 'job-money.png',
  163. '/logistique|stock|atelier|livr(eur|aison)/is' => 'job-logistic.png',
  164. );
  165. $path = __ROOT__.SLASH.'img'.SLASH.'contact'.SLASH.'default-contact.png';
  166. foreach ($mapping as $regex => $relatedPath) {
  167. if( preg_match($regex,$this->job) ){
  168. $path = __ROOT__.SLASH.'img'.SLASH.'contact'.SLASH.$relatedPath;
  169. }
  170. }
  171. return $path;
  172. }
  173. public function fullName(){
  174. return ucfirst($this->firstname).' '.strtoupper($this->name);
  175. }
  176. public function toReadable(){
  177. $text = $this->civility.' '.$this->fullName();
  178. if(isset($this->phone) && !empty($this->phone)) $text .=' Tel: '.$this->phone;
  179. if(isset($this->mail) && !empty($this->mail)) $text .=' Mail : '.$this->mail;
  180. return $text;
  181. }
  182. }
  183. ?>