Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plugin][content] - loadmodule by id #19362

Merged
merged 27 commits into from
Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions libraries/src/Helper/ModuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,111 @@ public static function isAdminMultilang()

return $enabled;
}

/**
* Get module by id
*
* @param string $id The name of the module
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to The id

* @param string $title The title of the module, optional
*
* @return \stdClass The Module object
*
* @since __DEPLOY_VERSION__
*/
public static function &getModuleid($id, $title = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the name of that method be something like getModuleById and not getModuleid?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

{
$app = \JFactory::getApplication();
$result = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.

$groups = implode(',', \JFactory::getUser()->getAuthorisedViewLevels());
$lang = \JFactory::getLanguage()->getTag();
$clientId = (int) $app->getClientId();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all the code from lone 659 to 693 as it is not used in the method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true again

// Build a cache ID for the resulting data object
$cacheId = $groups . $clientId . $id;

$db = \JFactory::getDbo();

$query = $db->getQuery(true)
->select('m.id, m.title, m.module, m.position, m.content, m.showtitle, m.params, mm.menuid')
->from('#__modules AS m')
->join('LEFT', '#__modules_menu AS mm ON mm.moduleid = m.id')
->where('m.id = ' . (int) $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');

$date = \JFactory::getDate();
$now = $date->toSql();
$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);

// Filter by language
if ($app->isClient('site') && $app->getLanguageFilter())
{
$query->where('m.language IN (' . $db->quote($lang) . ',' . $db->quote('*') . ')');
$cacheId .= $lang . '*';
}

if ($app->isClient('administrator') && static::isAdminMultilang())
{
$query->where('m.language IN (' . $db->quote($lang) . ',' . $db->quote('*') . ')');
$cacheId .= $lang . '*';
}

$query->order('m.position, m.ordering');

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

try
{
/** @var \JCacheControllerCallback $cache */
$cache = \JFactory::getCache('com_modules', 'callback');

$modules = $cache->get(array($db, 'loadObjectList'), array(), md5($cacheId), false);
}
catch (\RuntimeException $e)
{
\JLog::add(\JText::sprintf('JLIB_APPLICATION_ERROR_MODULE_LOAD', $e->getMessage()), \JLog::WARNING, 'jerror');

$modules = array();
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove blank line.

$total = count($modules);

for ($i = 0; $i < $total; $i++)
{
// Match the id of the module
if ($modules[$i]->id === $id)
{
// Match the title if we're looking for a specific instance of the module
if (!$title || $modules[$i]->title === $title)
{
// Found it
$result = &$modules[$i];
break;
}
}
}

// If we didn't find it, and the name is mod_something, create a dummy object
if ($result === null && strpos($name, 'mod_') === 0)
{
$result = new \stdClass;
$result->id = 0;
$result->title = '';
$result->module = $name;
$result->position = '';
$result->content = '';
$result->showtitle = 0;
$result->control = '';
$result->params = '';
}

return $result;
}
}
51 changes: 50 additions & 1 deletion plugins/content/loadmodule/loadmodule.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public function onContentPrepare($context, &$article, &$params, $page = 0)

// Expression to search for(modules)
$regexmod = '/{loadmodule\s(.*?)}/i';
$stylemod = $this->params->def('style', 'none');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove tabs.

// Expression to search for(id)
$regexmodid = '/{loadmoduleid\s(.*?)}/i';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change regex to match for digits.

$stylemod = $this->params->def('style', 'none');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move back to its original line so it is not assumed to be related to modid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved back


// Find all instances of plugin and put in $matches for loadposition
// $matches[0] is full pattern match, $matches[1] is the position
Expand Down Expand Up @@ -117,6 +120,26 @@ public function onContentPrepare($context, &$article, &$params, $page = 0)
$stylemod = $this->params->def('style', 'none');
}
}

// Find all instances of plugin and put in $matchesmodid for loadmoduleid
preg_match_all($regexmodid, $article->text, $matchesmodid, PREG_SET_ORDER);
// If no matches, skip this
if ($matchesmodid)
{
foreach ($matchesmodid as $match)
{
$matcheslist = explode('id:', $match[1]);

$id = trim($matcheslist[1]);

$output = $this->_loadid($id);

// We should replace only first occurrence in order to allow positions with the same name to regenerate their content:
$article->text = preg_replace("|$match[0]|", addcslashes($output, '\\$'), $article->text, 1);
$style = $this->params->def('style', 'none');
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove blank line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}

/**
Expand Down Expand Up @@ -187,4 +210,30 @@ protected function _loadmod($module, $title, $style = 'none')

return self::$mods[$module];
}

/**
* Loads and renders the module
*
* @param string $id The id of the module
* @param string $style The style assigned to the module
*
* @return mixed
*
* @since __DEPLOY_VERSION__
*/
protected function _loadid($id, $style = 'none')
Copy link
Contributor

@Quy Quy Jan 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The syntax {loadmoduleid 12345} does not allow style. Remove $style parameter or accommodate for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

{
self::$modules[$id] = '';
$document = JFactory::getDocument();
$renderer = $document->loadRenderer('module');
$modules = JModuleHelper::getModuleid($id);
$params = array('style' => $style);
ob_start();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove tab.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

echo $renderer->render($modules->module, $params);

self::$modules[$id] = ob_get_clean();

return self::$modules[$id];
}
}