example.plugin.php 10 KB

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