action.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. global $_,$conf;
  3. switch($_['action']){
  4. //GESTIONS DES CONTACTS
  5. //Enregistrement (ajout/modification)
  6. case 'contact_save':
  7. Action::write(function(&$response){
  8. global $myUser,$_;
  9. User::check_access('example','edit');
  10. require_once(__DIR__.SLASH.'Contact.class.php');
  11. require_once(PLUGIN_PATH.'notification'.SLASH.'Notification.class.php');
  12. $contact = Contact::provide();
  13. //on garde l'ancien objet a l'instant t pour le log comparatif (voir en fin d'action)
  14. $oldcontact = clone $contact;
  15. $title = isset($contact->id) ? 'Édition d\'un contact' : 'Création d\'un contact';
  16. $msg = isset($contact->id) ? 'Le contact '.$contact->label.' a été édité' : 'Création du contact '.$contact->label;
  17. $contact->fromArray($_);
  18. $contact->birth = timestamp_date($contact->birth);
  19. $contact->hour = timestamp_hour($contact->hour);
  20. $contact->save();
  21. //Ajout des fichiers joints
  22. if(!empty($_['document_temporary'])){
  23. $files = json_decode($_['document_temporary'],true);
  24. foreach($files as $file){
  25. $from = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? File::temp().utf8_decode($file['path']) : File::temp().$file['path'];
  26. $to = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($file['name']) : $file['name'];
  27. File::move($from, 'contact'.SLASH.'documents'.SLASH.$contact->id.SLASH.$to);
  28. }
  29. }
  30. //Ajout de l'avatar a la base de media
  31. if(!empty($_FILES['avatar']) && $_FILES['avatar']['size']!=0 ){
  32. $logo = File::upload('avatar','contact'.SLASH.$contact->id.'.{{ext}}',1048576,array('jpg','png','jpeg'));
  33. Image::resize($logo['absolute'],200,200);
  34. Image::toJpg($logo['absolute']);
  35. }
  36. // GESTION ENVOI NOTIFICATION
  37. Plugin::callHook('emit_notification',array(array(
  38. 'label' => $title,
  39. 'html' => $msg,
  40. 'type' => "notice",
  41. 'meta' => array('link' => ROOT_URL.'/index.php?module=example&page=sheet&id='.$contact->id),
  42. 'recipients' => array($myUser->login) // recipients contient login
  43. )
  44. ));
  45. $response['id'] = $contact->id;
  46. $response['contact'] = $contact->label;
  47. //Exemple de mise en place de logs comparatif
  48. Log::compare($oldcontact,$contact,function(&$log){
  49. //ajout d'une info supplémentaire sur le log comparatif
  50. $log->label['meta_info'] = "example";
  51. });
  52. });
  53. break;
  54. //Recherche d'une liste
  55. case 'contact_search':
  56. Action::write(function(&$response){
  57. global $myUser,$_;
  58. User::check_access('example','read');
  59. require_once(__DIR__.SLASH.'Contact.class.php');
  60. $query = 'SELECT * FROM {{table}} WHERE 1';
  61. $data = array();
  62. //Recherche simple
  63. if(!empty($_['filters']['keyword'])){
  64. $query .= ' AND label LIKE ?';
  65. $data[] = '%'.$_['filters']['keyword'].'%';
  66. }
  67. //Recherche avancée
  68. if(isset($_['filters']['advanced'])) filter_secure_query($_['filters']['advanced'],array('label','phone','birth','author','vehicle', 'mycheckbox1'),$query,$data);
  69. //Tri des colonnes
  70. if(isset($_['sort'])) sort_secure_query($_['sort'],array('label','phone'),$query,$data);
  71. //Pagination
  72. $response['pagination'] = Contact::paginate(20,(!empty($_['page'])?$_['page']:0),$query,$data);
  73. //Mise en forme des résultats
  74. foreach (Contact::staticQuery($query,$data,true) as $contact) {
  75. $row = $contact->toArray(true);
  76. $row['created'] = date('d/m/Y H:i',$contact->created);
  77. $row['updated'] = date('d/m/Y H:i',$contact->updated);
  78. $row['author'] = array();
  79. foreach (explode(',',$contact->author) as $login) {
  80. if(is_numeric($login)){
  81. //rank
  82. $item = Rank::getById($login);
  83. $item = !$item ? new Rank(): $item;
  84. $row['author'][] =$item->label;
  85. }else{
  86. //user
  87. $row['author'][] = User::byLogin($login)->fullName();
  88. }
  89. }
  90. $row['author'] = implode(',',$row['author']);
  91. $row['birth'] = date('d/m/Y',$contact->birth);
  92. $row['picture'] = $contact->picture().'&v='.time();
  93. $response['rows'][] = $row;
  94. }
  95. /* Mode export */
  96. if($_['export'] == 'true'){
  97. $stream = Excel::exportArray($response['rows'],null,'Export');
  98. File::downloadStream($stream,'export-'.date('d-m-Y').'.xlsx');
  99. exit();
  100. }
  101. });
  102. break;
  103. //Suppression par id
  104. case 'contact_delete':
  105. Action::write(function(&$response){
  106. global $myUser,$_;
  107. User::check_access('example','delete');
  108. require_once(__DIR__.SLASH.'Contact.class.php');
  109. if(!isset($_['id']) || !is_numeric($_['id'])) throw new Exception("Id non spécifié ou non numerique");
  110. //Exemple de mise en place de logs comparatif
  111. Log::compare(Contact::getById($_['id']),false);
  112. //suppression
  113. Contact::deleteById($_['id']);
  114. });
  115. break;
  116. //Suppression document
  117. case 'contact_delete_document':
  118. Action::write(function(&$response){
  119. global $myUser,$_;
  120. User::check_access('example','delete');
  121. require_once(__DIR__.SLASH.'Contact.class.php');
  122. if(!isset($_['path']) ) throw new Exception("Chemin non spécifié ou non numerique");
  123. //Le premier argument est un namspace de sécurité
  124. //et assure que le fichier sera toujours cloisoné dans un contexte file/contact/documents
  125. $path = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($_['path']) : $_['path'];
  126. File::delete('contact'.SLASH.'documents',$path);
  127. });
  128. break;
  129. case 'contact_add_document':
  130. Action::write(function(&$response){
  131. global $myUser,$_;
  132. User::check_access('example','edit');
  133. require_once(__DIR__.SLASH.'Contact.class.php');
  134. $contact = Contact::provide();
  135. $contact->save();
  136. foreach ($_['files'] as $file) {
  137. $name = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($file['name']) : $file['name'];
  138. $row = File::move(File::temp().$file['path'],'contact'.SLASH.'documents'.SLASH.$contact->id.SLASH.$name);
  139. $row['url'] = 'action.php?action=contact_download_document&path='.SLASH.$contact->id.SLASH.rawurlencode($file['name']);
  140. $row['oldPath'] = $file['path'];
  141. $response['files'][] = $row;
  142. }
  143. $response['id'] = $contact->id;
  144. });
  145. break;
  146. //Téléchargement des documents
  147. case 'contact_download_document':
  148. global $myUser,$_;
  149. User::check_access('example','read');
  150. $path = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($_['path']) : $_['path'];
  151. File::downloadFile(File::dir().'contact'.SLASH.'documents'.SLASH.$path);
  152. break;
  153. //Affichage de l'avatar
  154. case 'contact_download_picture':
  155. global $myUser,$_;
  156. User::check_access('example','read');
  157. try{
  158. File::downloadFile(File::dir().'contact'.SLASH.$_['contact'].'.'.$_['extension']);
  159. } catch(Exception $e){
  160. File::downloadFile('img'.SLASH.'default-avatar.png');
  161. }
  162. break;
  163. //Suppression image contact
  164. case 'contact_avatar_delete':
  165. Action::write(function(&$response){
  166. global $myUser,$_;
  167. User::check_access('example','edit');
  168. require_once(__DIR__.SLASH.'Contact.class.php');
  169. $item = Contact::provide();
  170. if(!$item) throw new Exception("Aucun contact ne correspond en base");
  171. foreach (glob(__ROOT__.FILE_PATH.'contact'.SLASH.$item->id.".*") as $filename)
  172. unlink($filename);
  173. if(!file_exists(__ROOT__.FILE_PATH.'contact'.SLASH.'.thumbnails')) return;
  174. foreach (glob(__ROOT__.FILE_PATH.'contact'.SLASH.'.thumbnails'.SLASH.$item->id.".*") as $filename) {
  175. unlink($filename);
  176. }
  177. });
  178. break;
  179. //Récupération card d'un contact
  180. case 'example_contact_card':
  181. Action::write(function(&$response){
  182. global $myUser,$myFirm,$_;
  183. User::check_access('example','read');
  184. require_once(__DIR__.SLASH.'Contact.class.php');
  185. $contact = Contact::provide();
  186. ob_start();
  187. require_once(__DIR__.SLASH.'card.example.contact.php');
  188. $stream = ob_get_clean();
  189. $response['content'] = $stream;
  190. });
  191. break;
  192. default :
  193. global $myFirm;
  194. if($myFirm->has_plugin('fr.idleman.stripe') && $_['action']=='example_stripe_pay'){
  195. Action::write(function(&$response){
  196. global $_;
  197. //paye la somme de 20 €
  198. $response = stripe_payment($_['token'],22.5,'Description paiement','Description acheteur');
  199. });
  200. }
  201. break;
  202. }
  203. ?>