From 75160d91b6f7edc1a821a06dd45f63463b809dd6 Mon Sep 17 00:00:00 2001 From: Shivani Parit Date: Fri, 28 Aug 2020 16:36:06 +0530 Subject: [PATCH 1/4] Adding menus api plugin to com_api-plugins --- menus/menu.php | 39 ++++++ menus/menu.xml | 16 +++ menus/menu/menus.php | 261 +++++++++++++++++++++++++++++++++++++++ menus/menu/showmenus.php | 79 ++++++++++++ 4 files changed, 395 insertions(+) create mode 100644 menus/menu.php create mode 100644 menus/menu.xml create mode 100644 menus/menu/menus.php create mode 100644 menus/menu/showmenus.php diff --git a/menus/menu.php b/menus/menu.php new file mode 100644 index 0000000..1fc8ccd --- /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..9cec54a --- /dev/null +++ b/menus/menu/menus.php @@ -0,0 +1,261 @@ +menus = $menu = $app->getMenu(); + $jinput = $app->input; + $user = JFactory::getUser(); + $aliasMenuItems = array(); + + $this->uriBase = (String) JUri::root(true); + + $filterKeys = array(); + $filterVals = array(); + + if ($menuType = $jinput->get('menutype', '', 'STRING')) + { + array_push($filterKeys, 'menutype'); + array_push($filterVals, $menuType); + } + + if ($component = $jinput->get('component', '', 'STRING')) + { + array_push($filterKeys, 'component'); + array_push($filterVals, $component); + } + + if ($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 = JFactory::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 ($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..50684f9 --- /dev/null +++ b/menus/menu/showmenus.php @@ -0,0 +1,79 @@ +getMenu(); + $jinput = $app->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); + } +} From e0b333b74c2226042937b36a712e21470baacf09 Mon Sep 17 00:00:00 2001 From: Shivani Parit Date: Fri, 28 Aug 2020 16:54:40 +0530 Subject: [PATCH 2/4] resolving code errors --- menus/menu/menus.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/menus/menu/menus.php b/menus/menu/menus.php index 9cec54a..e6252b5 100644 --- a/menus/menu/menus.php +++ b/menus/menu/menus.php @@ -41,19 +41,19 @@ public function get() $filterKeys = array(); $filterVals = array(); - if ($menuType = $jinput->get('menutype', '', 'STRING')) + if ($menuType == $jinput->get('menutype', '', 'STRING')) { array_push($filterKeys, 'menutype'); array_push($filterVals, $menuType); } - if ($component = $jinput->get('component', '', 'STRING')) + if ($component == $jinput->get('component', '', 'STRING')) { array_push($filterKeys, 'component'); array_push($filterVals, $component); } - if ($level = $jinput->get('level', 1, 'INT')) + if ($level == $jinput->get('level', 1, 'INT')) { array_push($filterKeys, 'level'); array_push($filterVals, $level); From 6c873165076252754ff04592fe778adceec8cdeb Mon Sep 17 00:00:00 2001 From: Shivani Parit Date: Fri, 28 Aug 2020 17:07:38 +0530 Subject: [PATCH 3/4] resolving code errors --- menus/menu/menus.php | 7 ++++--- menus/menu/showmenus.php | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/menus/menu/menus.php b/menus/menu/menus.php index e6252b5..78adc14 100644 --- a/menus/menu/menus.php +++ b/menus/menu/menus.php @@ -9,6 +9,7 @@ defined('_JEXEC') or die; jimport('joomla.plugin.plugin'); +use Joomla\CMS\Factory; /** * Menus Resource for Joomla Menu Plugin. @@ -30,10 +31,10 @@ class MenuApiResourceMenus extends ApiResource **/ public function get() { - $app = JFactory::getApplication(); + $app = Factory::getApplication(); $this->menus = $menu = $app->getMenu(); $jinput = $app->input; - $user = JFactory::getUser(); + $user = Factory::getUser(); $aliasMenuItems = array(); $this->uriBase = (String) JUri::root(true); @@ -102,7 +103,7 @@ public function get() **/ private function getSubMenus(&$items) { - $menu = JFactory::getApplication()->getMenu(); + $menu = Factory::getApplication()->getMenu(); foreach ($items as $i => &$mn) { diff --git a/menus/menu/showmenus.php b/menus/menu/showmenus.php index 50684f9..bf33b29 100644 --- a/menus/menu/showmenus.php +++ b/menus/menu/showmenus.php @@ -12,6 +12,8 @@ jimport('joomla.registry.registry'); JLoader::register('ModMenuHelper', JPATH_SITE . '/modules/mod_menu/helper.php'); +use Joomla\CMS\Factory; + /** * Showmenus Resource for Joomla Menu Plugin. * From 2ab4ed1212b6b7941448d03775213f5da752334b Mon Sep 17 00:00:00 2001 From: Shivani Parit Date: Fri, 28 Aug 2020 18:15:28 +0530 Subject: [PATCH 4/4] resolving code errors --- menus/menu.php | 2 +- menus/menu/menus.php | 15 +++++++++------ menus/menu/showmenus.php | 1 - 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/menus/menu.php b/menus/menu.php index 1fc8ccd..fa61970 100644 --- a/menus/menu.php +++ b/menus/menu.php @@ -28,7 +28,7 @@ class PlgAPIMenu extends ApiPlugin */ public function __construct(&$subject, $config = array()) { - parent::__construct($subject = 'api', $config = array()); + parent::__construct($subject , $config = array()); ApiResource::addIncludePath(dirname(__FILE__) . '/menu'); diff --git a/menus/menu/menus.php b/menus/menu/menus.php index 78adc14..8c398e8 100644 --- a/menus/menu/menus.php +++ b/menus/menu/menus.php @@ -42,20 +42,23 @@ public function get() $filterKeys = array(); $filterVals = array(); - if ($menuType == $jinput->get('menutype', '', 'STRING')) + if ($jinput->get('menutype', '', 'STRING')) { + $menuType = $jinput->get('menutype', '', 'STRING'); array_push($filterKeys, 'menutype'); array_push($filterVals, $menuType); } - if ($component == $jinput->get('component', '', 'STRING')) + if ($jinput->get('component', '', 'STRING')) { + $component = $jinput->get('component', '', 'STRING'); array_push($filterKeys, 'component'); array_push($filterVals, $component); } - if ($level == $jinput->get('level', 1, 'INT')) + if ($jinput->get('level', '' , 'INT')) { + $level = $jinput->get('level', 1, 'INT'); array_push($filterKeys, 'level'); array_push($filterVals, $level); } @@ -123,12 +126,12 @@ private function getSubMenus(&$items) private function setMenuConfigs(&$items) { $start = 1; - $end = 0; - $showAll = 0; + // $end = 0; + // $showAll = 0; $hidden_parents = array(); $lastitem = 0; - if ($items) + if (!empty($items)) { // This loop has been taken from mod_menu helper file to do some Joomla Checks foreach ($items as $i => $item) diff --git a/menus/menu/showmenus.php b/menus/menu/showmenus.php index bf33b29..a9c6a07 100644 --- a/menus/menu/showmenus.php +++ b/menus/menu/showmenus.php @@ -34,7 +34,6 @@ class MenuApiResourceShowmenus extends ApiResource public function get() { $app = JFactory::getApplication(); - $menu = $app->getMenu(); $jinput = $app->input; $menuType = $jinput->get('menutype', '', 'STRING');