/modules/mod_exmenu/exmenu/loader/autoexpandedmenu.menuloader.class.php

https://bitbucket.org/dreamriks/gift · PHP · 209 lines · 187 code · 10 blank · 12 comment · 30 complexity · a6fb855733be8b7f89029ea436567543 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: autoexpandedmenu.menuloader.class.php 517 2011-06-12 12:47:20Z $
  4. * @author Daniel Ecer
  5. * @package exmenu
  6. * @copyright (C) 2005-2011 Daniel Ecer (de.siteof.de)
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  8. */
  9. // no direct access
  10. if (!defined('EXTENDED_MENU_HOME')) {
  11. die('Restricted access');
  12. }
  13. require_once(EXTENDED_MENU_HOME.'/loader/section.menuloader.class.php');
  14. /**
  15. * @since 1.0.0
  16. */
  17. class AutoExpandedExtendedMenuLoader extends SectionExtendedMenuLoader {
  18. var $sectionMenuNodeList = array();
  19. var $categoryMenuNodeList = array();
  20. function copyArrayFlat($a) {
  21. $result = array();
  22. foreach(array_keys($a) as $key) {
  23. $value = $a[$key];
  24. $result[$key] = $value;
  25. }
  26. return $result;
  27. }
  28. function loadBySourceValues($sourceValues) {
  29. $result = $this->loadMenuItems($sourceValues);
  30. $rootMenuNode = $this->getRootMenuNode();
  31. $menuNodeList = $rootMenuNode->getChildNodeList();
  32. $this->findAutoExpandableMenuNodes($menuNodeList);
  33. $sectionMenuNodeList = $this->sectionMenuNodeList;
  34. $categoryMenuNodeList = $this->categoryMenuNodeList;
  35. $sectionVisible = $this->sectionVisible;
  36. $categoryVisible = $this->categoryVisible;
  37. $contentItemVisible = $this->contentItemVisible;
  38. if (count($sectionMenuNodeList) > 0) {
  39. $menuNodeListBySectionId = array(); // we use a list for each id to avoid problems in case there are multiple links to the same section
  40. foreach(array_keys($sectionMenuNodeList) as $key) {
  41. $menuNode = $sectionMenuNodeList[$key];
  42. $id = $this->getIdByUrl($menuNode->link);
  43. if ($id > 0) {
  44. if (!isset($menuNodeListBySectionId[$id])) {
  45. $menuNodeListBySectionId[$id] = array();
  46. }
  47. $menuNodeListBySectionId[$id][] = $menuNode;
  48. } else {
  49. $params = $this->getParsedParameters($menuNode->params);
  50. $ids = explode(',', $params->get('sectionid', 0));
  51. foreach($ids as $id) {
  52. $id = intval(trim($id));
  53. if (!isset($menuNodeListBySectionId[$id])) {
  54. $menuNodeListBySectionId[$id] = array();
  55. }
  56. $menuNodeListBySectionId[$id][] = $menuNode;
  57. }
  58. }
  59. }
  60. $sectionIds = array_keys($menuNodeListBySectionId);
  61. $sectionCache = $this->getSectionCache();
  62. $sectionCache->loadBySectionIds($sectionIds);
  63. $sectionList = $sectionCache->getSectionList();
  64. if ($categoryVisible) {
  65. $categoryCache = $this->getCategoryCache();
  66. $categoryCache->loadBySectionIds($sectionIds);
  67. }
  68. if ($contentItemVisible) {
  69. $contentItemCache = $this->getContentItemCache();
  70. $contentItemCache->loadBySectionIds($sectionIds);
  71. }
  72. foreach(array_keys($menuNodeListBySectionId) as $id) {
  73. $menuNodeList = $menuNodeListBySectionId[$id];
  74. $section = $sectionCache->getSectionById($id);
  75. if (is_object($section)) {
  76. if ($categoryVisible) {
  77. $categoryCache = $this->getCategoryCache();
  78. $categoryList = $categoryCache->getCategoryListBySectionId($section->id);
  79. foreach(array_keys($menuNodeList) as $key) {
  80. $menuNode = $menuNodeList[$key];
  81. $this->addCategoryMenuNodes($menuNode, $categoryList, $categoryVisible, $contentItemVisible);
  82. }
  83. } else {
  84. if ($contentItemVisible) {
  85. $contentItemCache = $this->getContentItemCache();
  86. $contentItemList = $contentItemCache->getContentListBySectionId($section->id);
  87. foreach(array_keys($menuNodeList) as $key) {
  88. $menuNode = $menuNodeList[$key];
  89. $this->addContentItemMenuNodes($menuNode, $contentItemList, $contentItemVisible);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. if ($this->sectionHidden) {
  96. foreach(array_keys($sectionMenuNodeList) as $key) {
  97. $this->replaceMenuNodesByChildren($sectionMenuNodeList[$key]);
  98. }
  99. }
  100. }
  101. if (count($categoryMenuNodeList) > 0) {
  102. $menuNodeListByCategoryId = array(); // we use a list for each id to avoid problems in case there are multiple links to the same category
  103. foreach(array_keys($categoryMenuNodeList) as $key) {
  104. $menuNode = $categoryMenuNodeList[$key];
  105. $id = $this->getIdByUrl($menuNode->link);
  106. if ($id > 0) {
  107. if (!isset($menuNodeListByCategoryId[$id])) {
  108. $menuNodeListByCategoryId[$id] = array();
  109. }
  110. $menuNodeListByCategoryId[$id][] = $menuNode;
  111. } else {
  112. $params = $this->getParsedParameters($menuNode->params);
  113. $ids = explode(',', $params->get('categoryid', 0));
  114. foreach($ids as $id) {
  115. $id = intval(trim($id));
  116. if (!isset($menuNodeListByCategoryId[$id])) {
  117. $menuNodeListByCategoryId[$id] = array();
  118. }
  119. $menuNodeListByCategoryId[$id][] = $menuNode;
  120. }
  121. }
  122. }
  123. $categoryIds = array_keys($menuNodeListByCategoryId);
  124. $categoryCache = $this->getCategoryCache();
  125. $categoryCache->loadByCategoryIds($categoryIds);
  126. if ($contentItemVisible) {
  127. $contentItemCache = $this->getContentItemCache();
  128. $contentItemCache->loadByCategoryIds($categoryIds);
  129. }
  130. foreach(array_keys($menuNodeListByCategoryId) as $id) {
  131. $menuNodeList = $menuNodeListByCategoryId[$id];
  132. $category = $categoryCache->getCategoryById($id);
  133. if (is_object($category)) {
  134. if ($contentItemVisible) {
  135. $contentItemCache = $this->getContentItemCache();
  136. $contentItemList = $contentItemCache->getContentListByCategoryId($category->id);
  137. foreach(array_keys($menuNodeList) as $key) {
  138. $menuNode = $menuNodeList[$key];
  139. $this->addContentItemMenuNodes($menuNode, $contentItemList, $contentItemVisible);
  140. }
  141. }
  142. }
  143. }
  144. if ($this->categoryHidden) {
  145. foreach(array_keys($categoryMenuNodeList) as $key) {
  146. $this->replaceMenuNodesByChildren($categoryMenuNodeList[$key]);
  147. }
  148. }
  149. }
  150. return $result;
  151. }
  152. function replaceMenuNodesByChildren($menuNode) {
  153. $menuNodeList = $menuNode->getChildNodeList();
  154. $parentMenuNode = $menuNode->getParent();
  155. $oldMenuNodeList = array();
  156. $oldMenuNodeList[] = $menuNode;
  157. $newMenuNodeList = $this->copyArrayFlat($menuNodeList);
  158. if (is_object($parentMenuNode)) {
  159. $this->replaceMenuNodes($parentMenuNode, $oldMenuNodeList, $newMenuNodeList);
  160. } else {
  161. trigger_error('No parent for the menu node found.', E_USER_NOTICE);
  162. }
  163. }
  164. function findAutoExpandableMenuNodes($menuNodeList, $level = 0) {
  165. $joomla16 = $this->isJoomla16();
  166. $categoryVisible = $this->categoryVisible;
  167. $contentItemVisible = $this->contentItemVisible;
  168. foreach(array_keys($menuNodeList) as $key) {
  169. $menuNode = $menuNodeList[$key];
  170. $type = $this->getLinkTypeByMenuNode($menuNode);
  171. switch($type) {
  172. case 'content_category':
  173. case 'content_blog_category':
  174. case 'content_archive_category':
  175. if ($contentItemVisible) {
  176. $this->categoryMenuNodeList[] = $menuNode;
  177. }
  178. break;
  179. case 'content_section':
  180. case 'content_blog_section':
  181. case 'content_archive_section':
  182. if (($categoryVisible) || ($contentItemVisible)) {
  183. if ($joomla16) {
  184. // we shouldn't really have this case in Joomla 1.6
  185. $this->categoryMenuNodeList[] = $menuNode;
  186. } else {
  187. $this->sectionMenuNodeList[] = $menuNode;
  188. }
  189. }
  190. break;
  191. }
  192. if ($menuNode->hasChildren()) {
  193. $childMenuNodeList = $menuNode->getChildNodeList();
  194. $this->findAutoExpandableMenuNodes($childMenuNodeList, $level + 1);
  195. }
  196. }
  197. }
  198. }
  199. ?>