12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- /**
- * Manage application and plugins lists with key/value pair.
- *
- * @author valentin carruesco
- *
- * @category Core
- *
- * @license copyright
- */
- class Dictionnary extends Entity
- {
- public $id,$slug,$label,$parent,$state,$sublistlabel;
- protected $fields =
- array(
- 'id' => 'key',
- 'slug' => 'string',
- 'label' => 'string',
- 'parent' => 'int',
- 'state' => 'string',
- 'sublistlabel' => 'longstring'
- );
- public function __construct()
- {
- parent::__construct();
- }
- //Retourne une liste formatée en fonction de son slug
- //(retourne ses enfants si le parametre childs est à true)
- public static function slugToArray($slug, $childs = false){
- $listArray = array();
- $lists = self::bySlug($slug, $childs);
- if(!$childs) return $lists;
- foreach($lists as $list)
- $listArray[$list->id] = $list;
- return $listArray;
- }
- //Retourne une liste en fonction de son slug (retourne ses enfants si le parametre childs est à true)
- public static function bySlug($slug, $childs = false){
- if($childs) return self::childs(array('slug'=>$slug));
- return self::load(array("slug"=>$slug));
- }
- public static function childs($param = array()) {
- $childs = array();
- $parent = self::load($param);
- if(!$parent) return $childs;
- foreach (self::loadAll(array('parent'=>$parent->id, 'state'=>self::ACTIVE), array( ' label ASC ')) as $child) {
- $childs[] = $child;
- }
- return $childs;
- }
- public static function hierarchy($id, &$elements=array()) {
- $current = self::getById($id);
- if(!$current) return array();
- if($current->parent != 0) {
- $parent = self::load(array('id' => $current->parent));
- $children = self::childs(array('id'=>$parent->id));
- $parent->childs = array();
- foreach ($children as $i => $child) {
- if ($child->id == $current->id) {
- $child = !empty($elements) ? $elements : $child;
- $child->selected = true;
- } else {
- $child->childs = array();
- $child->childs = self::childs(array('id'=>$child->id));
- }
- $parent->childs[] = $child;
- }
- $elements = $parent;
- return self::hierarchy($parent->id, $elements);
- } else {
- $elements->selected = true;
- return $elements;
- }
- }
-
- }
|