default.php
349 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 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 | JHtml::fetch('bootstrap.tooltip'); |
| 15 | JHtml::fetch('vaphtml.assets.fontawesome'); |
| 16 | JHtml::fetch('vaphtml.assets.select2'); |
| 17 | |
| 18 | $vik = VAPApplication::getInstance(); |
| 19 | |
| 20 | $recurrence_params = VikAppointments::getRecurrenceParams(); |
| 21 | |
| 22 | $title = JText::sprintf( |
| 23 | 'VAPSAYRESERVATIONDETAILS', |
| 24 | $this->order->service->name, |
| 25 | $this->order->employee->name, |
| 26 | $this->order->checkin->lc |
| 27 | ); |
| 28 | |
| 29 | ?> |
| 30 | |
| 31 | <form action="index.php" name="adminForm" id="adminForm"> |
| 32 | |
| 33 | <?php echo $vik->openCard(); ?> |
| 34 | |
| 35 | <div class="span12"> |
| 36 | <?php echo $vik->openFieldset($title); ?> |
| 37 | |
| 38 | <div class="vap-makerec-box"> |
| 39 | <div class="vap-recurrence-form"> |
| 40 | <span class="lbl"><?php echo JText::translate('VAPMANAGECONFIGREC2'); ?></span> |
| 41 | |
| 42 | <span> |
| 43 | <?php |
| 44 | $options = array( |
| 45 | JHtml::fetch('select.option', 1, JText::translate('VAPMANAGECONFIGRECSINGOPT1')), |
| 46 | JHtml::fetch('select.option', 2, JText::translate('VAPMANAGECONFIGRECSINGOPT2')), |
| 47 | JHtml::fetch('select.option', 4, JText::translate('VAPMANAGECONFIGRECSINGOPT4')), |
| 48 | JHtml::fetch('select.option', 3, JText::translate('VAPMANAGECONFIGRECSINGOPT3')), |
| 49 | JHtml::fetch('select.option', 5, JText::translate('VAPMANAGECONFIGRECSINGOPT5')), |
| 50 | JHtml::fetch('select.option', 6, JText::translate('VAPMANAGECONFIGRECSINGOPT6')), |
| 51 | JHtml::fetch('select.option', 7, JText::translate('VAPMANAGECONFIGRECSINGOPT7')), |
| 52 | JHtml::fetch('select.option', 8, JText::translate('VAPMANAGECONFIGRECSINGOPT8')), |
| 53 | ); |
| 54 | ?> |
| 55 | <select id="vaprepeatbyrecsel"> |
| 56 | <?php echo JHtml::fetch('select.options', $options); ?> |
| 57 | </select> |
| 58 | </span> |
| 59 | |
| 60 | |
| 61 | |
| 62 | <span class="lbl"><?php echo JText::translate('VAPMANAGECONFIGREC5'); ?></span> |
| 63 | |
| 64 | <span> |
| 65 | <?php |
| 66 | $options = array(); |
| 67 | for ($i = $recurrence_params['min']; $i <= $recurrence_params['max']; $i++) |
| 68 | { |
| 69 | $options[] = JHtml::fetch('select.option', $i, $i); |
| 70 | } |
| 71 | ?> |
| 72 | <select id="vapamountrecsel"> |
| 73 | <?php echo JHtml::fetch('select.options', $options); ?> |
| 74 | </select> |
| 75 | </span> |
| 76 | |
| 77 | <span> |
| 78 | <?php |
| 79 | $options = array(); |
| 80 | for ($i = 0; $i < count($recurrence_params['for']); $i++) |
| 81 | { |
| 82 | $options[] = JHtml::fetch('select.option', $i + 1, JText::translate('VAPMANAGECONFIGRECMULTOPT' . ($i + 1))); |
| 83 | } |
| 84 | ?> |
| 85 | <select id="vapfornextrecsel"> |
| 86 | <?php echo JHtml::fetch('select.options', $options); ?> |
| 87 | </select> |
| 88 | </span> |
| 89 | </div> |
| 90 | |
| 91 | <div class="vap-recurrence-button"> |
| 92 | <button type="button" class="btn" id="launch-recurrence-btn"> |
| 93 | <?php echo JText::translate('VAPMAKERECGETPREVIEW'); ?> |
| 94 | </button> |
| 95 | </div> |
| 96 | |
| 97 | </div> |
| 98 | |
| 99 | <div class="vap-recpreview-box" id="vap-recpreview-box" style="display: none;"> |
| 100 | <div class="vap-recpreview-container" id="vap-recpreview-container"> |
| 101 | |
| 102 | </div> |
| 103 | <div class="vap-recpreview-button"> |
| 104 | <button type="button" class="btn" id="vap-mkrec-btn"> |
| 105 | <?php echo JText::translate('VAPMAKERECLAUNCHPROC'); ?> |
| 106 | </button> |
| 107 | </div> |
| 108 | </div> |
| 109 | |
| 110 | <div class="vap-confirmrec-box" id="vap-confirmrec-box" style="display: none;"> |
| 111 | |
| 112 | </div> |
| 113 | |
| 114 | <div class="vap-recerror-box" id="vap-recerror-box" style="display: none;"> |
| 115 | |
| 116 | </div> |
| 117 | |
| 118 | <?php echo $vik->closeFieldset(); ?> |
| 119 | </div> |
| 120 | |
| 121 | <?php echo $vik->closeCard(); ?> |
| 122 | |
| 123 | <?php echo JHtml::fetch('form.token'); ?> |
| 124 | |
| 125 | <input type="hidden" name="option" value="com_vikappointments" /> |
| 126 | <input type="hidden" name="task" value="" /> |
| 127 | <input type="hidden" name="id" value="<?php echo $this->order->id; ?>" /> |
| 128 | </form> |
| 129 | |
| 130 | <?php |
| 131 | JText::script('VAPCONNECTIONLOSTERROR'); |
| 132 | JText::script('VAPMAKERECEMPHINT'); |
| 133 | JText::script('VAPMAKERECTIMEHINT'); |
| 134 | JText::script('VAPFILTERSELECTEMPLOYEE'); |
| 135 | JText::script('VAPFILTERSELECTTIME'); |
| 136 | ?> |
| 137 | |
| 138 | <script> |
| 139 | |
| 140 | jQuery(function($) { |
| 141 | $('#vaprepeatbyrecsel, #vapfornextrecsel').select2({ |
| 142 | minimumResultsForSearch: -1, |
| 143 | allowClear: false, |
| 144 | width: 120, |
| 145 | }); |
| 146 | |
| 147 | $('#vapamountrecsel').select2({ |
| 148 | minimumResultsForSearch: -1, |
| 149 | allowClear: false, |
| 150 | width: 75, |
| 151 | }); |
| 152 | |
| 153 | $('#vaprepeatbyrecsel').on('change', function() { |
| 154 | var repeatby = parseInt($(this).val()); |
| 155 | |
| 156 | if (repeatby == 4) { |
| 157 | // use week for "fortnight" |
| 158 | repeatby = 2; |
| 159 | } else if (repeatby == 5) { |
| 160 | // use month for "bi-month" |
| 161 | repeatby = 3; |
| 162 | } |
| 163 | |
| 164 | // make sure the "for next" dropdown supports the value set |
| 165 | // within the "repeat by" select |
| 166 | if ($('#vapfornextrecsel option[value="' + repeatby + '"]').length) { |
| 167 | $('#vapfornextrecsel').select2('val', repeatby); |
| 168 | } |
| 169 | }); |
| 170 | |
| 171 | $('#launch-recurrence-btn').on('click', function() { |
| 172 | // disable buttons to avoid double requests |
| 173 | $(this).prop('disabled', true); |
| 174 | $('#vap-mkrec-btn').prop('disabled', true); |
| 175 | |
| 176 | $('#vap-recpreview-box').hide(); |
| 177 | $('#vap-recpreview-container').html(''); |
| 178 | $('#vap-recerror-box').hide(); |
| 179 | $('#vap-confirmrec-box').hide(); |
| 180 | |
| 181 | UIAjax.do( |
| 182 | '<?php echo $vik->ajaxUrl('index.php?option=com_vikappointments&task=makerecurrence.preview'); ?>', |
| 183 | { |
| 184 | id: <?php echo (int) $this->order->id; ?>, |
| 185 | by: $('#vaprepeatbyrecsel').val(), |
| 186 | amount: $('#vapamountrecsel').val(), |
| 187 | for: $('#vapfornextrecsel').val(), |
| 188 | }, |
| 189 | (resp) => { |
| 190 | let at_least_one = false; |
| 191 | |
| 192 | resp.forEach((v) => { |
| 193 | const box = $('<div class="recurrence-date"></div>'); |
| 194 | |
| 195 | // append date format |
| 196 | box.append($('<span class="format"></span>').html(v.format)); |
| 197 | |
| 198 | const msg = $('<span class="msg"></span>') |
| 199 | .html(v.message) |
| 200 | .addClass((v.available ? 'available' : 'occupied')); |
| 201 | |
| 202 | if (!v.available && v.reason) { |
| 203 | msg.append( |
| 204 | $('<i class="fas fa-exclamation-circle"></i>') |
| 205 | .attr('title', v.reason) |
| 206 | .tooltip() |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | // append availability message |
| 211 | box.append(msg); |
| 212 | |
| 213 | if (!v.available && v.employees) { |
| 214 | // the appointment is available with a different employee |
| 215 | const empHint = $('<div class="hint-box"></div>').append( |
| 216 | $('<span class="hint-text"></span>').html(Joomla.JText._('VAPMAKERECEMPHINT')) |
| 217 | ); |
| 218 | |
| 219 | // create employee selection dropdown |
| 220 | const empSelect = $('<select name="hint[' + v.date + ']"></select>') |
| 221 | .append('<option></option>'); |
| 222 | |
| 223 | for (let k in v.employees) { |
| 224 | if (v.employees.hasOwnProperty(k)) { |
| 225 | empSelect.append( |
| 226 | $('<option></option>').val(k).html(v.employees[k]) |
| 227 | ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | empHint.prepend(empSelect); |
| 232 | |
| 233 | box.append(empHint); |
| 234 | |
| 235 | empSelect.select2({ |
| 236 | placeholder: Joomla.JText._('VAPFILTERSELECTEMPLOYEE'), |
| 237 | allowClear: true, |
| 238 | width: 190, |
| 239 | }); |
| 240 | } |
| 241 | |
| 242 | if (!v.available && v.times) { |
| 243 | // the appointment is available at a different time |
| 244 | const timeHint = $('<div class="hint-box"></div>').append( |
| 245 | $('<span class="hint-text"></span>').html(Joomla.JText._('VAPMAKERECTIMEHINT')) |
| 246 | ); |
| 247 | |
| 248 | // create time selection dropdown |
| 249 | const timeSelect = $('<select name="hint[' + v.date + ']"></select>') |
| 250 | .append('<option></option>'); |
| 251 | |
| 252 | for (let k in v.times) { |
| 253 | if (v.times.hasOwnProperty(k)) { |
| 254 | timeSelect.append( |
| 255 | $('<option></option>').val(k).html(v.times[k]) |
| 256 | ); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | timeHint.prepend(timeSelect); |
| 261 | |
| 262 | box.append(timeHint); |
| 263 | |
| 264 | timeSelect.select2({ |
| 265 | placeholder: Joomla.JText._('VAPFILTERSELECTTIME'), |
| 266 | allowClear: true, |
| 267 | width: 190, |
| 268 | }); |
| 269 | } |
| 270 | |
| 271 | $('#vap-recpreview-container').append(box); |
| 272 | |
| 273 | at_least_one = at_least_one || v.available; |
| 274 | }); |
| 275 | |
| 276 | $(this).prop('disabled', false); |
| 277 | $('#vap-mkrec-btn').prop('disabled', !at_least_one); |
| 278 | |
| 279 | $('#vap-recpreview-box').slideDown(); |
| 280 | }, |
| 281 | (error) => { |
| 282 | $('#vap-recerror-box').html(error.responseText || Joomla.JText._('VAPCONNECTIONLOSTERROR')); |
| 283 | $('#vap-recerror-box').slideDown(); |
| 284 | |
| 285 | $(this).prop('disabled', false); |
| 286 | } |
| 287 | ); |
| 288 | }); |
| 289 | |
| 290 | $('#vap-mkrec-btn').on('click', function() { |
| 291 | // disable button to avoid double requests |
| 292 | $(this).prop('disabled', true); |
| 293 | |
| 294 | $('#vap-confirmrec-box').hide(); |
| 295 | $('#vap-recerror-box').hide(); |
| 296 | |
| 297 | let hints = {}; |
| 298 | |
| 299 | // iterate all non-disabled hint dropdown |
| 300 | $('select[name^="hint["]').not('[disabled]').each(function() { |
| 301 | // get selected hint |
| 302 | let val = $(this).val(); |
| 303 | // get related date |
| 304 | let key = $(this).attr('name').match(/^hint\[([0-9:\-\s]+)\]$/); |
| 305 | |
| 306 | if (val && key) { |
| 307 | // register hint |
| 308 | hints[key.pop()] = val; |
| 309 | } |
| 310 | }); |
| 311 | |
| 312 | UIAjax.do( |
| 313 | '<?php echo $vik->ajaxUrl('index.php?option=com_vikappointments&task=makerecurrence.create'); ?>', |
| 314 | { |
| 315 | id: <?php echo (int) $this->order->id; ?>, |
| 316 | by: $('#vaprepeatbyrecsel').val(), |
| 317 | amount: $('#vapamountrecsel').val(), |
| 318 | for: $('#vapfornextrecsel').val(), |
| 319 | hints: hints, |
| 320 | }, |
| 321 | (resp) => { |
| 322 | $('#vap-confirmrec-box').html(resp.message); |
| 323 | $('#vap-confirmrec-box').slideDown(); |
| 324 | |
| 325 | $(this).prop('disabled', false); |
| 326 | }, |
| 327 | (error) => { |
| 328 | $('#vap-recerror-box').html(error.responseText || Joomla.JText._('VAPCONNECTIONLOSTERROR')); |
| 329 | $('#vap-recerror-box').slideDown(); |
| 330 | |
| 331 | $(this).prop('disabled', false); |
| 332 | } |
| 333 | ); |
| 334 | }); |
| 335 | |
| 336 | $(document).on('change', '[name^="hint["]', function() { |
| 337 | // get any other select matching the same name of the changed one |
| 338 | let other = $('select[name="' + $(this).attr('name') + '"]').not(this); |
| 339 | // unset value from other select and disabled them |
| 340 | other.select2('val', null); |
| 341 | other.prop('disabled', $(this).val()); |
| 342 | |
| 343 | // enable button used to creare the recurrence |
| 344 | $('#vap-mkrec-btn').prop('disabled', false); |
| 345 | }); |
| 346 | }); |
| 347 | |
| 348 | </script> |
| 349 |