EntityEffect.class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /*
  3. Effet de workflow
  4. Modifie une entité
  5. */
  6. class EntityEffect{
  7. //Descriptif du type d'effet
  8. public static function manifest($key = null){
  9. $manifest = array(
  10. 'slug' => 'entity',
  11. 'label' => 'Modifier/Supprimer une entité',
  12. 'class' => get_called_class(),
  13. 'path' => __FILE__,
  14. 'icon' => 'fas fa-database',
  15. 'color' => '#ff9f43'
  16. );
  17. if(!isset($key)) return $manifest;
  18. return isset($manifest[$key]) ? $manifest[$key] : '' ;
  19. }
  20. public static function actions($key = null){
  21. $actions = array(
  22. 'delete' => array('label' => 'Supprimer'),
  23. 'update' => array('label' => 'Modifier')
  24. );
  25. if(!isset($key)) return $actions;
  26. return isset($actions[$key]) ? $actions[$key] : array('label'=>'Non définit');
  27. }
  28. //méthode d'affichage de l'effet
  29. public static function form($item){
  30. $html = '';
  31. $class = get_called_class();
  32. ob_start();
  33. require_once(__DIR__.SLASH.'..'.SLASH.'WorkflowEvent.class.php');
  34. $events = WorkflowEvent::events(null,Workflow::TYPE_LIST);
  35. $randomId = sha1(mt_rand(0,1000).time());
  36. $conditions = !empty($item['effect-conditions']) ? base64_encode(json_encode(filters_set($item['effect-conditions']['advanced']))) : '';
  37. $changes = !empty($item['entity-change']) ? base64_encode(utf8_decode($item['entity-change'])) : '';
  38. ?>
  39. <div class="effect-block" data-id="<?php echo $randomId; ?>">
  40. <div class="input-group">
  41. <div class="input-group-text input-group-append input-group-prepend ">
  42. Action
  43. </div>
  44. <select data-id="entity-action" onchange="workflow_effect_entity_action($(this).closest('.workflow-effect-form'))" class="form-control entity-action" required>
  45. <option value = "">-</option>
  46. <?php foreach (self::actions() as $key => $value): ?>
  47. <option <?php echo !empty($item['entity-action']) && $key==$item['entity-action'] ? 'selected="selected"':'' ; ?> value="<?php echo $key; ?>"><?php echo $value['label']; ?></option>
  48. <?php endforeach; ?>
  49. </select>
  50. <div class="input-group-text input-group-prepend">
  51. Entité
  52. </div>
  53. <select onchange="workflow_effect_entity_conditions($(this).closest('.workflow-effect-form'))" data-id="entity-entity" class="form-control entity-entity" required>
  54. <option value = "">-</option>
  55. <?php foreach($events as $event): ?>
  56. <option data-entity="<?php echo base64_encode(json_encode($event['entity'])); ?>" <?php echo isset($item['entity-entity']) && $item['entity-entity']== $event['entity']['slug'] ? 'selected="selected"' : ''; ?> value="<?php echo $event['entity']['slug']; ?>">
  57. <?php echo $event['entity']['label']; ?>
  58. </option>
  59. <?php endforeach; ?>
  60. </select>
  61. </div>
  62. <div class="entity-effect-where-block <?php echo empty($item['entity-entity'])?'hidden':''; ?>" data-conditions="<?php echo $conditions; ?>">
  63. <hr/>
  64. <h5 class="text-muted">Conditions <i class="far fa-question-circle right" title="Utiliser le mot clé {{current}} pour acceder à l'entitée courant, exemple {{current.id}} pour récuperer son id"></i></h5>
  65. <div class="clear"></div>
  66. <div class="entity-effect-conditions"></div>
  67. </div>
  68. <div class="entity-effect-change-block <?php echo !empty($item['entity-action']) && $item['entity-action']!='update'?'hidden':''; ?>" data-changes="<?php echo $changes; ?>">
  69. <hr/>
  70. <h5 class="text-muted">Modifier les colonnes</h5>
  71. <div class="entity-effect-change">
  72. <select class="form-control form-control-sm effect-change" onchange="effect_entity_change_add($(this).closest('.workflow-effect-form') )"></select>
  73. <input type="hidden" data-id="entity-change" value="<?php echo isset($item['entity-change'])?$item['entity-change']:''; ?>">
  74. <ul class="list-group mt-2 effect-change-fields">
  75. <li class="list-group-item p-2 hidden" data-slug="{{slug}}" data-label="{{label}}" data-field-type="{{type}}">
  76. <div class="input-group input-group-sm">
  77. <div class="input-group-prepend ">
  78. <div class="input-group-text ">{{label}} <small class="text-primary font-weight-bold pl-2"> =</small></div>
  79. </div>
  80. <input value="{{value}}" onblur="effect_entity_change_save(this)" class="form-control value" type="text">
  81. <div class="btn btn-danger btn-sm" onclick="effect_entity_change_remove(this)"><i class="fas fa-trash"></i></div>
  82. </div>
  83. </li>
  84. </ul>
  85. </div>
  86. </div>
  87. </div>
  88. <script type="text/javascript">
  89. function workflow_effect_entity_conditions(form){
  90. var select = form.find('.entity-entity');
  91. var option = $('option:selected',select);
  92. $('.entity-effect-conditions,.effect-change',form).html('<option value="">-</option>');
  93. if(select.val()==''){
  94. $('.entity-effect-where-block',form).addClass('hidden');
  95. return;
  96. }
  97. var defaultConditions = $('.entity-effect-where-block',form).attr('data-conditions');
  98. if(defaultConditions){
  99. defaultConditions = atob(defaultConditions);
  100. }
  101. var entity = JSON.parse(atob(option.attr('data-entity')));
  102. var html = '<select data-id="effect-conditions" data-type="filter" data-default=\''+defaultConditions+'\' data-only-advanced>';
  103. for(var slug in entity.fields){
  104. var field = entity.fields[slug];
  105. html += '<option value="'+slug+'" data-filter-type="text">'+field.label+'</option>';
  106. $('.effect-change',form).append('<option value="'+slug+'" data-filter-type="text">'+field.label+'</option>');
  107. }
  108. html += '</select>';
  109. var whereSelect = $(html);
  110. $('.entity-effect-conditions',form).html(whereSelect);
  111. $('.entity-effect-where-block',form).removeClass('hidden');
  112. init_components(form);
  113. }
  114. function workflow_effect_entity_action(form){
  115. var select = form.find('.entity-action');
  116. if(select.val()=='update'){
  117. $('.entity-effect-change-block',form).removeClass('hidden');
  118. }else{
  119. $('.entity-effect-change-block',form).addClass('hidden');
  120. }
  121. }
  122. function effect_entity_change_add(form,data){
  123. var btn = form.find('.btn-add');
  124. var entity = JSON.parse(atob($('[data-id="entity-entity"] option:selected').attr('data-entity')));
  125. if(!data){
  126. var data = entity.fields[$('.effect-change',form).val()];
  127. data.slug = $('.effect-change',form).val();
  128. }
  129. var line = $('.effect-change-fields li:eq(0)').template(data);
  130. $('.effect-change-fields',form).append(line);
  131. effect_entity_change_save(btn);
  132. }
  133. function effect_entity_change_remove(element){
  134. $(element).closest('li').remove();
  135. effect_entity_change_save(element);
  136. }
  137. function effect_entity_change_search(form){
  138. var changes = $('.entity-effect-change-block',form).attr('data-changes');
  139. changes = changes ? JSON.parse(atob(changes)) : '';
  140. for(var key in changes){
  141. var change = changes[key];
  142. effect_entity_change_add(form,change);
  143. }
  144. }
  145. function effect_entity_change_save(element){
  146. var form = $(element).closest('.workflow-effect-form');
  147. var json = [];
  148. $('.effect-change-fields .list-group-item:not(:eq(0))').each(function(i,line){
  149. var line = $(line);
  150. json.push({
  151. value : line.find('.value').val(),
  152. slug : line.attr('data-slug'),
  153. label : line.attr('data-label')
  154. });
  155. });
  156. $('[data-id="entity-change"]',form).val(JSON.stringify(json));
  157. }
  158. function workflow_effect_entity_init(tr){
  159. var form = tr.find('.workflow-effect-form');
  160. workflow_effect_entity_conditions(form)
  161. effect_entity_change_search(form,function(){
  162. effect_entity_change_save(form.find('.entity-entity'));
  163. });
  164. }
  165. </script>
  166. <?php
  167. $html = ob_get_clean();
  168. return $html;
  169. }
  170. public static function run($effect,$parameters = array()){
  171. global $conf;
  172. $logs = '';
  173. if( in_array($parameters['workflow']['type'] , array( Workflow::TYPE_ENTITY, Workflow::TYPE_LIST)) ){
  174. if(isset($parameters['current'])) $parameters['current'] = $parameters['current']->toArray();
  175. if(isset($parameters['old'])) $parameters['old'] = $parameters['old']->toArray();
  176. }
  177. $effect->values['entity-entity'] = template($effect->values['entity-entity'],$parameters,true);
  178. //var_dump($effect,$parameters);
  179. $entitySlug = $effect->values['entity-entity'];
  180. $entity = array();
  181. foreach ( WorkflowEvent::events(null,Workflow::TYPE_LIST) as $event) {
  182. if($event['entity']['slug'] == $entitySlug){
  183. $entity = $event['entity'];
  184. break;
  185. }
  186. }
  187. //var_dump($effect);
  188. $filters = array();
  189. foreach($effect->values['effect-conditions']['advanced'] as $filter){
  190. if(isset($filter['value']) && isset($filter['value'][0])) $filter['value'][0] = template($filter['value'][0],$parameters,true);
  191. $filters[] = $filter;
  192. }
  193. $whereQuery = 'SELECT id FROM {{table}} WHERE 1 ';
  194. $queryData = array();
  195. filter_secure_query($filters,array_keys($entity['fields']),$whereQuery,$queryData);
  196. $targetsQuery = $entity['class']::staticQuery($whereQuery,$queryData);
  197. foreach ($targetsQuery->fetchAll() as $line)
  198. $targetsIds[] = $line['id'];
  199. //récuperation de l'entité cible de l'effet
  200. require_once($entity['file']);
  201. switch($effect->values['entity-action']){
  202. case 'update':
  203. $changes = array();
  204. foreach(json_decode($effect->values['entity-change'],true) as $field){
  205. $changes[$field['slug']] = $field['value'];
  206. }
  207. $entity['class']::change($changes,array('id:IN'=>$targetsIds));
  208. break;
  209. case 'delete':
  210. $entity['class']::delete(array('id:IN'=>$targetsIds));
  211. break;
  212. }
  213. $logs .= '<i class="'.self::manifest('icon').'"></i> Lancement de "'.self::manifest('label').'"<br>';
  214. $results = true;
  215. $logs .= 'Résultat '.$results.'<br>';
  216. return $logs;
  217. }
  218. }
  219. ?>