Skip to content
This repository has been archived by the owner on Nov 9, 2019. It is now read-only.

Commit

Permalink
Merge pull request #16 from betweenbrain/develop
Browse files Browse the repository at this point in the history
1.1.0 release
  • Loading branch information
betweenbrain committed Jul 24, 2014
2 parents 472a27f + 8c3804b commit 5da3056
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 55 deletions.
310 changes: 260 additions & 50 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class modMenuwrenchHelper
public function __construct($params)
{
$this->app = JFactory::getApplication();
$this->db = JFactory::getDbo();
$this->menu = $this->app->getMenu();
$this->active = $this->menu->getActive();
$this->params = $params;
Expand All @@ -39,9 +40,10 @@ public function __construct($params)
*/
function getBranches()
{
$renderedItems = $this->params->get('renderedItems', 0);
$showSubmenu = $this->params->get('showSubmenu', 1);
$hideSubmenu = $this->params->get('hideSubmenu', 0);
$renderedItems = $this->params->get('renderedItems', 0);
$showCategoryItems = $this->params->get('showCategoryItems', 0);
$showSubmenu = $this->params->get('showSubmenu', 1);
$hideSubmenu = $this->params->get('hideSubmenu', 0);
// http://stackoverflow.com/questions/3787669/how-to-get-specific-menu-items-from-joomla/10218419#10218419
$items = $this->menu->getItems(null, null);

Expand All @@ -65,6 +67,16 @@ function getBranches()

$items[$item->id] = $item;

// If menu item is a category, add all articles as menu items
if ($showCategoryItems && $this->isContent($item))
{
$items[$item->id]->children = $this->linkCategoryItems(
$this->getCategoryItems($item->query['id']),
$item->query['id'],
$item->id
);
}

unset($items[$key]);

if ($item->parent_id != 1)
Expand All @@ -85,37 +97,167 @@ function getBranches()
unset($items[$key]);
}

/**
* Builds object classes
*/
$item->class = 'item' . $item->id . ' ' . $item->alias;

// Add parent class to all parents
if (isset($item->children))
// Hide sub-menu items if parameter set to no and parent not active
if ((!in_array($item->id, $this->active->tree) && $showSubmenu == 0) || in_array($item->id, $hideSubmenu))
{
$item->class .= ' parent';
unset($item->children);
}

// Add current class to specific item
if ($item->id == $this->active->id)
// Add image to item object if one is defined in the menu item's parameters
if ($item->params->get('menu_image', ''))
{
$item->class .= ' current';
$item->menu_image = htmlspecialchars($item->params->get('menu_image', ''), ENT_COMPAT, 'UTF-8', false);
}
}

// Add active class to all items in active branch
if (in_array($item->id, $this->active->tree))
{
$item->class .= ' active';
}
return $items;
}

// Hide sub-menu items if parameter set to no and parent not active
if ((!in_array($item->id, $this->active->tree) && $showSubmenu == 0) || in_array($item->id, $hideSubmenu))
/**
* Get all of the published articles in a given category
*
* @param $categoryId
*
* @return mixed
*/
private function getCategoryItems($categoryId)
{

$query = $this->db->getQuery(true);

$query
->select($this->db->quoteName(array('id', 'alias', 'title')))
->from($this->db->quoteName('#__content'))
->where($this->db->quoteName('state') . ' = ' . $this->db->quote('1'), ' AND ')
->where($this->db->quoteName('catid') . ' = ' . $this->db->quote($categoryId))
->order('ordering ASC');

// Reset the query using our newly populated query object.
$this->db->setQuery($query);

// Load the results as a list of stdClass objects (see later for more options on retrieving data).
return $this->setNullProperties($this->db->loadObjectList());
}

private function getClasses($item)
{
/**
* Builds object classes
*/
$classes = 'item' . $item->id . ' ' . $item->alias;

// Add parent class to all parents
if (isset($item->children))
{
$classes .= ' parent';
}

// Add current class to specific item
if ($this->isActive($item))
{
$classes .= ' current';
}

// Add active class to all items in active branch
if (in_array($item->id, $this->active->tree))
{
$classes .= ' active';
}

return $classes;
}

/**
* Checks if the current item is active. Category items that are dynamically
* rendered as menu items will have the same Itemid and thus falsely match active->id
*
* @param $item
*
* @return bool
*/
private function isActive($item)
{
if ($item->id == $this->active->id && !$this->isActiveChild($item))
{
return true;
}
elseif ($this->isActiveArticle($item))
{
return true;
}
}

/**
* Checks if the current item has any active children
*
* @param $item
*
* @return bool
*/
private function isActiveChild($item)
{
if (property_exists($item, 'children'))
{
foreach ($item->children as $child)
{
unset($item->children);
if ($this->isActiveArticle($child))
{
return true;
}
}
}
}

/**
* Checks if the item is an article and active
*
* @param $item
*
* @return bool
*/
private function isActiveArticle($item)
{
if ($this->app->input->get('view') == 'article' && $item->id == $this->app->input->get('id', '', 'INT'))
{
return true;
}
}

/**
* Check if a menu item is com_content and a category type
*
* @param $item
*
* @return bool
*/
private function isContent($item)
{
if (array_key_exists('view', $item->query) && $item->query['option'] === 'com_content' && $item->query['view'] === 'category')
{
return true;
}

return false;
}

