example.plugin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. //Déclaration d'un item de menu dans le menu principal
  3. function example_menu(&$menuItems){
  4. global $myUser;
  5. if(!$myUser->can('example','read')) return;
  6. $menuItems[] = array(
  7. 'sort'=>3,
  8. 'url'=>'index.php?module=example',
  9. 'label'=>'Example',
  10. 'icon'=> 'fas fa-question',
  11. 'color'=> '#273c75'
  12. );
  13. }
  14. //Cette fonction va generer une page quand on clique sur Example dans menu
  15. function example_page(){
  16. global $_;
  17. if(!isset($_['module']) || $_['module'] !='example') return;
  18. $page = !isset($_['page']) ? 'list.contact' : $_['page'];
  19. $page = str_replace('..','',$page);
  20. $file = __DIR__.SLASH.'page.'.$page.'.php';
  21. if(!file_exists($file)) throw new Exception("Page ".$page." inexistante");
  22. require_once($file);
  23. }
  24. //Fonction executée lors de l'activation du plugin
  25. function example_install($id){
  26. if($id != 'fr.core.example') return;
  27. Entity::install(__DIR__);
  28. $dictionary = new Dictionary();
  29. $dictionary->slug = 'example_contact_vehicle';
  30. if(Dictionary::rowCount(array('slug'=>$dictionary->slug)) ==0){
  31. $dictionary->label = 'ContactExample : Véhicule';
  32. $dictionary->parent = 0;
  33. $dictionary->state = Dictionary::ACTIVE;
  34. $dictionary->save();
  35. foreach(array(
  36. 'renaud'=>'Renaud',
  37. 'dacia'=>'Dacia',
  38. 'tesla'=>'Tesla'
  39. ) as $key=>$value){
  40. $item = new Dictionary();
  41. $item->slug = 'example_contact_vehicle_'.$key;
  42. $item->label = $value;
  43. $item->parent = $dictionary->id;
  44. $item->state = Dictionary::ACTIVE;
  45. $item->save();
  46. }
  47. }
  48. $subitem = new Dictionary();
  49. $subitem->slug = 'example_contact_vehicle_dacia_logan';
  50. $subitem->label = 'Logan';
  51. $subitem->parent = $item->id;
  52. $subitem->state = Dictionary::ACTIVE;
  53. $subitem->save();
  54. $dictionary = new Dictionary();
  55. $dictionary->slug = 'example_contact_handicap';
  56. if(Dictionary::rowCount(array('slug'=>$dictionary->slug)) ==0){
  57. $dictionary->label = 'Example : Handicaps';
  58. $dictionary->parent = 0;
  59. $dictionary->state = Dictionary::ACTIVE;
  60. $dictionary->save();
  61. foreach(array(
  62. 'none'=>'Aucun(e)',
  63. 'death'=>'Sourd(e)',
  64. 'blind'=>'Aveugle',
  65. ) as $key=>$value){
  66. $item = new Dictionary();
  67. $item->slug = 'example_contact_handicap_'.$key;
  68. $item->label = $value;
  69. $item->parent = $dictionary->id;
  70. $item->state = Dictionary::ACTIVE;
  71. $item->save();
  72. }
  73. }
  74. }
  75. //Fonction executée lors de la désactivation du plugin
  76. function example_uninstall($id){
  77. if($id != 'fr.core.example') return;
  78. Entity::uninstall(__DIR__);
  79. Dictionary::remove('example_contact_vehicle');
  80. Dictionary::remove('example_contact_handicap');
  81. }
  82. //Déclaration des sections de droits du plugin
  83. Right::register('example',array('label'=>'Gestion des droits sur le plugin d\'exemple'));
  84. //Droits ciblés sur les widgets
  85. Right::register('example_contact',array(
  86. 'label'=>'Gestion des droits sur le plugin exemple ciblé sur les contacts',
  87. 'global'=> false,
  88. 'check' => function($action,$right){
  89. global $myUser;
  90. if($right->uid <= 0) throw new Exception('Id widget non spécifié');
  91. require_once(__DIR__.SLASH.'ContactExample.class.php');
  92. $contact = ContactExample::getById($right->uid);
  93. if($myUser->login != $contact->creator) throw new Exception('Seul le créateur de ce contact ('.$contact->creator.') peut définir des droits pour celui-çi');
  94. }
  95. ));
  96. //Déclaration du menu de réglages
  97. function example_menu_setting(&$settingMenu){
  98. global $myUser;
  99. if(!$myUser->can('example','configure')) return;
  100. $settingMenu[]= array(
  101. 'sort' =>1,
  102. 'url' => 'setting.php?section=global.example',
  103. 'icon' => 'fas fa-angle-right',
  104. 'label' => 'Example'
  105. );
  106. }
  107. function example_menu_account(&$accountMenu){
  108. global $_, $myUser;
  109. if(!$myUser->can('example', 'read')) return;
  110. $accountMenu[]= array(
  111. 'sort' =>0,
  112. 'url' => 'account.php?section=example',
  113. 'icon' => 'fas fa-angle-right',
  114. 'label' => 'Example',
  115. );
  116. $accountMenu[]= array(
  117. 'sort' =>0,
  118. 'url' => 'account.php?section=example.contact',
  119. 'icon' => 'fas fa-angle-right',
  120. 'label' => 'Example ContactExample',
  121. );
  122. }
  123. //Déclaration des pages de réglages
  124. function example_content_account(){
  125. global $_;
  126. if(file_exists(__DIR__.SLASH.'account.'.$_['section'].'.php'))
  127. require_once(__DIR__.SLASH.'account.'.$_['section'].'.php');
  128. }
  129. //Déclaration des pages de réglages
  130. function example_content_setting(){
  131. global $_;
  132. if(file_exists(__DIR__.SLASH.'setting.'.$_['section'].'.php'))
  133. require_once(__DIR__.SLASH.'setting.'.$_['section'].'.php');
  134. }
  135. //Affichage du/des widget(s)
  136. function example_widget(&$widgets){
  137. require_once(PLUGIN_PATH.'dashboard'.SLASH.'DashboardWidget.class.php');
  138. $modelWidget = new DashboardWidget();
  139. $modelWidget->model = 'example';
  140. $modelWidget->title = 'Example';
  141. $modelWidget->icon = 'fas fa-question';
  142. $modelWidget->background = '#273c75';
  143. $modelWidget->load = 'action.php?action=example_widget_load';
  144. $modelWidget->js = [Plugin::url().'/js/widget.js'];
  145. $modelWidget->css = [Plugin::url().'/css/widget.css'];
  146. $modelWidget->description = "Contient les différents outils créés et la structure de fichier attendue dans les plugins.";
  147. $widgets[] = $modelWidget;
  148. }
  149. function example_document_setting(){
  150. $documents = array();
  151. foreach(glob(__ROOT__.FILE_PATH.'contact'.SLASH.'documents'.SLASH.'settings'.SLASH.'*.*') as $file){
  152. if(get_OS() === 'WIN') $file = utf8_encode($file);
  153. $documents[] = array(
  154. 'path' => 'contact'.SLASH.'documents'.SLASH.'settings'.SLASH.basename($file),
  155. 'url' => 'action.php?action=contact_download_document&path=settings'.SLASH.rawurlencode(basename($file)),
  156. 'name' => basename($file),
  157. 'icon' => getExtIcon(getExt($file))
  158. );
  159. }
  160. return $documents;
  161. }
  162. require_once(__DIR__.SLASH.'action.php');
  163. Configuration::setting('example',array(
  164. "Général",
  165. 'example_dropzone' => array("label"=>"Dropzone de fichiers","type"=>"dropzone","legend"=>"la légende","attributes"=>array(
  166. "data-allowed" => "docx,pdf,txt,jpg,bmp,gif,xlsx,png,iso",
  167. "data-label" => "Faites glisser vos documents",
  168. "data-delete" => "contact_delete_document",
  169. "documents" => json_encode(example_document_setting())
  170. )),
  171. 'example_dictionary' => array("label"=>"Liste de véhicules","type"=>"dictionary","legend"=>"la légende","attributes"=>array(
  172. "data-slug" => "invoice-payment-mode"
  173. )),
  174. ));
  175. $api = new Api("example", "Api de gestion des exemples");
  176. $api->route('contact/search','retourne un contact et ses informations correspondantes','GET',function($request,&$response){
  177. global $myUser,$_;
  178. $_ = $request['parameters'];
  179. $_['export'] = false;
  180. if(!$myUser->connected()) throw new Exception("Credentials are missing",401);
  181. Action::run('example_contact_search',$response);
  182. });
  183. $api->register();
  184. global $myFirm;
  185. if($myFirm->has_plugin('fr.core.export')){
  186. require_once(__ROOT__.PLUGIN_PATH.'export'.SLASH.'ExportModel.class.php');
  187. ExportModel::add('example','contact-sheet', 'Fiche contact', function($parameters){
  188. global $myUser;
  189. require_once(__DIR__.SLASH.'ContactExample.class.php');
  190. $contact = new ContactExample();
  191. if(isset($parameters['description']) && $parameters['description']!=true && !empty($parameters['id']))
  192. $contact = ContactExample::getById($parameters['id']);
  193. $data['contact'] = array('label'=>'ContactExample', 'type'=>'object','value' => array());
  194. $data['contact']['value']['libellé'] = array('label' => 'Libellé du contact','value' => $contact->label);
  195. $data['contact']['value']['téléphone'] = array('label' => 'N° de téléphone du contact','value' => $contact->phone);
  196. $data['contact']['value']['anniversaire'] = array('label' => 'Date de naissance/d\'anniversaire du contact','value' => date('d/m/Y',$contact->birth));
  197. $data['contact']['value']['identifiant'] = array('label' => 'Identifiant du contact','value'=>$contact->login);
  198. $data['contact']['value']['photo'] = array(
  199. 'label'=>'Image de profil du contact',
  200. 'type'=>'image',
  201. 'value' => file_get_contents($contact->get_image('path'))
  202. );
  203. return $data;
  204. });
  205. ExportModel::add('example','contact-list','Liste de contacts',function($parameters){
  206. global $myUser;
  207. require_once(__DIR__.SLASH.'ContactExample.class.php');
  208. $contacts = array(new ContactExample());
  209. if(isset($parameters['description']) && $parameters['description']!=true) $contacts = ContactExample::loadAll();
  210. $data['contacts'] = array('label'=>'Boucle sur les contacts', 'type'=>'list','value' => array());
  211. $data['contacts']['nombre'] = array('label' => 'Nombre de contacts au total', 'value' => count($contacts));
  212. foreach($contacts as $contact){
  213. $data['contacts']['value'][] = array(
  214. 'libellé' => array('label' => 'Libellé du contact' , 'value' => $contact->label ),
  215. 'téléphone' => array('label' => 'N° de téléphone du contact' , 'value' => $contact->phone ),
  216. 'anniversaire' => array('label' => 'Date de naissance/d\'anniversaire du contact' , 'value' => $contact->birth ),
  217. 'identifiant' => array('label' => 'Identifiant du contact' , 'value' => $contact->login ),
  218. 'photo' => array(
  219. 'label' => 'Image de profil du contact',
  220. 'type'=>'image' ,
  221. 'value' => file_get_contents($contact->get_image('path'))
  222. ),
  223. );
  224. }
  225. return $data;
  226. });
  227. }
  228. //Déclation des assets
  229. Plugin::addCss("/css/main.css");
  230. Plugin::addJs("/js/main.js");
  231. //Mapping hook / fonctions
  232. Plugin::addHook("install", "example_install");
  233. Plugin::addHook("uninstall", "example_uninstall");
  234. Plugin::addHook("menu_main", "example_menu");
  235. Plugin::addHook("page", "example_page");
  236. Plugin::addHook("menu_account", "example_menu_account");
  237. Plugin::addHook("content_account", "example_content_account");
  238. Plugin::addHook("menu_setting", "example_menu_setting");
  239. Plugin::addHook("content_setting", "example_content_setting");
  240. Plugin::addHook("widget", "example_widget");
  241. //Declaration des évenements et entités workflow
  242. Plugin::addHook("workflow_event", function(&$events){
  243. Plugin::need('workflow/WorkflowEvent');
  244. //Evenement entité
  245. $events[] = WorkflowEvent::registerEntity(__DIR__.SLASH.'ContactExample.class.php');
  246. //Evenement liste d'entité
  247. $events[] = WorkflowEvent::registerList(__DIR__.SLASH.'ContactExample.class.php',array(
  248. 'events' => array('example-contact-cron' => 'Toutes les minutes'),
  249. ));
  250. });
  251. //Lancement de l'evenement liste entité toutes les minutes
  252. Plugin::addHook("cron", function(){
  253. Plugin::need('example/ContactExample');
  254. Plugin::need('workflow/WorkflowEvent');
  255. WorkflowEvent::trigger('example-contact-cron',array());
  256. });
  257. ?>