action.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. if(!$myUser->can('example','edit')) throw new Exception("Permissions insuffisantes",403);
  10. require_once(__DIR__.SLASH.'Contact.class.php');
  11. require_once(__ROOT__.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. Plugin::callHook('emit_notification', array(
  37. array(
  38. 'label' => $title,
  39. 'html' => $msg,
  40. 'meta' => array(
  41. 'link' => ROOT_URL.'/index.php?module=example&page=sheet&id='.$contact->id
  42. )
  43. ),
  44. array($myUser->login)
  45. ));
  46. $response['id'] = $contact->id;
  47. $response['contact'] = $contact->label;
  48. //Exemple de mise en place de logs comparatif
  49. Log::compare($oldcontact,$contact,function(&$log){
  50. //ajout d'une info supplémentaire sur le log comparatif
  51. $log->label['meta_info'] = "example";
  52. });
  53. });
  54. break;
  55. //Recherche d'une liste
  56. case 'contact_search':
  57. Action::write(function(&$response){
  58. global $myUser,$_;
  59. if(!$myUser->can('example','read')) throw new Exception("Permissions insuffisantes",403);
  60. require_once(__DIR__.SLASH.'Contact.class.php');
  61. $query = 'SELECT * FROM {{table}} WHERE 1';
  62. $data = array();
  63. //Recherche simple
  64. if(!empty($_['filters']['keyword'])){
  65. $query .= ' AND label LIKE ?';
  66. $data[] = '%'.$_['filters']['keyword'].'%';
  67. }
  68. //Recherche avancée
  69. if(isset($_['filters']['advanced'])) filter_secure_query($_['filters']['advanced'],array('label','phone','birth','author','vehicle'),$query,$data);
  70. //Tri des colonnes
  71. if(isset($_['sort'])) sort_secure_query($_['sort'],array('label','phone'),$query,$data);
  72. //Pagination
  73. $response['pagination'] = Contact::paginate(2,(!empty($_['page'])?$_['page']:0),$query,$data);
  74. //Mise en forme des résultats
  75. foreach (Contact::staticQuery($query,$data,true) as $contact) {
  76. $row = $contact->toArray(true);
  77. $row['created'] = date('d/m/Y H:i',$contact->created);
  78. $row['updated'] = date('d/m/Y H:i',$contact->updated);
  79. $row['author'] = array();
  80. foreach (explode(',',$contact->author) as $login) {
  81. if(is_numeric($login)){
  82. //rank
  83. $item = Rank::getById($login);
  84. $item = !$item ? new Rank(): $item;
  85. $row['author'][] =$item->label;
  86. }else{
  87. //user
  88. $row['author'][] = User::byLogin($login)->fullName();
  89. }
  90. }
  91. $row['author'] = implode(',',$row['author']);
  92. $row['birth'] = date('d/m/Y',$contact->birth);
  93. $row['picture'] = $contact->picture().'&v='.time();
  94. $response['rows'][]= $row;
  95. }
  96. });
  97. break;
  98. //Suppression par id
  99. case 'contact_delete':
  100. Action::write(function(&$response){
  101. global $myUser,$_;
  102. if(!$myUser->can('example','delete')) throw new Exception("Permissions insuffisantes",403);
  103. require_once(__DIR__.SLASH.'Contact.class.php');
  104. if(!isset($_['id']) || !is_numeric($_['id'])) throw new Exception("Id non spécifié ou non numerique");
  105. //Exemple de mise en place de logs comparatif
  106. Log::compare(Contact::getById($_['id']),false);
  107. //suppression
  108. Contact::deleteById($_['id']);
  109. });
  110. break;
  111. //Suppression document
  112. case 'contact_delete_document':
  113. Action::write(function(&$response){
  114. global $myUser,$_;
  115. if(!$myUser->can('example','delete')) throw new Exception("Permissions insuffisantes",403);
  116. require_once(__DIR__.SLASH.'Contact.class.php');
  117. if(!isset($_['path']) ) throw new Exception("Chemin non spécifié ou non numerique");
  118. //Le premier argument est un namspace de sécurité
  119. //et assure que le fichier sera toujours cloisoné dans un contexte file/contact/documents
  120. $path = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($_['path']) : $_['path'];
  121. File::delete('contact'.SLASH.'documents',$path);
  122. });
  123. break;
  124. case 'contact_add_document':
  125. Action::write(function(&$response){
  126. global $myUser,$_;
  127. if(!$myUser->can('example','edit')) throw new Exception("Permissions insuffisantes",403);
  128. require_once(__DIR__.SLASH.'Contact.class.php');
  129. $contact = Contact::provide();
  130. $contact->save();
  131. foreach ($_['files'] as $file) {
  132. $name = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($file['name']) : $file['name'];
  133. $row = File::move(File::temp().$file['path'],'contact'.SLASH.'documents'.SLASH.$contact->id.SLASH.$name);
  134. $row['url'] = 'action.php?action=contact_download_document&path='.SLASH.$contact->id.SLASH.rawurlencode($file['name']);
  135. $row['oldPath'] = $file['path'];
  136. $response['files'][] = $row;
  137. }
  138. $response['id'] = $contact->id;
  139. });
  140. break;
  141. //Téléchargement des documents
  142. case 'contact_download_document':
  143. global $myUser,$_;
  144. if(!$myUser->can('example','read')) throw new Exception("Permissions insuffisantes",403);
  145. $path = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? utf8_decode($_['path']) : $_['path'];
  146. File::downloadFile(File::dir().'contact'.SLASH.'documents'.SLASH.$path);
  147. break;
  148. //Affichage de l'avatar
  149. case 'contact_download_picture':
  150. global $myUser,$_;
  151. if(!$myUser->can('example','read')) throw new Exception("Permissions insuffisantes",403);
  152. try{
  153. File::downloadFile(File::dir().'contact'.SLASH.$_['contact'].'.'.$_['extension']);
  154. } catch(Exception $e){
  155. File::downloadFile('img'.SLASH.'default-avatar.png');
  156. }
  157. break;
  158. //Suppression image contact
  159. case 'contact_avatar_delete':
  160. Action::write(function(&$response){
  161. global $myUser,$_;
  162. if(!$myUser->can('example','edit')) throw new Exception("Permissions insuffisantes",403);
  163. require_once(__DIR__.SLASH.'Contact.class.php');
  164. $item = Contact::provide();
  165. if(!$item) throw new Exception("Aucun contact ne correspond en base");
  166. foreach (glob(__ROOT__.FILE_PATH.'contact'.SLASH.$item->id.".*") as $filename)
  167. unlink($filename);
  168. if(!file_exists(__ROOT__.FILE_PATH.'contact'.SLASH.'.thumbnails')) return;
  169. foreach (glob(__ROOT__.FILE_PATH.'contact'.SLASH.'.thumbnails'.SLASH.$item->id.".*") as $filename) {
  170. unlink($filename);
  171. }
  172. });
  173. break;
  174. //Récupération card d'un contact
  175. case 'example_contact_card':
  176. Action::write(function(&$response){
  177. global $myUser,$myFirm,$_;
  178. if(!$myUser->can('example','read')) throw new Exception("Permissions insuffisantes",403);
  179. require_once(__DIR__.SLASH.'Contact.class.php');
  180. $contact = Contact::provide();
  181. ob_start();
  182. require_once(__DIR__.SLASH.'card.example.contact.php');
  183. $stream = ob_get_clean();
  184. $response['content'] = $stream;
  185. });
  186. break;
  187. default :
  188. global $myFirm;
  189. if($myFirm->has_plugin('fr.idleman.stripe') && $_['action']=='example_stripe_pay'){
  190. Action::write(function(&$response){
  191. global $_;
  192. //paye la somme de 20 €
  193. $response = stripe_payment($_['token'],22.5,'Description paiement','Description acheteur');
  194. });
  195. }
  196. break;
  197. }
  198. ?>