diff --git a/menus/menu.php b/menus/menu.php new file mode 100644 index 0000000..fa61970 --- /dev/null +++ b/menus/menu.php @@ -0,0 +1,39 @@ +setResourceAccess('menus', 'public', 'get'); + $this->setResourceAccess('showmenus', 'public', 'get'); + + } +} diff --git a/menus/menu.xml b/menus/menu.xml new file mode 100644 index 0000000..453ef99 --- /dev/null +++ b/menus/menu.xml @@ -0,0 +1,16 @@ + + + Api - Joomla Menu + Techjoomla + November 2017 + Copyright (C) 2017 - 2019 Open Source Matters. All rights reserved. + GNU General Public License version 2 or later; see LICENSE.txt + connnect@techjoomla.com + connnect@techjoomla.com + 1.0.0 + Joomla Menu API + + menu.php + menu + + diff --git a/menus/menu/menus.php b/menus/menu/menus.php new file mode 100644 index 0000000..8c398e8 --- /dev/null +++ b/menus/menu/menus.php @@ -0,0 +1,265 @@ +menus = $menu = $app->getMenu(); + $jinput = $app->input; + $user = Factory::getUser(); + $aliasMenuItems = array(); + + $this->uriBase = (String) JUri::root(true); + + $filterKeys = array(); + $filterVals = array(); + + if ($jinput->get('menutype', '', 'STRING')) + { + $menuType = $jinput->get('menutype', '', 'STRING'); + array_push($filterKeys, 'menutype'); + array_push($filterVals, $menuType); + } + + if ($jinput->get('component', '', 'STRING')) + { + $component = $jinput->get('component', '', 'STRING'); + array_push($filterKeys, 'component'); + array_push($filterVals, $component); + } + + if ($jinput->get('level', '' , 'INT')) + { + $level = $jinput->get('level', 1, 'INT'); + array_push($filterKeys, 'level'); + array_push($filterVals, $level); + } + + if ($user->id) + { + array_push($filterKeys, 'access'); + array_push($filterVals, $user->getAuthorisedViewLevels()); + } + + $itemss = (array) $menu->getItems($filterKeys, $filterVals, false); + + + if($jinput->get('component', '', 'STRING') === 'com_content') + { + $aliasMenuItems = $menu->getItems(array('type'), array('alias'), false); + $items = array_merge($itemss, $aliasMenuItems); + } + else + { + $items = $itemss; + } + + $this->getSubMenus($items); + $this->setMenuConfigs($items); + + // if ($menuType == 'mainmenu') + // { + // $this->getSubMenus($items); + // } + + $items = array_values($items); + + $this->plugin->setResponse($items); + } + + /** + * Function to set sub-menu + * + * @param ARRAY &$items Menu Items + * + * @return JSON batch details + * + * @since 3.0 + **/ + private function getSubMenus(&$items) + { + $menu = Factory::getApplication()->getMenu(); + + foreach ($items as $i => &$mn) + { + $mn->submenus = (array) $menu->getItems('parent_id', $mn->id); + } + } + + /** + * Function to set menu configs- copied from mod_menu + * + * @param ARRAY &$items Menu Items + * + * @return JSON batch details + * + * @since 3.0 + **/ + private function setMenuConfigs(&$items) + { + $start = 1; + // $end = 0; + // $showAll = 0; + $hidden_parents = array(); + $lastitem = 0; + + if (!empty($items)) + { + // This loop has been taken from mod_menu helper file to do some Joomla Checks + foreach ($items as $i => $item) + { + $item->parent = false; + + if (isset($items[$lastitem]) && $items[$lastitem]->id == $item->parent_id && $item->params->get('menu_show', 1) == 1) + { + $items[$lastitem]->parent = true; + } + + //~ if (($start && $start > $item->level) + //~ || ($end && $item->level > $end) + //~ || (!$showAll && $item->level > 1 && !in_array($item->parent_id, $path)) + //~ || ($start > 1 && !in_array($item->tree[$start - 2], $path))) + //~ { + //~ unset($items[$i]); + //~ continue; + //~ } + + + // Exclude item with menu item option set to exclude from menu modules + if (($item->params->get('menu_show', 1) == 0) || in_array($item->parent_id, $hidden_parents)) + { + $hidden_parents[] = $item->id; + unset($items[$i]); + continue; + } + + $item->deeper = false; + $item->shallower = false; + $item->level_diff = 0; + + if (isset($items[$lastitem])) + { + $items[$lastitem]->deeper = ($item->level > $items[$lastitem]->level); + $items[$lastitem]->shallower = ($item->level < $items[$lastitem]->level); + $items[$lastitem]->level_diff = ($items[$lastitem]->level - $item->level); + } + + $lastitem = $i; + $item->active = false; + $item->flink = $item->link; + + // Reverted back for CMS version 2.5.6 + switch ($item->type) + { + case 'separator': + break; + + case 'heading': + // No further action needed. + break; + + case 'url': + if ((strpos($item->link, 'index.php?') === 0) && (strpos($item->link, 'Itemid=') === false)) + { + // If this is an internal Joomla link, ensure the Itemid is set. + $item->flink = $item->link . '&Itemid=' . $item->id; + } + break; + + case 'alias': + $item->flink = 'index.php?Itemid=' . $item->params->get('aliasoptions'); + + $aliasMenuItem = $this->menus->getItem($item->params->get('aliasoptions')); + + if($aliasMenuItem && $aliasMenuItem->id) + { + $item->query = $aliasMenuItem->query; + } + + break; + + default: + $item->flink = 'index.php?Itemid=' . $item->id; + break; + } + + // We prevent the double encoding because for some reason the $item is shared for menu modules and we get double encoding + // when the cause of that is found the argument should be removed + $item->anchor_css = $item->params->get('menu-anchor_css', ''); + $item->anchor_title = $item->params->get('menu-anchor_title', ''); + $item->anchor_rel = $item->params->get('menu-anchor_rel', ''); + $item->menu_image = $item->params->get('menu_image', '') ? $item->params->get('menu_image', '') : ''; + $item->menu_image_css = $item->params->get('menu_image_css', ''); + + $item->flink = $this->nonSefToSef($item->flink); + } + + if (isset($items[$lastitem])) + { + $items[$lastitem]->deeper = (($start ?: 1) > $items[$lastitem]->level); + $items[$lastitem]->shallower = (($start ?: 1) < $items[$lastitem]->level); + $items[$lastitem]->level_diff = ($items[$lastitem]->level - ($start ?: 1)); + } + } + } + + /** + * Function to convert non-sef urls to sef + * + * @param string $nonSef non-sef url + * + * @return String sef-url + * + * @since 3.0 + **/ + private function nonSefToSef($nonSef) + { + $nonSefUrl = $nonSef; + $sefUrl = JRoute::_($nonSefUrl); + $uriBase = $this->uriBase; + $replacements = array(); + + $uriBaseArr = array_filter(explode('/', $uriBase)); + + foreach ($uriBaseArr AS &$ele) + { + $ele = '/' . $ele . '/'; + $replacements[] = ''; + } + + if ($uriBase && (strpos($sefUrl, $uriBase) === 0)) + { + $sefUrl = preg_replace($uriBaseArr, $replacements, $sefUrl, 1); + } + + return trim($sefUrl, "/"); + } +} diff --git a/menus/menu/showmenus.php b/menus/menu/showmenus.php new file mode 100644 index 0000000..a9c6a07 --- /dev/null +++ b/menus/menu/showmenus.php @@ -0,0 +1,80 @@ +input; + $menuType = $jinput->get('menutype', '', 'STRING'); + + if (!$menuType) + { + ApiError::raiseError(400, JText::_('MenuType required')); + } + + $data = new stdClass; + + $data->menutype = $menuType; + $data->base = ''; + $data->startLevel = 1; + $data->endLevel = 0; + $data->showAllChildren = 0; + $data->cache = 0; + $data->cache_time = 0; + $data->cachemode = ''; + + $params = new JRegistry($data); + $list = ModMenuHelper::getList($params); + + $uriBaseArr = explode("/", JUri::base()); + $uriBaseLast = $uriBaseArr[count($uriBaseArr) - 2]; + + foreach ($list as &$listEle) + { + $listElemLinkArr = explode("/", $listEle->flink); + + if ($listElemLinkArr[1] === $uriBaseLast) + { + $listElemLinkArr = array_slice($listElemLinkArr, 2); + } + else + { + $listElemLinkArr = array_filter($listElemLinkArr); + } + + $listEle->flink = implode("/", $listElemLinkArr); + } + + $this->plugin->setResponse($list); + } +}