PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / libraries / mvc / admin / models / override.php
vikappointments / libraries / mvc / admin / models Last commit date
acl.php 1 month ago license.php 1 month ago override.php 1 month ago overrides.php 1 month ago shortcode.php 1 month ago shortcodes.php 1 month ago
override.php
152 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2024 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 JLoader::import('adapter.mvc.models.form');
15
16 /**
17 * VikAppointments plugin Override model.
18 * @wponly
19 *
20 * @since 1.2.13
21 * @see JModelForm
22 */
23 class VikAppointmentsModelOverride extends JModelForm
24 {
25 /**
26 * @override
27 * Saves the override.
28 *
29 * @param array &$data The array containing the override data.
30 *
31 * @return boolean True on success, otherwise false.
32 */
33 public function save(&$data)
34 {
35 // make sure we have a destination file
36 if (empty($data['file']))
37 {
38 $this->setError('Missing destination file');
39
40 return false;
41 }
42
43 // make sure we have a code to write
44 if (!isset($data['code']))
45 {
46 $this->setError('Missing override code');
47
48 return false;
49 }
50
51 /**
52 * Use the unpublished version in case the status is inactive.
53 *
54 * @since 1.2.14
55 */
56 if (($data['published'] ?? true) == false)
57 {
58 $data['file'] = dirname($data['file']) . '/__' . basename($data['file']);
59 }
60
61 // import file helper
62 JLoader::import('adapter.filesystem.file');
63 // generate override
64 return JFile::write($data['file'], $data['code']);
65 }
66
67 /**
68 * Deletes the specified records.
69 *
70 * @param mixed $ids The PK value (or a list of values) of the record(s) to remove.
71 *
72 * @return boolean True if at least a record has been removed, otherwise false.
73 */
74 public function delete($ids)
75 {
76 // import file helper
77 JLoader::import('adapter.filesystem.file');
78
79 $deleted = false;
80
81 // iterate files
82 foreach ((array) $ids as $file)
83 {
84 $src = $file;
85
86 // in case the override doesn't exist, try to
87 // look for an unpublished override
88 if (!JFile::exists($file))
89 {
90 // use the unpublished version
91 $src = JPath::clean(dirname($file) . '/__' . basename($file));
92 }
93
94 // try to delete the file
95 $deleted = JFile::delete($src) || $deleted;
96 }
97
98 return $deleted;
99 }
100
101 /**
102 * Changes the state of the specified records.
103 *
104 * @param mixed $ids The PK value (or a list of values) of the record(s) to update.
105 *
106 * @return mixed True if at least a record has been affected, otherwise false.
107 * In case of a single record, the new path will be returned.
108 */
109 public function publish($ids, $state)
110 {
111 // import file helper
112 JLoader::import('adapter.filesystem.path');
113
114 $changed = $dest = false;
115
116 $ids = (array) $ids;
117
118 // iterate files
119 foreach ($ids as $file)
120 {
121 $dest = dirname($file);
122 $name = basename($file);
123
124 if ($state)
125 {
126 // start from unpublished file
127 $src = JPath::clean($dest . '/__' . $name);
128 // restore default override
129 $dest = $file;
130 }
131 else
132 {
133 // start from existing override
134 $src = $file;
135 // move to unpublished status
136 $dest = JPath::clean($dest . '/__' . $name);
137 }
138
139 // try to rename the file
140 $changed = rename($src, $dest) || $changed;
141 }
142
143 // in case of a single file, return its new path
144 if ($changed && count($ids) == 1)
145 {
146 return $dest;
147 }
148
149 return $changed;
150 }
151 }
152