PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.18
VikAppointments Services Booking Calendar v1.2.18
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / subscription.php
vikappointments / admin / models Last commit date
apiban.php 5 months ago apilog.php 5 months ago apiplugin.php 5 months ago apiuser.php 5 months ago apiuseroptions.php 5 months ago backup.php 5 months ago caldays.php 5 months ago calendar.php 5 months ago city.php 5 months ago closure.php 5 months ago configapp.php 5 months ago configcldays.php 5 months ago configcron.php 5 months ago configemp.php 5 months ago configsmsapi.php 5 months ago configuration.php 5 months ago conversion.php 5 months ago country.php 5 months ago coupon.php 5 months ago couponemployee.php 5 months ago coupongroup.php 5 months ago couponservice.php 5 months ago cronjob.php 5 months ago cronjoblog.php 5 months ago customer.php 5 months ago customf.php 5 months ago customfservice.php 5 months ago customizer.php 5 months ago empgroup.php 5 months ago employee.php 5 months ago empsettings.php 5 months ago file.php 5 months ago findreservation.php 5 months ago group.php 5 months ago import.php 5 months ago index.html 5 months ago invoice.php 5 months ago langcustomf.php 5 months ago langempgroup.php 5 months ago langemployee.php 5 months ago langgroup.php 5 months ago langmedia.php 5 months ago langoption.php 5 months ago langoptiongroup.php 5 months ago langoptionvar.php 5 months ago langpackage.php 5 months ago langpackgroup.php 5 months ago langpayment.php 5 months ago langservice.php 5 months ago langstatuscode.php 5 months ago langsubscr.php 5 months ago langtax.php 5 months ago langtaxrule.php 5 months ago location.php 5 months ago mailtext.php 5 months ago makerecurrence.php 5 months ago media.php 5 months ago multiorder.php 5 months ago option.php 5 months ago optiongroup.php 5 months ago optionvar.php 5 months ago orderstatus.php 5 months ago package.php 5 months ago packageservice.php 5 months ago packgroup.php 5 months ago packorder.php 5 months ago packorderitem.php 5 months ago payment.php 5 months ago rate.php 5 months ago reportsemp.php 5 months ago reportsser.php 5 months ago reservation.php 5 months ago resoptassoc.php 5 months ago restriction.php 5 months ago review.php 5 months ago serempassoc.php 5 months ago seroptassoc.php 5 months ago serrateassoc.php 5 months ago serrestrassoc.php 5 months ago service.php 5 months ago state.php 5 months ago statswidget.php 5 months ago statuscode.php 5 months ago subscription.php 5 months ago subscrorder.php 5 months ago tag.php 5 months ago tax.php 5 months ago taxrule.php 5 months ago updateprogram.php 5 months ago usernote.php 5 months ago waitinglist.php 5 months ago webhook.php 5 months ago worktime.php 5 months ago
subscription.php
238 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 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 if (!empty($data['trial']))
35 {
36 // we are going to set the TRIAL state for the
37 // given record, so we need to turn it off from
38 // any other subscription first
39 $this->clearTrialState(@$data['group']);
40 }
41
42 // attempt to save subscription
43 return parent::save($data);
44 }
45
46 /**
47 * Extend delete implementation to delete any related records
48 * stored within a separated table.
49 *
50 * @param mixed $ids Either the record ID or a list of records.
51 *
52 * @return boolean True on success, false otherwise.
53 */
54 public function delete($ids)
55 {
56 // only int values are accepted
57 $ids = array_map('intval', (array) $ids);
58
59 // invoke parent first
60 if (!parent::delete($ids))
61 {
62 // nothing to delete
63 return false;
64 }
65
66 $dbo = JFactory::getDbo();
67
68 // load any assigned translation
69 $q = $dbo->getQuery(true)
70 ->select($dbo->qn('id'))
71 ->from($dbo->qn('#__vikappointments_lang_subscr'))
72 ->where($dbo->qn('id_subscr') . ' IN (' . implode(',', $ids) . ')' );
73
74 $dbo->setQuery($q);
75
76 if ($lang_ids = $dbo->loadColumn())
77 {
78 // get translation model
79 $model = JModelVAP::getInstance('langsubscr');
80 // delete assigned translations
81 $model->delete($lang_ids);
82 }
83
84 return true;
85 }
86
87 /**
88 * Extends the specified date by the duration of the subscription.
89 *
90 * @param string $date The UTC date to extend.
91 * @param mixed $subscription Either a subscription object or its ID.
92 *
93 * @return mixed The resulting date on success, -1 for lifetime subscription
94 * false in case the subscription doesn't exist.
95 */
96 public function extend($date, $subscription)
97 {
98 // check if we have an ID
99 if (is_numeric($subscription))
100 {
101 // get subscription table
102 $table = $this->getTable();
103 // attempt to load subscription details
104 if (!$table->load((int) $subscription))
105 {
106 // subscription not found
107 return false;
108 }
109
110 // get subscription details
111 $subscription = $table->getProperties();
112 }
113
114 // always cast to object
115 $subscription = (object) $subscription;
116
117 // check if we have a lifetime subscription
118 if ($subscription->type == 5)
119 {
120 // use lifetime constant
121 return -1;
122 }
123
124 switch ($subscription->type)
125 {
126 case 2:
127 // weekly subscription
128 $add = 'weeks';
129 break;
130
131 case 3:
132 // monthly subscription
133 $add = 'months';
134 break;
135
136 case 4:
137 // yearly subscription
138 $add = 'years';
139 break;
140
141 default:
142 // daily subscription
143 $add = 'days';
144 }
145
146 if ($subscription->amount == 1)
147 {
148 // get rid of plural in case amount is 1
149 $add = rtrim($add, 's');
150 }
151
152 // create date add string
153 $add = '+' . $subscription->amount . ' ' . $add;
154
155 // create date instance and extend it
156 $date = new JDate($date);
157 $date->modify($add);
158
159 return $date->toSql();
160 }
161
162 /**
163 * Removes the TRIAL status from any existing subscription.
164 *
165 * @param mixed $id Either an array or an ID.
166 * @param boolean $state The new trial state.
167 * @param integer $group The subscription group. Use "1"
168 * for customers, "0" for employees.
169 *
170 * @return boolean
171 */
172 public function setTrialState($id, $state = true, $group = null)
173 {
174 if (is_array($id))
175 {
176 // in case of array, take only the first element of the list
177 $id = array_shift($id);
178 }
179
180 if ($state)
181 {
182 // group not specified, recover it from subscription details
183 if (is_null($group))
184 {
185 $item = $this->getItem($id);
186
187 if (!$item)
188 {
189 // item not found, abort
190 return false;
191 }
192
193 // use the subscription group
194 $group = $item->group;
195 }
196
197 // we are going to set the TRIAL state for the
198 // given record, so we need to turn it off from
199 // any other subscription first
200 $this->clearTrialState($group);
201 }
202
203 // change state of selected records
204 $this->publish($id, $state, 'trial');
205
206 return true;
207 }
208
209 /**
210 * Removes the TRIAL status from any existing subscription.
211 *
212 * @param integer $group The subscription group. Use "1"
213 * for customers, "0" for employees.
214 *
215 * @return void
216 */
217 protected function clearTrialState($group = 0)
218 {
219 $dbo = JFactory::getDbo();
220
221 /**
222 * Mass update all the subscription without taking care
223 * of tracking events/hooks. Keep this process as smooth
224 * as possible.
225 *
226 * We need to clear all the records that belong to the
227 * specified group (@since 1.7).
228 */
229 $q = $dbo->getQuery(true)
230 ->update($dbo->qn('#__vikappointments_subscription'))
231 ->set($dbo->qn('trial') . ' = 0')
232 ->where($dbo->qn('group') . ' = ' . (int) $group);
233
234 $dbo->setQuery($q);
235 $dbo->execute();
236 }
237 }
238