WorkflowEvent.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Define a workflowevent.
  4. * Un evenement peut etre déclaré (register) puis executé (triiger) et
  5. * lancement de ce fait tous les workflow attachés a celui ci
  6. * @author Administrateur
  7. * @category Plugin
  8. * @license MIT
  9. */
  10. class WorkflowEvent extends Entity{
  11. public $id;
  12. public $slug; //Slug (Texte)
  13. public $workflow; //Workflow (Entier)
  14. const EVENT_CRON = 'cron';
  15. protected $TABLE_NAME = 'workflow_workflow_event';
  16. public $fields = array(
  17. 'id' => 'key',
  18. 'slug' => 'string',
  19. 'workflow' => array('type'=>'int','link'=>'plugin/workflow/Workflow.class.php')
  20. );
  21. //Colonnes indexées
  22. public $indexes = array();
  23. //Renvois les événements (par défaut et plugin) en fonction du slug fournis ou du type de workflow compatibles
  24. public static function events($slug = null,$type=null,$getContext = false){
  25. //Récuperation des événements plugins et par défaut
  26. $events = array();
  27. Plugin::callHook("workflow_event",array(&$events) );
  28. //Si le type est précisé, on ne retourne que les evenements qui correspondent
  29. if(isset($type)){
  30. foreach ($events as $key => $event) {
  31. if($event['type'] != $type) unset($events[$key]);
  32. }
  33. }
  34. if(!isset($slug)) return $events;
  35. //On ne retourne que les evenements et pas les autres infos fournies dans la déclaration de l'evenemtn (register)
  36. foreach ($events as $eventGroup) {
  37. foreach ($eventGroup['events'] as $currentSlug=>$event) {
  38. if($currentSlug == $slug){
  39. if($getContext) return $eventGroup;
  40. return array(
  41. 'label' => $event,
  42. 'type' =>$eventGroup['type']
  43. );
  44. }
  45. }
  46. }
  47. return array(
  48. 'label' => 'Non définit',
  49. 'type' => Workflow::TYPE_GLOBAL
  50. );
  51. }
  52. public static function registerEntity($file,$options = array()){
  53. require_once($file);
  54. $class = str_replace('.class.php','',basename($file));
  55. $plugin = basename(dirname($file));
  56. $slug = strtolower($plugin.'-'.$class);
  57. $instance = new $class();
  58. $fields = array();
  59. foreach ($instance->fields as $field => $type) {
  60. if(isset($options['fields-remove']) && in_array($field, $options['fields-remove'])) continue;
  61. if(!is_array($type)) $type = array('type'=>$type,'column'=>$field);
  62. if(!isset( $type['column'] )) $type['column'] = $field;
  63. $label = isset($type['label'])? $type['label']:$field;
  64. $fields[$field] = array('label'=>$label,'type'=>$type['type']);
  65. }
  66. return array(
  67. 'type' => Workflow::TYPE_ENTITY,
  68. 'entity' => array(
  69. 'label' => $class::entityLabel(),
  70. 'file' => $file,
  71. 'class' => $class,
  72. 'slug' => $slug,
  73. 'fields' => $fields
  74. ),
  75. 'events' => array(
  76. $slug.'-create' => 'A la création',
  77. $slug.'-update' => 'A la modification',
  78. $slug.'-delete' => 'A la suppression'
  79. )
  80. );
  81. }
  82. public static function registerList($file,$options = array()){
  83. require_once($file);
  84. $class = str_replace('.class.php','',basename($file));
  85. $plugin = basename(dirname($file));
  86. $slug = strtolower($plugin.'-'.$class);
  87. $instance = new $class();
  88. $fields = array();
  89. foreach ($instance->fields as $field => $type) {
  90. if(isset($options['fields-remove']) && in_array($field, $options['fields-remove'])) continue;
  91. if(!is_array($type)) $type = array('type'=>$type,'column'=>$field);
  92. if(!isset( $type['column'] )) $type['column'] = $field;
  93. $label = isset($type['label'])? $type['label']:$field;
  94. $fields[$field] = array('label'=>$label,'type'=>$type['type']);
  95. }
  96. $options = array_merge(array('events'=>array()),$options);
  97. return array(
  98. 'type' => Workflow::TYPE_LIST,
  99. 'list' => function(){
  100. require_once($file);
  101. return $class::loadAll();
  102. },
  103. 'entity' => array(
  104. 'label' => $class::entityLabel(),
  105. 'file' => $file,
  106. 'class' => $class,
  107. 'slug' => $slug,
  108. 'fields' => $fields
  109. ),
  110. 'events' => $options['events']
  111. );
  112. }
  113. //lancer un evenement en fonction de son slug et des parametres fournis lors du lancement
  114. public static function trigger($slug,$parameters = array(),$workflow = null){
  115. require_once(__DIR__.SLASH.'Workflow.class.php');
  116. $causes = Workflow::causes();
  117. $logs = '';
  118. $where = array('slug'=>$slug);
  119. if(isset($workflow)) $where['workflow'] = $workflow;
  120. //continue l'execution du workflow même si le navigateur se déconnecte
  121. ignore_user_abort(true);
  122. set_time_limit(0);
  123. //Récuperation des workflows lié a cet évenement
  124. foreach(self::loadAll($where, null, null, array('*'), 1) as $event){
  125. //récuperation du workflow
  126. $workflow = $event->join('workflow');
  127. //Ajout des infos de l'évenement au tableau des parametres utilisé par les causes/effet
  128. $parameters['event'] = $event->toArray();
  129. $eventType = WorkflowEvent::events($event->slug,null,true);
  130. if($eventType['type'] == Workflow::TYPE_LIST && isset($eventType['list'])){
  131. $parameters['list'] = $eventType['list']();
  132. }
  133. //Lancement du workflow concerné (on lui donne les type de causes possible pour des raison de perfs)
  134. $logs = $workflow->run($causes,$parameters);
  135. //Ajout des résultats du lancement à l'historique du workflow
  136. $history = new History();
  137. $history->scope = 'workflow';
  138. $history->uid = $workflow->id;
  139. $history->comment = implode('<br/>',$logs);
  140. $history->icon = History::TYPE_COMMENT;
  141. $history->save();
  142. }
  143. return $logs;
  144. }
  145. }
  146. ?>