PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / statuscode.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
statuscode.php
266 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 status code model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelStatuscode extends JModelVAP
22 {
23 /**
24 * Basic save implementation.
25 *
26 * @param mixed $data Either an array or an object of data to save.
27 *
28 * @return mixed The ID of the record on success, false otherwise.
29 */
30 public function save($data)
31 {
32 $data = (array) $data;
33
34 $old = null;
35
36 // in case of update of a status, we should make
37 // sure whether the given code is changing
38 if (!empty($data['code']) && !empty($data['id']))
39 {
40 $table = $this->getTable();
41
42 // attempt to load the status details and look for any changes
43 if ($table->load($data['id']) && $data['code'] != $table->code)
44 {
45 // the code seems to change, register old properties for later use
46 $old = $table->getProperties();
47 }
48 }
49
50 // attempt to save the relation
51 $id = parent::save($data);
52
53 if (!$id)
54 {
55 // an error occurred, do not go ahead
56 return false;
57 }
58
59 if ($old)
60 {
61 // The code changed, we need to update all the records that
62 // are currently assigned to that status code.
63 // Mass update records without caring of triggering any events,
64 // since we are doing a stability update.
65 $dbo = JFactory::getDbo();
66
67 if ($old['appointments'])
68 {
69 $q = $dbo->getQuery(true)
70 ->update($dbo->qn('#__vikappointments_reservation'))
71 ->set($dbo->qn('status') . ' = ' . $dbo->q($data['code']))
72 ->where($dbo->qn('status') . ' = ' . $dbo->q($old['code']));
73
74 $dbo->setQuery($q);
75 $dbo->execute();
76 }
77
78 if ($old['packages'])
79 {
80 $q = $dbo->getQuery(true)
81 ->update($dbo->qn('#__vikappointments_package_order'))
82 ->set($dbo->qn('status') . ' = ' . $dbo->q($data['code']))
83 ->where($dbo->qn('status') . ' = ' . $dbo->q($old['code']));
84
85 $dbo->setQuery($q);
86 $dbo->execute();
87 }
88
89 if ($old['subscriptions'])
90 {
91 $q = $dbo->getQuery(true)
92 ->update($dbo->qn('#__vikappointments_subscr_order'))
93 ->set($dbo->qn('status') . ' = ' . $dbo->q($data['code']))
94 ->where($dbo->qn('status') . ' = ' . $dbo->q($old['code']));
95
96 $dbo->setQuery($q);
97 $dbo->execute();
98 }
99
100 $q = $dbo->getQuery(true)
101 ->update($dbo->qn('#__vikappointments_cust_mail'))
102 ->set($dbo->qn('status') . ' = ' . $dbo->q($data['code']))
103 ->where($dbo->qn('status') . ' = ' . $dbo->q($old['code']));
104
105 $dbo->setQuery($q);
106 $dbo->execute();
107 }
108
109 return $id;
110 }
111
112 /**
113 * Extend delete implementation to delete any related records
114 * stored within a separated table.
115 *
116 * @param mixed $ids Either the record ID or a list of records.
117 *
118 * @return boolean True on success, false otherwise.
119 */
120 public function delete($ids)
121 {
122 // only int values are accepted
123 $ids = array_map('intval', (array) $ids);
124
125 // invoke parent first
126 if (!parent::delete($ids))
127 {
128 // nothing to delete
129 return false;
130 }
131
132 $dbo = JFactory::getDbo();
133
134 // load any assigned translation
135 $q = $dbo->getQuery(true)
136 ->select($dbo->qn('id'))
137 ->from($dbo->qn('#__vikappointments_lang_status_code'))
138 ->where($dbo->qn('id_status_code') . ' IN (' . implode(',', $ids) . ')' );
139
140 $dbo->setQuery($q);
141
142 if ($lang_ids = $dbo->loadColumn())
143 {
144 // get translation model
145 $model = JModelVAP::getInstance('langstatuscode');
146 // delete assigned translations
147 $model->delete($lang_ids);
148 }
149
150 return true;
151 }
152
153 /**
154 * Helper method used to ensure that all the required status codes have been
155 * properly configured for all the sections.
156 *
157 * It is possible to use the getErrors() method to fetch the list of errors
158 * that have been registered while running the tests.
159 *
160 * @return boolean True in case of success, false otherwise.
161 *
162 * @since 1.7.1
163 */
164 public function runTests()
165 {
166 // build the array of tests
167 $tests = [
168 // define an array of status codes required to the appointments group
169 'appointments' => [
170 'confirmed',
171 'paid',
172 'pending',
173 'removed',
174 'cancelled',
175 ],
176 // define an array of status codes required to the packages group
177 'packages' => [
178 'confirmed',
179 'paid',
180 'pending',
181 ],
182 // define an array of status codes required to the subscriptions group
183 'subscriptions' => [
184 'confirmed',
185 'paid',
186 'pending',
187 ],
188 ];
189
190 // ignore tests for the packages in case this section is unused
191 if (VAPFactory::getConfig()->getBool('enablepackages') === false)
192 {
193 // packages disabled, ignore tests for this group
194 unset($tests['packages']);
195 }
196
197 VAPLoader::import('libraries.models.subscriptions');
198
199 // ignore tests for the subscriptions in case this section is unused
200 if (!VAPSubscriptions::has(0) && !VAPSubscriptions::has(1))
201 {
202 unset($tests['subscriptions']);
203 }
204
205 $status = true;
206
207 // iterate all groups
208 foreach ($tests as $group => $roles)
209 {
210 // iterate all roles
211 foreach ($roles as $role)
212 {
213 try
214 {
215 // try to fetch the status code
216 JHtml::fetch('vaphtml.status.' . $role, $group, $column = 'code', $strict = true);
217 }
218 catch (Exception $e)
219 {
220 // status not found, register the error message (include the group alias)
221 $this->setError($e->getMessage() . ' (' . $group . ')');
222 $status = false;
223 }
224 }
225 }
226
227 return $status;
228 }
229
230 /**
231 * Restores the status codes to the factory settings.
232 *
233 * @return void
234 *
235 * @since 1.7.1
236 */
237 public function restore()
238 {
239 $dbo = JFactory::getDbo();
240
241 // delete all the existing status codes
242 $q = "TRUNCATE TABLE `#__vikappointments_status_code`";
243 $dbo->setQuery($q);
244 $dbo->execute();
245
246 // delete all the existing status codes translations
247 $q = "TRUNCATE TABLE `#__vikappointments_lang_status_code`";
248 $dbo->setQuery($q);
249 $dbo->execute();
250
251 // re-create all the default status codes
252 $q = "INSERT INTO `#__vikappointments_status_code`
253 ( `name`, `code`, `color`, `ordering`, `approved`, `reserved`, `expired`, `cancelled`, `paid`, `appointments`, `packages`, `subscriptions`) VALUES
254 ('Confirmed', 'C', '008000', 1, 1, 1, 0, 0, 0, 1, 1, 1),
255 ( 'Paid', 'P', '339CCC', 2, 1, 1, 0, 0, 1, 1, 1, 1),
256 ( 'Pending', 'W', 'FF7000', 3, 0, 1, 0, 0, 0, 1, 1, 1),
257 ( 'Removed', 'E', '990000', 4, 0, 0, 1, 0, 0, 1, 0, 0),
258 ('Cancelled', 'X', 'F01B17', 5, 0, 0, 0, 1, 0, 1, 1, 1),
259 ( 'Refunded', 'R', '8116C9', 6, 0, 0, 0, 1, 1, 1, 1, 1),
260 ( 'No-Show', 'N', '828282', 7, 1, 1, 0, 0, 0, 1, 0, 0);";
261
262 $dbo->setQuery($q);
263 $dbo->execute();
264 }
265 }
266