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
package.php
302 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 package model. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VikAppointmentsModelPackage 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 | // attempt to save the relation |
| 35 | $id = parent::save($data); |
| 36 | |
| 37 | if (!$id) |
| 38 | { |
| 39 | // an error occurred, do not go ahead |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | if (isset($data['services'])) |
| 44 | { |
| 45 | // get package-service model |
| 46 | $model = JModelVAP::getInstance('packageservice'); |
| 47 | // define relations |
| 48 | $model->setRelation($id, $data['services']); |
| 49 | } |
| 50 | |
| 51 | return $id; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Extend duplicate implementation to clone any related records |
| 56 | * stored within a separated table. |
| 57 | * |
| 58 | * @param mixed $ids Either the record ID or a list of records. |
| 59 | * @param mixed $src Specifies some values to be used while duplicating. |
| 60 | * @param array $ignore A list of columns to skip. |
| 61 | * |
| 62 | * @return mixed The ID of the records on success, false otherwise. |
| 63 | */ |
| 64 | public function duplicate($ids, $src = array(), $ignore = array()) |
| 65 | { |
| 66 | $new_ids = array(); |
| 67 | |
| 68 | // do not copy ordering |
| 69 | $ignore[] = 'ordering'; |
| 70 | |
| 71 | $dbo = JFactory::getDbo(); |
| 72 | |
| 73 | // get package translation model |
| 74 | $langModel = JModelVAP::getInstance('langpackage'); |
| 75 | |
| 76 | // check whether we should use the services provided by the caller |
| 77 | $has_services = isset($src['services']); |
| 78 | |
| 79 | if (!isset($src['published'])) |
| 80 | { |
| 81 | // if not specified, auto-unpublished cloned packages |
| 82 | $src['published'] = 0; |
| 83 | } |
| 84 | |
| 85 | foreach ($ids as $id_package) |
| 86 | { |
| 87 | if (!$has_services) |
| 88 | { |
| 89 | // load any assigned services |
| 90 | $q = $dbo->getQuery(true) |
| 91 | ->select($dbo->qn('id_service')) |
| 92 | ->from($dbo->qn('#__vikappointments_package_service')) |
| 93 | ->where($dbo->qn('id_package') . ' = ' . (int) $id_package); |
| 94 | |
| 95 | $dbo->setQuery($q); |
| 96 | |
| 97 | // include assigned services to duplicate them too |
| 98 | $src['services'] = $dbo->loadColumn(); |
| 99 | } |
| 100 | |
| 101 | // start by duplicating the whole record |
| 102 | $new_id = parent::duplicate($id_package, $src, $ignore); |
| 103 | |
| 104 | if ($new_id) |
| 105 | { |
| 106 | $new_id = array_shift($new_id); |
| 107 | |
| 108 | // register copied |
| 109 | $new_ids[] = $new_id; |
| 110 | |
| 111 | // load any assigned translation |
| 112 | $q = $dbo->getQuery(true) |
| 113 | ->select($dbo->qn('id')) |
| 114 | ->from($dbo->qn('#__vikappointments_lang_package')) |
| 115 | ->where($dbo->qn('id_package') . ' = ' . (int) $id_package); |
| 116 | |
| 117 | $dbo->setQuery($q); |
| 118 | |
| 119 | if ($duplicate = $dbo->loadColumn()) |
| 120 | { |
| 121 | $lang_data = array(); |
| 122 | $lang_data['id_package'] = $new_id; |
| 123 | |
| 124 | // duplicate languages by using the new package ID |
| 125 | $langModel->duplicate($duplicate, $lang_data); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return $new_ids; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Extend delete implementation to delete any related records |
| 135 | * stored within a separated table. |
| 136 | * |
| 137 | * @param mixed $ids Either the record ID or a list of records. |
| 138 | * |
| 139 | * @return boolean True on success, false otherwise. |
| 140 | */ |
| 141 | public function delete($ids) |
| 142 | { |
| 143 | // only int values are accepted |
| 144 | $ids = array_map('intval', (array) $ids); |
| 145 | |
| 146 | // invoke parent first |
| 147 | if (!parent::delete($ids)) |
| 148 | { |
| 149 | // nothing to delete |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | $dbo = JFactory::getDbo(); |
| 154 | |
| 155 | // load any assigned translation |
| 156 | $q = $dbo->getQuery(true) |
| 157 | ->select($dbo->qn('id')) |
| 158 | ->from($dbo->qn('#__vikappointments_lang_package')) |
| 159 | ->where($dbo->qn('id_package') . ' IN (' . implode(',', $ids) . ')' ); |
| 160 | |
| 161 | $dbo->setQuery($q); |
| 162 | |
| 163 | if ($lang_ids = $dbo->loadColumn()) |
| 164 | { |
| 165 | // get translation model |
| 166 | $model = JModelVAP::getInstance('langpackage'); |
| 167 | // delete assigned translations |
| 168 | $model->delete($lang_ids); |
| 169 | } |
| 170 | |
| 171 | // DO NOT remove package-service relations. |
| 172 | // The associations are required for the orders already stored, otherwise the |
| 173 | // customers wouldn't be able to select the proper services in the front-end. |
| 174 | |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Returns all the services supported by the given package. |
| 180 | * |
| 181 | * @param integer $id The package ID. |
| 182 | * @param boolean $strict True to validate the user capabilities. |
| 183 | * When empty, it will depend on the current |
| 184 | * application client. |
| 185 | * |
| 186 | * @return mixed An array of services (when empty, all the array are supported). |
| 187 | * False will be returned in case no services are supported. |
| 188 | */ |
| 189 | public function getServices($id, $strict = null) |
| 190 | { |
| 191 | $dbo = JFactory::getDbo(); |
| 192 | |
| 193 | $q = $dbo->getQuery(true) |
| 194 | ->select($dbo->qn('id_service')) |
| 195 | ->from($dbo->qn('#__vikappointments_package_service')) |
| 196 | ->where($dbo->qn('id_package') . ' = ' . (int) $id); |
| 197 | |
| 198 | $dbo->setQuery($q); |
| 199 | $services = $dbo->loadColumn(); |
| 200 | |
| 201 | if (!$services) |
| 202 | { |
| 203 | // all the services are supported |
| 204 | return array(); |
| 205 | } |
| 206 | |
| 207 | if (is_null($strict)) |
| 208 | { |
| 209 | // lean on the current application client |
| 210 | $strict = JFactory::getApplication()->isClient('site'); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Retrieve only the services that belong to the view |
| 215 | * access level of the current user. |
| 216 | * |
| 217 | * @since 1.6 |
| 218 | */ |
| 219 | $levels = JFactory::getUser()->getAuthorisedViewLevels(); |
| 220 | |
| 221 | $dispatcher = VAPFactory::getEventDispatcher(); |
| 222 | |
| 223 | // get service model |
| 224 | $model = JModelVAP::getInstance('service'); |
| 225 | // cache services details |
| 226 | static $lookup = array(); |
| 227 | |
| 228 | $now = JFactory::getDate()->toSql(); |
| 229 | |
| 230 | $list = array(); |
| 231 | |
| 232 | // iterate services |
| 233 | foreach ($services as $id_service) |
| 234 | { |
| 235 | // look for a cached service first |
| 236 | if (!array_key_exists($id_service, $lookup)) |
| 237 | { |
| 238 | // load service details only once |
| 239 | $lookup[$id_service] = $model->getItem($id_service); |
| 240 | } |
| 241 | |
| 242 | if (!$lookup[$id_service]) |
| 243 | { |
| 244 | // service not found... |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | $service = $lookup[$id_service]; |
| 249 | |
| 250 | // make sure the access levels of the current user match |
| 251 | if (($levels && !in_array($service->level, $levels)) || !$service->published) |
| 252 | { |
| 253 | // the current user doesn't own enough capabilities |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | // validate end publishing of the service |
| 258 | if (!VAPDateHelper::isNull($service->end_publishing) && $service->end_publishing < $now) |
| 259 | { |
| 260 | // the service is expired |
| 261 | continue; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * This event can be used to apply additional conditions while checking whether a |
| 266 | * service can be redeemed by the specified package. When this event is triggered, |
| 267 | * the system already validated the standard conditions and the service is going |
| 268 | * to be included within the list of available services. |
| 269 | * |
| 270 | * @param integer $id_package The package identifier. |
| 271 | * @param object $service The service to check. |
| 272 | * |
| 273 | * @return boolean Return false to flag the service as NOT available. |
| 274 | * |
| 275 | * @since 1.7 |
| 276 | */ |
| 277 | if ($dispatcher->false('onCheckServicePackageAvailability', array($id, $service))) |
| 278 | { |
| 279 | // the service is not available for this package |
| 280 | continue; |
| 281 | } |
| 282 | |
| 283 | // register service details |
| 284 | $list[] = $service; |
| 285 | } |
| 286 | |
| 287 | if (!$list) |
| 288 | { |
| 289 | // no supported services |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | // sort services by using their ordering |
| 294 | usort($list, function($a, $b) |
| 295 | { |
| 296 | return $a->ordering - $b->ordering; |
| 297 | }); |
| 298 | |
| 299 | return $list; |
| 300 | } |
| 301 | } |
| 302 |