/**
* Generate single article item link, based on supplied parameters
*
* @param $items
* @param $catId
* @param $itemId
*
* @return mixed
*/
private function linkCategoryItems($items, $catId, $itemId)
{
foreach ($items as $item)
{
$item->link = 'index.php?option=com_content&view=article&id=' . $item->id . ':' . $item->alias . '&catid=' . $catId . '&Itemid=' . $itemId;
}

return $items;

}

/**
Expand All @@ -134,25 +276,48 @@ function getBranches()

public function render($item, $containerTag = '<ul>', $containerClass = 'menu', $itemTag = '<li>', $level = 0)
{

$itemOpenTag = str_replace('>', ' class="' . $item->class . '">', $itemTag);
$itemOpenTag = str_replace('>', ' class="' . $this->getClasses($item) . '">', $itemTag);
$itemCloseTag = str_replace('<', '</', $itemTag);
$containerOpenTag = str_replace('>', ' class="' . $containerClass . '">', $containerTag);
$containerCloseTag = str_replace('<', '</', $containerTag);
$renderDepth = htmlspecialchars($this->params->get('renderDepth', 10));

switch ($item->browserNav) :
default:
case 0:
$browserNav = '';
break;
case 1:
$browserNav = 'target="_blank"';
break;
case 2:
$browserNav = "onclick=\"window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,'" . $this->params->get('window_open') . ");return false;\"";
break;
endswitch;
$output = $this->startOutput($item, $itemOpenTag);

$level++;

if (isset($item->children) && $level <= $renderDepth)
{

$output .= $containerOpenTag;

foreach ($item->children as $item)
{

$output .= $this->render($item, $containerTag, $containerClass, $itemTag, $level);
}
$output .= $itemCloseTag;
$output .= $containerCloseTag;
}

$output .= $itemCloseTag;

return $output;
}

/**
* Starts rendering of the item output
*
* @param $item
* @param $itemOpenTag
*
* @return string
*/
private function startOutput($item, $itemOpenTag)
{

$browserNav = property_exists($item, 'browserNav') ? $this->setBrowsernav($item) : '';
$item->title = property_exists($item, 'menu_image') ? $this->setTitle($item) : $item->title;

switch ($item->type)
{
Expand All @@ -176,28 +341,73 @@ public function render($item, $containerTag = '<ul>', $containerClass = 'menu',
break;

default:
$output = $itemOpenTag . '<a ' . $browserNav . ' href="' . JRoute::_($item->link . '&Itemid=' . $item->id) . '"/>' . $item->title . '</a>';
$item->link = strpos($item->link, 'Itemid') ? $item->link : $item->link . '&Itemid=' . $item->id;
$output = $itemOpenTag . '<a ' . $browserNav . ' href="' . JRoute::_($item->link) . '"/>' . $item->title . '</a>';
break;
}

$level++;

if (isset($item->children) && $level <= $renderDepth)
{
return $output;
}

$output .= $containerOpenTag;
/**
* Conditionally sets the item's browsernav property
*
* @param $item
*
* @return string
*/
private function setBrowsernav($item)
{
switch ($item->browserNav) :
default:
case 0:
$browserNav = '';
break;
case 1:
$browserNav = 'target="_blank"';
break;
case 2:
$browserNav = "onclick=\"window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,'" . $this->params->get('window_open') . ");return false;\"";
break;
endswitch;

foreach ($item->children as $item)
{
return $browserNav;
}

$output .= $this->render($item, $containerTag, $containerClass, $itemTag, $level);
}
$output .= $itemCloseTag;
$output .= $containerCloseTag;
/**
* Sets null properties if they don't exist
*
* @param $items
*
* @return mixed
*/
private function setNullProperties($items)
{
foreach ($items as $item)
{
$item->type = property_exists($item, 'type') ? $this->type : '';
}

$output .= $itemCloseTag;
return $items;
}

return $output;
/**
* Render item title with image if it is set
*
* @param $item
*
* @return string
*/
private function setTitle($item)
{
if ($item->menu_image)
{
$item->params->get('menu_text', 1) ?
$item->title = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" /><span class="image-title">' . $item->title . '</span> ' :
$item->title = '<img src="' . $item->menu_image . '" alt="' . $item->title . '" />';
}

return $item->title;
}

}
2 changes: 2 additions & 0 deletions language/en-GB/en-GB.mod_menuwrench.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
MOD_MENU_WRENCH_DESC="<a href="_QQ_"http://betweenbrain.com/"_QQ_" target="_QQ_"_blank"_QQ_"><img src="_QQ_"http://betweenbrain.com/images/logo.png"_QQ_" /></a><p class="_QQ_"clear"_QQ_">Throw a wrench at your Joomla menus! This module allows you to render selected menu items from any number of Joomla menus, at any depth in the menu tree, and render them as one menu.</p>"
MOD_MENU_WRENCH_SHOW_CATEGORY_ITEMS_DESC="Yes to render items belonging to a category blog or list as sub-menu items of that category menu item."
MOD_MENU_WRENCH_SHOW_CATEGORY_ITEMS_LABEL="Show Category Items"
MOD_MENU_WRENCH_HIDE_SUBMENU_ITEMS_DESC="Menu item, or items, to NEVER render the menu branch of."
MOD_MENU_WRENCH_HIDE_SUBMENU_ITEMS_LABEL="Hide Sub-menu Items"
MOD_MENU_WRENCH_RENDER_DEPTH_DESC="The levels of depth to render sub-menu items of. Enter 0 to render none."
Expand Down
Loading

0 comments on commit 5da3056

Please sign in to comment.