action.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /** CONTACTPERSON / CONTACT **/
  3. //Récuperation d'une liste de contact
  4. Action::register('contact_contact_person_search',function(&$response){
  5. global $_;
  6. User::check_access('contact','read');
  7. // OPTIONS DE RECHERCHE, A ACTIVER POUR UNE RECHERCHE AVANCEE
  8. $query = 'SELECT * FROM '.ContactPerson::tableName().' WHERE 1';
  9. $data = array();
  10. //Recherche simple
  11. if(!empty($_['filters']['keyword'])){
  12. $query .= ' AND (name LIKE ? OR firstname LIKE ?)';
  13. $data[] = '%'.$_['filters']['keyword'].'%';
  14. $data[] = '%'.$_['filters']['keyword'].'%';
  15. }
  16. //Recherche avancée
  17. if(isset($_['filters']['advanced'])) filter_secure_query($_['filters']['advanced'],array('name','firstname','job','civility','state','phone','mail','scope','uid','comment','tag'),$query,$data);
  18. //Tri des colonnes
  19. if(isset($_['sort'])) sort_secure_query($_['sort'],array('name','firstname','job','civility','state','phone','mail','scope','uid','tag'),$query,$data);
  20. $pageNumber = 20;
  21. if($_['export'] == 'true') $pageNumber = 5000;
  22. //Pagination
  23. $response['pagination'] = ContactPerson::paginate($pageNumber,(!empty($_['page'])?$_['page']:0),$query,$data);
  24. $contactpersons = ContactPerson::staticQuery($query,$data,true,0);
  25. $ids = array();
  26. foreach($contactpersons as $contactperson)
  27. $ids[] = $contactperson->id;
  28. $contactInfos = array();
  29. if(!empty($ids)){
  30. foreach (Contact::loadAll(array('scope'=>'contact_person','uid:IN'=>$ids)) as $contact) {
  31. if(empty($contactInfos[$contact->uid])) $contactInfos[$contact->uid] = array();
  32. $contactInfos[$contact->uid][] = $contact;
  33. }
  34. }
  35. $response['rows'] = array();
  36. foreach($contactpersons as $contactperson){
  37. $row = $contactperson->toArray();
  38. $row['civility'] = ContactPerson::civilities($row['civility']);
  39. $row['fullname'] = $contactperson->fullName();
  40. $row['phones'] = array();
  41. $row['mails'] = array();
  42. $row['tag'] = $contactperson->getTags();
  43. $row['hasTag'] = !empty($row['tag']);
  44. $row['rawphones'] = array();
  45. $row['rawmails'] = array();
  46. if(!empty($contactInfos[$contactperson->id])){
  47. foreach ($contactInfos[$contactperson->id] as $key => $contact) {
  48. if(in_array($contact->type ,array(Contact::PHONE,Contact::MOBILE))){
  49. $row['phones'][] = $contact->toArray();
  50. $row['rawphones'][] = $contact->value;
  51. }else if(in_array($contact->type ,array(Contact::PERSONAL_MAIL,Contact::PROFESSIONAL_MAIL))){
  52. $row['mails'][] = $contact->toArray();
  53. $row['rawmails'][] = $contact->value;
  54. }
  55. }
  56. }
  57. if($_['export'] == 'true'){
  58. $row['rawphones'] = implode(', ',$row['rawphones']);
  59. $row['rawmails'] = implode(', ',$row['rawmails']);
  60. $row['created'] = date('d-m-Y',$row['created']);
  61. $row['updated'] = date('d-m-Y',$row['updated']);
  62. $row['name'] = html_entity_decode($row['name']);
  63. $row['firstname'] = html_entity_decode($row['firstname']);
  64. $row['job'] = html_entity_decode($row['job']);
  65. $row['tag'] = implode(',',$row['tag']);
  66. $row['comment'] = html_entity_decode($row['comment']);
  67. }
  68. $row['avatar'] = $row['avatar'] = 'action.php?action=contact_avatar_load&path='.base64_encode($contactperson->avatar());
  69. $response['rows'][] = $row;
  70. }
  71. /* Mode export */
  72. if($_['export'] == 'true'){
  73. $fieldsMapping = array();
  74. foreach (ContactPerson::fields(false) as $key => $value)
  75. $fieldsMapping[$value['label']] = $key;
  76. $fieldsMapping['Téléphones'] = 'rawphones';
  77. $fieldsMapping['E-mails'] = 'rawmails';
  78. $stream = Excel::exportArray($response['rows'],$fieldsMapping ,'Export');
  79. File::downloadStream($stream,'export-contacts-'.date('d-m-Y').'.xlsx');
  80. exit();
  81. }
  82. });
  83. Action::register('contact_contact_search',function(&$response){
  84. global $_;
  85. User::check_access('contact','read');
  86. $response['rows'] = array();
  87. foreach (Contact::loadAll(array('scope'=>$_['scope'],'uid'=>$_['uid'])) as $key => $contact) {
  88. $response['rows'][] = $contact->toArray();
  89. }
  90. });
  91. //Ajout ou modification d'élément contact
  92. Action::register('contact_contact_person_save',function(&$response){
  93. global $_,$myFirm;
  94. User::check_access('contact','edit');
  95. $item = ContactPerson::provide();
  96. $oldItem = clone $item;
  97. $item->name = $_['name'];
  98. $item->firstname = $_['firstname'];
  99. $item->job = $_['job'];
  100. $item->civility = $_['civility'];
  101. $item->setTags($_['tag']);
  102. $item->state = ContactPerson::ACTIVE;
  103. //$item->phone = $_['phone'];
  104. //$item->mail = $_['mail'];
  105. $item->scope = $_['scope'];
  106. $item->uid = $_['uid'];
  107. $item->save();
  108. History::entityChange('contactperson',$oldItem,$item);
  109. if(empty($item->uid)){
  110. $item->uid = $item->id;
  111. $item->save();
  112. }
  113. //Gestion des champs dynamiques
  114. if($myFirm->has_plugin('fr.core.dynamicform')){
  115. Plugin::need('dynamicform/DynamicForm');
  116. Dynamicform::record('contact-form',array(
  117. 'scope'=>'contact_person',
  118. 'uid'=>$item->id
  119. ),$_);
  120. }
  121. Contact::delete(array('scope'=>$item->scope,'uid'=>$item->uid));
  122. if(!empty($_['contacts'])){
  123. $contacts = json_decode(base64_decode($_['contacts']),true);
  124. foreach ($contacts as $line) {
  125. $contact = new Contact();
  126. $contact->scope = 'contact_person';
  127. $contact->uid = $item->id;
  128. $contact->type = $line["type"];
  129. $contact->value = $line["value"];
  130. $contact->save();
  131. }
  132. }
  133. $response = $item->toArray();
  134. });
  135. Action::register('contact_avatar_load',function(&$response){
  136. global $myUser,$_;
  137. User::check_access('contact','read');
  138. $_['path'] = str_replace("..", "", base64_decode($_['path']));
  139. File::downloadFile($_['path']);
  140. });
  141. //Suppression d'élement contact
  142. Action::register('contact_contact_person_delete',function(&$response){
  143. global $_;
  144. User::check_access('contact','delete');
  145. if(empty($_['id']) || !is_numeric($_['id'])) throw new Exception("Identifiant incorrect");
  146. ContactPerson::deleteById($_['id']);
  147. });
  148. //Sauvegarde des configurations de Annuaire de contacts
  149. Action::register('contact_setting_save',function(&$response){
  150. global $_,$conf;
  151. User::check_access('contact','configure');
  152. //Si input file "multiple", possibilité de normaliser le
  153. //tableau $_FILES récupéré avec la fonction => normalize_php_files();
  154. foreach(Configuration::setting('contact') as $key=>$value){
  155. if(!is_array($value)) continue;
  156. $allowed[] = $key;
  157. }
  158. foreach ($_['fields'] as $key => $value) {
  159. if(in_array($key, $allowed))
  160. $conf->put($key,$value);
  161. }
  162. });
  163. ?>