conversion.php
1 month ago
customer.php
1 month ago
customfields.php
1 month ago
index.html
1 month ago
locations.php
1 month ago
orderstatus.php
1 month ago
restrictions.php
1 month ago
specialrates.php
1 month ago
statistics.php
1 month ago
subscriptions.php
1 month ago
subscriptions.php
259 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 | /** |
| 15 | * VikAppointments subscriptions class handler. |
| 16 | * |
| 17 | * @since 1.7 |
| 18 | */ |
| 19 | abstract class VAPSubscriptions |
| 20 | { |
| 21 | /** |
| 22 | * A list of cached subscriptions, grouped by section: |
| 23 | * - 0 for customers; |
| 24 | * - 1 for employees. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected static $subscriptions = array(); |
| 29 | |
| 30 | /** |
| 31 | * Returns the group that whould be immediately highlighted when |
| 32 | * the user starts a new session. The preferred group is always |
| 33 | * based on the latest ordered subscription. |
| 34 | * |
| 35 | * @return integer The preferred group (0: customers, 1: employees). |
| 36 | */ |
| 37 | public static function getPreferredGroup() |
| 38 | { |
| 39 | static $preferred = null; |
| 40 | |
| 41 | // fetche preferred group only once |
| 42 | if (is_null($preferred)) |
| 43 | { |
| 44 | // use the default customers group |
| 45 | $preferred = 0; |
| 46 | |
| 47 | $dbo = JFactory::getDbo(); |
| 48 | |
| 49 | $q = $dbo->getQuery(true) |
| 50 | ->select($dbo->qn('id_employee')) |
| 51 | ->from($dbo->qn('#__vikappointments_subscr_order')) |
| 52 | ->order($dbo->qn('id') . ' DESC'); |
| 53 | |
| 54 | $dbo->setQuery($q, 0, 1); |
| 55 | |
| 56 | // check whether the latest order is assigned to an employee |
| 57 | if ((int) $dbo->loadResult() > 0) |
| 58 | { |
| 59 | // use the employees group |
| 60 | $preferred = 1; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // return cached version |
| 65 | return $preferred; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Checks if there is at least a published subscription for the given group. |
| 70 | * |
| 71 | * @param integer $group The group to use (0: customers, 1: employees). |
| 72 | * |
| 73 | * @return boolean True if any, false otherwise. |
| 74 | * |
| 75 | * @uses search() |
| 76 | */ |
| 77 | public static function has($group = 0) |
| 78 | { |
| 79 | return (bool) static::search(array('published' => 1, 'group' => (int) $group), $limit = 1, $translate = false); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the trial subscription (if any). |
| 84 | * |
| 85 | * @param integer $group The group to use (0: customers, 1: employees). |
| 86 | * @param boolean $translate True to translate the subscriptions, false otherwise. |
| 87 | * |
| 88 | * @return mixed The trial subscription array if exists, false otherwise. |
| 89 | * |
| 90 | * @uses search() |
| 91 | */ |
| 92 | public static function getTrial($group = 0, $translate = true) |
| 93 | { |
| 94 | return static::search(array('published' => 1, 'trial' => 1, 'group' => (int) $group), $limit = 1, $translate); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns a list of active subscriptions. |
| 99 | * |
| 100 | * @param integer $group The group to use (0: customers, 1: employees). |
| 101 | * @param boolean $trial True to include the trial subscription, false otherwise. |
| 102 | * @param boolean $translate True to translate the subscriptions, false otherwise. |
| 103 | * |
| 104 | * @return array A list of published subscriptions. |
| 105 | * |
| 106 | * @uses search() |
| 107 | */ |
| 108 | public static function getList($group = 0, $trial = false, $translate = true) |
| 109 | { |
| 110 | return static::search(array('published' => 1, 'trial' => (int) $trial, 'group' => (int) $group), $limit = null, $translate); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the details of the given subscription. |
| 115 | * |
| 116 | * @param integer $group The group to use (0: customers, 1: employees). |
| 117 | * @param integer $id The subscription ID. |
| 118 | * @param boolean $strict True to get the subscription only if it is published. |
| 119 | * @param boolean $translate True to translate the subscriptions, false otherwise. |
| 120 | * |
| 121 | * @return array The subscription array if exists, false otherwise. |
| 122 | * |
| 123 | * @uses search() |
| 124 | */ |
| 125 | public static function get($id, $group = 0, $strict = true, $translate = true) |
| 126 | { |
| 127 | $where = array( |
| 128 | 'id' => (int) $id, |
| 129 | 'group' => (int) $group, |
| 130 | ); |
| 131 | |
| 132 | if ($strict) |
| 133 | { |
| 134 | $where['published'] = 1; |
| 135 | } |
| 136 | |
| 137 | return static::search($where, $limit = 1, $translate); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns a list of subscriptions matching the given query. |
| 142 | * |
| 143 | * @param array $where An associative array containing the query terms. |
| 144 | * @param integer $lim The number of records to retrieve. Null to ignore this value. |
| 145 | * @param boolean $translate True to translate the subscriptions, false otherwise. |
| 146 | * |
| 147 | * @return array The subscriptions list. The associative array of the subscription in case |
| 148 | * $lim is equals to 1 (false in case the subscription does not exist). |
| 149 | */ |
| 150 | public static function search(array $where = array(), $lim = null, $translate = false) |
| 151 | { |
| 152 | // in case the group is not specified, use the default one, |
| 153 | // because it doesn't make sense to load both them |
| 154 | $group = 0; |
| 155 | |
| 156 | if (isset($where['group'])) |
| 157 | { |
| 158 | $group = (int) $where['group']; |
| 159 | // unset from query to avoid redundant comparisons |
| 160 | unset($where['group']); |
| 161 | } |
| 162 | |
| 163 | if (!isset(static::$subscriptions[$group])) |
| 164 | { |
| 165 | static::$subscriptions[$group] = array(); |
| 166 | |
| 167 | // lazy load all the subscriptions |
| 168 | $dbo = JFactory::getDbo(); |
| 169 | |
| 170 | $q = $dbo->getQuery(true) |
| 171 | ->select('*') |
| 172 | ->from($dbo->qn('#__vikappointments_subscription')) |
| 173 | ->where($dbo->qn('group') . ' = ' . (int) $group) |
| 174 | ->order($dbo->qn('ordering') . ' ASC'); |
| 175 | |
| 176 | $dbo->setQuery($q); |
| 177 | |
| 178 | foreach ($dbo->loadAssocList() as $subscr) |
| 179 | { |
| 180 | // decode services list |
| 181 | $subscr['services'] = $subscr['services'] ? array_values(array_filter(explode(',', $subscr['services']))) : []; |
| 182 | |
| 183 | // register subscriptions |
| 184 | static::$subscriptions[$group][] = $subscr; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | $matching = array(); |
| 189 | |
| 190 | // find all subscriptions that match the query |
| 191 | foreach (static::$subscriptions[$group] as $subscr) |
| 192 | { |
| 193 | // validate subscription against search query |
| 194 | if (static::match($subscr, $where)) |
| 195 | { |
| 196 | // matching record, register it |
| 197 | $matching[] = $subscr; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (!$matching) |
| 202 | { |
| 203 | if ($lim == 1) |
| 204 | { |
| 205 | // return false in case the limit is equals to 1 |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | // return an empty array |
| 210 | return array(); |
| 211 | } |
| 212 | |
| 213 | if ($lim) |
| 214 | { |
| 215 | // splice the array to keep only the records between the given limit |
| 216 | $matching = array_splice($matching, 0, $lim); |
| 217 | } |
| 218 | |
| 219 | if ($translate) |
| 220 | { |
| 221 | // translate subscriptions |
| 222 | VikAppointments::translateSubscriptions($matching); |
| 223 | } |
| 224 | |
| 225 | if ($lim == 1) |
| 226 | { |
| 227 | // return the first element of the list |
| 228 | return $matching[0]; |
| 229 | } |
| 230 | |
| 231 | // return list of subscriptions |
| 232 | return $matching; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Checks whether the specified subscription matches the query. |
| 237 | * |
| 238 | * @param array $subscription The subscription details. |
| 239 | * @param array $where An associative array containing the query terms. |
| 240 | * |
| 241 | * @return boolean True if matching, false otherwise. |
| 242 | */ |
| 243 | protected static function match($subscription, $where) |
| 244 | { |
| 245 | foreach ($where as $k => $v) |
| 246 | { |
| 247 | // compare only in case the subscription array owns the current property |
| 248 | if (isset($subscription[$k]) && $subscription[$k] != $v) |
| 249 | { |
| 250 | // the values do not match |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // all the values seem to match |
| 256 | return true; |
| 257 | } |
| 258 | } |
| 259 |