10 defined(
'JPATH_PLATFORM') or die;
27 public static $instances = array();
51 protected $_extension = null;
59 protected $_table = null;
67 protected $_field = null;
75 protected $_key = null;
83 protected $_statefield = null;
91 protected $_options = null;
100 public function __construct($options)
102 $this->_extension = $options[
'extension'];
103 $this->_table = $options[
'table'];
104 $this->_field = (isset($options[
'field']) && $options[
'field']) ? $options[
'field'] :
'catid';
105 $this->_key = (isset($options[
'key']) && $options[
'key']) ? $options[
'key'] :
'id';
106 $this->_statefield = (isset($options[
'statefield'])) ? $options[
'statefield'] :
'state';
107 $options[
'access'] = (isset($options[
'access'])) ? $options[
'access'] :
'true';
108 $options[
'published'] = (isset($options[
'published'])) ? $options[
'published'] : 1;
109 $this->_options = $options;
124 public static function getInstance($extension, $options = array())
126 $hash = md5($extension . serialize($options));
128 if (isset(self::$instances[$hash]))
130 return self::$instances[$hash];
133 $parts = explode(
'.', $extension);
134 $component =
'com_' . strtolower($parts[0]);
135 $section = count($parts) > 1 ? $parts[1] :
'';
136 $classname = ucfirst(substr($component, 4)) . ucfirst($section) .
'Categories';
138 if (!class_exists($classname))
140 $path = JPATH_SITE .
'/components/' . $component .
'/helpers/category.php';
151 self::$instances[$hash] =
new $classname($options);
153 return self::$instances[$hash];
166 public function get($id =
'root', $forceload =
false)
179 if ((!isset($this->_nodes[$id]) && !isset($this->_checkedCategories[$id])) || $forceload)
185 if (isset($this->_nodes[$id]))
187 return $this->_nodes[$id];
190 elseif (isset($this->_checkedCategories[$id]))
207 protected function _load($id)
211 $extension = $this->_extension;
214 $this->_checkedCategories[$id] =
true;
216 $query = $db->getQuery(
true);
219 $query->select(
'c.id, c.asset_id, c.access, c.alias, c.checked_out, c.checked_out_time,
220 c.created_time, c.created_user_id, c.description, c.extension, c.hits, c.language, c.level,
221 c.lft, c.metadata, c.metadesc, c.metakey, c.modified_time, c.note, c.params, c.parent_id,
222 c.path, c.published, c.rgt, c.title, c.modified_user_id, c.version');
223 $case_when =
' CASE WHEN ';
224 $case_when .= $query->charLength(
'c.alias',
'!=',
'0');
225 $case_when .=
' THEN ';
226 $c_id = $query->castAsChar(
'c.id');
227 $case_when .= $query->concatenate(array($c_id,
'c.alias'),
':');
228 $case_when .=
' ELSE ';
229 $case_when .= $c_id .
' END as slug';
230 $query->select($case_when)
231 ->from(
'#__categories as c')
232 ->where(
'(c.extension=' . $db->quote($extension) .
' OR c.extension=' . $db->quote(
'system') .
')');
234 if ($this->_options[
'access'])
236 $query->where(
'c.access IN (' . implode(
',', $user->getAuthorisedViewLevels()) .
')');
239 if ($this->_options[
'published'] == 1)
241 $query->where(
'c.published = 1');
244 $query->order(
'c.lft');
250 $query->join(
'LEFT',
'#__categories AS s ON (s.lft <= c.lft AND s.rgt >= c.rgt) OR (s.lft > c.lft AND s.rgt < c.rgt)')
251 ->where(
's.id=' . (
int) $id);
254 $subQuery =
' (SELECT cat.id as id FROM #__categories AS cat JOIN #__categories AS parent ' .
255 'ON cat.lft BETWEEN parent.lft AND parent.rgt WHERE parent.extension = ' . $db->quote($extension) .
256 ' AND parent.published != 1 GROUP BY cat.id) ';
257 $query->join(
'LEFT', $subQuery .
'AS badcats ON badcats.id = c.id')
258 ->where(
'badcats.id is null');
261 if (isset($this->_options[
'countItems']) && $this->_options[
'countItems'] == 1)
263 if ($this->_options[
'published'] == 1)
267 $db->quoteName($this->_table) .
' AS i ON i.' . $db->quoteName($this->_field) .
' = c.id AND i.' . $this->_statefield .
' = 1'
272 $query->join(
'LEFT', $db->quoteName($this->_table) .
' AS i ON i.' . $db->quoteName($this->_field) .
' = c.id');
275 $query->select(
'COUNT(i.' . $db->quoteName($this->_key) .
') AS numitems');
280 'c.id, c.asset_id, c.access, c.alias, c.checked_out, c.checked_out_time,
281 c.created_time, c.created_user_id, c.description, c.extension, c.hits, c.language, c.level,
282 c.lft, c.metadata, c.metadesc, c.metakey, c.modified_time, c.note, c.params, c.parent_id,
283 c.path, c.published, c.rgt, c.title, c.modified_user_id, c.version'
287 $db->setQuery($query);
288 $results = $db->loadObjectList(
'id');
289 $childrenLoaded =
false;
294 foreach ($results as $result)
297 if ($result->id == 1)
299 $result->id =
'root';
303 if ($result->parent_id == 1)
305 $result->parent_id =
'root';
309 if (!isset($this->_nodes[$result->id]))
312 $this->_nodes[$result->id] =
new JCategoryNode($result, $this);
315 if ($result->id !=
'root' && (isset($this->_nodes[$result->parent_id]) || $result->parent_id == 1))
318 $this->_nodes[$result->id]->setParent($this->_nodes[$result->parent_id]);
323 if (!(isset($this->_nodes[$result->parent_id]) || $result->parent_id == 0))
325 unset($this->_nodes[$result->id]);
329 if ($result->id == $id || $childrenLoaded)
331 $this->_nodes[$result->id]->setAllLoaded();
332 $childrenLoaded =
true;
335 elseif ($result->id == $id || $childrenLoaded)
338 $this->_nodes[$result->id] =
new JCategoryNode($result, $this);
340 if ($result->id !=
'root' && (isset($this->_nodes[$result->parent_id]) || $result->parent_id))
343 $this->_nodes[$result->id]->setParent($this->_nodes[$result->parent_id]);
346 if (!isset($this->_nodes[$result->parent_id]))
348 unset($this->_nodes[$result->id]);
352 if ($result->id == $id || $childrenLoaded)
354 $this->_nodes[$result->id]->setAllLoaded();
355 $childrenLoaded =
true;
362 $this->_nodes[$id] = null;
662 public function __construct($category = null, $constructor = null)
669 $this->_constructor = $constructor;
693 if (!is_null($this->_parent))
695 $key = array_search($this, $this->_parent->_children);
696 unset($this->_parent->_children[$key]);
699 if (!is_null($parent))
701 $parent->_children[] = & $this;
704 $this->_parent = $parent;
706 if ($this->
id !=
'root')
708 if ($this->parent_id != 1)
710 $this->_path = $parent->getPath();
715 if (count($parent->_children) > 1)
717 end($parent->_children);
718 $this->_leftSibling = prev($parent->_children);
719 $this->_leftSibling->_rightsibling = & $this;
739 $child->setParent($this);
754 $key = array_search($this, $this->_parent->_children);
755 unset($this->_parent->_children[$key]);
769 if (!$this->_allChildrenloaded)
771 $temp = $this->_constructor->get($this->
id,
true);
774 $this->_children = $temp->getChildren();
775 $this->_leftSibling = $temp->getSibling(
false);
776 $this->_rightSibling = $temp->getSibling(
true);
784 foreach ($this->_children as $child)
787 $items = array_merge($items, $child->getChildren(
true));
816 return count($this->_children);
845 $this->_rightSibling = $sibling;
849 $this->_leftSibling = $sibling;
865 if (!$this->_allChildrenloaded)
867 $temp = $this->_constructor->get($this->
id,
true);
868 $this->_children = $temp->getChildren();
869 $this->_leftSibling = $temp->getSibling(
false);
870 $this->_rightSibling = $temp->getSibling(
true);
893 if (!($this->params instanceof
JRegistry))
895 $temp =
new JRegistry;
896 $temp->loadString($this->params);
897 $this->params = $temp;
912 if (!($this->metadata instanceof
JRegistry))
914 $temp =
new JRegistry;
915 $temp->loadString($this->metadata);
916 $this->metadata = $temp;
962 $this->_allChildrenloaded =
true;
963 foreach ($this->_children as $child)
965 $child->setAllLoaded();
986 $count = $count + $child->getNumItems(
true);