| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2025 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 task manager controller (site). |
| 16 |
* |
| 17 |
* @since 1.18.0 (J) - 1.8.0 (WP) |
| 18 |
*/ |
| 19 |
class VikBookingControllerTaskmanager extends JControllerAdmin |
| 20 |
{ |
| 21 |
/** |
| 22 |
* AJAX endpoint to update an existing TM task. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function updateTask() |
| 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 |
// the name of the tool to access |
| 36 |
$tool = 'task_manager'; |
| 37 |
|
| 38 |
try { |
| 39 |
// obtain data from the validation of the current operator and tool permissions |
| 40 |
list($operator, $permissions, $tool_uri) = VikBooking::getOperatorInstance()->authOperatorToolData($tool); |
| 41 |
} catch (Exception $e) { |
| 42 |
// abort |
| 43 |
VBOHttpDocument::getInstance($app)->close($e->getCode(), $e->getMessage()); |
| 44 |
} |
| 45 |
|
| 46 |
// gather request values to update |
| 47 |
$data = (array) $app->input->get('data', [], 'array'); |
| 48 |
|
| 49 |
if (empty($data['id'])) { |
| 50 |
// missing task id |
| 51 |
VBOHttpDocument::getInstance($app)->close(400, 'Missing task record ID.'); |
| 52 |
} |
| 53 |
|
| 54 |
// build a list of properties supported for the update |
| 55 |
$supportedProperties = [ |
| 56 |
'dueon', |
| 57 |
'status_enum', |
| 58 |
'tags', |
| 59 |
]; |
| 60 |
|
| 61 |
// filter out unsupported record properties for modification |
| 62 |
$data = array_filter($data, function($value, $property) use ($supportedProperties) { |
| 63 |
return !empty($value) && ($property == 'id' || in_array($property, $supportedProperties)); |
| 64 |
}, ARRAY_FILTER_USE_BOTH); |
| 65 |
|
| 66 |
if (count($data) < 2) { |
| 67 |
// nothing to update except the ID |
| 68 |
VBOHttpDocument::getInstance($app)->close(500, 'Nothing to update.'); |
| 69 |
} |
| 70 |
|
| 71 |
// update the existing record |
| 72 |
if (!VBOTaskModelTask::getInstance()->update($data)) { |
| 73 |
// query failed |
| 74 |
VBOHttpDocument::getInstance($app)->close(500, 'Could not update the database record. Please try again.'); |
| 75 |
} |
| 76 |
|
| 77 |
// send the response to output |
| 78 |
VBOHttpDocument::getInstance($app)->json([ |
| 79 |
'success' => 1, |
| 80 |
'taskId' => $data['id'], |
| 81 |
]); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* AJAX endpoint to update the N-th checkbox of a task. |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function updateChecklist() |
| 90 |
{ |
| 91 |
$app = JFactory::getApplication(); |
| 92 |
|
| 93 |
if (!JSession::checkToken()) { |
| 94 |
// missing CSRF-proof token |
| 95 |
VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); |
| 96 |
} |
| 97 |
|
| 98 |
// the name of the tool to access |
| 99 |
$tool = 'task_manager'; |
| 100 |
|
| 101 |
try { |
| 102 |
// obtain data from the validation of the current operator and tool permissions |
| 103 |
list($operator, $permissions, $tool_uri) = VikBooking::getOperatorInstance()->authOperatorToolData($tool); |
| 104 |
|
| 105 |
$taskId = $app->input->getUint('id', 0); |
| 106 |
$index = $app->input->getUint('index', 0); |
| 107 |
$status = $app->input->getBool('status', null); |
| 108 |
|
| 109 |
VBOTaskModelTask::getInstance()->updateChecklist($taskId, $index, $status); |
| 110 |
} catch (Exception $e) { |
| 111 |
// abort |
| 112 |
VBOHttpDocument::getInstance($app)->close($e->getCode(), $e->getMessage()); |
| 113 |
} |
| 114 |
|
| 115 |
// send the response to output |
| 116 |
$app->close(); |
| 117 |
} |
| 118 |
} |
| 119 |
|