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
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 = JFactory::getDate($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 |