header.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /**
  3. * Loads the main structure
  4. *
  5. * @see modManagerController::getHeader
  6. * @see modManagerController::loadController
  7. *
  8. * @var modX $modx
  9. * @var modManagerController $this
  10. *
  11. * @package modx
  12. * @subpackage manager.controllers
  13. */
  14. class TopMenu
  15. {
  16. /**
  17. * @var modManagerController
  18. */
  19. public $controller;
  20. /**
  21. * @var modX
  22. */
  23. public $modx;
  24. /**
  25. * The current menu HTML output
  26. *
  27. * @var string
  28. */
  29. protected $output = '';
  30. /**
  31. * Whether or not to display menus description
  32. *
  33. * @var bool
  34. */
  35. protected $showDescriptions = true;
  36. /**
  37. * Current menu index
  38. *
  39. * @var int
  40. */
  41. protected $order = 0;
  42. /**
  43. * Current children menu index
  44. *
  45. * @var int
  46. */
  47. protected $childrenCt = 0;
  48. public function __construct(modManagerController &$controller)
  49. {
  50. $this->controller =& $controller;
  51. $this->modx =& $controller->modx;
  52. $this->showDescriptions = (boolean) $this->modx->getOption('topmenu_show_descriptions', null, true);
  53. }
  54. /**
  55. * Build the top menu
  56. *
  57. * @return void
  58. */
  59. public function render()
  60. {
  61. // First assign most variables so they could be used within menus
  62. $this->setPlaceholders();
  63. // Then process menu "containers"
  64. $mainNav = $this->modx->smarty->getTemplateVars('navb');
  65. if (empty($mainNav)) {
  66. $this->buildMenu(
  67. $this->modx->getOption('main_nav_parent', null, 'topnav', true),
  68. 'navb'
  69. );
  70. }
  71. $userNav = $this->modx->smarty->getTemplateVars('userNav');
  72. if (empty($userNav)) {
  73. $this->buildMenu(
  74. $this->modx->getOption('user_nav_parent', null, 'usernav', true),
  75. 'userNav'
  76. );
  77. }
  78. }
  79. /**
  80. * Set a bunch of placeholders to be used within Smarty templates
  81. *
  82. * @return void
  83. */
  84. public function setPlaceholders()
  85. {
  86. $username = '';
  87. if ($this->modx->getOption('manager_use_fullname') == true) {
  88. $userProfile = $this->modx->user->getOne('Profile');
  89. $username = $userProfile->get('fullname');
  90. }
  91. if (empty($username)) {
  92. $username = $this->modx->getLoginUserName();
  93. }
  94. $placeholders = array(
  95. 'username' => $username,
  96. 'userImage' => $this->getUserImage(),
  97. );
  98. $this->controller->setPlaceholders($placeholders);
  99. }
  100. /**
  101. * Retrieve/compute the user picture profile
  102. *
  103. * @return string The HTML output
  104. */
  105. public function getUserImage()
  106. {
  107. // Default to FontAwesome
  108. $output = '<i class="icon icon-user icon-large"></i>&nbsp;';
  109. $img = $this->modx->user->getPhoto(128, 128);
  110. if (!empty($img)) {
  111. $output = '<img src="' . $img . '" />';
  112. }
  113. return $output;
  114. }
  115. /**
  116. * Build the requested menu "container" and set it as a placeholder
  117. *
  118. * @param string $name The container name (topnav, usernav)
  119. * @param string $placeholder The placeholder to display the built menu to
  120. *
  121. * @return void
  122. */
  123. public function buildMenu($name, $placeholder)
  124. {
  125. if (!$placeholder) {
  126. $placeholder = $name;
  127. }
  128. // Grab the menus to process
  129. $menus = $this->getCache($name);
  130. // Iterate
  131. foreach ($menus as $menu) {
  132. $this->childrenCt = 0;
  133. if (!$this->hasPermission($menu['permissions'])) {
  134. continue;
  135. }
  136. $description = '';
  137. if ($this->showDescriptions && !empty($menu['description'])) {
  138. $description = '<span class="description">'.$menu['description'].'</span>'."\n";
  139. }
  140. $label = $menu['text'];
  141. $title = ' title="' . $menu['description'] .'"';
  142. $icon = false;
  143. if (!empty($menu['icon'])) {
  144. $icon = true;
  145. // Use the icon as label
  146. $label = $menu['icon'];
  147. // Reset the description (which is set as text in $title)
  148. $description = '';
  149. }
  150. $top = (!empty($menu['children'])) ? ' class="top"' : '';
  151. $menuTpl = '<li id="limenu-'.$menu['id'].'"'.$top.'>'."\n";
  152. if (!empty($menu['action'])) {
  153. if ($menu['namespace'] != 'core') {
  154. // Handle the namespace
  155. $menu['action'] .= '&namespace='.$menu['namespace'];
  156. }
  157. if (!$icon) {
  158. // No icon, no title property
  159. $title = '';
  160. }
  161. $onclick = (!empty($menu['handler'])) ? ' onclick="'.str_replace('"','\'',$menu['handler']).'"' : '';
  162. $menuTpl .= '<a href="?a='.$menu['action'].$menu['params'].'"'.( $top ? ' class="top-link"': '' ).$onclick.$title.'>'.$label.$description.'</a>'."\n";
  163. } elseif (!empty($menu['handler'])) {
  164. $menuTpl .= '<a href="javascript:;" onclick="'.str_replace('"','\'',$menu['handler']).'">'.$label.'</a>'."\n";
  165. } else {
  166. $menuTpl .= '<a href="javascript:;">'.$label.'</a>'."\n";
  167. }
  168. if (!empty($menu['children'])) {
  169. $menuTpl .= '<ul class="modx-subnav">'."\n";
  170. $this->processSubMenus($menuTpl, $menu['children']);
  171. $menuTpl .= '</ul>'."\n";
  172. }
  173. $menuTpl .= '</li>'."\n";
  174. /* if has no permissable children, and is not clickable, hide top menu item */
  175. if (!empty($this->childrenCt) || !empty($menu['action']) || !empty($menu['handler'])) {
  176. $this->output .= $menuTpl;
  177. }
  178. $this->order++;
  179. }
  180. //$this->cleanEmptySubMenus();
  181. $this->controller->setPlaceholder($placeholder, $this->output);
  182. $this->resetCounters();
  183. }
  184. /**
  185. * Retrieve the menus for the given "container"
  186. *
  187. * @param string $name
  188. *
  189. * @return array
  190. */
  191. protected function getCache($name)
  192. {
  193. $key = $this->getCacheKey($name);
  194. $menus = $this->modx->cacheManager->get($key, array(
  195. xPDO::OPT_CACHE_KEY => $this->modx->getOption('cache_menu_key', null, 'menu'),
  196. xPDO::OPT_CACHE_HANDLER => $this->modx->getOption(
  197. 'cache_menu_handler',
  198. null,
  199. $this->modx->getOption(xPDO::OPT_CACHE_HANDLER)
  200. ),
  201. xPDO::OPT_CACHE_FORMAT => (integer) $this->modx->getOption(
  202. 'cache_menu_format',
  203. null,
  204. $this->modx->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP)
  205. ),
  206. ));
  207. if ($menus == null || !is_array($menus)) {
  208. /** @var modMenu $menu */
  209. $menu = $this->modx->newObject('modMenu');
  210. $menus = $menu->rebuildCache($name);
  211. unset($menu);
  212. }
  213. return $menus;
  214. }
  215. /**
  216. * Compute the cache key for the given menu "container"
  217. *
  218. * @param string $name
  219. *
  220. * @return string
  221. */
  222. protected function getCacheKey($name)
  223. {
  224. return "menus/{$name}/" . $this->modx->getOption(
  225. 'manager_language',
  226. null,
  227. $this->modx->getOption('cultureKey', null, 'en')
  228. );
  229. }
  230. /**
  231. * Reset menu HTML output & indexes counters
  232. *
  233. * @return void
  234. */
  235. protected function resetCounters()
  236. {
  237. $this->output = '';
  238. $this->order = 0;
  239. $this->childrenCt = 0;
  240. }
  241. /**
  242. * Check if the current user is allowed to view the menu record
  243. *
  244. * @param string $perms
  245. *
  246. * @return bool
  247. */
  248. public function hasPermission($perms)
  249. {
  250. if (empty($perms)) {
  251. return true;
  252. }
  253. $permissions = array();
  254. $exploded = explode(',', $perms);
  255. foreach ($exploded as $permission) {
  256. $permissions[trim($permission)] = true;
  257. }
  258. return $this->modx->hasPermission($permissions);
  259. }
  260. /**
  261. * Process the given sub menus
  262. *
  263. * @param string $output The existing menu HTML "output"
  264. * @param array $menus The sub menus to process
  265. *
  266. * @return void
  267. */
  268. public function processSubMenus(&$output, array $menus = array())
  269. {
  270. //$output .= '<ul class="modx-subnav">'."\n";
  271. foreach ($menus as $menu) {
  272. if (!$this->hasPermission($menu['permissions'])) {
  273. continue;
  274. }
  275. $smTpl = '<li id="'.$menu['id'].'">'."\n";
  276. $description = '';
  277. if ($this->showDescriptions && !empty($menu['description'])) {
  278. $description = '<span class="description">'.$menu['description'].'</span>'."\n";
  279. }
  280. $attributes = '';
  281. if (!empty($menu['action'])) {
  282. if ($menu['namespace'] != 'core') {
  283. $menu['action'] .= '&namespace='.$menu['namespace'];
  284. }
  285. $attributes = ' href="?a='.$menu['action'].$menu['params'].'"';
  286. }
  287. if (!empty($menu['handler'])) {
  288. $attributes .= ' onclick="{literal} '.str_replace('"','\'',$menu['handler']).'{/literal} "';
  289. }
  290. $smTpl .= '<a'.$attributes.'>'.$menu['text'].$description.'</a>'."\n";
  291. if (!empty($menu['children'])) {
  292. $smTpl .= '<ul class="modx-subsubnav">'."\n";
  293. $this->processSubMenus($smTpl, $menu['children']);
  294. $smTpl .= '</ul>'."\n";
  295. }
  296. $smTpl .= '</li>';
  297. $output .= $smTpl;
  298. $this->childrenCt++;
  299. }
  300. //$output .= '</ul>'."\n";
  301. }
  302. /**
  303. * Clean "orphan" sub menus
  304. *
  305. * @return void
  306. */
  307. public function cleanEmptySubMenus()
  308. {
  309. $emptySub = '<ul class="modx-subsubnav">'."\n".'</ul>'."\n";
  310. $this->output = str_replace($emptySub, '', $this->output);
  311. }
  312. }
  313. // Set Smarty placeholder to display search bar, if appropriate
  314. $this->setPlaceholder('_search', $modx->hasPermission('search'));
  315. $menu = new TopMenu($this);
  316. $menu->render();