Skip to content

Commit

Permalink
Added module event triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter van Westen committed Mar 5, 2015
1 parent 2265941 commit 6e82ff7
Showing 1 changed file with 71 additions and 22 deletions.
93 changes: 71 additions & 22 deletions libraries/cms/module/helper.php
Expand Up @@ -231,6 +231,23 @@ public static function renderModule($module, $attribs = array())
$attribs['style'] .= ' outline';
}

// onRenderModule is allowed to alter the $module, $attribs
// and may return a boolean.
// true wll return an empty content, any other value will render the module normally.
// $result holds an array of booleans, 1 from each event call.
// we return empty if any of them are true.
$result = $app->triggerEvent('onRenderModule', array(&$module, &$attribs));

if (!is_array($result))
{
$result = array($result);
}

if (array_search(true, $result, true) !== false)
{
return '';
}

foreach (explode(' ', $attribs['style']) as $style)
{
$chromeMethod = 'modChrome_' . $style;
Expand Down Expand Up @@ -324,17 +341,44 @@ protected static function &_load()
*/
protected static function &load()
{
static $clean;
static $modules;

if (isset($clean))
if (isset($modules))
{
return $clean;
return $modules;
}

$app = JFactory::getApplication();

$modules = null;

$app->triggerEvent('onPrepareModuleList', array(&$modules));

// If the onPrepareModuleList event returns an array of modules, then ignore the default module list creation
if (!is_array($modules))
{
$modules = self::getModuleList();
}

$app->triggerEvent('onAfterModuleList', array(&$modules));

$modules = self::cleanModuleList($modules);

$app->triggerEvent('onAfterCleanModuleList', array(&$modules));

return $modules;
}

/**
* Module list
*
* @return array
*/
public static function getModuleList()
{
$app = JFactory::getApplication();
$Itemid = $app->input->getInt('Itemid');
$user = JFactory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
$groups = implode(',', JFactory::getUser()->getAuthorisedViewLevels());
$lang = JFactory::getLanguage()->getTag();
$clientId = (int) $app->getClientId();

Expand All @@ -345,7 +389,6 @@ protected static function &load()
->from('#__modules AS m')
->join('LEFT', '#__modules_menu AS mm ON mm.moduleid = m.id')
->where('m.published = 1')

->join('LEFT', '#__extensions AS e ON e.element = m.module AND e.client_id = m.client_id')
->where('e.enabled = 1');

Expand All @@ -354,7 +397,6 @@ protected static function &load()
$nullDate = $db->getNullDate();
$query->where('(m.publish_up = ' . $db->quote($nullDate) . ' OR m.publish_up <= ' . $db->quote($now) . ')')
->where('(m.publish_down = ' . $db->quote($nullDate) . ' OR m.publish_down >= ' . $db->quote($now) . ')')

->where('m.access IN (' . $groups . ')')
->where('m.client_id = ' . $clientId)
->where('(mm.menuid = ' . (int) $Itemid . ' OR mm.menuid <= 0)');
Expand All @@ -369,7 +411,6 @@ protected static function &load()

// Set the query
$db->setQuery($query);
$clean = array();

try
{
Expand All @@ -379,27 +420,36 @@ protected static function &load()
{
JLog::add(JText::sprintf('JLIB_APPLICATION_ERROR_MODULE_LOAD', $e->getMessage()), JLog::WARNING, 'jerror');

return $clean;
return array();
}

return $modules;
}

/**
* Clean the module list
*
* @return array
*/
public static function cleanModuleList($modules)
{
// Apply negative selections and eliminate duplicates
$Itemid = JFactory::getApplication()->input->getInt('Itemid');
$negId = $Itemid ? -(int) $Itemid : false;
$dupes = array();

for ($i = 0, $n = count($modules); $i < $n; $i++)
foreach ($modules as $i => &$module)
{
$module = &$modules[$i];

// The module is excluded if there is an explicit prohibition
$negHit = ($negId === (int) $module->menuid);

if (isset($dupes[$module->id]))
{
// If this item has been excluded, keep the duplicate flag set,
// but remove any item from the cleaned array.
// but remove any item from the modules array.
if ($negHit)
{
unset($clean[$module->id]);
unset($modules[$i]);
}

continue;
Expand All @@ -408,21 +458,20 @@ protected static function &load()
$dupes[$module->id] = true;

// Only accept modules without explicit exclusions.
if (!$negHit)
if ($negHit)
{
$module->name = substr($module->module, 4);
$module->style = null;
$module->position = strtolower($module->position);
$clean[$module->id] = $module;
continue;
}

$module->name = substr($module->module, 4);
$module->style = null;
$module->position = strtolower($module->position);
}

unset($dupes);

// Return to simple indexing that matches the query order.
$clean = array_values($clean);

return $clean;
return array_values($modules);
}

/**
Expand Down

0 comments on commit 6e82ff7

Please sign in to comment.