| 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 |
* Obtain vars from arguments received in the layout file. |
| 16 |
* |
| 17 |
* @var string $caller The identifier of who's calling this layout file. |
| 18 |
* @var array $events The custom event names to trigger for certain actions. |
| 19 |
* @var array $active List of active area IDs by default. |
| 20 |
*/ |
| 21 |
extract($displayData); |
| 22 |
|
| 23 |
// load context menu assets |
| 24 |
VikBooking::getVboApplication()->loadContextMenuAssets(); |
| 25 |
|
| 26 |
// load all the existing task areas |
| 27 |
$taskAreas = VBOTaskModelArea::getInstance()->getItems(); |
| 28 |
|
| 29 |
// define the default events |
| 30 |
if (!isset($events) || !is_array($events)) { |
| 31 |
$events = []; |
| 32 |
} |
| 33 |
$events['render_task'] = $events['render_task'] ?? 'vbo-tm-area-render-task'; |
| 34 |
$events['hide_task'] = $events['hide_task'] ?? 'vbo-tm-area-hide-task'; |
| 35 |
$events['update_areas'] = $events['update_areas'] ?? 'vbo-tm-area-update-status'; |
| 36 |
$events['create_areas'] = $events['create_areas'] ?? 'vbo-tm-area-create-trigger'; |
| 37 |
|
| 38 |
// define the default active areas |
| 39 |
if (!isset($active) || !is_array($active)) { |
| 40 |
$active = []; |
| 41 |
} |
| 42 |
|
| 43 |
?> |
| 44 |
|
| 45 |
<button type="button" class="btn vbo-context-menu-btn vbo-context-menu-tm-areas"> |
| 46 |
<span class="vbo-context-menu-ico"><?php VikBookingIcons::e('sort-down'); ?></span> |
| 47 |
<span class="vbo-context-menu-lbl"><?php echo JText::translate('VBO_PROJECTS_AREAS'); ?></span> |
| 48 |
</button> |
| 49 |
|
| 50 |
<script type="text/javascript"> |
| 51 |
jQuery(function() { |
| 52 |
|
| 53 |
// all task areas |
| 54 |
const taskAreas = <?php echo json_encode($taskAreas); ?>; |
| 55 |
|
| 56 |
// active task areas |
| 57 |
const activeAreas = <?php echo json_encode($active); ?>; |
| 58 |
|
| 59 |
const createNewTaskManagerArea = () => { |
| 60 |
// define the modal cancel button |
| 61 |
let cancel_btn = jQuery('<button></button>') |
| 62 |
.attr('type', 'button') |
| 63 |
.addClass('btn') |
| 64 |
.text(<?php echo json_encode(JText::translate('VBANNULLA')); ?>) |
| 65 |
.on('click', () => { |
| 66 |
VBOCore.emitEvent('vbo-tm-newarea-dismiss'); |
| 67 |
}); |
| 68 |
|
| 69 |
// define the modal save button |
| 70 |
let save_btn = jQuery('<button></button>') |
| 71 |
.attr('type', 'button') |
| 72 |
.addClass('btn btn-success') |
| 73 |
.text(<?php echo json_encode(JText::translate('VBSAVE')); ?>) |
| 74 |
.on('click', function() { |
| 75 |
// disable button to prevent double submissions |
| 76 |
let submit_btn = jQuery(this); |
| 77 |
submit_btn.prop('disabled', true); |
| 78 |
|
| 79 |
// start loading animation |
| 80 |
VBOCore.emitEvent('vbo-tm-newarea-loading'); |
| 81 |
|
| 82 |
// get form data |
| 83 |
const areaForm = new FormData(document.querySelector('#vbo-tm-area-manage-form')); |
| 84 |
|
| 85 |
// build query parameters for the request |
| 86 |
let qpRequest = new URLSearchParams(areaForm).toString(); |
| 87 |
|
| 88 |
// make the request |
| 89 |
VBOCore.doAjax( |
| 90 |
"<?php echo VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=taskmanager.createArea'); ?>", |
| 91 |
qpRequest, |
| 92 |
(resp) => { |
| 93 |
// reload current page on success |
| 94 |
window.location.reload(); |
| 95 |
}, |
| 96 |
(error) => { |
| 97 |
// display error message |
| 98 |
alert(error.responseText); |
| 99 |
|
| 100 |
// re-enable submit button |
| 101 |
submit_btn.prop('disabled', false); |
| 102 |
|
| 103 |
// stop loading |
| 104 |
VBOCore.emitEvent('vbo-tm-newarea-loading'); |
| 105 |
} |
| 106 |
); |
| 107 |
}); |
| 108 |
|
| 109 |
// display modal |
| 110 |
let modalBody = VBOCore.displayModal({ |
| 111 |
suffix: 'tm_newarea_modal', |
| 112 |
title: <?php echo json_encode(JText::translate('VBO_PROJECT_AREA')); ?> + ' - ' + <?php echo json_encode(JText::translate('VBO_ADD_NEW')); ?>, |
| 113 |
extra_class: 'vbo-modal-rounded vbo-modal-tall vbo-modal-taller', |
| 114 |
body_prepend: true, |
| 115 |
lock_scroll: true, |
| 116 |
footer_left: cancel_btn, |
| 117 |
footer_right: save_btn, |
| 118 |
loading_event: 'vbo-tm-newarea-loading', |
| 119 |
dismiss_event: 'vbo-tm-newarea-dismiss', |
| 120 |
}); |
| 121 |
|
| 122 |
// start loading animation |
| 123 |
VBOCore.emitEvent('vbo-tm-newarea-loading'); |
| 124 |
|
| 125 |
// make the request |
| 126 |
VBOCore.doAjax( |
| 127 |
"<?php echo VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=taskmanager.renderLayout'); ?>", |
| 128 |
{ |
| 129 |
type: 'areas.managearea', |
| 130 |
data: { |
| 131 |
form_id: 'vbo-tm-area-manage-form', |
| 132 |
}, |
| 133 |
}, |
| 134 |
(resp) => { |
| 135 |
// stop loading |
| 136 |
VBOCore.emitEvent('vbo-tm-newarea-loading'); |
| 137 |
|
| 138 |
try { |
| 139 |
// decode the response (if needed), and append the content to the modal body |
| 140 |
let obj_res = typeof resp === 'string' ? JSON.parse(resp) : resp; |
| 141 |
modalBody.append(obj_res['html']); |
| 142 |
} catch (err) { |
| 143 |
console.error('Error decoding the response', err, resp); |
| 144 |
} |
| 145 |
}, |
| 146 |
(error) => { |
| 147 |
// display error message |
| 148 |
alert(error.responseText); |
| 149 |
|
| 150 |
// stop loading |
| 151 |
VBOCore.emitEvent('vbo-tm-newarea-loading'); |
| 152 |
} |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
// build task area buttons |
| 157 |
let btns = []; |
| 158 |
|
| 159 |
taskAreas.forEach((area, index) => { |
| 160 |
// build button text element |
| 161 |
let btnTxtEl = jQuery('<span></span>') |
| 162 |
.addClass('vbo-ctxmenu-entry-icn') |
| 163 |
.html(jQuery('<span></span>').text(area.name)); |
| 164 |
let btnTxtIcn = jQuery('<i></i>') |
| 165 |
.addClass('vbo-tm-area-edit') |
| 166 |
.addClass('<?php echo VikBookingIcons::i('edit'); ?>'); |
| 167 |
btnTxtEl.append(btnTxtIcn); |
| 168 |
|
| 169 |
// push area button |
| 170 |
btns.push({ |
| 171 |
activeState: activeAreas.includes(parseInt(area.id)), |
| 172 |
areaId: area.id, |
| 173 |
class: 'vbo-context-menu-entry-secondary', |
| 174 |
text: btnTxtEl, |
| 175 |
separator: (++index == taskAreas.length), |
| 176 |
icon: function() { |
| 177 |
return this.activeState === true ? '<?php echo VikBookingIcons::i('check-square'); ?>' : '<?php echo VikBookingIcons::i('far fa-square'); ?>'; |
| 178 |
}, |
| 179 |
action: function(root, event) { |
| 180 |
if (!event?.target || !jQuery(event.target).hasClass('vbo-tm-area-edit')) { |
| 181 |
// toggle active state |
| 182 |
this.activeState = !this.activeState; |
| 183 |
|
| 184 |
if (this.activeState) { |
| 185 |
// push the current area ID to the active list |
| 186 |
if (typeof vboTmActiveAreas !== 'undefined' && Array.isArray(vboTmActiveAreas)) { |
| 187 |
vboTmActiveAreas.push(parseInt(this.areaId)); |
| 188 |
} |
| 189 |
|
| 190 |
// render tasks for the selected area |
| 191 |
VBOCore.emitEvent('<?php echo $events['render_task']; ?>', { |
| 192 |
areaId: this.areaId, |
| 193 |
}); |
| 194 |
} else { |
| 195 |
// delete the current area ID from the active list |
| 196 |
if (typeof vboTmActiveAreas !== 'undefined' && Array.isArray(vboTmActiveAreas)) { |
| 197 |
vboTmActiveAreas.forEach((activeAreaId, activeAreaIndex) => { |
| 198 |
if (activeAreaId == this.areaId) { |
| 199 |
// remove the area ID from the active list |
| 200 |
vboTmActiveAreas.splice(activeAreaIndex, 1); |
| 201 |
return; |
| 202 |
} |
| 203 |
}); |
| 204 |
} |
| 205 |
|
| 206 |
// hide tasks for the selected area |
| 207 |
VBOCore.emitEvent('<?php echo $events['hide_task']; ?>', { |
| 208 |
areaId: this.areaId, |
| 209 |
}); |
| 210 |
} |
| 211 |
|
| 212 |
// do not proceed |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
// define the modal cancel button |
| 217 |
let cancel_btn = jQuery('<button></button>') |
| 218 |
.attr('type', 'button') |
| 219 |
.addClass('btn') |
| 220 |
.text(<?php echo json_encode(JText::translate('VBANNULLA')); ?>) |
| 221 |
.on('click', () => { |
| 222 |
VBOCore.emitEvent('vbo-tm-editarea-dismiss'); |
| 223 |
}); |
| 224 |
|
| 225 |
// define the modal save button |
| 226 |
let save_btn = jQuery('<button></button>') |
| 227 |
.attr('type', 'button') |
| 228 |
.addClass('btn btn-success') |
| 229 |
.text(<?php echo json_encode(JText::translate('VBSAVE')); ?>) |
| 230 |
.on('click', function() { |
| 231 |
// disable button to prevent double submissions |
| 232 |
let submit_btn = jQuery(this); |
| 233 |
submit_btn.prop('disabled', true); |
| 234 |
|
| 235 |
// start loading animation |
| 236 |
VBOCore.emitEvent('vbo-tm-editarea-loading'); |
| 237 |
|
| 238 |
// get form data |
| 239 |
const areaForm = new FormData(document.querySelector('#vbo-tm-area-manage-form')); |
| 240 |
|
| 241 |
// build query parameters for the request |
| 242 |
let qpRequest = new URLSearchParams(areaForm); |
| 243 |
|
| 244 |
// make sure the request always includes the tags query parameter, even if the list is empty |
| 245 |
if (!qpRequest.has('area[tags][]')) { |
| 246 |
qpRequest.append('area[tags][]', []); |
| 247 |
} |
| 248 |
|
| 249 |
// make sure the request always includes the statuses query parameter, even if the list is empty |
| 250 |
if (!qpRequest.has('area[status_enums][]')) { |
| 251 |
qpRequest.append('area[status_enums][]', []); |
| 252 |
} |
| 253 |
|
| 254 |
// make the request |
| 255 |
VBOCore.doAjax( |
| 256 |
"<?php echo VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=taskmanager.updateArea'); ?>", |
| 257 |
qpRequest.toString(), |
| 258 |
(resp) => { |
| 259 |
// reload current page on success |
| 260 |
window.location.reload(); |
| 261 |
}, |
| 262 |
(error) => { |
| 263 |
// display error message |
| 264 |
alert(error.responseText); |
| 265 |
|
| 266 |
// re-enable submit button |
| 267 |
submit_btn.prop('disabled', false); |
| 268 |
|
| 269 |
// stop loading |
| 270 |
VBOCore.emitEvent('vbo-tm-editarea-loading'); |
| 271 |
} |
| 272 |
); |
| 273 |
}); |
| 274 |
|
| 275 |
// display modal |
| 276 |
let modalBody = VBOCore.displayModal({ |
| 277 |
suffix: 'tm_editarea_modal', |
| 278 |
title: <?php echo json_encode(JText::translate('VBO_PROJECT_AREA')); ?> + ' - ' + <?php echo json_encode(JText::translate('VBMAINPAYMENTSEDIT')); ?>, |
| 279 |
extra_class: 'vbo-modal-rounded vbo-modal-tall vbo-modal-taller', |
| 280 |
body_prepend: true, |
| 281 |
lock_scroll: true, |
| 282 |
footer_left: cancel_btn, |
| 283 |
footer_right: save_btn, |
| 284 |
loading_event: 'vbo-tm-editarea-loading', |
| 285 |
dismiss_event: 'vbo-tm-editarea-dismiss', |
| 286 |
}); |
| 287 |
|
| 288 |
// start loading animation |
| 289 |
VBOCore.emitEvent('vbo-tm-editarea-loading'); |
| 290 |
|
| 291 |
// make the request |
| 292 |
VBOCore.doAjax( |
| 293 |
"<?php echo VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=taskmanager.renderLayout'); ?>", |
| 294 |
{ |
| 295 |
type: 'areas.managearea', |
| 296 |
data: { |
| 297 |
id: area.id, |
| 298 |
form_id: 'vbo-tm-area-manage-form', |
| 299 |
}, |
| 300 |
}, |
| 301 |
(resp) => { |
| 302 |
// stop loading |
| 303 |
VBOCore.emitEvent('vbo-tm-editarea-loading'); |
| 304 |
|
| 305 |
try { |
| 306 |
// decode the response (if needed), and append the content to the modal body |
| 307 |
let obj_res = typeof resp === 'string' ? JSON.parse(resp) : resp; |
| 308 |
modalBody.append(obj_res['html']); |
| 309 |
} catch (err) { |
| 310 |
console.error('Error decoding the response', err, resp); |
| 311 |
} |
| 312 |
}, |
| 313 |
(error) => { |
| 314 |
// display error message |
| 315 |
alert(error.responseText); |
| 316 |
|
| 317 |
// stop loading |
| 318 |
VBOCore.emitEvent('vbo-tm-editarea-loading'); |
| 319 |
} |
| 320 |
); |
| 321 |
}, |
| 322 |
}); |
| 323 |
}); |
| 324 |
|
| 325 |
if (taskAreas.length) { |
| 326 |
// push show all button |
| 327 |
btns.push({ |
| 328 |
class: 'vbo-context-menu-entry-warning', |
| 329 |
text: <?php echo json_encode(JText::translate('VBPARAMSEASONCALENDARPRICESANY')); ?>, |
| 330 |
icon: '<?php echo VikBookingIcons::i('eye'); ?>', |
| 331 |
action: function(root, config) { |
| 332 |
let all_btns = jQuery(root).vboContextMenu('buttons'); |
| 333 |
all_btns.forEach((btn) => { |
| 334 |
if (typeof btn.activeState === 'undefined') { |
| 335 |
return; |
| 336 |
} |
| 337 |
if (!btn.activeState) { |
| 338 |
// call button click action |
| 339 |
btn.action(root, config); |
| 340 |
} |
| 341 |
}); |
| 342 |
}, |
| 343 |
disabled: function(root, config) { |
| 344 |
// disabled when all buttons are active |
| 345 |
return config.buttons.every(btn => btn.activeState !== false); |
| 346 |
}, |
| 347 |
}); |
| 348 |
|
| 349 |
// push hide all button |
| 350 |
btns.push({ |
| 351 |
class: 'vbo-context-menu-entry-warning', |
| 352 |
text: <?php echo json_encode(JText::translate('VBO_HIDE_ALL')); ?>, |
| 353 |
icon: '<?php echo VikBookingIcons::i('eye-slash'); ?>', |
| 354 |
separator: true, |
| 355 |
action: function(root, config) { |
| 356 |
let all_btns = jQuery(root).vboContextMenu('buttons'); |
| 357 |
all_btns.forEach((btn) => { |
| 358 |
if (typeof btn.activeState === 'undefined') { |
| 359 |
return; |
| 360 |
} |
| 361 |
if (btn.activeState) { |
| 362 |
// call button click action |
| 363 |
btn.action(root, config); |
| 364 |
} |
| 365 |
}); |
| 366 |
}, |
| 367 |
disabled: function(root, config) { |
| 368 |
// disabled when all buttons are inactive |
| 369 |
return config.buttons.every(btn => btn.activeState !== true); |
| 370 |
}, |
| 371 |
}); |
| 372 |
} |
| 373 |
|
| 374 |
// push create new area button |
| 375 |
btns.push({ |
| 376 |
class: 'vbo-context-menu-entry-success', |
| 377 |
text: <?php echo json_encode(JText::translate('VBO_ADD_NEW')); ?>, |
| 378 |
icon: '<?php echo VikBookingIcons::i('plus-circle'); ?>', |
| 379 |
action: function(root, config) { |
| 380 |
createNewTaskManagerArea(); |
| 381 |
} |
| 382 |
}); |
| 383 |
|
| 384 |
// start context menu on the proper button element |
| 385 |
jQuery('.vbo-context-menu-tm-areas').vboContextMenu({ |
| 386 |
placement: 'bottom-left', |
| 387 |
buttons: btns, |
| 388 |
}); |
| 389 |
|
| 390 |
// register event for updating the task area status |
| 391 |
document.addEventListener('<?php echo $events['update_areas']; ?>', (e) => { |
| 392 |
let all_btns = jQuery('.vbo-context-menu-tm-areas').vboContextMenu('buttons'); |
| 393 |
all_btns.forEach((btn) => { |
| 394 |
if (typeof btn.areaId === 'undefined') { |
| 395 |
return; |
| 396 |
} |
| 397 |
if (!document.querySelector('.vbo-tm-area-wrap[data-area-id="' + btn.areaId + '"]')) { |
| 398 |
// turn off this area active state |
| 399 |
btn.activeState = false; |
| 400 |
} |
| 401 |
}); |
| 402 |
}); |
| 403 |
|
| 404 |
// register event for creating a new task area |
| 405 |
document.addEventListener('<?php echo $events['create_areas']; ?>', (e) => { |
| 406 |
createNewTaskManagerArea(); |
| 407 |
}); |
| 408 |
|
| 409 |
}); |
| 410 |
</script> |