| 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 HTML tasl manager scripts helper. |
| 16 |
* |
| 17 |
* @since 1.8 |
| 18 |
*/ |
| 19 |
abstract class VBOHtmlTmscripts |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Creates a javascript variable holding all the available status codes. |
| 23 |
* |
| 24 |
* @param VBOTaskManager|null $taskManager |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
public static function statuses(?VBOTaskManager $taskManager = null) |
| 29 |
{ |
| 30 |
static $loaded = 0; |
| 31 |
|
| 32 |
if ($loaded) { |
| 33 |
// do not load again |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$loaded = 1; |
| 38 |
|
| 39 |
if (!$taskManager) { |
| 40 |
// access the task manager |
| 41 |
$taskManager = VBOFactory::getTaskManager(); |
| 42 |
} |
| 43 |
|
| 44 |
// load all the available status types |
| 45 |
$statusTypes = []; |
| 46 |
|
| 47 |
foreach ($taskManager->getStatusGroupElements() as $groupId => $group) { |
| 48 |
// push group button |
| 49 |
$statusTypes[] = [ |
| 50 |
'id' => null, |
| 51 |
'text' => $group['text'], |
| 52 |
]; |
| 53 |
|
| 54 |
// iterate over the statuses of this group |
| 55 |
foreach ($group['elements'] as $statusType) { |
| 56 |
// push status button |
| 57 |
$statusTypes[] = [ |
| 58 |
'id' => $statusType['id'], |
| 59 |
'text' => $statusType['text'], |
| 60 |
'color' => $statusType['color'], |
| 61 |
]; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** @var object[] TASK_MANAGER_STATUSES */ |
| 66 |
JFactory::getDocument()->addScriptDeclaration('window.TASK_MANAGER_STATUSES = ' . json_encode($statusTypes) . ';'); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Script used to handle the status change event. |
| 71 |
* |
| 72 |
* @param string $selector The selector to use for auto-init. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public static function changestatus(string $selector = '.change-status-trigger') |
| 77 |
{ |
| 78 |
static $loaded = 0; |
| 79 |
|
| 80 |
if ($loaded) { |
| 81 |
// do not load again |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$loaded = 1; |
| 86 |
|
| 87 |
// make the status codes accessible |
| 88 |
static::statuses(); |
| 89 |
|
| 90 |
$ajaxUrl = VikBooking::ajaxUrl('index.php?option=com_vikbooking&task=taskmanager.updateTask'); |
| 91 |
|
| 92 |
JFactory::getDocument()->addScriptDeclaration( |
| 93 |
<<<JAVASCRIPT |
| 94 |
(function($, w) { |
| 95 |
'use strict'; |
| 96 |
|
| 97 |
$(function() { |
| 98 |
// automatically initialize the context menu for the status changes every time the contents are loaded |
| 99 |
document.addEventListener('vbo-tm-contents-loaded', (event) => { |
| 100 |
// take only the statuses inside the element actually updated |
| 101 |
w.setupStatusHandler($(event.detail.element).find('{$selector}'), event.detail?.statuses); |
| 102 |
}); |
| 103 |
}); |
| 104 |
|
| 105 |
/** |
| 106 |
* Set up the context menu used to change status for a given task. |
| 107 |
*/ |
| 108 |
w.setupStatusHandler = (selector, areaStatuses) => { |
| 109 |
if (!w.TASK_MANAGER_STATUSES) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
// obtain only the statuses that are actually supported |
| 114 |
const iterableStatuses = w.TASK_MANAGER_STATUSES.filter((s) => { |
| 115 |
return typeof areaStatuses === 'undefined' || s.id == null || areaStatuses.length === 0 || areaStatuses.indexOf(s.id) != -1; |
| 116 |
}); |
| 117 |
|
| 118 |
// build buttons |
| 119 |
let statusButtons = []; |
| 120 |
|
| 121 |
iterableStatuses.forEach((elem, index) => { |
| 122 |
// build button icon element |
| 123 |
let btnIconEl = $('<span></span>') |
| 124 |
.addClass('vbo-colortag-circle') |
| 125 |
.addClass('vbo-tm-colortag-circle') |
| 126 |
.addClass('vbo-tm-statustype-circle'); |
| 127 |
|
| 128 |
if (elem?.color) { |
| 129 |
btnIconEl.addClass(elem.color); |
| 130 |
} |
| 131 |
|
| 132 |
// push status button |
| 133 |
statusButtons.push({ |
| 134 |
statusId: elem.id, |
| 135 |
color: elem.color, |
| 136 |
class: elem.id ? 'vbo-context-menu-entry-secondary' : 'btngroup', |
| 137 |
searchable: elem.id ? true : false, |
| 138 |
text: elem.text, |
| 139 |
icon: elem.id ? btnIconEl : null, |
| 140 |
disabled: () => { |
| 141 |
return !elem.id || (vboTmFilters && vboTmFilters?.statusId == elem.id); |
| 142 |
}, |
| 143 |
visible: (root) => { |
| 144 |
return $(root).attr('data-status') != elem.id; |
| 145 |
}, |
| 146 |
action: function(root, event) { |
| 147 |
const taskId = parseInt($(root).closest('[data-task-id]').attr('data-task-id')); |
| 148 |
|
| 149 |
// make the request |
| 150 |
VBOCore.doAjax( |
| 151 |
"{$ajaxUrl}", |
| 152 |
{ |
| 153 |
data: { |
| 154 |
id: taskId, |
| 155 |
status_enum: this.statusId, |
| 156 |
} |
| 157 |
}, |
| 158 |
(resp) => { |
| 159 |
$(root).removeClass($(root).attr('data-color')) |
| 160 |
.addClass(this.color) |
| 161 |
.attr('data-status', this.statusId) |
| 162 |
.attr('data-color', this.color) |
| 163 |
.text(this.text); |
| 164 |
|
| 165 |
if (typeof VBOCore !== 'undefined') { |
| 166 |
VBOCore.emitEvent('vbo-task-status-changed', { |
| 167 |
task: { |
| 168 |
id: taskId, |
| 169 |
status: this.statusId, |
| 170 |
} |
| 171 |
}); |
| 172 |
} |
| 173 |
}, |
| 174 |
(error) => { |
| 175 |
// display error message |
| 176 |
alert(error.responseText); |
| 177 |
} |
| 178 |
); |
| 179 |
}, |
| 180 |
}); |
| 181 |
}); |
| 182 |
|
| 183 |
// start context menu on the proper button element |
| 184 |
$(selector).not('.initialized').vboContextMenu({ |
| 185 |
placement: 'bottom-left', |
| 186 |
buttons: statusButtons, |
| 187 |
search: true, |
| 188 |
}).addClass('initialized'); |
| 189 |
} |
| 190 |
})(jQuery, window); |
| 191 |
JAVASCRIPT |
| 192 |
); |
| 193 |
} |
| 194 |
} |
| 195 |
|