Dictionnary.class.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 copyright
  10. */
  11. class Dictionnary extends Entity
  12. {
  13. public $id,$slug,$label,$parent,$state,$sublistlabel;
  14. protected $fields =
  15. array(
  16. 'id' => 'key',
  17. 'slug' => 'string',
  18. 'label' => 'string',
  19. 'parent' => 'int',
  20. 'state' => 'string',
  21. 'sublistlabel' => 'longstring'
  22. );
  23. public function __construct()
  24. {
  25. parent::__construct();
  26. }
  27. //Retourne une liste formatée en fonction de son slug
  28. //(retourne ses enfants si le parametre childs est à true)
  29. public static function slugToArray($slug, $childs = false, $state = self::ACTIVE){
  30. $listArray = array();
  31. $lists = self::bySlug($slug, $childs, $state);
  32. if(!$childs) return $lists;
  33. foreach($lists as $list)
  34. $listArray[$list->id] = $list;
  35. return $listArray;
  36. }
  37. //Retourne une liste en fonction de son slug (retourne ses enfants si le parametre childs est à true)
  38. public static function bySlug($slug, $childs = false, $state = self::ACTIVE){
  39. if($childs) return self::childs(array('slug'=>$slug,'state:IN'=>$state));
  40. return self::load(array("slug"=>$slug,'state:IN'=>$state));
  41. }
  42. public static function childs($param = array()) {
  43. $childs = array();
  44. $parent = self::load($param);
  45. if(!$parent) return $childs;
  46. $state = isset($param['state:IN']) ? $param['state:IN'] : self::ACTIVE;
  47. foreach (self::loadAll(array('parent'=>$parent->id, 'state:IN'=>$state), array( ' label ASC ')) as $child) {
  48. $childs[] = $child;
  49. }
  50. return $childs;
  51. }
  52. public static function hierarchy($id, &$elements=array()) {
  53. $current = self::getById($id);
  54. if(!$current) return array();
  55. if($current->parent != 0) {
  56. $parent = self::load(array('id' => $current->parent));
  57. $children = self::childs(array('id'=>$parent->id));
  58. $parent->childs = array();
  59. foreach ($children as $i => $child) {
  60. if ($child->id == $current->id) {
  61. $child = !empty($elements) ? $elements : $child;
  62. $child->selected = true;
  63. } else {
  64. $child->childs = array();
  65. $child->childs = self::childs(array('id'=>$child->id));
  66. }
  67. $parent->childs[] = $child;
  68. }
  69. $elements = $parent;
  70. return self::hierarchy($parent->id, $elements);
  71. } else {
  72. $elements->selected = true;
  73. return $elements;
  74. }
  75. }
  76. }