action.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /** WORKFLOW **/
  3. //Récuperation d'une liste de workflow
  4. Action::register('workflow_workflow_search',function(&$response){
  5. global $_;
  6. User::check_access('workflow','read');
  7. require_once(__DIR__.SLASH.'Workflow.class.php');
  8. Workflow::create();
  9. // OPTIONS DE RECHERCHE, A ACTIVER POUR UNE RECHERCHE AVANCEE
  10. $query = 'SELECT * FROM '.Workflow::tableName().' WHERE 1';
  11. $data = array();
  12. //Recherche simple
  13. if(!empty($_['filters']['keyword'])){
  14. $query .= ' AND label LIKE ?';
  15. $data[] = '%'.$_['filters']['keyword'].'%';
  16. }
  17. //Recherche avancée
  18. if(isset($_['filters']['advanced'])) filter_secure_query($_['filters']['advanced'],array('label'),$query,$data);
  19. //Tri des colonnes
  20. if(isset($_['sort'])) sort_secure_query($_['sort'],array('label'),$query,$data);
  21. //Pagination
  22. $response['pagination'] = Workflow::paginate(20,(!empty($_['page'])?$_['page']:0),$query,$data);
  23. $workflows = Workflow::staticQuery($query,$data,true,0);
  24. foreach($workflows as $workflow){
  25. $row = $workflow->toArray();
  26. $row['type'] = Workflow::types($row['type']);
  27. $row['label'] = truncate(html_entity_decode($row['label']),60);
  28. $response['rows'][] = $row;
  29. }
  30. });
  31. //Ajout ou modification d'élément workflow
  32. Action::register('workflow_workflow_save',function(&$response){
  33. global $_;
  34. User::check_access('workflow','edit');
  35. require_once(__DIR__.SLASH.'Workflow.class.php');
  36. $item = Workflow::provide();
  37. $item->label = $_['label'];
  38. $item->icon = $_['icon'];
  39. $item->type = $_['type'];
  40. $item->entity = $_['entity'];
  41. $item->color = $_['color'];
  42. $item->cause = !empty($_['cause']) ? json_encode($_['cause']) : '{}';
  43. $item->save();
  44. $response = $item->toArray();
  45. });
  46. //Suppression d'élement workflow
  47. Action::register('workflow_workflow_delete',function(&$response){
  48. global $_;
  49. User::check_access('workflow','delete');
  50. if(empty($_['id'])) throw new Exception("Id non spécifiée");
  51. require_once(__DIR__.SLASH.'Workflow.class.php');
  52. require_once(__DIR__.SLASH.'WorkflowEffect.class.php');
  53. require_once(__DIR__.SLASH.'WorkflowEvent.class.php');
  54. Workflow::deleteById($_['id']);
  55. WorkflowEvent::delete(array('workflow'=>$_['id']));
  56. WorkflowEffect::delete(array('workflow'=>$_['id']));
  57. });
  58. //Lancement manuel d'un workflow
  59. Action::register('workflow_workflow_run',function(&$response){
  60. global $_;
  61. User::check_access('workflow','edit');
  62. require_once(__DIR__.SLASH.'Workflow.class.php');
  63. require_once(__DIR__.SLASH.'WorkflowEvent.class.php');
  64. $item = Workflow::provide();
  65. $parameters = array();
  66. WorkflowEvent::trigger($_['eventSlug'],$parameters,$item->id);
  67. });
  68. //Sauvegarde des configurations de workflow
  69. Action::register('workflow_setting_save',function(&$response){
  70. global $_,$conf;
  71. User::check_access('workflow','configure');
  72. //Si input file "multiple", possibilité de normlaiser le
  73. //tableau $_FILES récupéré avec la fonction => normalize_php_files();
  74. foreach(Configuration::setting('workflow') as $key=>$value){
  75. if(!is_array($value)) continue;
  76. $allowed[] = $key;
  77. }
  78. foreach ($_['fields'] as $key => $value) {
  79. if(in_array($key, $allowed))
  80. $conf->put($key,$value);
  81. }
  82. });
  83. //Récuperation des effets, des causes et des évenements pour le workflow courant
  84. Action::register('workflow_effect_search',function(&$response){
  85. global $_;
  86. User::check_access('workflow','read');
  87. require_once(__DIR__.SLASH.'WorkflowEffect.class.php');
  88. require_once(__DIR__.SLASH.'Workflow.class.php');
  89. $workflow = Workflow::provide('workflow');
  90. //Récuperation des effets déja définis
  91. foreach(WorkflowEffect::loadAll(array('workflow'=>$workflow->id),array('sort')) as $effect){
  92. $row = $effect->toArray();
  93. $row['workflow'] = $workflow->toArray();
  94. $classType = WorkflowEffect::types($row['type']);
  95. $row['type'] = $classType::manifest();
  96. $row['form'] = $classType::form(json_decode($effect->values,true));
  97. $response['rows'][] = $row;
  98. }
  99. });
  100. //Récuperation des effets, des causes et des évenements pour le workflow courant
  101. Action::register('workflow_cause_search',function(&$response){
  102. global $_;
  103. User::check_access('workflow','read');
  104. require_once(__DIR__.SLASH.'WorkflowEffect.class.php');
  105. require_once(__DIR__.SLASH.'WorkflowEvent.class.php');
  106. require_once(__DIR__.SLASH.'Workflow.class.php');
  107. $workflow = Workflow::provide('workflow');
  108. //Si le workflow est de type entité ou liste d'entité on récupere le type d'entité concerné dans $entity
  109. // pour la fournir aux causes qui utilisent les colonnes / label de l'entité
  110. $entity = array();
  111. if( in_array($workflow->type, array(Workflow::TYPE_ENTITY)) ){
  112. foreach(WorkflowEvent::events(null,Workflow::TYPE_ENTITY) as $eventGroup) {
  113. if($eventGroup['entity']['slug'] == $workflow->entity){
  114. $entity = $eventGroup['entity'];
  115. break;
  116. }
  117. }
  118. }else if(in_array($workflow->type, array(Workflow::TYPE_LIST)) ){
  119. foreach(WorkflowEvent::events(null,Workflow::TYPE_LIST) as $eventGroup) {
  120. if($eventGroup['entity']['slug'] == $workflow->entity){
  121. $entity = $eventGroup['entity'];
  122. break;
  123. }
  124. }
  125. }
  126. $response['filters'] = array();
  127. //Récuperation des causes disponibles pour ce ttype de workflow
  128. $availableCauses = array();
  129. foreach(Workflow::causes(null,$workflow->type) as $cause){
  130. //Si la cause demande un type de filtre custom, on demande a l'ui de l'ajouter en caché sur la page
  131. if(method_exists ($cause,'filter')) $response['filters'][] = $cause::filter($entity);
  132. $availableCauses[] = array(
  133. 'slug' => $cause::manifest('slug'),
  134. 'attributes' => $cause::manifest('attributes'),
  135. 'label' => $cause::manifest('label'),
  136. );
  137. }
  138. //Récuperation des causes déja définies pour le workflow
  139. $enabledCauses = json_encode(filters_set(json_decode($workflow->cause,true)));
  140. //Envois des causes disponibles et des causes définies
  141. $response['causes'] = array( 'available' => $availableCauses, 'enabled' => $enabledCauses) ;
  142. });
  143. /** WORKFLOW EFFECT **/
  144. //Ajout ou modification d'élément workfloweffect
  145. Action::register('workflow_workflow_effect_save',function(&$response){
  146. global $_;
  147. User::check_access('workflow','edit');
  148. require_once(__DIR__.SLASH.'WorkflowEffect.class.php');
  149. $item = WorkflowEffect::provide();
  150. if(!empty($_['workflow'])) $item->workflow = $_['workflow'];
  151. if(!empty($_['effect-type'])) $item->type = $_['effect-type'];
  152. if(!empty($_['values'])) $item->values = json_encode($_['values']);
  153. if(!isset($item->sort)) $item->sort = 1000;
  154. $item->save();
  155. $response = $item->toArray();
  156. });
  157. //Tri des effets
  158. Action::register('workflow_workflow_effect_sort',function(&$response){
  159. global $_;
  160. User::check_access('workflow','edit');
  161. require_once(__DIR__.SLASH.'WorkflowEffect.class.php');
  162. foreach ($_['sort'] as $sort=>$id ) {
  163. $item = WorkflowEffect::getById($id);
  164. $item->sort = $sort;
  165. $item->save();
  166. }
  167. });
  168. //Récuperation ou edition d'élément workfloweffect
  169. Action::register('workflow_workflow_effect_edit',function(&$response){
  170. global $_;
  171. User::check_access('workflow','edit');
  172. require_once(__DIR__.SLASH.'WorkflowEffect.class.php');
  173. require_once(__DIR__.SLASH.'Workflow.class.php');
  174. $response = WorkflowEffect::getById($_['id'],1);
  175. });
  176. //Suppression d'élement workfloweffect
  177. Action::register('workflow_workflow_effect_delete',function(&$response){
  178. global $_;
  179. User::check_access('workflow','delete');
  180. require_once(__DIR__.SLASH.'WorkflowEffect.class.php');
  181. WorkflowEffect::deleteById($_['id']);
  182. });
  183. /** WORKFLOW EVENT **/
  184. //Récuperation d'une liste de workflowevent
  185. Action::register('workflow_event_search',function(&$response){
  186. global $_;
  187. User::check_access('workflow','read');
  188. require_once(__DIR__.SLASH.'WorkflowEvent.class.php');
  189. require_once(__DIR__.SLASH.'Workflow.class.php');
  190. //Récuperation des évenements définis pour le workflow
  191. $workflowevents = WorkflowEvent::loadAll(array('workflow'=>$_['workflow']));
  192. foreach($workflowevents as $workflowevent){
  193. $row = WorkflowEvent::events($workflowevent->slug);
  194. $row['id'] = $workflowevent->id;
  195. $row['slug'] = $workflowevent->slug;
  196. $response['rows'][] = $row;
  197. }
  198. //Récuperation des évenements disponibles pour ce type de workflow
  199. $response['events'] = array();
  200. foreach(WorkflowEvent::events(null,$_['type']) as $eventGroup){
  201. if(!empty($_['entity']) && (!isset($eventGroup['entity']['slug']) || $eventGroup['entity']['slug']!=$_['entity'] ) ) continue;
  202. $response['events'] = array_merge($response['events'] ,$eventGroup['events']);
  203. }
  204. });
  205. //Ajout ou modification d'élément workflowevent
  206. Action::register('workflow_workflow_event_save',function(&$response){
  207. global $_;
  208. User::check_access('workflow','edit');
  209. require_once(__DIR__.SLASH.'WorkflowEvent.class.php');
  210. $item = WorkflowEvent::provide();
  211. $item->slug = $_['event'];
  212. $item->workflow = $_['workflow'];
  213. $item->save();
  214. });
  215. //Suppression d'élement workflowevent
  216. Action::register('workflow_workflow_event_delete',function(&$response){
  217. global $_;
  218. User::check_access('workflow','delete');
  219. require_once(__DIR__.SLASH.'WorkflowEvent.class.php');
  220. WorkflowEvent::deleteById($_['id']);
  221. });
  222. ?>