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

[com_ajax] to support templates #16532

Merged
merged 12 commits into from
Aug 20, 2017
1 change: 1 addition & 0 deletions administrator/language/en-GB/en-GB.com_ajax.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ COM_AJAX_SPECIFY_FORMAT="Please specify a valid response format, other than that
COM_AJAX_METHOD_NOT_EXISTS="Method %s does not exist."
COM_AJAX_FILE_NOT_EXISTS="The file at %s does not exist."
COM_AJAX_MODULE_NOT_ACCESSIBLE="Module %s is not published, you do not have access to it, or it's not assigned to the current menu item."
COM_AJAX_TEMPLATE_NOT_ACCESSIBLE="Template %s is not assigned to the current menu item."
93 changes: 93 additions & 0 deletions components/com_ajax/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,99 @@
$results = $e;
}
}
/*
* Template support.
*
* tplFooHelper::getAjax() is called where 'foo' is the value
* of the 'template' variable passed via the URL
* (i.e. index.php?option=com_ajax&template=foo).
*
*/
elseif ($input->get('template'))
{
$template = $input->get('template');

/*
* Only allow access to current menu item's template (like modules) in the front end.
* Allow full access in the back end as user is authenticated.
*/
if (($app->isSite() && $template === $app->getTemplate()) || $app->isAdmin())
Copy link
Member

@laoneo laoneo Jun 5, 2017

Choose a reason for hiding this comment

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

Use the isClient function please

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 :)

{
$basePath = JPATH_BASE;
$helperFile = $basePath . '/templates/' . $template . '/helper.php';

if (strpos($template, '_'))
{
$parts = explode('_', $template);
}
elseif (strpos($template, '-'))
{
$parts = explode('-', $template);
}

if ($parts)
{
$class = 'Tpl';

foreach ($parts as $part)
{
$class .= ucfirst($part);
}

$class .= 'Helper';
}
else
{
$class = 'Tpl' . ucfirst($template) . 'Helper';
}

$method = $input->get('method') ?: 'get';

// Is the back end accessing a front end template?
if ($app->isAdmin() && !is_file($helperFile))
{
$basePath = JPATH_ROOT;
$helperFile = $basePath . '/templates/' . $template . '/helper.php';
}

if (is_file($helperFile))
{
JLoader::register($class, $helperFile);

if (method_exists($class, $method . 'Ajax'))
{
// Load language file for template
$lang = JFactory::getLanguage();
$lang->load('tpl_' . $template, $basePath, null, false, true)
|| $lang->load('tpl_' . $template, $basePath . '/templates/' . $template, null, false, true);

try
{
$results = call_user_func($class . '::' . $method . 'Ajax');
}
catch (Exception $e)
{
$results = $e;
}
}
// Method does not exist
else
{
$results = new LogicException(JText::sprintf('COM_AJAX_METHOD_NOT_EXISTS', $method . 'Ajax'), 404);
}
}
// The helper file does not exist
else
{
$results = new RuntimeException(JText::sprintf('COM_AJAX_FILE_NOT_EXISTS', 'tpl_' . $template . '/helper.php'), 404);
}
}
// Template is not assigned to the current menu item
else
{
$results = new LogicException(JText::sprintf('COM_AJAX_TEMPLATE_NOT_ACCESSIBLE', 'tpl_' . $template), 404);
}
}

// Return the results in the desired format
switch ($format)
Expand Down
1 change: 1 addition & 0 deletions language/en-GB/en-GB.com_ajax.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ COM_AJAX_SPECIFY_FORMAT="Please specify a valid response format, other than that
COM_AJAX_METHOD_NOT_EXISTS="Method %s does not exist."
COM_AJAX_FILE_NOT_EXISTS="The file at %s does not exist."
COM_AJAX_MODULE_NOT_ACCESSIBLE="Module %s is not published, you do not have access to it, or it's not assigned to the current menu item."
COM_AJAX_TEMPLATE_NOT_ACCESSIBLE="Template %s is not assigned to the current menu item."