PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / customizer.php
vikappointments / admin / models Last commit date
apiban.php 4 years ago apilog.php 2 years ago apiplugin.php 4 years ago apiuser.php 2 years ago apiuseroptions.php 2 years ago backup.php 4 months ago caldays.php 1 month ago calendar.php 1 month ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 4 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 4 months ago conversion.php 4 years ago country.php 2 years ago coupon.php 2 years ago couponemployee.php 4 years ago coupongroup.php 2 years ago couponservice.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 4 months ago customf.php 2 years ago customfservice.php 4 years ago customizer.php 4 years ago empgroup.php 2 years ago employee.php 2 years ago empsettings.php 4 years ago file.php 4 years ago findreservation.php 1 month ago group.php 2 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langempgroup.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 2 years ago langoptiongroup.php 4 years ago langoptionvar.php 4 years ago langpackage.php 4 years ago langpackgroup.php 4 years ago langpayment.php 4 years ago langservice.php 4 years ago langstatuscode.php 4 years ago langsubscr.php 4 years ago langtax.php 2 years ago langtaxrule.php 4 years ago location.php 2 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 2 years ago multiorder.php 1 month ago option.php 1 year ago optiongroup.php 2 years ago optionvar.php 1 year ago orderstatus.php 2 years ago package.php 2 years ago packageservice.php 4 years ago packgroup.php 2 years ago packorder.php 2 years ago packorderitem.php 1 month ago payment.php 2 years ago rate.php 2 years ago reportsemp.php 4 months ago reportsser.php 4 months ago reservation.php 1 month ago resoptassoc.php 2 years ago restriction.php 2 years ago review.php 3 years ago serempassoc.php 1 year ago seroptassoc.php 4 years ago serrateassoc.php 4 years ago serrestrassoc.php 4 years ago service.php 2 years ago state.php 2 years ago statswidget.php 2 years ago statuscode.php 2 years ago subscription.php 1 month ago subscrorder.php 1 month ago tag.php 4 years ago tax.php 2 years ago taxrule.php 2 years ago updateprogram.php 4 years ago usernote.php 2 years ago waitinglist.php 1 month ago webhook.php 1 year ago worktime.php 1 month ago
customizer.php
377 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 VAPLoader::import('libraries.mvc.model');
15
16 /**
17 * VikAppointments customizer model.
18 *
19 * @since 1.7.2
20 */
21 class VikAppointmentsModelCustomizer extends JModelVAP
22 {
23 /**
24 * Returns the path in which the default environment CSS file is stored.
25 *
26 * @return string
27 */
28 public function getDefaultPath()
29 {
30 return JPath::clean(VAPBASE . '/assets/css/environment.css');
31 }
32
33 /**
34 * Returns the URL in which the default environment CSS file is stored.
35 *
36 * @return string
37 */
38 public function getDefaultUrl()
39 {
40 return VAPASSETS_URI . 'css/environment.css';
41 }
42
43 /**
44 * Returns the path in which the custom environment CSS file is stored.
45 *
46 * @param string $pk The name of the file to use ("environment" by default).
47 *
48 * @return string
49 */
50 public function getCustomPath($pk = null)
51 {
52 if (!$pk)
53 {
54 $pk = 'environment';
55 }
56 else
57 {
58 $pk = preg_replace("/\.css$/i", '', $pk);
59 }
60
61 return JPath::clean(VAP_CSS_CUSTOMIZER . '/' . $pk . '.css');
62 }
63
64 /**
65 * Returns the URL in which the custom environment CSS file is stored.
66 *
67 * @param string $pk The name of the file to use ("environment" by default).
68 *
69 * @return string
70 */
71 public function getCustomUrl($pk = null)
72 {
73 if (!$pk)
74 {
75 $pk = 'environment';
76 }
77 else
78 {
79 $pk = preg_replace("/\.css$/i", '', $pk);
80 }
81
82 return VAP_CSS_CUSTOMIZER_URI . $pk . '.css';
83 }
84
85 /**
86 * Returns the path of the environment file to load.
87 *
88 * @param string $pk The name of the file to use ("environment" by default).
89 *
90 * @return string
91 */
92 public function getEnvironmentFile($pk = null)
93 {
94 // get custom path first
95 $path = $this->getCustomPath($pk);
96
97 // make sure the custom file exists
98 if (JFile::exists($path))
99 {
100 return $path;
101 }
102
103 // nope, return the default one
104 return $this->getDefaultPath();
105 }
106
107 /**
108 * Returns the URL of the environment file to load.
109 *
110 * @param string $pk The name of the file to use ("environment" by default).
111 *
112 * @return string
113 */
114 public function getEnvironmentUrl($pk = null)
115 {
116 // get custom path first
117 $path = $this->getCustomPath($pk);
118
119 // make sure the custom file exists
120 if (JFile::exists($path))
121 {
122 return $this->getCustomUrl($pk);
123 }
124
125 // nope, return the default one
126 return $this->getDefaultUrl();
127 }
128
129 /**
130 * Basic item loading implementation.
131 *
132 * @param mixed $pk An optional primary key value to load the row by, or an array of fields to match.
133 * If not set the instance property value is used.
134 * @param boolean $new True to return an empty object if missing.
135 *
136 * @return mixed The record object on success, null otherwise.
137 */
138 public function getItem($pk = null, $new = false)
139 {
140 // build default path
141 $defaultPath = $this->getDefaultPath();
142
143 if (!$pk)
144 {
145 // use default file
146 $pk = $defaultPath;
147 }
148 else
149 {
150 // use custom file
151 $pk = $this->getEnvironmentFile($pk);
152
153 if ($pk === $defaultPath && !$new)
154 {
155 // Custom file not found...
156 // There's no need to return a default item.
157 return [];
158 }
159 }
160
161 if (!JFile::exists($pk))
162 {
163 // nothing to parse
164 return [];
165 }
166
167 // read css variables from default environment file
168 $buffer = file_get_contents($pk);
169
170 $vars = [];
171
172 // extract variables from buffer
173 if (preg_match_all("/\s*(--[a-zA-Z0-9_\-]+):\s*(.*?);/s", $buffer, $matches))
174 {
175 for ($i = 0; $i < count($matches[0]); $i++)
176 {
177 $k = $matches[1][$i];
178 $v = $matches[2][$i];
179
180 $vars[$k] = $v;
181 }
182 }
183
184 if ($pk !== $defaultPath)
185 {
186 // get default vars
187 $defaultVars = $this->getItem(null, true);
188
189 // scan the default vars, because new properties might have
190 // been introduced in a second time
191 foreach ($defaultVars as $k => $v)
192 {
193 // check whether the custom file supports the property
194 if (isset($vars[$k]))
195 {
196 // yep, overwrite with custom value
197 $defaultVars[$k] = $vars[$k];
198 }
199 }
200
201 // replace vars array
202 $vars = $defaultVars;
203 }
204
205 return $vars;
206 }
207
208 /**
209 * Scans the supported environment variables and group them in sections.
210 *
211 * @param integer $max_levels The maximum number of levels.
212 *
213 * @return array
214 */
215 public function getVarsTree($max_levels = 2)
216 {
217 // get array holding all the declared variables
218 $vars = $this->getItem('environment', $blank = true);
219
220 $tree = [];
221
222 foreach ($vars as $k => $v)
223 {
224 // extract nodes from variable (exclude initial prefix)
225 $nodes = preg_split('/[\-_]+/', preg_replace("/^--vap-/i", '', $k));
226
227 // keep only up to 2 nodes
228 $levels = array_splice($nodes, 0, $max_levels);
229
230 // create pointer to tree object
231 $seek = &$tree;
232
233 // scan the variable levels
234 foreach ($levels as $node)
235 {
236 // create node if not yet specified
237 if (!isset($seek[$node]))
238 {
239 $seek[$node] = [];
240 }
241
242 // register pointer to the current node
243 $seek = &$seek[$node];
244 }
245
246 // prepare leaf data
247 $data = [
248 'key' => $k,
249 'val' => $v,
250 'type' => 'color',
251 ];
252
253 // create leaf by merging all the remaining levels
254 $leaf = implode('_', $nodes);
255
256 if ($leaf)
257 {
258 // register leaf into the last created node
259 $seek[$leaf] = $data;
260 }
261 else
262 {
263 // no leaf to register, overwrite the last created node
264 $seek = $data;
265 }
266 }
267
268 return $tree;
269 }
270
271 /**
272 * Basic save implementation.
273 *
274 * @param mixed $data Either an array or an object of data to save.
275 *
276 * @return mixed The ID of the record on success, false otherwise.
277 */
278 public function save($data)
279 {
280 $data = (array) $data;
281
282 $buffer = '';
283
284 // register all CSS variables into a buffer
285 foreach ($data as $k => $v)
286 {
287 // check if we are dealing with a color property
288 if (preg_match("/-(?:color|background|border)$/", $k))
289 {
290 // make HEX safe
291 $v = '#' . ltrim($v, '#');
292 }
293
294 $buffer .= "\t{$k}: {$v};\n";
295 }
296
297 // set up CSS file
298 $buffer = sprintf(":root {\n%s}", $buffer);
299
300 // fetch custom path
301 $path = $this->getCustomPath();
302
303 // save CSS into the customizer file
304 $saved = JFile::write($path, $buffer);
305
306 if ($saved)
307 {
308 return $path;
309 }
310
311 return false;
312 }
313
314 /**
315 * Extend delete implementation to delete any related records
316 * stored within a separated table.
317 *
318 * @param mixed $ids Either the record ID or a list of records.
319 *
320 * @return boolean True on success, false otherwise.
321 */
322 public function delete($ids)
323 {
324 $res = false;
325
326 if (!$ids)
327 {
328 // use default environment file
329 $ids = 'environment';
330 }
331
332 // iterate all the specified files
333 foreach ((array) $ids as $id)
334 {
335 // delete the current custom environment file
336 $res = JFile::delete($this->getCustomPath($id)) || $res;
337 }
338
339 return $res;
340 }
341
342 /**
343 * Reads the custom CSS code from the default file loaded in the front-end.
344 *
345 * @return string
346 */
347 public function getCustomCSS()
348 {
349 // build path of the custom CSS file
350 $path = JPath::clean(VAPBASE . '/assets/css/vap-custom.css');
351
352 if (!JFile::exists($path))
353 {
354 // file missing
355 return '';
356 }
357
358 // read contents from CSS file
359 return file_get_contents($path);
360 }
361
362 /**
363 * Writes the specified CSS code into the custom file.
364 *
365 * @param string $css The CSS code to write.
366 *
367 * @return boolean True on success, false otherwise.
368 */
369 public function setCustomCSS($css)
370 {
371 // build path of the custom CSS file
372 $path = JPath::clean(VAPBASE . '/assets/css/vap-custom.css');
373
374 return (bool) JFile::write($path, $css);
375 }
376 }
377