action.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. foreach($contactpersons as $contactperson){
  36. $row = $contactperson->toArray();
  37. $row['civility'] = ContactPerson::civilities($row['civility']);
  38. $row['fullname'] = $contactperson->fullName();
  39. $row['phones'] = array();
  40. $row['mails'] = array();
  41. $row['tag'] = $contactperson->getTags();
  42. $row['hasTag'] = !empty($row['tag']);
  43. $row['rawphones'] = array();
  44. $row['rawmails'] = array();
  45. if(!empty($contactInfos[$contactperson->id])){
  46. foreach ($contactInfos[$contactperson->id] as $key => $contact) {
  47. if(in_array($contact->type ,array(Contact::PHONE,Contact::MOBILE))){
  48. $row['phones'][] = $contact->toArray();
  49. $row['rawphones'][] = $contact->value;
  50. }else if(in_array($contact->type ,array(Contact::PERSONAL_MAIL,Contact::PROFESSIONAL_MAIL))){
  51. $row['mails'][] = $contact->toArray();
  52. $row['rawmails'][] = $contact->value;
  53. }
  54. }
  55. }
  56. if($_['export'] == 'true'){
  57. $row['rawphones'] = implode(', ',$row['rawphones']);
  58. $row['rawmails'] = implode(', ',$row['rawmails']);
  59. $row['created'] = date('d-m-Y',$row['created']);
  60. $row['updated'] = date('d-m-Y',$row['updated']);
  61. $row['name'] = html_entity_decode($row['name']);
  62. $row['firstname'] = html_entity_decode($row['firstname']);
  63. $row['job'] = html_entity_decode($row['job']);
  64. $row['tag'] = implode(',',$row['tag']);
  65. $row['comment'] = html_entity_decode($row['comment']);
  66. }
  67. $row['avatar'] = $row['avatar'] = 'action.php?action=contact_avatar_load&path='.base64_encode($contactperson->avatar());
  68. $response['rows'][] = $row;
  69. }
  70. /* Mode export */
  71. if($_['export'] == 'true'){
  72. $fieldsMapping = array();
  73. foreach (ContactPerson::fields(false) as $key => $value)
  74. $fieldsMapping[$value['label']] = $key;
  75. $fieldsMapping['Téléphones'] = 'rawphones';
  76. $fieldsMapping['E-mails'] = 'rawmails';
  77. $stream = Excel::exportArray($response['rows'],$fieldsMapping ,'Export');
  78. File::downloadStream($stream,'export-contacts-'.date('d-m-Y').'.xlsx');
  79. exit();
  80. }
  81. });
  82. Action::register('contact_contact_search',function(&$response){
  83. global $_;
  84. User::check_access('contact','read');
  85. $response['rows'] = array();
  86. foreach (Contact::loadAll(array('scope'=>$_['scope'],'uid'=>$_['uid'])) as $key => $contact) {
  87. $response['rows'][] = $contact->toArray();
  88. }
  89. });
  90. //Ajout ou modification d'élément contact
  91. Action::register('contact_contact_person_save',function(&$response){
  92. global $_,$myFirm;
  93. User::check_access('contact','edit');
  94. $item = ContactPerson::provide();
  95. $oldItem = clone $item;
  96. $item->name = $_['name'];
  97. $item->firstname = $_['firstname'];
  98. $item->job = $_['job'];
  99. $item->civility = $_['civility'];
  100. $item->setTags($_['tag']);
  101. $item->state = ContactPerson::ACTIVE;
  102. //$item->phone = $_['phone'];
  103. //$item->mail = $_['mail'];
  104. $item->scope = $_['scope'];
  105. $item->uid = $_['uid'];
  106. $item->save();
  107. History::entityChange('contactperson',$oldItem,$item);
  108. if(empty($item->uid)){
  109. $item->uid = $item->id;
  110. $item->save();
  111. }
  112. //Gestion des champs dynamiques
  113. if($myFirm->has_plugin('fr.core.dynamicform')){
  114. Plugin::need('dynamicform/DynamicForm');
  115. Dynamicform::record('contact-form',array(
  116. 'scope'=>'contact_person',
  117. 'uid'=>$item->id
  118. ),$_);
  119. }
  120. Contact::delete(array('scope'=>$item->scope,'uid'=>$item->uid));
  121. if(!empty($_['contacts'])){
  122. $contacts = json_decode(base64_decode($_['contacts']),true);
  123. foreach ($contacts as $line) {
  124. $contact = new Contact();
  125. $contact->scope = 'contact_person';
  126. $contact->uid = $item->id;
  127. $contact->type = $line["type"];
  128. $contact->value = $line["value"];
  129. $contact->save();
  130. }
  131. }
  132. $response = $item->toArray();
  133. });
  134. Action::register('contact_avatar_load',function(&$response){
  135. global $myUser,$_;
  136. User::check_access('contact','read');
  137. $_['path'] = str_replace("..", "", base64_decode($_['path']));
  138. File::downloadFile($_['path']);
  139. });
  140. //Suppression d'élement contact
  141. Action::register('contact_contact_person_delete',function(&$response){
  142. global $_;
  143. User::check_access('contact','delete');
  144. if(empty($_['id']) || !is_numeric($_['id'])) throw new Exception("Identifiant incorrect");
  145. ContactPerson::deleteById($_['id']);
  146. });
  147. //Sauvegarde des configurations de Annuaire de contacts
  148. Action::register('contact_setting_save',function(&$response){
  149. global $_,$conf;
  150. User::check_access('contact','configure');
  151. //Si input file "multiple", possibilité de normaliser le
  152. //tableau $_FILES récupéré avec la fonction => normalize_php_files();
  153. foreach(Configuration::setting('contact') as $key=>$value){
  154. if(!is_array($value)) continue;
  155. $allowed[] = $key;
  156. }
  157. foreach ($_['fields'] as $key => $value) {
  158. if(in_array($key, $allowed))
  159. $conf->put($key,$value);
  160. }
  161. });
  162. ?>