PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / helpers / html / scripts.php
vikappointments / admin / helpers / html Last commit date
admin.php 4 days ago index.html 4 days ago inspector.php 4 days ago mediamanager.php 4 days ago scripts.php 4 days ago
scripts.php
293 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 /**
15 * VikAppointments HTML admin scripts helper.
16 *
17 * @since 1.7
18 */
19 abstract class VAPHtmlScripts
20 {
21 /**
22 * Registers a script that will be used to handle the dismiss
23 * button of the alerts. If dismissed, they won't appear anymore
24 * until the expiration date.
25 *
26 * @param string $selector The alert HTML selector.
27 *
28 * @return void
29 */
30 public static function cookiealert($selector = '.alert')
31 {
32 JFactory::getDocument()->addScriptDeclaration(
33 <<<JS
34 jQuery(function() {
35
36 jQuery('{$selector}').find('button[data-signature]').on('click', function() {
37 var cookie = [];
38 cookie.push('alert_dismiss_' + jQuery(this).data('signature') + '=1');
39
40 if (jQuery(this).data('expdate')) {
41 var date = new Date(jQuery(this).data('expdate'));
42 cookie.push('expires=' + date.toUTCString());
43 }
44
45 cookie.push('path=/');
46
47 document.cookie = cookie.join('; ');
48 });
49
50 });
51 JS
52 );
53 }
54
55 /**
56 * Returns the script used to store the selected view tab
57 * within the user state as a cookie.
58 *
59 * @param string $tab The tab group name.
60 * @param string $key The cookie name.
61 * @param integer $days The number of days for which the cookie should exist.
62 *
63 * @return string The script (without <script> delimiters).
64 */
65 public static function tabhandler($tab, $key, $days = null)
66 {
67 $days = (int) $days;
68
69 $is_j4 = (int) VersionListener::isJoomla4x();
70
71 return
72 <<<JS
73 (function($) {
74 'use strict';
75
76 $(function() {
77 if (document.location.hash) {
78 // Hash set, try to auto-access the given tab.
79 // Trigger before registering the click event because
80 // we don't want to cookie a tab selected via URL.
81 if ({$is_j4}) {
82 $('div[role="tablist"] button[role="tab"][aria-controls="' + document.location.hash + '"]').trigger('click');
83 } else {
84 $('a[href="' + document.location.hash + '"]').trigger('click');
85 }
86 }
87
88 let selector = 'a[href^="#{$tab}_"]';
89
90 if ({$is_j4}) {
91 selector = 'div[role="tablist"] button[role="tab"][aria-controls^="{$tab}"]';
92 }
93
94 $(selector).on('click', function() {
95 let href;
96
97 if ({$is_j4}) {
98 href = $(this).attr('aria-controls');
99 } else {
100 href = $(this).attr('href').substr(1);
101 }
102
103 if ({$days} > 0) {
104 var date = new Date();
105 date.setDate(date.getDate() + {$days});
106
107 document.cookie = '{$key}=' + href + '; expires=' + date.toUTCString() + '; path=/';
108 } else {
109 // keep only for current session
110 document.cookie = '{$key}=' + href + '; path=/';
111 }
112 });
113 });
114 })(jQuery);
115 JS
116 ;
117 }
118
119 /**
120 * Renders the flag image within the options of the dropdowns
121 * matching the specified selector.
122 *
123 * @param mixed $options An array of options. In case of a scalar
124 * value, it will be considered as the width.
125 *
126 * @return void
127 */
128 public static function selectflags($selector = '.vap-flag-sel', $options = array())
129 {
130 // make sure select2 is loaded
131 JHtml::fetch('vaphtml.assets.select2');
132
133 $uri = VAPASSETS_URI . 'css/flags/';
134
135 if (is_scalar($options))
136 {
137 // B.C. create an empty configuration with the given width
138 $options = array('width' => $options);
139 }
140
141 // create default configuration
142 $default = array(
143 'width' => 200,
144 'allowClear' => false,
145 'minimumResultsForSearch' => -1,
146 );
147
148 // merge given options with default ones
149 $options = array_merge($default, $options);
150
151 if ($options['allowClear'] && !isset($options['placeholder']))
152 {
153 // use default placeholder
154 $options['placeholder'] = '--';
155 }
156
157 // prepare options for JS usage
158 $options = json_encode($options);
159
160 JFactory::getDocument()->addScriptDeclaration(
161 <<<JS
162 jQuery(function($) {
163 // create dropdown configuration
164 const options = Object.assign({$options}, {
165 formatResult: vapFormatFlags,
166 formatSelection: vapFormatFlags,
167 escapeMarkup: function(m) { return m; },
168 });
169
170 $('{$selector}').select2(options);
171
172 function vapFormatFlags(opt) {
173 if (!opt.id) {
174 // optgroup
175 return opt.text;
176 }
177
178 var tag = opt.id;
179
180 if (opt.id.match(/^[a-z]{2,3}-[a-z]{2,2}$/i)) {
181 // we have a langtag
182 tag = tag.split('-').pop();
183 }
184
185 tag = tag.toLowerCase();
186
187 switch (tag) {
188 case 'el':
189 tag = 'gr';
190 break;
191 }
192
193 return '<img class="vap-opt-flag" src="{$uri}' + tag + '.png" />' + opt.text;
194 }
195 });
196 JS
197 );
198 }
199
200 /**
201 * Method to make the specified table listable.
202 *
203 * @param string $tableId DOM id of the table.
204 * @param string $formId DOM id of the form.
205 * @param string $sortDir Sort direction.
206 * @param string $saveUrl Save ordering url, ajax-load after an item is dropped.
207 * @param mixed $filters A list of filters to use when rearranging the records.
208 *
209 * @return void
210 */
211 public static function sortablelist($tableId, $formId = 'adminForm', $sortDir = 'asc', $saveOrderingUrl = null, $filters = array())
212 {
213 $vik = VAPApplication::getInstance();
214
215 // load sortable list script
216 $vik->addScript(VAPASSETS_URI . 'js/sortablelist.js');
217
218 // create JSON data
219 $data = array(
220 'form' => '#' . $formId,
221 'direction' => strtolower($sortDir),
222 'saveUrl' => $vik->ajaxUrl($saveOrderingUrl),
223 'inputSelector' => 'input[name="order[]"]',
224 );
225
226 if ($filters)
227 {
228 // inject filters if specified
229 $data['filters'] = $filters;
230 }
231
232 $data = json_encode($data);
233
234 JFactory::getDocument()->addScriptDeclaration(
235 <<<JS
236 jQuery(function($) {
237 $('#{$tableId}').viksortablelist({$data});
238 });
239 JS
240 );
241 }
242
243 /**
244 * Replaces the default behavior.modal function provided by Joomla,
245 * by supporting our Fancybox jQuery plugin.
246 *
247 * @return void
248 */
249 public static function modal($selector = 'a.modal', $params = array())
250 {
251 static $loaded = 0;
252
253 if ($loaded)
254 {
255 return;
256 }
257
258 $loaded = 1;
259
260 // load fancybox scripts
261 JHtml::fetch('vaphtml.assets.fancybox');
262
263 // add script to support modal
264 JFactory::getDocument()->addScriptDeclaration(
265 <<<JS
266 jQuery(function($) {
267 $('{$selector}').on('click', function(e) {
268 // get link HREF
269 var href = $(this).attr('href');
270
271 // prevent default link action
272 e.preventDefault();
273
274 // extract href from link
275 var href = $(this).attr('href');
276
277 // check if we have an image
278 if (href.match(/\.(png|jpe?g|gif|bmp)$/i)) {
279 // open fancybox containing image preview
280 vapOpenModalImage(href);
281 } else {
282 // otherwise fallback to default browser opening
283 vapOpenPopup(href);
284 }
285
286 return false;
287 }).removeClass('modal').removeAttr('target');
288 });
289 JS
290 );
291 }
292 }
293