History.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Define a history.
  4. * @author Valentin CARRUESCO
  5. * @category Core
  6. * @license MIT
  7. */
  8. class History extends Entity{
  9. public $id;
  10. public $scope; //Scope (plugin, page...) (Texte)
  11. public $uid; //Item ID/UID concerné (Texte)
  12. public $comment; //Commentaire (Texte Long)
  13. public $type; //Type d'historique (Texte)
  14. public $importance; //Importance de message (Texte)
  15. public $meta; //Meta données (Texte Long)
  16. public $sort; //Ordre (int)
  17. const TYPE_READ = 'read';
  18. const TYPE_COMMENT = 'comment';
  19. const TYPE_EDIT = 'edit';
  20. const TYPE_DELETE = 'delete';
  21. const IMPORTANCE_NORMAL = 'normal';
  22. const IMPORTANCE_IMPORTANT = 'important';
  23. public $entityLabel = 'Historique';
  24. protected $TABLE_NAME = 'history';
  25. public $fields = array(
  26. 'id' => array('label'=> 'Identifiant' , 'type'=>'key'),
  27. 'scope' => array('label'=> 'Périmetre de l\'entité liée' , 'type'=>'text'),
  28. 'uid' => array('label'=> 'Identifiant de l\'entité liée' , 'type'=>'text'),
  29. 'comment' => array('label'=> 'Commentaire' , 'type'=>'textarea'),
  30. 'type' => array('label'=> 'Type d\'historique' , 'type'=>'text'),
  31. 'importance' => array('label'=> 'Importance' , 'type'=>'text'),
  32. 'sort' => array('label'=> 'Ordre' , 'type'=>'integer'),
  33. 'meta' => array('label'=> 'Meta informations' , 'type'=>'textarea')
  34. );
  35. function __construct(){
  36. parent::__construct();
  37. $this->importance = 'normal';
  38. }
  39. public static function importance($key=null){
  40. $importances = array(
  41. self::IMPORTANCE_NORMAL => array('label'=>'Normal','color' => '#222222'),
  42. self::IMPORTANCE_IMPORTANT => array('label'=>'Urgent','color' => '#dc3545'),
  43. );
  44. if(!isset($key)) return $importances;
  45. return isset($importances[$key]) ? $importances[$key] : array();
  46. }
  47. //Colonnes indexées
  48. public $indexes = array();
  49. public static function types($key=null){
  50. $types = array(
  51. self::TYPE_READ => array('slug'=>self::TYPE_READ,'label' => 'Consultation','icon' => 'far fa-eye','color' => '#e2ec55'),
  52. self::TYPE_COMMENT => array('slug'=>self::TYPE_COMMENT,'label' => 'Commentaire','icon' => 'far fa-comments','color' => '#6ab04c'),
  53. self::TYPE_EDIT => array('slug'=>self::TYPE_EDIT,'label' => 'Édition','icon' => 'fas fa-marker','color' => '#686de0'),
  54. self::TYPE_DELETE => array('slug'=>self::TYPE_DELETE,'label' => 'Suppression','icon' => 'fas fa-trash-restore','color' => '#eb4d4b')
  55. );
  56. Plugin::callHook('history_type',array(&$types));
  57. if(!isset($key)) return $types;
  58. return isset($types[$key]) ? $types[$key] : array('slug'=>'undefined','label'=>'','icon'=>'','color'=>'');
  59. }
  60. //Ajout rapide d'un historique
  61. public static function put($scope,$uid,$comment,$type=null){
  62. $history = new History();
  63. $history->scope = $scope;
  64. $history->uid = $uid;
  65. $history->comment = $comment;
  66. if(isset($type)) $history->type = $type;
  67. $history->save();
  68. }
  69. // Créé un historique sur changement d'une entité
  70. // prend en compte le scope, l'item dans l'ancien etat, l'item dans l'etat actuel
  71. // et détaille en Human readable les modifications
  72. public static function entityChange($scope,$oldItem,$item){
  73. $differenties = Entity::compare($oldItem,$item);
  74. $fieldTypes = FieldType::available();
  75. $detail = '';
  76. foreach($differenties as $difference){
  77. $fields = $oldItem->fields(false);
  78. $fieldKey = $difference['field'];
  79. if(!isset($fields[$fieldKey])) continue;
  80. $field = $fields[$fieldKey];
  81. $value1 = $difference['value1'];
  82. $value2 = $difference['value2'];
  83. //gestion des transformations de valeurs de liste en dur
  84. if($field['fieldtype']=='list' && method_exists ($item,$fieldKey.'s' ) ){
  85. $method = $fieldKey.'s';
  86. $value1 = $item::$method($difference['value1']);
  87. $value1 =isset($value1['label']) ? $value1['label'] : $difference['value1'];
  88. $value2 = $item::$method($difference['value2']);
  89. $value2 =isset($value2['label']) ? $value2['label'] : $difference['value2'];
  90. //gestion de stransformations de valeurs de fieldtype
  91. }else if(isset($fieldTypes[$field['fieldtype']]) ){
  92. $fieldType = $fieldTypes[$field['fieldtype']];
  93. if(property_exists($fieldType,'onRawDisplay')){
  94. $method = $fieldType->onRawDisplay;
  95. $value1 = $method($difference['value1'], $field);
  96. $value2 = $method($difference['value2'], $field);
  97. }
  98. //si le champs est une foreign key on affiche l'eventuel libellé de cette foreign key
  99. if(isset($field['link'])){
  100. require_once $field['link'];
  101. $class = str_replace('.class.php','',basename($field['link']));
  102. if(property_exists($class,'label')){
  103. $instance1 = $class::getById($difference['value1']);
  104. if($instance1) $value1 = $instance1->label;
  105. $instance2 = $class::getById($difference['value2']);
  106. if($instance2) $value2 = $instance2->label;
  107. }
  108. }
  109. }
  110. $detail.= '<br/>Champ <code>'.$field['label'].'</code> ';
  111. if(empty($value1)){
  112. $detail.= ' ajout de la valeur <span class="font-weight-bold">'.truncate($value2,100).'</span>';
  113. }else if(empty($value2)){
  114. $detail.= ' suppression de la valeur <span class="font-weight-bold">'.truncate($value1,100).'</span>';
  115. }else{
  116. $detail.=' modifié de <span class="font-weight-bold">'.truncate($value1,100).'</span> en <span class="font-weight-bold">'.truncate($value2,100).'</span>';
  117. }
  118. }
  119. $history = new History();
  120. $history->scope = $scope;
  121. $history->uid = $item->id;
  122. $history->comment = (!empty($oldItem->id) ? "Modification":"Création")." ".$detail;
  123. $history->type = History::TYPE_EDIT;
  124. $history->save();
  125. }
  126. }
  127. ?>