Example #1
0
 /**
  * Returns children nodes
  * @return TreeNode[]
  */
 public function getChildren()
 {
     $nodes = array();
     if (!$this->hasChildren()) {
         return $nodes;
     }
     $callback = $this->callback;
     if (is_callable($callback)) {
         $children = call_user_func($callback, $this->getEntity(), $this->options);
     } else {
         $children = hypeCategories()->categories->getSubcategories($this->getEntity(), $this->options, true);
     }
     if (is_array($children)) {
         $children = ItemCollection::create($children)->guids();
     }
     foreach ($children as $child) {
         $nodes[] = new TreeNode($child, $this, $this->callback, $this->options);
     }
     return $nodes;
 }
 /**
  * Replaces entity categories
  *
  * @param ElggEntity $entity     Entity
  * @param array      $categories New categories (guids or entities)
  * @param array      $options    ege* options for retrieving current entity categories
  * @return array
  */
 public function setItemCategories(ElggEntity $entity, array $categories = array(), array $options = array())
 {
     $input_categories = ItemCollection::create($categories)->guids();
     $future_categories = array();
     $current_categories_batch = $this->getItemCategories($entity, $options, true);
     foreach ($input_categories as $guid) {
         $category = get_entity($guid);
         if (!$category) {
             continue;
         }
         $universal_categories[] = $category->getDisplayName();
         $hierarchy = $this->getHierarchy($category, true, true);
         $future_categories = array_merge($future_categories, $hierarchy);
     }
     $current_categories = array();
     foreach ($current_categories_batch as $c) {
         $current_categories[] = $c->guid;
     }
     // Storing categories metadata for compatibility with categories plugin
     $entity->universal_categories = $universal_categories;
     $relationship = $this->config->get('relationship');
     $to_remove = array_diff($current_categories, $future_categories);
     $to_add = array_diff($future_categories, $current_categories);
     foreach ($to_remove as $guid) {
         remove_entity_relationship($entity->guid, $relationship, $guid);
     }
     foreach ($to_add as $guid) {
         add_entity_relationship($entity->guid, $relationship, $guid);
     }
     return $to_add;
 }
 /**
  * Create a new group from a mixed data set
  *
  * @param array $data Data set
  * @return ItemCollection
  */
 public static function create($data)
 {
     $group = new ItemCollection();
     return $group->add($data);
 }