| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* VikBooking report controller. |
| 16 |
* |
| 17 |
* @since 1.17.1 (J) - 1.7.1 (WP) |
| 18 |
*/ |
| 19 |
class VikBookingControllerReport extends JControllerAdmin |
| 20 |
{ |
| 21 |
/** |
| 22 |
* AJAX endpoint to render the custom settings of a report. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function renderSettings() |
| 27 |
{ |
| 28 |
$app = JFactory::getApplication(); |
| 29 |
|
| 30 |
if (!JSession::checkToken()) { |
| 31 |
// missing CSRF-proof token |
| 32 |
VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); |
| 33 |
} |
| 34 |
|
| 35 |
$report = $app->input->getString('report', ''); |
| 36 |
|
| 37 |
$report_obj = VikBooking::getReportInstance($report); |
| 38 |
|
| 39 |
if (!$report_obj) { |
| 40 |
// invalid report requested |
| 41 |
VBOHttpDocument::getInstance($app)->close(404, sprintf('Could not find the report [%s] to load the settings from.', $report)); |
| 42 |
} |
| 43 |
|
| 44 |
// fetch the report form settings |
| 45 |
$layout_data = [ |
| 46 |
'report' => $report, |
| 47 |
'fields' => $report_obj->getProfileSettingFields(), |
| 48 |
'settings' => $report_obj->loadSettings(), |
| 49 |
'instance' => $report_obj, |
| 50 |
]; |
| 51 |
|
| 52 |
$form_html = JLayoutHelper::render('reports.report.settings', $layout_data); |
| 53 |
|
| 54 |
// send the response to output |
| 55 |
VBOHttpDocument::getInstance($app)->json([ |
| 56 |
'html' => $form_html, |
| 57 |
]); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* AJAX endpoint to save the custom settings of a report. |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function saveSettings() |
| 66 |
{ |
| 67 |
$app = JFactory::getApplication(); |
| 68 |
|
| 69 |
if (!JSession::checkToken()) { |
| 70 |
// missing CSRF-proof token |
| 71 |
VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); |
| 72 |
} |
| 73 |
|
| 74 |
$report = $app->input->getString('report', ''); |
| 75 |
$data = $app->input->get('data', [], 'array'); |
| 76 |
|
| 77 |
$profile = $app->input->getString('_profile', ''); |
| 78 |
$profile_name = $app->input->getString('_newprofile', ''); |
| 79 |
|
| 80 |
$report_obj = VikBooking::getReportInstance($report); |
| 81 |
|
| 82 |
if (!$report_obj) { |
| 83 |
// invalid report requested |
| 84 |
VBOHttpDocument::getInstance($app)->close(404, sprintf('Could not find the report [%s] to save the settings for.', $report)); |
| 85 |
} |
| 86 |
|
| 87 |
// check for settings profile identifier |
| 88 |
$use_profile_id = null; |
| 89 |
if ($report_obj->allowsProfileSettings() && !empty($profile)) { |
| 90 |
if ($profile == '_new' && empty($profile_name)) { |
| 91 |
VBOHttpDocument::getInstance($app)->close(500, 'Please specify the name for the new settings profile.'); |
| 92 |
} |
| 93 |
|
| 94 |
// set active profile |
| 95 |
$use_profile_id = $profile; |
| 96 |
|
| 97 |
if (!empty($profile_name)) { |
| 98 |
// add the new profile |
| 99 |
list($use_profile_id, $profile_name) = $report_obj->setSettingProfile($profile_name); |
| 100 |
} |
| 101 |
|
| 102 |
// update active profile |
| 103 |
$report_obj->setActiveProfile($use_profile_id); |
| 104 |
} |
| 105 |
|
| 106 |
// save report settings |
| 107 |
$report_obj->saveSettings($data, $merge = true, (string) $use_profile_id); |
| 108 |
|
| 109 |
// send the response to output |
| 110 |
VBOHttpDocument::getInstance($app)->json([ |
| 111 |
'success' => 1, |
| 112 |
'profiles' => $report_obj->getSettingProfiles(), |
| 113 |
'active_profile' => $use_profile_id, |
| 114 |
]); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* AJAX endpoint to execute a custom scoped action of a report. |
| 119 |
* |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
public function executeCustomAction() |
| 123 |
{ |
| 124 |
$app = JFactory::getApplication(); |
| 125 |
|
| 126 |
if (!JSession::checkToken()) { |
| 127 |
// missing CSRF-proof token |
| 128 |
VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); |
| 129 |
} |
| 130 |
|
| 131 |
$report = $app->input->getString('report_file', ''); |
| 132 |
$action = $app->input->getString('report_action', ''); |
| 133 |
$scope = $app->input->getString('report_scope', ''); |
| 134 |
$data = $app->input->get('report_data', [], 'array'); |
| 135 |
|
| 136 |
$report_obj = VikBooking::getReportInstance($report); |
| 137 |
|
| 138 |
if (!$report_obj) { |
| 139 |
// invalid report requested |
| 140 |
VBOHttpDocument::getInstance($app)->close(404, sprintf('Could not find the report [%s] for executing the action.', $report)); |
| 141 |
} |
| 142 |
|
| 143 |
// get all the available scoped actions, hidden and visible |
| 144 |
$actions = $report_obj->getScopedActions($scope, $visible = false); |
| 145 |
if (!in_array($action, array_column($actions, 'id'))) { |
| 146 |
// unsupported action |
| 147 |
VBOHttpDocument::getInstance($app)->close(403, sprintf('Unsupported report action [%s].', $action)); |
| 148 |
} |
| 149 |
|
| 150 |
try { |
| 151 |
$result = $report_obj->executeAction($action, $scope, $data); |
| 152 |
} catch (Exception $e) { |
| 153 |
VBOHttpDocument::getInstance($app)->close($e->getCode(), $e->getMessage()); |
| 154 |
} |
| 155 |
|
| 156 |
// send the response to output |
| 157 |
VBOHttpDocument::getInstance($app)->json($result); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* AJAX endpoint to set the report active profile settings identifier. |
| 162 |
* |
| 163 |
* @return void |
| 164 |
* |
| 165 |
* @since 1.17.7 (J) - 1.7.7 (WP) |
| 166 |
*/ |
| 167 |
public function setActiveProfile() |
| 168 |
{ |
| 169 |
$app = JFactory::getApplication(); |
| 170 |
|
| 171 |
if (!JSession::checkToken()) { |
| 172 |
// missing CSRF-proof token |
| 173 |
VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); |
| 174 |
} |
| 175 |
|
| 176 |
$report = $app->input->getString('report_file', ''); |
| 177 |
$profile = $app->input->getString('report_profile', ''); |
| 178 |
|
| 179 |
$report_obj = VikBooking::getReportInstance($report); |
| 180 |
|
| 181 |
if (!$report_obj) { |
| 182 |
// invalid report requested |
| 183 |
VBOHttpDocument::getInstance($app)->close(404, sprintf('Could not find the report [%s].', $report)); |
| 184 |
} |
| 185 |
|
| 186 |
$report_obj->setActiveProfile($profile); |
| 187 |
|
| 188 |
// send response to output |
| 189 |
VBOHttpDocument::getInstance($app)->json([ |
| 190 |
'profile' => $profile, |
| 191 |
]); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* AJAX endpoint to clear all report profile settings. |
| 196 |
* |
| 197 |
* @return void |
| 198 |
* |
| 199 |
* @since 1.17.7 (J) - 1.7.7 (WP) |
| 200 |
*/ |
| 201 |
public function clearProfiles() |
| 202 |
{ |
| 203 |
$app = JFactory::getApplication(); |
| 204 |
|
| 205 |
if (!JSession::checkToken()) { |
| 206 |
// missing CSRF-proof token |
| 207 |
VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); |
| 208 |
} |
| 209 |
|
| 210 |
$report = $app->input->getString('report_file', ''); |
| 211 |
|
| 212 |
$report_obj = VikBooking::getReportInstance($report); |
| 213 |
|
| 214 |
if (!$report_obj) { |
| 215 |
// invalid report requested |
| 216 |
VBOHttpDocument::getInstance($app)->close(404, sprintf('Could not find the report [%s].', $report)); |
| 217 |
} |
| 218 |
|
| 219 |
$report_obj->clearProfiles(); |
| 220 |
|
| 221 |
// send response to output |
| 222 |
VBOHttpDocument::getInstance($app)->json([ |
| 223 |
'success' => true, |
| 224 |
]); |
| 225 |
} |
| 226 |
} |
| 227 |
|