Dictionary.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Manage application and plugins lists with key/value pair.
  4. *
  5. * @author valentin carruesco
  6. *
  7. * @category Core
  8. *
  9. * @license MIT
  10. */
  11. class Dictionary extends Entity {
  12. public $id,$slug,$label,$parent,$state,$sublistlabel,$childs;
  13. public $entityLabel = 'Liste configurable';
  14. protected $fields = array(
  15. 'id' => array('label'=>'Identifiant', 'type' => 'key'),
  16. 'slug' => array('label'=>'Identifiant lisible', 'type'=>'text'),
  17. 'label' => array('label'=>'Libellé', 'type' => 'text'),
  18. 'parent' => array('label'=>'ID liste parente', 'type'=>'integer'),
  19. 'state' => array('label'=>'État', 'type'=>'text'),
  20. 'sublistlabel' => array('label'=>'Libellé de la sous liste', 'type'=>'textarea')
  21. );
  22. public function __construct() {
  23. parent::__construct();
  24. }
  25. public function toArray($decoded=false) {
  26. $array = parent::toArray();
  27. $array['childs'] = $this->childs;
  28. return $array;
  29. }
  30. //Retourne une liste formatée en fonction de son slug
  31. //(retourne ses enfants si le parametre childs est à true)
  32. public static function slugToArray($slug, $childs = false, $state = self::ACTIVE){
  33. $listArray = array();
  34. $lists = self::bySlug($slug, $childs, $state);
  35. if(!$childs) return $lists;
  36. foreach($lists as $list)
  37. $listArray[$list->id] = $list;
  38. return $listArray;
  39. }
  40. //Retourne une liste en fonction de son slug (retourne ses enfants si le parametre childs est à true)
  41. public static function bySlug($slug, $childs = false, $state = self::ACTIVE){
  42. if($childs) return self::childs(array('slug'=>$slug,'state:IN'=>$state));
  43. return self::load(array("slug"=>$slug,'state:IN'=>$state));
  44. }
  45. public static function childs($param = array(),$options = array('depth'=>1,'format'=>'object'),$level=1) {
  46. $childs = array();
  47. if(isset($param['id']) && $param['id']==0){
  48. $parent = new Dictionary();
  49. $parent->id = 0;
  50. $parent->label = 'Racine';
  51. }else{
  52. $parent = self::load($param);
  53. }
  54. if(!$parent) return $childs;
  55. $state = isset($param['state:IN']) ? $param['state:IN'] : self::ACTIVE;
  56. foreach (self::loadAll(array('parent'=>$parent->id, 'state:IN'=>$state), array(' label ASC ')) as $child) {
  57. if($options['depth']>$level){
  58. $param['id'] = $child->id;
  59. $child->childs = self::childs($param,$options,$level+1);
  60. }
  61. $childs[] = $options['format'] == 'object' ? $child : $child->toArray();
  62. }
  63. return $childs;
  64. }
  65. public static function hierarchy($idOrSlug, $slug, &$elements=array()) {
  66. $current = (is_numeric($idOrSlug)) ? self::getById($idOrSlug) : self::bySlug($idOrSlug);
  67. if(!$current) return array();
  68. if(($current->parent == 0 && $slug != '') || ($current->parent == -1 && $slug == '')) return $elements;
  69. //gestion du slug racine
  70. if($slug == ''){
  71. $parent = new Dictionary();
  72. $parent->id = 0;
  73. //gestion des slugs classiques
  74. }else{
  75. $parent = self::load(array('id' => $current->parent));
  76. }
  77. $children = self::childs(array('id'=>$parent->id));
  78. $parent->childs = array();
  79. foreach($children as $child){
  80. if($child->id == $current->id) {
  81. $child = !empty($elements) ? $elements : $child;
  82. $child->selected = true;
  83. }else{
  84. $child->childs = array();
  85. $child->childs = self::childs(array('id'=>$child->id));
  86. }
  87. $parent->childs[] = $child;
  88. }
  89. $elements = $parent;
  90. return self::hierarchy($parent->id, $slug, $elements);
  91. }
  92. //Supprime un dictionary et ses enfants de manières récursive
  93. public static function remove($slug){
  94. $childs = self::bySlug($slug,true);
  95. self::delete(array('slug'=>$slug));
  96. if(!is_array($childs)) return;
  97. foreach ($childs as $item)
  98. self::remove($item->slug);
  99. }
  100. }