Dictionnary.class.php 2.2 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){
  30. $listArray = array();
  31. $lists = self::bySlug($slug, $childs);
  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){
  39. if($childs) return self::childs(array('slug'=>$slug));
  40. return self::load(array("slug"=>$slug));
  41. }
  42. public static function childs($param = array()) {
  43. $childs = array();
  44. $parent = self::load($param);
  45. if(!$parent) return $childs;
  46. foreach (self::loadAll(array('parent'=>$parent->id, 'state'=>self::ACTIVE), array( ' label ASC ')) as $child) {
  47. $childs[] = $child;
  48. }
  49. return $childs;
  50. }
  51. public static function hierarchy($id, &$elements=array()) {
  52. $current = self::getById($id);
  53. if(!$current) return array();
  54. if($current->parent != 0) {
  55. $parent = self::load(array('id' => $current->parent));
  56. $children = self::childs(array('id'=>$parent->id));
  57. $parent->childs = array();
  58. foreach ($children as $i => $child) {
  59. if ($child->id == $current->id) {
  60. $child = !empty($elements) ? $elements : $child;
  61. $child->selected = true;
  62. } else {
  63. $child->childs = array();
  64. $child->childs = self::childs(array('id'=>$child->id));
  65. }
  66. $parent->childs[] = $child;
  67. }
  68. $elements = $parent;
  69. return self::hierarchy($parent->id, $elements);
  70. } else {
  71. $elements->selected = true;
  72. return $elements;
  73. }
  74. }
  75. }