default_fields.php
| 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', '.hasTooltip'); |
| 15 | |
| 16 | $vik = VAPApplication::getInstance(); |
| 17 | |
| 18 | // get user custom fields |
| 19 | if ($this->user) |
| 20 | { |
| 21 | $fields = $this->user->fields; |
| 22 | } |
| 23 | else |
| 24 | { |
| 25 | $fields = []; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Checks whether there's at least an editable custom field. |
| 30 | * Otherwise unset the array to avoid display the custom fields form. |
| 31 | * |
| 32 | * @since 1.7.2 |
| 33 | */ |
| 34 | if (VAPCustomFieldsRenderer::hasEditableCustomFields($this->customFields, $fields) === false) |
| 35 | { |
| 36 | // display hidden fields and immediately return |
| 37 | echo VAPCustomFieldsRenderer::display($this->customFields, $fields); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | ?> |
| 42 | |
| 43 | <div class="vapcompleteorderdiv"> |
| 44 | |
| 45 | <!-- TITLE --> |
| 46 | |
| 47 | <h3 class="vap-confirmapp-h3"><?php echo JText::translate('VAPCOMPLETEORDERHEADTITLE'); ?></h3> |
| 48 | |
| 49 | <!-- ERROR BOX --> |
| 50 | |
| 51 | <div id="vapordererrordiv" class="vapordererrordiv" style="display: none;"> </div> |
| 52 | |
| 53 | <!-- FIELDS --> |
| 54 | |
| 55 | <div class="vapcustomfields"> |
| 56 | <?php |
| 57 | /** |
| 58 | * Tries to auto-populate the fields with the details assigned |
| 59 | * to the currently logged-in user. |
| 60 | * |
| 61 | * The third boolean flag is set to instruct the method that the |
| 62 | * customers are usual to enter the first name before the last name. |
| 63 | * Use false to auto-populate the fields in the opposite way. |
| 64 | * |
| 65 | * @since 1.7 |
| 66 | */ |
| 67 | VikAppointments::populateFields($this->customFields, $fields, $firstNameComesFirst = true); |
| 68 | |
| 69 | /** |
| 70 | * Render the custom fields form by using the apposite helper. |
| 71 | * |
| 72 | * Looking for a way to override the custom fields? Take a look |
| 73 | * at "/layouts/form/fields/" folder, which should contain all |
| 74 | * the supported types of custom fields. |
| 75 | * |
| 76 | * @since 1.7 |
| 77 | */ |
| 78 | echo VAPCustomFieldsRenderer::display($this->customFields, $fields); |
| 79 | |
| 80 | /** |
| 81 | * Trigger event to retrieve an optional field that could be used |
| 82 | * to confirm the subscription to a mailing list. |
| 83 | * |
| 84 | * @param object $user The user details. |
| 85 | * |
| 86 | * @return string The HTML to display. |
| 87 | * |
| 88 | * @since 1.6.3 |
| 89 | */ |
| 90 | $html = VAPFactory::getEventDispatcher()->triggerOnce('onDisplayMailingSubscriptionInput', array($this->user)); |
| 91 | |
| 92 | // display field if provided |
| 93 | if ($html) |
| 94 | { |
| 95 | ?> |
| 96 | <div> |
| 97 | <div class="cf-value"><?php echo $html; ?></div> |
| 98 | </div> |
| 99 | <?php |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Only in case of guest users, try to display the |
| 104 | * ReCAPTCHA validation form. |
| 105 | * |
| 106 | * @since 1.7 |
| 107 | */ |
| 108 | $is_captcha = !$this->user && $vik->isGlobalCaptcha(); |
| 109 | |
| 110 | if ($is_captcha) |
| 111 | { |
| 112 | ?> |
| 113 | <div> |
| 114 | <div class="cf-value"><?php echo $vik->reCaptcha(); ?></div> |
| 115 | </div> |
| 116 | <?php |
| 117 | } |
| 118 | ?> |
| 119 | </div> |
| 120 | |
| 121 | </div> |
| 122 | |
| 123 | <script> |
| 124 | |
| 125 | /** |
| 126 | * Flag used to check whether the ZIP of the user has |
| 127 | * been proprly validated. The flag is automatically |
| 128 | * validated in case there is no ZIP field. |
| 129 | * |
| 130 | * @var boolean |
| 131 | */ |
| 132 | var ZIP_VALIDATED = <?php echo $this->zipFieldID ? 'false' : 'true'; ?>; |
| 133 | |
| 134 | jQuery(function($) { |
| 135 | // render dropdown elements with Select2 jQuery plugin |
| 136 | $('.vapcustomfields .cf-value select').each(function() { |
| 137 | let option = $(this).find('option').first(); |
| 138 | |
| 139 | let data = { |
| 140 | // hide search bar in case the number of options is lower than 10 |
| 141 | minimumResultsForSearch: $(this).find('option').length >= 10 ? 1 : -1, |
| 142 | // allow clear selection in case the value of the first option is empty |
| 143 | allowClear: option.val() ? false : true, |
| 144 | // take the whole space |
| 145 | width: '100%', |
| 146 | }; |
| 147 | |
| 148 | if (data.allowClear && !$(this).prop('multiple')) { |
| 149 | // set placeholder by using the option text |
| 150 | data.placeholder = option.text(); |
| 151 | |
| 152 | // unset the text from the option for a correct rendering |
| 153 | option.text(''); |
| 154 | } |
| 155 | |
| 156 | $(this).select2(data); |
| 157 | }); |
| 158 | |
| 159 | onInstanceReady(() => { |
| 160 | return vapCustomFieldsValidator; |
| 161 | }).then((form) => { |
| 162 | /** |
| 163 | * Overwrite getLabel method to properly access the |
| 164 | * label by using our custom layout. |
| 165 | * |
| 166 | * @param mixed input The input element. |
| 167 | * |
| 168 | * @param mixed The label of the input. |
| 169 | */ |
| 170 | form.getLabel = (input) => { |
| 171 | return $(input).closest('.cf-control').find('.cf-label *[id^="vapcf"]'); |
| 172 | } |
| 173 | }); |
| 174 | |
| 175 | if (!ZIP_VALIDATED) { |
| 176 | // auto-trigger change on load to automatically validate the existing ZIP code, if any |
| 177 | $('#vapcf<?php echo $this->zipFieldID; ?>').on('change', function() { |
| 178 | // get selected ZIP code |
| 179 | let zip = $(this).val(); |
| 180 | |
| 181 | // reset ZIP status before validation |
| 182 | ZIP_VALIDATED = false; |
| 183 | |
| 184 | if (!zip) { |
| 185 | // missing ZIP code, there's no need to interrogate the system |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | UIAjax.do( |
| 190 | '<?php echo $vik->ajaxUrl('index.php?option=com_vikappointments&task=confirmapp.checkzip' . ($this->itemid ? '&Itemid=' . $this->itemid : '')); ?>', |
| 191 | { |
| 192 | zip: zip, |
| 193 | }, |
| 194 | (resp) => { |
| 195 | // ZIP validated |
| 196 | ZIP_VALIDATED = true; |
| 197 | }, |
| 198 | (err) => { |
| 199 | if (err.responseText) { |
| 200 | alert(err.responseText); |
| 201 | // clear field value on error |
| 202 | $(this).val(''); |
| 203 | } |
| 204 | } |
| 205 | ); |
| 206 | }).trigger('change'); |
| 207 | |
| 208 | onInstanceReady(() => { |
| 209 | return vapCustomFieldsValidator; |
| 210 | }).then((form) => { |
| 211 | /** |
| 212 | * Add callback to validate the customer ZIP against the list |
| 213 | * of accepted ZIP codes. |
| 214 | * |
| 215 | * @return boolean True if valid, false otherwise. |
| 216 | */ |
| 217 | form.addCallback(() => { |
| 218 | const field = $('#vapcf<?php echo $this->zipFieldID; ?>'); |
| 219 | |
| 220 | if (!ZIP_VALIDATED) { |
| 221 | form.setInvalid(field); |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | form.unsetInvalid(field); |
| 226 | return true; |
| 227 | }); |
| 228 | }); |
| 229 | } |
| 230 | |
| 231 | <?php |
| 232 | if ($is_captcha) |
| 233 | { |
| 234 | ?> |
| 235 | onInstanceReady(() => { |
| 236 | return vapCustomFieldsValidator; |
| 237 | }).then((form) => { |
| 238 | /** |
| 239 | * Add callback to validate whether the ReCAPTCHA quiz |
| 240 | * was completed or not. |
| 241 | * |
| 242 | * @return boolean True if completed, false otherwise. |
| 243 | */ |
| 244 | form.addCallback(() => { |
| 245 | // get recaptcha elements |
| 246 | var captcha = $('#vappayform .g-recaptcha').first(); |
| 247 | var iframe = captcha.find('iframe').first(); |
| 248 | |
| 249 | // get widget ID |
| 250 | var widget_id = captcha.data('recaptcha-widget-id'); |
| 251 | |
| 252 | // check if recaptcha instance exists |
| 253 | // and whether the recaptcha was completed |
| 254 | if (typeof grecaptcha !== 'undefined' |
| 255 | && widget_id !== undefined |
| 256 | && !grecaptcha.getResponse(widget_id)) { |
| 257 | // captcha not completed |
| 258 | iframe.addClass('vapinvalid'); |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | // captcha completed |
| 263 | iframe.removeClass('vapinvalid'); |
| 264 | return true; |
| 265 | }); |
| 266 | }); |
| 267 | <?php |
| 268 | } |
| 269 | ?> |
| 270 | }); |
| 271 | |
| 272 | </script> |
| 273 |