action.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. {"label":"Action","syntax":"php"}
  2. <?php
  3. /** {{ENTITY}} / {{ENTITYLABEL}} **/
  4. //Récuperation d'une liste de {{entityLabel}}
  5. Action::register('{{plugin_entity_deduplicate}}_search',function(&$response){
  6. global $_;
  7. User::check_access('{{plugin}}','read');
  8. require_once(__DIR__.SLASH.'{{Entity}}.class.php');{{#links}}
  9. require_once({{linkedRequirePath}});{{/links}}
  10. {{#advanced-search}}
  11. // OPTIONS DE RECHERCHE, A ACTIVER POUR UNE RECHERCHE AVANCEE
  12. $query = 'SELECT main.*{{#linksCount}},main.id as id,{{/linksCount}}{{#links}} '.{{linkedEntity}}::joinString('{{linkedEntity}}').'{{;}},{{/;}}{{/links}} FROM '.{{Entity}}::tableName().' main {{#links}} LEFT JOIN '.{{linkedEntity}}::tableName().' {{linkedEntity}} ON main.{{linkedKey}}={{linkedEntity}}.id {{/links}} WHERE 1';
  13. $data = array();
  14. //Recherche simple
  15. if(!empty($_['filters']['keyword'])){
  16. $query .= ' AND main.label LIKE ?';
  17. $data[] = '%'.$_['filters']['keyword'].'%';
  18. }
  19. //Recherche avancée
  20. if(isset($_['filters']['advanced'])) filter_secure_query($_['filters']['advanced'],array({{#fields}}'main.{{key}}'{{;}},{{/;}}{{/fields}}),$query,$data);
  21. //Tri des colonnes
  22. if(isset($_['sort'])) sort_secure_query($_['sort'],array({{#fields}}'main.{{key}}'{{;}},{{/;}}{{/fields}}),$query,$data);
  23. //Pagination
  24. //Par défaut pour une recherche, 20 items, pour un export 5000 max
  25. $itemPerPage = !empty($_['itemPerPage']) ? $_['itemPerPage'] : 20;
  26. //force le nombre de page max a 50 coté serveur
  27. $itemPerPage = $itemPerPage>50 ? 50 : $itemPerPage;
  28. if($_['export'] == 'true') $itemPerPage = 5000;
  29. $response['pagination'] = {{Entity}}::paginate($itemPerPage,(!empty($_['page'])?$_['page']:0),$query,$data,'main');
  30. ${{entity}}s = {{Entity}}::staticQuery($query,$data,true,{{linksCount}});{{/advanced-search}}
  31. {{^advanced-search}}${{entity}}s = {{Entity}}::loadAll();{{/advanced-search}}
  32. {{#fields}}{{#isDictionary}}${{key}}List = Dictionary::slugToArray('{{plugin}}_{{entity}}_{{key}}',true);{{/isDictionary}}{{/fields}}
  33. $response['rows'] = array();
  34. foreach(${{entity}}s as ${{entity}}){
  35. $row = ${{entity}}->toArray();{{#links}}
  36. $row['{{linkedKey}}'] = ${{entity}}->join('{{linkedKey}}')->toArray();{{/links}}{{#fields}}{{#isDate}}
  37. $row['{{key}}-readable'] = complete_date($row['{{key}}']).' à '.date('H:i',$row['{{key}}']); {{/isDate}}{{#isDictionary}}
  38. $row['{{key}}'] = isset(${{key}}List[$row['{{key}}']]) ? ${{key}}List[$row['{{key}}']] : new Dictionary(); {{/isDictionary}}{{#isList}}
  39. $row['{{key}}'] = {{Entity}}::{{key}}s($row['{{key}}']); {{/isList}}{{#isChoice}}
  40. $row['{{key}}'] = {{Entity}}::{{key}}s($row['{{key}}']); {{/isChoice}}{{#isWysiwyg}}
  41. $row['{{key}}'] = html_entity_decode($row['{{key}}']); {{/isWysiwyg}}{{#isUser}}
  42. $user = User::byLogin($row['{{key}}']);
  43. $row['{{key}}'] = $user->toArray();
  44. $row['{{key}}']['fullname'] = $user->fullname();
  45. $row['{{key}}']['avatar'] = $user->getAvatar();
  46. {{/isUser}}{{#isImage}}
  47. $row['{{key}}'] = 'action.php?action={{plugin}}_{{entity}}_{{key}}&type=download&path='.base64_encode('{{plugin}}/{{entity}}/'.$row['id'].'/{{key}}.*');
  48. {{/isImage}}{{#isFile}}
  49. $row['{{key}}'] = array();
  50. foreach (glob(File::dir().'{{plugin}}'.SLASH.'{{entity}}'.SLASH.$row['id'].'/{{key}}/*') as $file) {
  51. $row['{{key}}'][] = array(
  52. 'label' => basename($file),
  53. 'url' => 'action.php?action={{plugin}}_{{entity}}_{{key}}&type=download&path='.base64_encode('{{plugin}}/{{entity}}/'.$row['id'].'/{{key}}/'.basename($file)),
  54. );
  55. }
  56. {{/isFile}}{{/fields}}
  57. if($_['export'] == 'true'){
  58. $row['created'] = date('d-m-Y',$row['created']);
  59. $row['updated'] = date('d-m-Y',$row['updated']);
  60. }
  61. $response['rows'][] = $row;
  62. }
  63. {{#search-export}}
  64. /* Mode export */
  65. if($_['export'] == 'true'){
  66. if(empty($response['rows'])) $response['rows'][] = array('Vide'=>'Aucune données');
  67. $fieldsMapping = array();
  68. foreach ({{Entity}}::fields(false) as $key => $value)
  69. $fieldsMapping[$value['label']] = $key;
  70. $stream = Excel::exportArray($response['rows'],$fieldsMapping ,'Export');
  71. File::downloadStream($stream,'export-{{entity}}-'.date('d-m-Y').'.xlsx');
  72. exit();
  73. }
  74. {{/search-export}}
  75. });
  76. //Ajout ou modification d'élément {{entityLabel}}
  77. Action::register('{{plugin_entity_deduplicate}}_save',function(&$response){
  78. global $_;
  79. User::check_access('{{plugin}}','edit');
  80. require_once(__DIR__.SLASH.'{{Entity}}.class.php');
  81. $item = {{Entity}}::provide();{{#history-save}}
  82. $oldItem = clone $item;{{/history-save}}{{#fields}}{{^isFile}}{{^isImage}}
  83. {{#isFloat}}if(is_numeric($_['{{key}}'])) {{/isFloat}}{{#isDictionary}}if(!empty($_['{{key}}']) && is_numeric($_['{{key}}'])) {{/isDictionary}}{{#isChoice}}if(!empty($_['{{key}}'])) {{/isChoice}}$item->{{key}} = {{#isDate}}timestamp_date($_['{{key}}']);{{/isDate}}{{^isDate}}$_['{{key}}'];{{/isDate}}{{/isImage}}{{/isFile}}{{/fields}}
  84. $item->save();{{#fields}}{{#isImage}}
  85. //Ajout upload {{label}}
  86. if(!empty($_['{{key}}']))
  87. File::save_component('{{key}}', '{{plugin}}/{{entity}}/'.$item->id.'/{{key}}.[-[-extension-]-]');{{/isImage}}{{#isFile}}
  88. //Ajout upload {{label}}
  89. if(!empty($_['{{key}}']))
  90. File::save_component('{{key}}', '{{plugin}}/{{entity}}/'.$item->id.'/{{key}}/[-[-label-]-]');{{/isFile}}{{/fields}}{{#history-save}}
  91. History::entityChange('{{entity}}',$oldItem,$item);{{/history-save}}
  92. $response = $item->toArray();
  93. });
  94. {{#edit-form}}//Récuperation ou edition d'élément {{entityLabel}}
  95. Action::register('{{plugin_entity_deduplicate}}_edit',function(&$response){
  96. global $_;
  97. User::check_access('{{plugin}}','edit');
  98. require_once(__DIR__.SLASH.'{{Entity}}.class.php');{{:links}}
  99. require_once(__DIR__.SLASH.'{{value.entity}}.class.php');{{/:links}}
  100. $response = {{Entity}}::getById($_['id'],{{linksCount}})->toArray();
  101. });
  102. {{/edit-form}}
  103. //Suppression d'élement {{entityLabel}}
  104. Action::register('{{plugin_entity_deduplicate}}_delete',function(&$response){
  105. global $_;
  106. User::check_access('{{plugin}}','delete');
  107. require_once(__DIR__.SLASH.'{{Entity}}.class.php');
  108. {{^logic-remove}}if(empty($_['id']) || !is_numeric($_['id'])) throw new Exception("Identifiant incorrect");
  109. {{Entity}}::deleteById($_['id']);{{/logic-remove}}{{#logic-remove}}$item = {{Entity}}::getById($_['id']);
  110. $item->state = {{Entity}}::INACTIVE;
  111. $item->save();{{/logic-remove}}
  112. });
  113. {{#fields}}{{#isImage}}//{{Entity}} : Gestion upload {{label}}
  114. Action::register('{{plugin_entity_deduplicate}}_{{key}}',function(&$response){
  115. File::handle_component(array(
  116. 'namespace' => '{{plugin}}', //stockés dans file/{{plugin}}/*.*
  117. 'access' => '{{plugin}}', // crud sur {{plugin}},
  118. 'size' => '1000000000', // taille max
  119. 'limit' => '1', // nb max de fichiers
  120. 'storage' => '{{plugin}}/{{entity}}/[-[-data.id-]-]/{{key}}.*' //chemin complet vers le fichier stocké
  121. ),$response);
  122. });{{/isImage}}{{#isFile}}
  123. //{{Entity}} : Gestion upload {{label}}
  124. Action::register('{{plugin_entity_deduplicate}}_{{key}}',function(&$response){
  125. File::handle_component(array(
  126. 'namespace' => '{{plugin}}', //stockés dans file/{{plugin}}/*.*
  127. 'access' => '{{plugin}}', // crud sur {{plugin}},
  128. 'size' => '1000000000', // taille max
  129. 'storage' => '{{plugin}}/{{entity}}/[-[-data.id-]-]/{{key}}/*' //chemin complet vers le fichier stocké
  130. ),$response);
  131. });{{/isFile}}{{/fields}}
  132. {{#setting}}//Sauvegarde des configurations de {{plugin.label}}
  133. Action::register('{{plugin}}_setting_save',function(&$response){
  134. global $_,$conf;
  135. User::check_access('{{plugin}}','configure');
  136. //Si input file "multiple", possibilité de normaliser le
  137. //tableau $_FILES récupéré avec la fonction => normalize_php_files();
  138. foreach(Configuration::setting('{{plugin}}') as $key=>$value){
  139. if(!is_array($value)) continue;
  140. $allowed[] = $key;
  141. }
  142. foreach ($_['fields'] as $key => $value) {
  143. if(in_array($key, $allowed))
  144. $conf->put($key,$value);
  145. }
  146. });
  147. {{/setting}}
  148. {{#component}}
  149. /* COMPOSANT*/
  150. //recherche autocomplete
  151. Action::register('{{entity}}_autocomplete',function(&$response){
  152. global $myUser,$_;
  153. require_once(__DIR__.SLASH.'{{Entity}}.class.php');
  154. if (!$myUser->connected()) throw new Exception("Vous devez être connecté", 401);
  155. $response['rows'] = array();
  156. $data = array("%".$_['keyword']."%");
  157. //retourne en priorité les matchs à 100%, pour les match keyword%, puis les autres
  158. $query = 'SELECT c.* FROM '.{{Entity}}::tableName().' c WHERE (c.label LIKE ?) ';
  159. $query .= ' LIMIT 10';
  160. $devices = {{Entity}}::staticQuery($query,$data,true);
  161. foreach($devices as $item){
  162. $response['rows'][] = array(
  163. 'label'=>html_entity_decode($item->label, ENT_QUOTES),
  164. 'id'=>$item->id,
  165. );
  166. }
  167. });
  168. //Récuperation valeur composant depuis l'uid
  169. Action::register('{{entity}}_by_uid',function(&$response){
  170. global $myUser,$_;
  171. if (!$myUser->connected()) throw new Exception("Vous devez être connecté",401);
  172. require_once(__DIR__.SLASH.'{{Entity}}.class.php');
  173. $response['items'] = array();
  174. $query = 'SELECT main.* FROM '.{{Entity}}::tableName().' main WHERE main.id IN(';
  175. $query .= implode(',', array_fill(0, count($_['items']), '?'));
  176. $query .= ')';
  177. foreach({{Entity}}::staticQuery($query,$_['items'],true) as $item) {
  178. $row = $item->toArray();
  179. $row['label'] = html_entity_decode($row['label'], ENT_QUOTES);
  180. $response['items'][$row['id']] = $row;
  181. }
  182. });{{/component}}
  183. {{#widget}}Action::register('{{plugin}}_widget_load',function(&$response){
  184. Plugin::need('dashboard/DashboardWidget');
  185. User::check_access('{{plugin}}','read');
  186. $widget = DashboardWidget::current();
  187. $widget->title = 'Widget {{Plugin}}';
  188. ob_start();
  189. //Décommenter après avoir créé widget.php
  190. //require_once(__DIR__.SLASH.'widget.php');
  191. //$widget->content = ob_get_clean();
  192. $widget->content = 'Widget non développé';
  193. $response = $widget->toArray();
  194. });
  195. {{/widget}}
  196. ?>