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: Changes response format #1960

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions administrator/language/en-GB/en-GB.com_ajax.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@

COM_AJAX="Ajax Interface"
COM_AJAX_DESC="An extensible Ajax interface for Joomla"
COM_AJAX_METHOD_DOES_NOT_EXIST="Method %s does not exist"
COM_AJAX_HELPER_DOES_NOT_EXIST="The file at %s does not exist"
COM_AJAX_MODULE_NOT_PUBLISHED="Module %s is not published, you do not have access to it, or it's not assigned to the current menu item"
COM_AJAX_NO_PLUGIN_RESPONSE="The %s plugin returned no response"
94 changes: 62 additions & 32 deletions components/com_ajax/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
// Requested format passed via URL
$format = strtolower($input->getWord('format'));

// Initialized to prevent notices
// Initialize default response
$results = null;
$error = null;

// Check for valid format
if (!$format)
{
$results = new InvalidArgumentException('Please specify response format other that HTML (json, raw, etc.)', 404);
}
/*
* Module support.
*
Expand All @@ -30,7 +34,7 @@
* (i.e. index.php?option=com_ajax&module=foo).
*
*/
if ($input->get('module'))
elseif ($input->get('module'))
{
$module = $input->get('module');
$moduleObject = JModuleHelper::getModule('mod_' . $module, null);
Expand All @@ -48,28 +52,37 @@

if (is_file($helperFile))
{
require_once($helperFile);
require_once $helperFile;

if (method_exists($class, $method . 'Ajax'))
{
$results = call_user_func($class . '::' . $method . 'Ajax');
try
{
$results = call_user_func($class . '::' . $method . 'Ajax');
}
catch (Exception $e)
{
$results = $e;
}
}
// Method does not exist
else
{
$error = JText::sprintf('COM_AJAX_METHOD_DOES_NOT_EXIST', $method . 'Ajax');
$results = new LogicException(sprintf('Method %s does not exist', $method . 'Ajax'), 404);
}
}
// The helper file does not exist
else
{
$error = JText::sprintf('COM_AJAX_HELPER_DOES_NOT_EXIST', 'mod_' . $module . '/helper.php');
$results = new RuntimeException(sprintf('The file at %s does not exist', 'mod_' . $module . '/helper.php'), 404);
}
}
// Module is not published, you do not have access to it, or it is not assigned to the current menu item
else
{
$error = JText::sprintf('COM_AJAX_MODULE_NOT_PUBLISHED', 'mod_' . $module);
$results = new LogicException(sprintf('Module %s is not published, you do not have access to it, or it\'s not assigned to the current menu item', 'mod_' . $module), 404);
}
}

/*
* Plugin support is based on the "Ajax" plugin group.
*
Expand All @@ -78,45 +91,62 @@
* (i.e. index.php?option=com_ajax&plugin=foo)
*
*/
if ($input->get('plugin'))
elseif ($input->get('plugin'))
{
JPluginHelper::importPlugin('ajax');
$plugin = ucfirst($input->get('plugin'));
$dispatcher = JEventDispatcher::getInstance();
$response = $dispatcher->trigger('onAjax' . $plugin);
$results = $response ? $response : ($error = JText::sprintf('COM_AJAX_NO_PLUGIN_RESPONSE', $plugin));
}

if (!is_null($error))
{
echo $error;
$app->close();
try
{
$results = $dispatcher->trigger('onAjax' . $plugin);
}
catch (Exception $e)
{
$results = $e;
}
}

// Return the results in the desired format
switch ($format)
{
// JSONinzed
case 'json':
header('Content-Type: application/json');
echo json_encode($results);
$app->close();
echo new JResponseJson($results, null, false, $input->get('ignoreMessages', true, 'bool'));
break;

// Human-readable format
case 'debug':
echo '<pre>' . print_r($results, true) . '</pre>';
$app->close();
break;

// Handle as raw format
default:
echo is_array($results) ? implode($results) : $results;
// Emulates format=raw by closing $app
$app->close();
// Output exception
if ($results instanceof Exception)
{
// Log an error
JLog::add($results->getMessage(), JLog::ERROR);

// Set status header code
JResponse::setHeader('status', $results->getCode(), true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Now @mbabker 's JApplication changes have gone through we shouldn't be using JResponse anymore but the JApplication alternative


// Echo exception type and message
$out = get_class($results) . ': ' . $results->getMessage();
}
// Output string/ null
elseif (is_scalar($results))
{
$out = (string) $results;
}
// Output array/ object
else
{
$out = implode((array) $results);
}

echo $out;

break;
}

/*
* References
* Support plugins in your component
* - http://docs.joomla.org/Supporting_plugins_in_your_component
*
* Best way for JSON output
* - https://groups.google.com/d/msg/joomla-dev-cms/WsC0nA9Fixo/Ur-gPqpqh-EJ
*/
4 changes: 0 additions & 4 deletions language/en-GB/en-GB.com_ajax.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@

COM_AJAX="Ajax Interface"
COM_AJAX_DESC="An extensible Ajax interface for Joomla"
COM_AJAX_METHOD_DOES_NOT_EXIST="Method %s does not exist"
COM_AJAX_HELPER_DOES_NOT_EXIST="The file at %s does not exist"
COM_AJAX_MODULE_NOT_PUBLISHED="Module %s is not published, you do not have access to it, or it is not assigned to the current menu item"
COM_AJAX_NO_PLUGIN_RESPONSE="The %s plugin returned no response"