/**
* Gets the name of the current template.
*
* @param boolean $params True to return the template parameters
*
* @return string The name of the template.
*
* @since 3.2
* @throws \InvalidArgumentException
*/
public function getTemplate($params = false)
{
if (\is_object($this->template)) {
if ($this->template->parent) {
if (!is_file(JPATH_THEMES . '/' . $this->template->template . '/index.php')) {
if (!is_file(JPATH_THEMES . '/' . $this->template->parent . '/index.php')) {
throw new \InvalidArgumentException(Text::sprintf('JERROR_COULD_NOT_FIND_TEMPLATE', $this->template->template));
}
}
} elseif (!is_file(JPATH_THEMES . '/' . $this->template->template . '/index.php')) {
throw new \InvalidArgumentException(Text::sprintf('JERROR_COULD_NOT_FIND_TEMPLATE', $this->template->template));
}
if ($params) {
return $this->template;
}
return $this->template->template;
}
// Get the id of the active menu item
$menu = $this->getMenu();
$item = $menu->getActive();
if (!$item) {
$item = $menu->getItem($this->input->getInt('Itemid', null));
}
$id = 0;
if (\is_object($item)) {
// Valid item retrieved
$id = $item->template_style_id;
}
$tid = $this->input->getUint('templateStyle', 0);
if (is_numeric($tid) && (int) $tid > 0) {
$id = (int) $tid;
}
/** @var OutputController $cache */
$cache = Factory::getContainer()->get(CacheControllerFactoryInterface::class)->createCacheController('output', ['defaultgroup' => 'com_templates']);
if ($this->getLanguageFilter()) {
$tag = $this->getLanguage()->getTag();
} else {
$tag = '';
}
$cacheId = 'templates0' . $tag;
if ($cache->contains($cacheId)) {
$templates = $cache->get($cacheId);
} else {
// Load styles
$db = Factory::getDbo();
$query = $db->getQuery(true)->select($db->quoteName(['id', 'home', 'template', 's.params', 'inheritable', 'parent']))->from($db->quoteName('#__template_styles', 's'))->where([$db->quoteName('s.client_id') . ' = 0', $db->quoteName('e.enabled') . ' = 1'])->join('LEFT', $db->quoteName('#__extensions', 'e'), $db->quoteName('e.element') . ' = ' . $db->quoteName('s.template') . ' AND ' . $db->quoteName('e.type') . ' = ' . $db->quote('template') . ' AND ' . $db->quoteName('e.client_id') . ' = ' . $db->quoteName('s.client_id'));
$db->setQuery($query);
$templates = $db->loadObjectList('id');
foreach ($templates as &$template) {
// Create home element
if ($template->home == 1 && !isset($template_home) || $this->getLanguageFilter() && $template->home == $tag) {
$template_home = clone $template;
}
$template->params = new Registry($template->params);
}
// Unset the $template reference to the last $templates[n] item cycled in the foreach above to avoid editing it later
unset($template);
// Add home element, after loop to avoid double execution
if (isset($template_home)) {
$template_home->params = new Registry($template_home->params);
$templates[0] = $template_home;
}
$cache->store($templates, $cacheId);
}
if (isset($templates[$id])) {
$template = $templates[$id];
} else {
$template = $templates[0];
}
// Allows for overriding the active template from the request
$template_override = $this->input->getCmd('template', '');
// Only set template override if it is a valid template (= it exists and is enabled)
if (!empty($template_override)) {
if (is_file(JPATH_THEMES . '/' . $template_override . '/index.php')) {
foreach ($templates as $tmpl) {
if ($tmpl->template === $template_override) {
$template = $tmpl;
break;
}
}
}
}
// Need to filter the default value as well
$template->template = InputFilter::getInstance()->clean($template->template, 'cmd');
// Fallback template
if (!empty($template->parent)) {
if (!is_file(JPATH_THEMES . '/' . $template->template . '/index.php')) {
if (!is_file(JPATH_THEMES . '/' . $template->parent . '/index.php')) {
$this->enqueueMessage(Text::_('JERROR_ALERTNOTEMPLATE'), 'error');
// Try to find data for 'cassiopeia' template
$original_tmpl = $template->template;
foreach ($templates as $tmpl) {
if ($tmpl->template === 'cassiopeia') {
$template = $tmpl;
break;
}
}
// Check, the data were found and if template really exists
if (!is_file(JPATH_THEMES . '/' . $template->template . '/index.php')) {
throw new \InvalidArgumentException(Text::sprintf('JERROR_COULD_NOT_FIND_TEMPLATE', $original_tmpl));
}
}
}
} elseif (!is_file(JPATH_THEMES . '/' . $template->template . '/index.php')) {
$this->enqueueMessage(Text::_('JERROR_ALERTNOTEMPLATE'), 'error');
// Try to find data for 'cassiopeia' template
$original_tmpl = $template->template;
foreach ($templates as $tmpl) {
if ($tmpl->template === 'cassiopeia') {
$template = $tmpl;
break;
}
}
// Check, the data were found and if template really exists
if (!is_file(JPATH_THEMES . '/' . $template->template . '/index.php')) {
throw new \InvalidArgumentException(Text::sprintf('JERROR_COULD_NOT_FIND_TEMPLATE', $original_tmpl));
}
}
// Cache the result
$this->template = $template;
if ($params) {
return $template;
}
return $template->template;
}