action.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. $contact->fromArray($_);
  16. $contact->story = str_replace("&quot;", "'", $contact->story);
  17. $contact->author = stripslashes($contact->author);
  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 = (get_OS() === 'WIN') ? File::temp().utf8_decode($file['path']) : File::temp().$file['path'];
  26. $to = (get_OS() === '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}}',10048576,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' => isset($contact->id) ? 'Édition d\'un contact' : 'Création d\'un contact',
  39. 'html' => isset($contact->id) ? 'Le contact '.$contact->label.' a été édité' : 'Création du contact '.$contact->label,
  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 c1.* FROM {{table}} c1 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','login'),$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,'c1');
  73. $users = array();
  74. foreach (User::getAll(false, false) as $user)
  75. $users[$user->login] = $user->fullName();
  76. //Mise en forme des résultats
  77. foreach (Contact::staticQuery($query,$data,true) as $contact) {
  78. $row = $contact->toArray(true);
  79. $row['created'] = date('d/m/Y H:i',$contact->created);
  80. $row['updated'] = date('d/m/Y H:i',$contact->updated);
  81. $row['author'] = array();
  82. foreach (explode(',',$contact->author) as $login) {
  83. if(is_numeric($login)){
  84. //rank
  85. $item = Rank::getById($login);
  86. $item = !$item ? new Rank(): $item;
  87. $row['author'][] = $item->label;
  88. }else{
  89. //user
  90. $row['author'][] = isset($users[$login]) ? $users[$login] : $login;
  91. }
  92. }
  93. $row['author'] = implode(', ',$row['author']);
  94. $row['birth'] = date('d/m/Y',$contact->birth);
  95. $row['picture'] = $contact->picture();
  96. $response['rows'][] = $row;
  97. }
  98. /* Mode export */
  99. if($_['export'] == 'true'){
  100. $stream = Excel::exportArray($response['rows'],null,'Export');
  101. File::downloadStream($stream,'export-'.date('d-m-Y').'.xlsx');
  102. exit();
  103. }
  104. });
  105. break;
  106. //Suppression par id
  107. case 'contact_delete':
  108. Action::write(function(&$response){
  109. global $myUser,$_;
  110. User::check_access('example','delete');
  111. require_once(__DIR__.SLASH.'Contact.class.php');
  112. if(!isset($_['id']) || !is_numeric($_['id'])) throw new Exception("Id non spécifié ou non numerique");
  113. //Exemple de mise en place de logs comparatif
  114. Log::compare(Contact::getById($_['id']),false);
  115. //suppression
  116. Contact::deleteById($_['id']);
  117. });
  118. break;
  119. //Création rapide par quickform
  120. case 'contact_quick_create':
  121. Action::write(function(&$response){
  122. global $myUser,$_;
  123. User::check_access('example','edit');
  124. require_once(__DIR__.SLASH.'Contact.class.php');
  125. ob_start();
  126. require_once(__DIR__.SLASH.'page.quick.example.php');
  127. $response['content'] = ob_get_clean();
  128. });
  129. break;
  130. //Suppression document
  131. case 'contact_delete_document':
  132. Action::write(function(&$response){
  133. global $myUser,$_;
  134. User::check_access('example','delete');
  135. require_once(__DIR__.SLASH.'Contact.class.php');
  136. if(!isset($_['path']) ) throw new Exception("Chemin non spécifié ou non numerique");
  137. //Le premier argument est un namspace de sécurité
  138. //et assure que le fichier sera toujours cloisoné dans un contexte file/contact/documents
  139. $path = (get_OS() === 'WIN') ? utf8_decode($_['path']) : $_['path'];
  140. File::delete('contact'.SLASH.'documents',$path);
  141. });
  142. break;
  143. case 'contact_add_document':
  144. Action::write(function(&$response){
  145. global $myUser,$_;
  146. User::check_access('example','edit');
  147. require_once(__DIR__.SLASH.'Contact.class.php');
  148. $contact = Contact::provide();
  149. $contact->save();
  150. foreach ($_['files'] as $file) {
  151. $name = (get_OS() === 'WIN') ? utf8_decode($file['name']) : $file['name'];
  152. $row = File::move(File::temp().$file['path'],'contact'.SLASH.'documents'.SLASH.$contact->id.SLASH.$name);
  153. $row['url'] = 'action.php?action=contact_download_document&path='.SLASH.$contact->id.SLASH.rawurlencode($file['name']);
  154. $row['oldPath'] = $file['path'];
  155. $response['files'][] = $row;
  156. }
  157. $response['id'] = $contact->id;
  158. });
  159. break;
  160. //Téléchargement des documents
  161. case 'contact_download_document':
  162. global $myUser,$_;
  163. User::check_access('example','read');
  164. $path = (get_OS() === 'WIN') ? utf8_decode($_['path']) : $_['path'];
  165. File::downloadFile(File::dir().'contact'.SLASH.'documents'.SLASH.$path);
  166. break;
  167. //Affichage de l'avatar
  168. case 'contact_download_picture':
  169. global $myUser,$_;
  170. User::check_access('example','read');
  171. try{
  172. File::downloadFile(File::dir().'contact'.SLASH.$_['contact'].'.'.$_['extension']);
  173. } catch(Exception $e){
  174. File::downloadFile('img'.SLASH.'default-avatar.png');
  175. }
  176. break;
  177. //Suppression image contact
  178. case 'contact_avatar_delete':
  179. Action::write(function(&$response){
  180. global $myUser,$_;
  181. User::check_access('example','edit');
  182. require_once(__DIR__.SLASH.'Contact.class.php');
  183. $item = Contact::provide();
  184. if(!$item) throw new Exception("Aucun contact ne correspond en base");
  185. foreach (glob(__ROOT__.FILE_PATH.'contact'.SLASH.$item->id.".*") as $filename)
  186. unlink($filename);
  187. if(!file_exists(__ROOT__.FILE_PATH.'contact'.SLASH.'.thumbnails')) return;
  188. foreach (glob(__ROOT__.FILE_PATH.'contact'.SLASH.'.thumbnails'.SLASH.$item->id.".*") as $filename) {
  189. unlink($filename);
  190. }
  191. });
  192. break;
  193. //Récupération card d'un contact
  194. case 'example_contact_card':
  195. Action::write(function(&$response){
  196. global $myUser,$myFirm,$_;
  197. User::check_access('example','read');
  198. require_once(__DIR__.SLASH.'Contact.class.php');
  199. $contact = Contact::provide();
  200. ob_start();
  201. require_once(__DIR__.SLASH.'card.example.contact.php');
  202. $stream = ob_get_clean();
  203. $response['content'] = $stream;
  204. });
  205. break;
  206. case 'example_setting_save':
  207. Action::write(function(&$response){
  208. global $myUser,$_,$conf;
  209. User::check_access('example','configure');
  210. foreach(Configuration::setting('example') as $key=>$value){
  211. if(!is_array($value)) continue;
  212. $allowed[] = $key;
  213. }
  214. foreach ($_['fields'] as $key => $value)
  215. if(in_array($key, $allowed)) $conf->put($key,$value);
  216. //Ajout des fichiers joints
  217. if(isset($_['fields']['document_temporary']) && !empty($_['fields']['document_temporary'])){
  218. $files = json_decode($_['fields']['document_temporary'],true);
  219. foreach($files as $file){
  220. $from = (get_OS() === 'WIN') ? File::temp().utf8_decode($file['path']) : File::temp().$file['path'];
  221. $to = (get_OS() === 'WIN') ? utf8_decode($file['name']) : $file['name'];
  222. File::move($from, 'contact'.SLASH.'documents'.SLASH.'settings'.SLASH.$to);
  223. }
  224. }
  225. });
  226. break;
  227. default :
  228. global $myFirm;
  229. if($myFirm->has_plugin('fr.sys1.stripe') && $_['action']=='example_stripe_pay'){
  230. Action::write(function(&$response){
  231. global $_;
  232. //paye la somme de 20 €
  233. $response = stripe_payment($_['token'],22.5,'Description paiement','Description acheteur');
  234. });
  235. }
  236. break;
  237. }
  238. ?>