Dictionnary.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 Dictionnary 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 Dictionnary();
  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. if(is_numeric($idOrSlug)){
  67. $current = self::getById($idOrSlug);
  68. }else{
  69. $current = self::bySlug($idOrSlug);
  70. }
  71. if(!$current) return array();
  72. if(($current->parent == 0 && $slug != '') || ($current->parent == -1 && $slug == '')) return $elements;
  73. //gestion du slug racine
  74. if($slug == ''){
  75. $parent = new Dictionnary();
  76. $parent->id = 0;
  77. //gestion des slugs classiques
  78. }else{
  79. $parent = self::load(array('id' => $current->parent));
  80. }
  81. $children = self::childs(array('id'=>$parent->id));
  82. $parent->childs = array();
  83. foreach($children as $child){
  84. if($child->id == $current->id) {
  85. $child = !empty($elements) ? $elements : $child;
  86. $child->selected = true;
  87. }else{
  88. $child->childs = array();
  89. $child->childs = self::childs(array('id'=>$child->id));
  90. }
  91. $parent->childs[] = $child;
  92. }
  93. $elements = $parent;
  94. return self::hierarchy($parent->id, $slug, $elements);
  95. }
  96. //Supprime un dictionnary et ses enfants de manières récursive
  97. public static function remove($slug){
  98. $childs = self::bySlug($slug,true);
  99. self::delete(array('slug'=>$slug));
  100. if(!is_array($childs)) return;
  101. foreach ($childs as $item)
  102. self::remove($item->slug);
  103. }
  104. }