script.php
104 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments - Libraries |
| 4 | * @subpackage html.managetos |
| 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('vaphtml.assets.toast', 'bottom-center'); |
| 15 | |
| 16 | $field = $displayData['field']; |
| 17 | |
| 18 | $vik = VAPApplication::getInstance(); |
| 19 | |
| 20 | // render inspector to manage ToS fields |
| 21 | echo JHtml::fetch( |
| 22 | 'vaphtml.inspector.render', |
| 23 | 'tos-inspector-' . $field['id'], |
| 24 | array( |
| 25 | 'title' => JText::translate('VAPMAINTITLEEDITCUSTOMF'), |
| 26 | 'closeButton' => true, |
| 27 | 'keyboard' => false, |
| 28 | 'footer' => '<button type="button" class="btn btn-success" id="tos-save-' . $field['id'] . '">' . JText::translate('JAPPLY') . '</button>', |
| 29 | 'width' => 400, |
| 30 | ), |
| 31 | JLayoutHelper::render('html.managetos.modal', $displayData) |
| 32 | ); |
| 33 | |
| 34 | JText::script('VAPCONNECTIONLOSTERROR'); |
| 35 | ?> |
| 36 | |
| 37 | <script> |
| 38 | (function($) { |
| 39 | 'use strict'; |
| 40 | |
| 41 | $(function() { |
| 42 | // get ToS table row |
| 43 | var tr = $('input[name="cid[]"][value="<?php echo (int) $field['id']; ?>"]').closest('tr'); |
| 44 | |
| 45 | // get column that contains the field name |
| 46 | var nameTD = tr.children().eq(2); |
| 47 | |
| 48 | // wrap name within a div |
| 49 | nameTD.html('<div class="td-pull-left"> ' + nameTD.html() + ' </div>'); |
| 50 | |
| 51 | // create edit button |
| 52 | var editButton = $('<a href="javascript:void(0)" class="td-pull-right no-underline"><i class="fas fa-pen-square big"></i></a>'); |
| 53 | |
| 54 | // register click event |
| 55 | editButton.on('click', function() { |
| 56 | // open inspector |
| 57 | vapOpenInspector('tos-inspector-<?php echo (int) $field['id']; ?>'); |
| 58 | }); |
| 59 | |
| 60 | // append edit button |
| 61 | nameTD.append(editButton); |
| 62 | |
| 63 | // register save event |
| 64 | $('#tos-save-<?php echo (int) $field['id']; ?>').on('click', function() { |
| 65 | // get form containing the field value |
| 66 | var form = $('form#tos-form-<?php echo (int) $field['id']; ?>'); |
| 67 | |
| 68 | // make save request |
| 69 | UIAjax.do( |
| 70 | // request end-point |
| 71 | 'admin-ajax.php?action=vikappointments&task=customf.savetosajax', |
| 72 | // serialize form |
| 73 | form.serialize(), |
| 74 | // successful response |
| 75 | (data) => { |
| 76 | if (typeof data === 'string') { |
| 77 | data = JSON.parse(data); |
| 78 | } |
| 79 | |
| 80 | // update name within table column |
| 81 | nameTD.find('.td-primary').html(data.name); |
| 82 | |
| 83 | // auto-close inspector on successful save |
| 84 | vapCloseInspector('tos-inspector-<?php echo (int) $field['id']; ?>'); |
| 85 | }, |
| 86 | // failure |
| 87 | (error) => { |
| 88 | if (!error.responseText) { |
| 89 | // use default connection lost error |
| 90 | error.responseText = Joomla.JText._('VAPCONNECTIONLOSTERROR'); |
| 91 | } |
| 92 | |
| 93 | // raise error |
| 94 | ToastMessage.enqueue({ |
| 95 | text: error.responseText, |
| 96 | status: 0, |
| 97 | }); |
| 98 | } |
| 99 | ); |
| 100 | }); |
| 101 | }); |
| 102 | })(jQuery); |
| 103 | </script> |
| 104 |