/library/massiveart/website/navigation/tree.class.php

https://github.com/massiveart-webservices/ZOOLU · PHP · 299 lines · 138 code · 26 blank · 135 comment · 19 complexity · ec52a4874485489453cae1c208f3674d MD5 · raw file

  1. <?php
  2. /**
  3. * ZOOLU - Content Management System
  4. * Copyright (c) 2008-2012 HID GmbH (http://www.hid.ag)
  5. *
  6. * LICENSE
  7. *
  8. * This file is part of ZOOLU.
  9. *
  10. * ZOOLU is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * ZOOLU is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with ZOOLU. If not, see http://www.gnu.org/licenses/gpl-3.0.html.
  22. *
  23. * For further information visit our website www.getzoolu.org
  24. * or contact us at zoolu@getzoolu.org
  25. *
  26. * @category ZOOLU
  27. * @package library.massiveart.website.cache.navigation
  28. * @copyright Copyright (c) 2008-2012 HID GmbH (http://www.hid.ag)
  29. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, Version 3
  30. * @version $Id: version.php
  31. */
  32. /**
  33. * NavigationTree
  34. *
  35. *
  36. * Version history (please keep backward compatible):
  37. * 1.0, 2009-02-17: Thomas Schedler
  38. *
  39. * @author Thomas Schedler <tsh@massiveart.com>
  40. * @version 1.0
  41. * @package massiveart.website.navigation
  42. * @subpackage NavigationTree
  43. */
  44. require_once(dirname(__FILE__) . '/item.class.php');
  45. class NavigationTree extends NavigationItem implements Iterator, Countable
  46. {
  47. private $blnOrderUpdated = false;
  48. private $arrItems = array();
  49. private $arrTrees = array();
  50. private $arrOrder = array();
  51. /**
  52. * construct
  53. * @author Thomas Schedler <tsh@massiveart.com>
  54. * @version 1.0
  55. */
  56. public function __construct()
  57. {
  58. }
  59. /**
  60. * addItem
  61. * @param NavigationItem $objItem
  62. * @param string $strName
  63. * @author Thomas Schedler <tsh@massiveart.com>
  64. * @version 1.0
  65. */
  66. public function addItem(NavigationItem $objItem, $strName = null)
  67. {
  68. $this->arrItems[$strName] = $objItem;
  69. $this->arrOrder[$strName] = $this->arrItems[$strName]->getOrder();
  70. $this->blnOrderUpdated = true;
  71. }
  72. /**
  73. * hasSubTrees
  74. * @return boolean
  75. * @author Thomas Schedler <tsh@massiveart.com>
  76. * @version 1.0
  77. */
  78. public function hasSubTrees()
  79. {
  80. return (count($this->arrTrees) > 0) ? true : false;
  81. }
  82. /**
  83. * hasSubTree
  84. * @param string $strName
  85. * @return boolean
  86. * @author Thomas Schedler <tsh@massiveart.com>
  87. * @version 1.0
  88. */
  89. public function hasSubTree($strName)
  90. {
  91. return (array_key_exists($strName, $this->arrTrees)) ? true : false;
  92. }
  93. /**
  94. * addTree
  95. * @param NavigationTree $objTree
  96. * @param string $strName
  97. * @author Thomas Schedler <tsh@massiveart.com>
  98. * @version 1.0
  99. */
  100. public function addTree(NavigationTree $objTree, $strName = null)
  101. {
  102. $this->arrTrees[$strName] = $objTree;
  103. $this->arrOrder[$strName] = $this->arrTrees[$strName]->getOrder();
  104. $this->blnOrderUpdated = true;
  105. }
  106. /**
  107. * addToParentTree
  108. * @param NavigationTree|NavigationItem $objNav
  109. * @param string $strName
  110. * @author Thomas Schedler <tsh@massiveart.com>
  111. * @version 1.0
  112. */
  113. public function addToParentTree($objNav, $strName = null)
  114. {
  115. if ($this->intId == $objNav->getParentId()) {
  116. if ($objNav instanceof NavigationTree) {
  117. $this->addTree($objNav, $strName);
  118. } else if ($objNav instanceof NavigationItem) {
  119. $this->addItem($objNav, $strName);
  120. }
  121. return true;
  122. } else {
  123. foreach ($this->arrTrees as $objSubTree) {
  124. if ($objSubTree->addToParentTree($objNav, $strName)) {
  125. break;
  126. }
  127. }
  128. }
  129. }
  130. /**
  131. * sort()
  132. * Sort elements according to their order
  133. * @author Thomas Schedler <tsh@massiveart.com>
  134. * @version 1.0
  135. */
  136. protected function sort()
  137. {
  138. if ($this->blnOrderUpdated) {
  139. $arrItems = array();
  140. $intIndex = 0;
  141. foreach ($this->arrOrder as $strKey => $intOrder) {
  142. if ($intOrder === null) {
  143. if (($intOrder = $this->{$strKey}->getOrder()) === null) {
  144. while (array_search($intIndex, $this->arrOrder, true)) {
  145. $intIndex++;
  146. }
  147. $arrItems[$intIndex] = $strKey;
  148. $intIndex++;
  149. } else {
  150. $arrItems[$intOrder] = $strKey;
  151. }
  152. } else {
  153. $arrItems[$intOrder] = $strKey;
  154. }
  155. }
  156. $arrItems = array_flip($arrItems);
  157. asort($arrItems);
  158. $this->arrOrder = $arrItems;
  159. $this->blnOrderUpdated = false;
  160. }
  161. }
  162. /**
  163. * Overloading: access to navigation items and trees
  164. * @param string $strName
  165. * @return NavigationItem|NavigationTree|null
  166. * @author Thomas Schedler <tsh@massiveart.com>
  167. * @version 1.0
  168. */
  169. public function __get($strName)
  170. {
  171. if (isset($this->arrItems[$strName])) {
  172. return $this->arrItems[$strName];
  173. } elseif (isset($this->arrTrees[$strName])) {
  174. return $this->arrTrees[$strName];
  175. }
  176. return null;
  177. }
  178. /**
  179. * Overloading: access to navigation items and trees
  180. * @param string $strName
  181. * @param NavigationItem|NavigationTree $obj
  182. * @author Thomas Schedler <tsh@massiveart.com>
  183. * @version 1.0
  184. */
  185. public function __set($strName, $obj)
  186. {
  187. if ($obj instanceof NavigationItem) {
  188. $this->addtem($obj, $strName);
  189. return;
  190. } elseif ($obj instanceof NavigationTree) {
  191. $this->addTree($obj, $strName);
  192. return;
  193. }
  194. if (is_object($obj)) {
  195. $strType = get_class($obj);
  196. } else {
  197. $strType = gettype($obj);
  198. }
  199. throw new Zend_Form_Exception('Only navigation items and trees may be overloaded; variable of type "' . $strType . '" provided');
  200. }
  201. /**
  202. * rewind
  203. * @author Thomas Schedler <tsh@massiveart.com>
  204. * @version 1.0
  205. */
  206. public function rewind()
  207. {
  208. $this->sort();
  209. reset($this->arrOrder);
  210. }
  211. /**
  212. * current
  213. * @return NavigationItem|NavigationTree
  214. * @author Thomas Schedler <tsh@massiveart.com>
  215. * @version 1.0
  216. */
  217. public function current()
  218. {
  219. $this->sort();
  220. current($this->arrOrder);
  221. $strKey = key($this->arrOrder);
  222. if (isset($this->arrItems[$strKey])) {
  223. return $this->arrItems[$strKey];
  224. } elseif (isset($this->arrTrees[$strKey])) {
  225. return $this->arrTrees[$strKey];
  226. } else {
  227. throw new Exception('Corruption detected in navigation tree; invalid key ("' . $strKey . '") found in internal iterator');
  228. }
  229. }
  230. /**
  231. * key
  232. * @return string
  233. * @author Thomas Schedler <tsh@massiveart.com>
  234. * @version 1.0
  235. */
  236. public function key()
  237. {
  238. $this->sort();
  239. return key($this->arrOrder);
  240. }
  241. /**
  242. * next
  243. * @author Thomas Schedler <tsh@massiveart.com>
  244. * @version 1.0
  245. */
  246. public function next()
  247. {
  248. $this->sort();
  249. next($this->arrOrder);
  250. }
  251. /**
  252. * valid
  253. * @return boolean
  254. * @author Thomas Schedler <tsh@massiveart.com>
  255. * @version 1.0
  256. */
  257. public function valid()
  258. {
  259. $this->sort();
  260. return (current($this->arrOrder) !== false);
  261. }
  262. /**
  263. * count
  264. * @return integer
  265. * @author Thomas Schedler <tsh@massiveart.com>
  266. * @version 1.0
  267. */
  268. public function count()
  269. {
  270. return count($this->arrOrder);
  271. }
  272. }
  273. ?>