diff --git a/tjreports/plugins/api/reports/language/en-GB/en-GB.plg_api_reports.ini b/tjreports/plugins/api/reports/language/en-GB/en-GB.plg_api_reports.ini new file mode 100644 index 0000000..c01b6f1 --- /dev/null +++ b/tjreports/plugins/api/reports/language/en-GB/en-GB.plg_api_reports.ini @@ -0,0 +1,7 @@ +; @package Com_Tjreports +; @copyright Copyright © 2009-2018 Techjoomla. All rights reserved. +; @license GNU General Public License version 2, or later +; Note: All ini files need to be saved as UTF-8 + +PLG_API_REPORTS_REPORT_NAME_MISSSING="Please enter report name" +PLG_API_REPORTS_REPORT_NAME_INVALID="Please enter valid report name" diff --git a/tjreports/plugins/api/reports/language/en-GB/en-GB.plg_api_reports.sys.ini b/tjreports/plugins/api/reports/language/en-GB/en-GB.plg_api_reports.sys.ini new file mode 100644 index 0000000..5dc53f0 --- /dev/null +++ b/tjreports/plugins/api/reports/language/en-GB/en-GB.plg_api_reports.sys.ini @@ -0,0 +1,7 @@ +; @package Com_Tjreports +; @copyright Copyright © 2009-2018 Techjoomla. All rights reserved. +; @license GNU General Public License version 2, or later +; Note: All ini files need to be saved as UTF-8 + +PLG_API_REPORTS="TJ Reports Api Plugin" +PLG_API_REPORTS_DESCRIPTION="TJ Reports Api Plugin" diff --git a/tjreports/plugins/api/reports/reports.php b/tjreports/plugins/api/reports/reports.php new file mode 100644 index 0000000..5d4a54b --- /dev/null +++ b/tjreports/plugins/api/reports/reports.php @@ -0,0 +1,43 @@ + + * @copyright Copyright (C) 2009 - 2018 Techjoomla. All rights reserved. + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +// No direct access. +defined('_JEXEC') or die; + +jimport('joomla.plugin.plugin'); +JLoader::import('components.com_tjreports.models.report', JPATH_SITE); + +/** + * Tjreports API plugin + * + * @since 1.0 + */ +class PlgAPIReports extends ApiPlugin +{ + /** + * Constructor + * + * @param STRING &$subject subject + * @param array $config config + * + * @since 1.0 + */ + public function __construct(&$subject, $config = array()) + { + parent::__construct($subject, $config = array()); + + // Set resource path + ApiResource::addIncludePath(dirname(__FILE__) . '/reports'); + + // Load language files + $lang = JFactory::getLanguage(); + $lang->load('plg_api_reports', JPATH_SITE . "/plugins/api/reports/", 'en-GB', true); + } +} diff --git a/tjreports/plugins/api/reports/reports.xml b/tjreports/plugins/api/reports/reports.xml new file mode 100644 index 0000000..6bf8411 --- /dev/null +++ b/tjreports/plugins/api/reports/reports.xml @@ -0,0 +1,21 @@ + + + PLG_API_REPORTS + 1.0 + 28th May 2018 + Techjoomla + extensions@techjoomla.com + https://techjoomla.com + Techjoomla. All rights reserved. + http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL + PLG_API_REPORTS_DESCRIPTION + + reports.php + reports + language + + + en-GB/en-GB.plg_api_reports.ini + en-GB/en-GB.plg_api_reports.sys.ini + + diff --git a/tjreports/plugins/api/reports/reports/filters.php b/tjreports/plugins/api/reports/reports/filters.php new file mode 100644 index 0000000..c6ff182 --- /dev/null +++ b/tjreports/plugins/api/reports/reports/filters.php @@ -0,0 +1,61 @@ + + * @copyright Copyright (C) 2009 - 2018 Techjoomla. All rights reserved. + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +// No direct access. +defined('_JEXEC') or die; + +/** + * Tjreports API report class + * + * @since 1.0.0 + */ +class ReportsApiResourceFilters extends ApiResource +{ + /** + * Function get filters data + * + * @return boolean + */ + public function get() + { + $app = JFactory::getApplication(); + $jinput = $app->input; + $reportName = $jinput->getString('id'); + + if (!isset($reportName)) + { + ApiError::raiseError(400, JText::_('PLG_API_REPORTS_REPORT_NAME_MISSSING'), 'APIValidationException'); + } + + $lang = JFactory::getLanguage(); + //load default joomla language file + $lang->load('', JPATH_ADMINISTRATOR, 'en-GB', true); + + // Make object of the tjreports plugin to load filters for + JLoader::import('plugins.tjreports.' . $reportName . "." . $reportName, JPATH_SITE); + $className = 'TjreportsModel' . ucfirst($reportName); + + if (!class_exists($className)) + { + ApiError::raiseError(400, JText::_('PLG_API_REPORTS_REPORT_NAME_INVALID'), 'APIValidationException'); + } + + $reportPlugin = new $className; + + $filters = $reportPlugin->displayFilters(); + $filter_array = []; + foreach ($filters[0] as $key => $value) { + $value['name'] = $key; + $filter_array[] = $value; + } + + $this->plugin->setResponse($filter_array); + } +} diff --git a/tjreports/plugins/api/reports/reports/report.php b/tjreports/plugins/api/reports/reports/report.php new file mode 100644 index 0000000..3ba048c --- /dev/null +++ b/tjreports/plugins/api/reports/reports/report.php @@ -0,0 +1,97 @@ + + * @copyright Copyright (C) 2009 - 2018 Techjoomla. All rights reserved. + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +// No direct access. +defined('_JEXEC') or die; + +/** + * Tjreports API report class + * + * @since 1.0.0 + */ +class ReportsApiResourceReport extends ApiResource +{ + /** + * Function to get report data from tjreports plugin + * + * @return json + */ + public function post() + { + $app = JFactory::getApplication(); + $jinput = $app->input; + $formData = $jinput->post; + $reportName = $app->input->getString('id'); + + if (empty($reportName)) + { + $reportName = $formData->getString('report'); + } + + if (!isset($reportName)) + { + ApiError::raiseError(400, JText::_('PLG_API_REPORTS_REPORT_NAME_MISSSING'), 'APIValidationException'); + } + + // Create object of tjreports plugin class + + JLoader::import('plugins.tjreports.' . $reportName . "." . $reportName, JPATH_SITE); + $className = 'TjreportsModel' . ucfirst($reportName); + + if (!class_exists($className)) + { + ApiError::raiseError(400, JText::_('PLG_API_REPORTS_REPORT_NAME_INVALID'), 'APIValidationException'); + } + + $reportPlugin = new $className; + + // Load language files + $lang = JFactory::getLanguage(); + $lang->load('com_tjreports', JPATH_ADMINISTRATOR, 'en-GB', true); + $lang->load('plg_tjreports_' . $reportName, JPATH_SITE . "/plugins/tjreports/" . $reportName, 'en-GB', true); + + // Get filters and cols + $reportId = $reportPlugin->getDefaultReport($reportName); + $reportFilters = ($formData->get('filters')) ? $formData->get('filters') : []; + $reportCols = ($formData->get('colToshow')) ? $formData->get('colToshow') : []; + + $reportPlugin->setState('filters', $reportFilters); + $reportPlugin->setState('colToshow', $reportCols); + $reportPlugin->setState('reportId', $reportId); + + // Get results and errors if any + $report = $reportPlugin->getItems(); + $errors = $reportPlugin->getTJRMessages(); + + if (!empty($errors)) + { + ApiError::raiseError(400, $errors[0], 'APIValidationException'); + } + + // @TODO Handle else condition first to reduce nesting + if (!empty($reportCols)) + { + foreach ($report as $key => $value) + { + foreach ($value as $k => $v) + { + if (!in_array($k, $reportCols)) + { + unset($value[$k]); + } + } + + $report[$key] = $value; + } + } + + $this->plugin->setResponse($report); + } +} diff --git a/tjreports/script.tjreports.php b/tjreports/script.tjreports.php index 7d95be2..1585029 100755 --- a/tjreports/script.tjreports.php +++ b/tjreports/script.tjreports.php @@ -59,6 +59,9 @@ class Com_TjreportsInstallerScript ), 'user' => array( 'tjreportsindexer' => 0 + ), + 'api' => array( + 'reports' => 1 ) ) );