sigil88
5/18/2017 - 9:42 AM

bs3 menu helper.php - fixed file after updating Joomla to 3.7.1. (fixes no dropdowns/children loading)

bs3 menu helper.php - fixed file after updating Joomla to 3.7.1. (fixes no dropdowns/children loading)

<?php defined('_JEXEC') or die;

class ModBootstrap3MenuHelper
{
	private $menu = [];

	public function getList(&$params)
	{
		$appMenu    = JFactory::getApplication()->getMenu();
		$items      = $appMenu->getItems('menutype', $params->get('menutype'));

		$this->addItemsToMenu($items);

		return $this->getMenu($params);
	}

	/**
	 * @param $params
	 * @return mixed|object
	 * @throws Exception
	 */
	private function getMenu(&$params)
	{
		$appMenu = JFactory::getApplication()->getMenu();

		if ($params->get('base'))
		{
			// how is this still returning the new menu structure ?????
			return $appMenu->getItem( $params->get('base') )->children;
		}

		return $this->menu;
	}

	/**
	 * @param $items
	 * @return array
	 * @throws Exception
	 */
	private function addItemsToMenu($items)
	{
		foreach($items as &$item)
		{
			$item->flink    = $this->getLink($item);
			$item->active   = $this->isActive($item);
			$item->class    = $item->active ? 'active' : '';

			$this->addItemToMenu($item);
		}
	}

	/**
	 * @param $item
	 */
	private function addItemToMenu($item)
	{
		$menu = &$this->menu;

		foreach ($item->tree as $tree)
		{
			if( $tree == end($item->tree) )
			{
				$menu[$tree] = $item;
				continue;
			}

			if(!isset($menu[$tree]->children))
				$menu[$tree]->children = [];

			$menu = &$menu[$tree]->children;
		}
	}

	/**
	 * @param $item
	 * @return string
	 */
	private function getLink($item)
	{
		switch($item->type)
		{
			case 'separator':
			case 'heading':
				return '';
				break;
			case 'url' :
				return $item->link;
				break;
			case 'alias' :
				return 'index.php?Itemid=' . $item->params->get('aliasoptions');
				break;
			case 'component' :
			default :
				return JRoute::_('index.php?Itemid=' . $item->id);
				break;
		}
	}

	/**
	 * @param $item
	 * @return bool
	 * @throws Exception
	 */
	private function isActive($item)
	{
		$appMenu = JFactory::getApplication()->getMenu();

		$active = $appMenu->getActive();

		if( is_object($active) && $item->id === $active->id)
		{
			return true;
		}

		return false;
	}
}