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 / models / subscription.php
vikappointments / admin / models Last commit date
apiban.php 2 days ago apilog.php 2 days ago apiplugin.php 2 days ago apiuser.php 2 days ago apiuseroptions.php 2 days ago backup.php 2 days ago caldays.php 2 days ago calendar.php 2 days ago city.php 2 days ago closure.php 2 days ago configapp.php 2 days ago configcldays.php 2 days ago configcron.php 2 days ago configemp.php 2 days ago configsmsapi.php 2 days ago configuration.php 2 days ago conversion.php 2 days ago country.php 2 days ago coupon.php 2 days ago couponemployee.php 2 days ago coupongroup.php 2 days ago couponservice.php 2 days ago cronjob.php 2 days ago cronjoblog.php 2 days ago customer.php 2 days ago customf.php 2 days ago customfservice.php 2 days ago customizer.php 2 days ago empgroup.php 2 days ago employee.php 2 days ago empsettings.php 2 days ago file.php 2 days ago findreservation.php 2 days ago group.php 2 days ago import.php 2 days ago index.html 2 days ago invoice.php 2 days ago langcustomf.php 2 days ago langempgroup.php 2 days ago langemployee.php 2 days ago langgroup.php 2 days ago langmedia.php 2 days ago langoption.php 2 days ago langoptiongroup.php 2 days ago langoptionvar.php 2 days ago langpackage.php 2 days ago langpackgroup.php 2 days ago langpayment.php 2 days ago langservice.php 2 days ago langstatuscode.php 2 days ago langsubscr.php 2 days ago langtax.php 2 days ago langtaxrule.php 2 days ago location.php 2 days ago mailtext.php 2 days ago makerecurrence.php 2 days ago media.php 2 days ago multiorder.php 2 days ago option.php 2 days ago optiongroup.php 2 days ago optionvar.php 2 days ago orderstatus.php 2 days ago package.php 2 days ago packageservice.php 2 days ago packgroup.php 2 days ago packorder.php 2 days ago packorderitem.php 2 days ago payment.php 2 days ago rate.php 2 days ago reportsemp.php 2 days ago reportsser.php 2 days ago reservation.php 2 days ago resoptassoc.php 2 days ago restriction.php 2 days ago review.php 2 days ago serempassoc.php 2 days ago seroptassoc.php 2 days ago serrateassoc.php 2 days ago serrestrassoc.php 2 days ago service.php 2 days ago state.php 2 days ago statswidget.php 2 days ago statuscode.php 2 days ago subscription.php 2 days ago subscrorder.php 2 days ago tag.php 2 days ago tax.php 2 days ago taxrule.php 2 days ago updateprogram.php 2 days ago usernote.php 2 days ago waitinglist.php 2 days ago webhook.php 2 days ago worktime.php 2 days ago
subscription.php
320 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 subscription model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelSubscription extends JModelVAP
22 {
23 /**
24 * Basic item loading implementation.
25 *
26 * @param mixed $pk An optional primary key value to load the row by, or an array of fields to match.
27 * If not set the instance property value is used.
28 * @param boolean $new True to return an empty object if missing.
29 *
30 * @return mixed The record object on success, null otherwise.
31 *
32 * @since 1.7.9
33 */
34 public function getItem($pk, $new = false)
35 {
36 // load item through parent
37 $item = parent::getItem($pk, $new);
38
39 if ($item)
40 {
41 $item->services = $item->services ? array_values(array_filter(explode(',', $item->services))) : [];
42 }
43
44 return $item;
45 }
46
47 /**
48 * Basic save implementation.
49 *
50 * @param mixed $data Either an array or an object of data to save.
51 *
52 * @return mixed The ID of the record on success, false otherwise.
53 */
54 public function save($data)
55 {
56 $data = (array) $data;
57
58 if (!empty($data['trial']))
59 {
60 // we are going to set the TRIAL state for the
61 // given record, so we need to turn it off from
62 // any other subscription first
63 $this->clearTrialState(@$data['group']);
64 }
65
66 // attempt to save subscription
67 return parent::save($data);
68 }
69
70 /**
71 * Extend delete implementation to delete any related records
72 * stored within a separated table.
73 *
74 * @param mixed $ids Either the record ID or a list of records.
75 *
76 * @return boolean True on success, false otherwise.
77 */
78 public function delete($ids)
79 {
80 // only int values are accepted
81 $ids = array_map('intval', (array) $ids);
82
83 // invoke parent first
84 if (!parent::delete($ids))
85 {
86 // nothing to delete
87 return false;
88 }
89
90 $dbo = JFactory::getDbo();
91
92 // load any assigned translation
93 $q = $dbo->getQuery(true)
94 ->select($dbo->qn('id'))
95 ->from($dbo->qn('#__vikappointments_lang_subscr'))
96 ->where($dbo->qn('id_subscr') . ' IN (' . implode(',', $ids) . ')' );
97
98 $dbo->setQuery($q);
99
100 if ($lang_ids = $dbo->loadColumn())
101 {
102 // get translation model
103 $model = JModelVAP::getInstance('langsubscr');
104 // delete assigned translations
105 $model->delete($lang_ids);
106 }
107
108 return true;
109 }
110
111 /**
112 * Extends the specified date by the duration of the subscription.
113 *
114 * @param string $date The UTC date to extend.
115 * @param mixed $subscription Either a subscription object or its ID.
116 *
117 * @return mixed The resulting date on success, -1 for lifetime subscription
118 * false in case the subscription doesn't exist.
119 */
120 public function extend($date, $subscription)
121 {
122 // check if we have an ID
123 if (is_numeric($subscription))
124 {
125 // get subscription table
126 $table = $this->getTable();
127 // attempt to load subscription details
128 if (!$table->load((int) $subscription))
129 {
130 // subscription not found
131 return false;
132 }
133
134 // get subscription details
135 $subscription = $table->getProperties();
136 }
137
138 // always cast to object
139 $subscription = (object) $subscription;
140
141 // check if we have a lifetime subscription
142 if ($subscription->type == 5)
143 {
144 // use lifetime constant
145 return -1;
146 }
147
148 switch ($subscription->type)
149 {
150 case 1:
151 // daily subscription
152 $add = 'days';
153 break;
154
155 case 2:
156 // weekly subscription
157 $add = 'weeks';
158 break;
159
160 case 3:
161 // monthly subscription
162 $add = 'months';
163 break;
164
165 case 4:
166 // yearly subscription
167 $add = 'years';
168 break;
169
170 default:
171 $add = null;
172 }
173
174 // create date instance
175 $date = JFactory::getDate($date);
176
177 if ($add)
178 {
179 if ($subscription->amount == 1)
180 {
181 // get rid of plural in case amount is 1
182 $add = rtrim($add, 's');
183 }
184
185 // create date add string
186 $add = '+' . $subscription->amount . ' ' . $add;
187
188 // extend the date by the provided time
189 $date->modify($add);
190 }
191
192 /**
193 * Fires while extending the subscription of a customer or an employee.
194 * It is possible to rely on this hook to alter the new expiration date
195 * of the subscription.
196 *
197 * @param \JDate $date The expiration date.
198 * @param object $subscription The details of the purchased subscription.
199 *
200 * @return void
201 *
202 * @since 1.7.9
203 */
204 VAPFactory::getEventDispatcher()->trigger('onBeforeExtendSubscription', [$date, $subscription]);
205
206 return $date->toSql();
207 }
208
209 /**
210 * Removes the TRIAL status from any existing subscription.
211 *
212 * @param mixed $id Either an array or an ID.
213 * @param boolean $state The new trial state.
214 * @param integer $group The subscription group. Use "1"
215 * for customers, "0" for employees.
216 *
217 * @return boolean
218 */
219 public function setTrialState($id, $state = true, $group = null)
220 {
221 if (is_array($id))
222 {
223 // in case of array, take only the first element of the list
224 $id = array_shift($id);
225 }
226
227 if ($state)
228 {
229 // group not specified, recover it from subscription details
230 if (is_null($group))
231 {
232 $item = $this->getItem($id);
233
234 if (!$item)
235 {
236 // item not found, abort
237 return false;
238 }
239
240 // use the subscription group
241 $group = $item->group;
242 }
243
244 // we are going to set the TRIAL state for the
245 // given record, so we need to turn it off from
246 // any other subscription first
247 $this->clearTrialState($group);
248 }
249
250 // change state of selected records
251 $this->publish($id, $state, 'trial');
252
253 return true;
254 }
255
256 /**
257 * Removes the TRIAL status from any existing subscription.
258 *
259 * @param integer $group The subscription group. Use "1"
260 * for customers, "0" for employees.
261 *
262 * @return void
263 */
264 protected function clearTrialState($group = 0)
265 {
266 $dbo = JFactory::getDbo();
267
268 /**
269 * Mass update all the subscription without taking care
270 * of tracking events/hooks. Keep this process as smooth
271 * as possible.
272 *
273 * We need to clear all the records that belong to the
274 * specified group (@since 1.7).
275 */
276 $q = $dbo->getQuery(true)
277 ->update($dbo->qn('#__vikappointments_subscription'))
278 ->set($dbo->qn('trial') . ' = 0')
279 ->where($dbo->qn('group') . ' = ' . (int) $group);
280
281 $dbo->setQuery($q);
282 $dbo->execute();
283 }
284
285 /**
286 * Returns a list holding all the supported subscription types.
287 *
288 * @return array
289 *
290 * @since 1.7.9
291 */
292 public function getSupportedSubscriptionTypes()
293 {
294 $types = [];
295
296 for ($i = 1; $i <= 5; $i++)
297 {
298 $types[$i] = JText::translate('VAPSUBSCRTYPE' . $i);
299 }
300
301 /**
302 * Fires while loading the supported subscription types.
303 * It is possible to rely on this hook to support custom durations.
304 *
305 * @return array An associative array with id-label pairs.
306 *
307 * @since 1.7.9
308 */
309 $results = VAPFactory::getEventDispatcher()->trigger('onLoadSupportedSubscriptionTypes');
310
311 foreach ($results as $custom)
312 {
313 // use native array concat instead of array_merge to preserve associative keys
314 $types = $types + (array) $custom;
315 }
316
317 return $types;
318 }
319 }
320