EntityFieldCause.class.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. Cause de workflow
  4. Compare un champ de l'entité ciblé à la valeur définie dans la cause
  5. Ne s'applique qu'aux workflows de type entité et liste
  6. */
  7. class EntityFieldCause{
  8. //Descriptif de la cause
  9. public static function manifest($key = null){
  10. $manifest = array(
  11. 'slug' => 'entity-field',
  12. 'label' => 'Entité',
  13. 'type' => array(Workflow::TYPE_ENTITY,Workflow::TYPE_LIST),
  14. 'class' => get_called_class(),
  15. 'path' => __FILE__,
  16. 'icon' => 'fas fa-leaf',
  17. 'attributes' => ' data-filter-type="entity-field" ',
  18. 'color' => '#ff9f43',
  19. );
  20. if(!isset($key)) return $manifest;
  21. return isset($manifest[$key]) ? $manifest[$key] : '' ;
  22. }
  23. //Type de filtre de cause custom pour cette cause (champs additionnels)
  24. public static function filter($entity = array()){
  25. $stream = '<div class="filter-value-block" data-value-type="entity-field" data-value-selector=".filter-value:last-child">
  26. <div class="filter-subfield d-inline-block">
  27. '.$entity['label'].'.
  28. <select data-id="entity-field" class="d-inline-block mt-2 border-0 font-weight-bold text-primary" value="Ma colonne">
  29. <option value="">- Attribut - </option>';
  30. if(isset($entity['fields'] )):
  31. foreach($entity['fields'] as $slug=>$attribute):
  32. $stream .=' <option data-attribute-type="'.$attribute['type'].'" value="'.$slug.'">'.$attribute['label'].'</option>';
  33. endforeach;
  34. endif;
  35. $stream .='</select>
  36. </div>
  37. <select class="form-control filter-operator border-0 text-primary">
  38. <option data-default-view="'.base64_encode(json_encode(array('view' => "text"))).'" value="=">Égal</option>
  39. <option data-default-view="'.base64_encode(json_encode(array('view' => "text"))).'" value="!=">Différent</option>
  40. <option data-default-view="'.base64_encode(json_encode(array('view' => "text"))).'" value=">">Inférieur</option>
  41. <option data-default-view="'.base64_encode(json_encode(array('view' => "text"))).'" value="<">Supérieur</option>
  42. <option data-default-view="'.base64_encode(json_encode(array('view' => "text"))).'" value="like">Contient</option>
  43. </select>
  44. </div>';
  45. return $stream;;
  46. }
  47. //Méthode de vérification de la cause
  48. public static function check($filter,$parameters = array()){
  49. if(!isset($parameters['current'])) throw new Exception("Aucun item entité n'est présent");
  50. $column = $filter['subcolumn']['entity-field'];
  51. $class = get_class($parameters['current']);
  52. $columnValue = null;
  53. //la colonne évoquée peut être un attribut de classe ou une méthode de classe
  54. if( property_exists ( $class , $column ) ) $columnValue = $parameters['current']->$column;
  55. if( method_exists ( $class , $column ) ) $columnValue = $parameters['current']->$column();
  56. switch($filter['operator']){
  57. case '=':
  58. if(!isset($columnValue) ) return false;
  59. if($columnValue != $filter['value'][0] ) return false;
  60. break;
  61. case '!=':
  62. if(isset($columnValue) && $columnValue == $filter['value'][0] ) return false;
  63. break;
  64. case '<':
  65. if($columnValue > $filter['value'][0]) return false;
  66. break;
  67. case '>':
  68. if($columnValue < $filter['value'][0]) return false;
  69. break;
  70. case 'like':
  71. if( strpos( mb_strtolower($columnValue), mb_strtolower($filter['value'][0]) ) === false ) return false;
  72. break;
  73. }
  74. return true;
  75. }
  76. }
  77. ?>