auth.php
1032 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.employee.area.manager'); |
| 15 | |
| 16 | /** |
| 17 | * Class used to check the authorisations of the attached employee. |
| 18 | * |
| 19 | * @since 1.2 |
| 20 | * @since 1.7 Renamed from EmployeeAuth. |
| 21 | */ |
| 22 | class VAPEmployeeAuth |
| 23 | { |
| 24 | /** |
| 25 | * A list of instances. |
| 26 | * |
| 27 | * @var array |
| 28 | */ |
| 29 | protected static $instances = array(); |
| 30 | |
| 31 | /** |
| 32 | * An associative array containing the details of the employee. |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | protected $employee = null; |
| 37 | |
| 38 | /** |
| 39 | * An associative array holding the employee preferences. |
| 40 | * |
| 41 | * @var object |
| 42 | */ |
| 43 | protected $settings = null; |
| 44 | |
| 45 | /** |
| 46 | * Configuration class handler. |
| 47 | * |
| 48 | * @var VAPConfig |
| 49 | */ |
| 50 | protected $config; |
| 51 | |
| 52 | /** |
| 53 | * Class constructor. |
| 54 | * |
| 55 | * @param integer $id The user ID. |
| 56 | * @param VAPConfig $config The config handler. |
| 57 | */ |
| 58 | public function __construct($id, ?VAPConfig $config = null) |
| 59 | { |
| 60 | if (is_null($config)) |
| 61 | { |
| 62 | $this->config = VAPFactory::getConfig(); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | $this->config = $config; |
| 67 | } |
| 68 | |
| 69 | $this->loadEmployee($id); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Provides the instance of the employee auth object, |
| 74 | * only creating it if it doesn't already exist. |
| 75 | * |
| 76 | * @param integer $id The user ID. |
| 77 | * @param VAPConfig $config The config handler. |
| 78 | * |
| 79 | * @return self A new instance. |
| 80 | * |
| 81 | * @since 1.6 |
| 82 | */ |
| 83 | public static function getInstance($id = null, $config = null) |
| 84 | { |
| 85 | if (is_null($id)) |
| 86 | { |
| 87 | $id = JFactory::getUser()->id; |
| 88 | } |
| 89 | |
| 90 | if (!isset(static::$instances[$id])) |
| 91 | { |
| 92 | static::$instances[$id] = new static($id, $config); |
| 93 | } |
| 94 | |
| 95 | return static::$instances[$id]; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Method used to load the details of the employee |
| 100 | * assigned to the specified user ID. |
| 101 | * |
| 102 | * @param integer $user_id The Joomla user ID. |
| 103 | * |
| 104 | * @return void |
| 105 | * |
| 106 | * @since 1.6 |
| 107 | */ |
| 108 | protected function loadEmployee($user_id) |
| 109 | { |
| 110 | if ($user_id <= 0) |
| 111 | { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | $dbo = JFactory::getDbo(); |
| 116 | |
| 117 | $q = $dbo->getQuery(true) |
| 118 | ->select('*') |
| 119 | ->from($dbo->qn('#__vikappointments_employee')) |
| 120 | ->where($dbo->qn('jid') . ' = ' . (int) $user_id); |
| 121 | |
| 122 | $dbo->setQuery($q, 0, 1); |
| 123 | $this->employee = $dbo->loadAssoc(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Magic method to access the properties of the employee. |
| 128 | * |
| 129 | * @param string $name The property name. |
| 130 | * |
| 131 | * @return mixed The property value if exists, otherwise null. |
| 132 | * |
| 133 | * @since 1.6 |
| 134 | */ |
| 135 | public function __get($name) |
| 136 | { |
| 137 | if ($this->isEmployee() && isset($this->employee[$name])) |
| 138 | { |
| 139 | if ($name === 'timezone' && !$this->employee['timezone']) |
| 140 | { |
| 141 | // get system timezone if not specified |
| 142 | return JFactory::getApplication()->get('offset', 'UTC'); |
| 143 | } |
| 144 | |
| 145 | return $this->employee[$name]; |
| 146 | } |
| 147 | |
| 148 | return null; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Checks if the current user is an employee. |
| 153 | * |
| 154 | * @return boolean |
| 155 | * |
| 156 | * @since 1.6 |
| 157 | */ |
| 158 | public function isEmployee() |
| 159 | { |
| 160 | return $this->employee !== null; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns the employee details. |
| 165 | * |
| 166 | * @return mixed An array if exists, otherwise null. |
| 167 | * |
| 168 | * @since 1.6 |
| 169 | */ |
| 170 | public function getEmployee() |
| 171 | { |
| 172 | return $this->employee; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns the settings of the employee. |
| 177 | * |
| 178 | * @return object The employee settings. |
| 179 | * |
| 180 | * @since 1.7 |
| 181 | */ |
| 182 | public function getSettings() |
| 183 | { |
| 184 | if ($this->isEmployee() && is_null($this->settings)) |
| 185 | { |
| 186 | $model = JModelVAP::getInstance('empsettings'); |
| 187 | |
| 188 | // attempt to load existing settings or use the default ones |
| 189 | $this->settings = $model->getItem([ |
| 190 | 'id_employee' => $this->id |
| 191 | ], $blank = true); |
| 192 | } |
| 193 | |
| 194 | return $this->settings; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Checks if an employee can manage its profile. |
| 199 | * |
| 200 | * @return boolean True if allowed, false otherwise. |
| 201 | */ |
| 202 | public function manage() |
| 203 | { |
| 204 | if (!$this->isEmployee()) |
| 205 | { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | // get default configuration value |
| 210 | $setting = $this->config->getBool('empmanage'); |
| 211 | |
| 212 | // allow plugins to override this setting |
| 213 | return (bool) VAPEmployeeAreaManager::override('profile.manage', $setting, $this); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Checks if an employee can create a new service. |
| 218 | * |
| 219 | * @return boolean True if allowed, false otherwise. |
| 220 | * |
| 221 | * @deprecated 1.8 Use createService() instead. |
| 222 | */ |
| 223 | public function create() |
| 224 | { |
| 225 | return $this->createService(); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Checks if an employee can create a new service. |
| 230 | * |
| 231 | * @param boolean $count True to count the created services |
| 232 | * against the maximum threshold. |
| 233 | * |
| 234 | * @return boolean True if allowed, false otherwise. |
| 235 | * |
| 236 | * @since 1.7 |
| 237 | */ |
| 238 | public function createService($count = false) |
| 239 | { |
| 240 | if (!$this->isEmployee()) |
| 241 | { |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | // get default configuration value |
| 246 | $setting = $this->config->getBool('empcreate'); |
| 247 | |
| 248 | // allow plugins to override this setting |
| 249 | $setting = (bool) VAPEmployeeAreaManager::override('service.create', $setting, $this); |
| 250 | |
| 251 | if ($count && $setting) |
| 252 | { |
| 253 | $dbo = JFactory::getDbo(); |
| 254 | |
| 255 | $q = $dbo->getQuery(true) |
| 256 | ->select('COUNT(1)') |
| 257 | ->from($dbo->qn('#__vikappointments_service', 's')) |
| 258 | ->leftjoin($dbo->qn('#__vikappointments_ser_emp_assoc', 'a') . ' ON ' . $dbo->qn('s.id') . ' = ' . $dbo->qn('a.id_service')) |
| 259 | ->where(array( |
| 260 | $dbo->qn('a.id_employee') . ' = ' . $this->id, |
| 261 | // consider only the services created by the employee |
| 262 | $dbo->qn('s.createdby') . ' = ' . $this->jid, |
| 263 | )); |
| 264 | |
| 265 | $dbo->setQuery($q, 0, 1); |
| 266 | $count = (int) $dbo->loadResult(); |
| 267 | |
| 268 | // can create only in case the number of existing services is lower |
| 269 | // than the maximum threshold |
| 270 | $setting = $count < $this->getServicesMaximumNumber(); |
| 271 | } |
| 272 | |
| 273 | return $setting; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Checks if an employee can remove an existing service. |
| 278 | * |
| 279 | * @return boolean True if allowed, false otherwise. |
| 280 | * |
| 281 | * @deprecated 1.8 Use removeService() instead. |
| 282 | */ |
| 283 | public function remove() |
| 284 | { |
| 285 | return $this->removeService(); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Checks if an employee can remove an existing service. |
| 290 | * |
| 291 | * @return boolean True if allowed, false otherwise. |
| 292 | * |
| 293 | * @since 1.7 |
| 294 | */ |
| 295 | public function removeService() |
| 296 | { |
| 297 | if (!$this->isEmployee()) |
| 298 | { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | // get default configuration value |
| 303 | $setting = $this->config->getBool('empremove'); |
| 304 | |
| 305 | // allow plugins to override this setting |
| 306 | return (bool) VAPEmployeeAreaManager::override('service.remove', $setting, $this); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Returns the maximum number of services that an employee |
| 311 | * can own simultaneously. |
| 312 | * |
| 313 | * @return integer The maximum number of services. |
| 314 | */ |
| 315 | public function getServicesMaximumNumber() |
| 316 | { |
| 317 | if (!$this->isEmployee()) |
| 318 | { |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | // get default configuration value |
| 323 | $setting = $this->config->getInt('empmaxser'); |
| 324 | |
| 325 | // allow plugins to override this setting |
| 326 | return (int) VAPEmployeeAreaManager::override('service.max', $setting, $this); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Checks if an employee can update an existing service. |
| 331 | * |
| 332 | * @param mixed $service The service details of the service ID. |
| 333 | * @param boolean $readOnly True to check whether there's a relation. |
| 334 | * |
| 335 | * @return boolean True if allowed, false otherwise. |
| 336 | */ |
| 337 | public function manageServices($service = array(), $readOnly = false) |
| 338 | { |
| 339 | if (!$this->isEmployee()) |
| 340 | { |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | $dbo = JFactory::getDbo(); |
| 345 | |
| 346 | /** |
| 347 | * Only checks whether there's a relation between the specified |
| 348 | * service and the current employee. |
| 349 | * |
| 350 | * @since 1.7 |
| 351 | */ |
| 352 | if ($readOnly) |
| 353 | { |
| 354 | if (!empty($service) && is_scalar($service)) |
| 355 | { |
| 356 | $id_service = $service; |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | $id_service = (array) $service; |
| 361 | $id_service = @$id_service['id']; |
| 362 | } |
| 363 | |
| 364 | $q = $dbo->getQuery(true) |
| 365 | ->select(1) |
| 366 | ->from($dbo->qn('#__vikappointments_ser_emp_assoc')) |
| 367 | ->where($dbo->qn('id_employee') . ' = ' . $this->id) |
| 368 | ->where($dbo->qn('id_service') . ' = ' . (int) $id_service); |
| 369 | |
| 370 | $dbo->setQuery($q, 0, 1); |
| 371 | $dbo->execute(); |
| 372 | |
| 373 | return $dbo->getNumRows(); |
| 374 | } |
| 375 | |
| 376 | if (!empty($service) && is_scalar($service)) |
| 377 | { |
| 378 | // the service is an ID, load it from the database |
| 379 | $q = $dbo->getQuery(true) |
| 380 | ->select($dbo->qn('createdby')) |
| 381 | ->from($dbo->qn('#__vikappointments_service')) |
| 382 | ->where($dbo->qn('id') . ' = ' . (int) $service); |
| 383 | |
| 384 | $dbo->setQuery($q, 0, 1); |
| 385 | $service = $dbo->loadAssoc(); |
| 386 | } |
| 387 | |
| 388 | $service = (array) $service; |
| 389 | |
| 390 | // check if the service has been created by this employee |
| 391 | if (!empty($service['createdby']) && $service['createdby'] == $this->jid) |
| 392 | { |
| 393 | // in this case, we don't need to check the configuration |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | // get default configuration value |
| 398 | $setting = $this->config->getBool('empmanageser'); |
| 399 | |
| 400 | // allow plugins to override this setting |
| 401 | return (bool) VAPEmployeeAreaManager::override('service.manage', $setting, $this); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Checks if the employee can create relationships with global services. |
| 406 | * |
| 407 | * @return boolean True if allowed, false otherwise. |
| 408 | * |
| 409 | * @since 1.6 |
| 410 | */ |
| 411 | public function attachServices() |
| 412 | { |
| 413 | if (!$this->isEmployee()) |
| 414 | { |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | // get default configuration value |
| 419 | $setting = $this->config->getBool('empattachser'); |
| 420 | |
| 421 | // allow plugins to override this setting |
| 422 | return (bool) VAPEmployeeAreaManager::override('service.assign', $setting, $this); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Checks if an employee can update the service rates. |
| 427 | * |
| 428 | * @return boolean True if allowed, false otherwise. |
| 429 | */ |
| 430 | public function manageServicesRates() |
| 431 | { |
| 432 | if (!$this->isEmployee()) |
| 433 | { |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | // get default configuration value |
| 438 | $setting = $this->config->getBool('empmanagerate'); |
| 439 | |
| 440 | // allow plugins to override this setting |
| 441 | return (bool) VAPEmployeeAreaManager::override('service.override', $setting, $this); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Checks if an employee can create, edit and remove payments. |
| 446 | * If the payment ID is provided, checks if the payment is |
| 447 | * owned by the current employee. |
| 448 | * |
| 449 | * @param integer $id The payment ID. |
| 450 | * |
| 451 | * @return boolean True if allowed, false otherwise. |
| 452 | */ |
| 453 | public function managePayments($id = 0) |
| 454 | { |
| 455 | if (!$this->isEmployee()) |
| 456 | { |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | // check if the employee is the owner |
| 461 | if ($id > 0) |
| 462 | { |
| 463 | $dbo = JFactory::getDbo(); |
| 464 | |
| 465 | $q = $dbo->getQuery(true) |
| 466 | ->select(1) |
| 467 | ->from($dbo->qn('#__vikappointments_gpayments')) |
| 468 | ->where(array( |
| 469 | $dbo->qn('id_employee') . ' = ' . $this->id, |
| 470 | $dbo->qn('id') . ' = ' . (int) $id, |
| 471 | )); |
| 472 | |
| 473 | $dbo->setQuery($q, 0, 1); |
| 474 | $dbo->execute(); |
| 475 | |
| 476 | if (!$dbo->getNumRows()) |
| 477 | { |
| 478 | return false; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | // get default configuration value |
| 483 | $setting = $this->config->getBool('empmanagepay'); |
| 484 | |
| 485 | // allow plugins to override this setting |
| 486 | return (bool) VAPEmployeeAreaManager::override('payment.manage', $setting, $this); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Checks if an employee can create, edit and remove coupons. |
| 491 | * If the coupon ID is provided, checks if the coupon is |
| 492 | * owned by the current employee. |
| 493 | * |
| 494 | * @param integer $id The coupon ID. |
| 495 | * |
| 496 | * @return boolean True if allowed, false otherwise. |
| 497 | */ |
| 498 | public function manageCoupons($id = 0) |
| 499 | { |
| 500 | if (!$this->isEmployee()) |
| 501 | { |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | // check if the employee is the owner |
| 506 | if ($id > 0) |
| 507 | { |
| 508 | $dbo = JFactory::getDbo(); |
| 509 | |
| 510 | $q = $dbo->getQuery(true) |
| 511 | ->select(1) |
| 512 | ->from($dbo->qn('#__vikappointments_coupon_employee_assoc')) |
| 513 | ->where(array( |
| 514 | $dbo->qn('id_employee') . ' = ' . $this->id, |
| 515 | $dbo->qn('id_coupon') . ' = ' . (int) $id, |
| 516 | )); |
| 517 | |
| 518 | $dbo->setQuery($q, 0, 1); |
| 519 | $dbo->execute(); |
| 520 | |
| 521 | if (!$dbo->getNumRows()) |
| 522 | { |
| 523 | return false; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // get default configuration value |
| 528 | $setting = $this->config->getBool('empmanagecoupon'); |
| 529 | |
| 530 | // allow plugins to override this setting |
| 531 | return (bool) VAPEmployeeAreaManager::override('coupon.manage', $setting, $this); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Checks if an employee can create, edit and remove custom fields. |
| 536 | * If the custom field ID is provided, checks if it is |
| 537 | * owned by the current employee. |
| 538 | * |
| 539 | * @param integer $id The field ID. |
| 540 | * |
| 541 | * @return boolean True if allowed, false otherwise. |
| 542 | */ |
| 543 | public function manageCustomFields($id = 0) |
| 544 | { |
| 545 | if (!$this->isEmployee()) |
| 546 | { |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | // check if the employee is the owner |
| 551 | if ($id > 0) |
| 552 | { |
| 553 | $dbo = JFactory::getDbo(); |
| 554 | |
| 555 | $q = $dbo->getQuery(true) |
| 556 | ->select(1) |
| 557 | ->from($dbo->qn('#__vikappointments_custfields')) |
| 558 | ->where(array( |
| 559 | $dbo->qn('id_employee') . ' = ' . $this->id, |
| 560 | $dbo->qn('id') . ' = ' . (int) $id, |
| 561 | )); |
| 562 | |
| 563 | $dbo->setQuery($q, 0, 1); |
| 564 | $dbo->execute(); |
| 565 | |
| 566 | if (!$dbo->getNumRows()) |
| 567 | { |
| 568 | return false; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // get default configuration value |
| 573 | $setting = $this->config->getBool('empmanagecustfield'); |
| 574 | |
| 575 | // allow plugins to override this setting |
| 576 | return (bool) VAPEmployeeAreaManager::override('field.manage', $setting, $this); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * Checks if an employee can create, edit and remove working days. |
| 581 | * |
| 582 | * @param integer $id The working day ID. |
| 583 | * |
| 584 | * @return boolean True if allowed, false otherwise. |
| 585 | */ |
| 586 | public function manageWorkDays($id = 0) |
| 587 | { |
| 588 | if (!$this->isEmployee()) |
| 589 | { |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | // check if the employee is the owner |
| 594 | if ($id > 0) |
| 595 | { |
| 596 | $dbo = JFactory::getDbo(); |
| 597 | |
| 598 | $q = $dbo->getQuery(true) |
| 599 | ->select(1) |
| 600 | ->from($dbo->qn('#__vikappointments_emp_worktime')) |
| 601 | ->where(array( |
| 602 | $dbo->qn('id_employee') . ' = ' . $this->id, |
| 603 | $dbo->qn('id') . ' = ' . (int) $id, |
| 604 | )); |
| 605 | |
| 606 | $dbo->setQuery($q, 0, 1); |
| 607 | $dbo->execute(); |
| 608 | |
| 609 | if (!$dbo->getNumRows()) |
| 610 | { |
| 611 | return false; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // get default configuration value |
| 616 | $setting = $this->config->getBool('empmanagewd'); |
| 617 | |
| 618 | // allow plugins to override this setting |
| 619 | return (bool) VAPEmployeeAreaManager::override('worktime.manage', $setting, $this); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Checks if an employee can create, edit and remove locations. |
| 624 | * If the location ID is provided, checks if the location is |
| 625 | * owned by the current employee. |
| 626 | * |
| 627 | * @param integer $id The location ID. |
| 628 | * @param boolean $readOnly True to include also the global locations. |
| 629 | * |
| 630 | * @return boolean True if allowed, false otherwise. |
| 631 | */ |
| 632 | public function manageLocations($id = 0, $readOnly = false) |
| 633 | { |
| 634 | if (!$this->isEmployee()) |
| 635 | { |
| 636 | return false; |
| 637 | } |
| 638 | |
| 639 | // check if the employee is the owner |
| 640 | if ($id > 0) |
| 641 | { |
| 642 | $dbo = JFactory::getDbo(); |
| 643 | |
| 644 | $q = $dbo->getQuery(true) |
| 645 | ->select(1) |
| 646 | ->from($dbo->qn('#__vikappointments_employee_location')) |
| 647 | ->where($dbo->qn('id') . ' = ' . (int) $id); |
| 648 | |
| 649 | if ($readOnly) |
| 650 | { |
| 651 | $q->andWhere(array( |
| 652 | $dbo->qn('id_employee') . ' = ' . $this->id, |
| 653 | $dbo->qn('id_employee') . ' <= 0', |
| 654 | )); |
| 655 | } |
| 656 | else |
| 657 | { |
| 658 | $q->where($dbo->qn('id_employee') . ' = ' . $this->id); |
| 659 | } |
| 660 | |
| 661 | $dbo->setQuery($q, 0, 1); |
| 662 | $dbo->execute(); |
| 663 | |
| 664 | if (!$dbo->getNumRows()) |
| 665 | { |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * In case of read only, immediately return true without |
| 671 | * checking whether the employee is allowed to manage the |
| 672 | * locations. |
| 673 | * |
| 674 | * @since 1.7.2 |
| 675 | */ |
| 676 | if ($readOnly) |
| 677 | { |
| 678 | return true; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | // get default configuration value |
| 683 | $setting = $this->config->getBool('empmanageloc'); |
| 684 | |
| 685 | // allow plugins to override this setting |
| 686 | return (bool) VAPEmployeeAreaManager::override('location.manage', $setting, $this); |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Checks if an employee can create new reservations. |
| 691 | * |
| 692 | * @return boolean True if allowed, false otherwise. |
| 693 | * |
| 694 | * @deprecated 1.8 Use createReservation() instead. |
| 695 | */ |
| 696 | public function rescreate() |
| 697 | { |
| 698 | return $this->createReservation(); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Checks if an employee can create new reservations. |
| 703 | * |
| 704 | * @return boolean True if allowed, false otherwise. |
| 705 | * |
| 706 | * @since 1.7 |
| 707 | */ |
| 708 | public function createReservation() |
| 709 | { |
| 710 | if (!$this->isEmployee()) |
| 711 | { |
| 712 | return false; |
| 713 | } |
| 714 | |
| 715 | // get default configuration value |
| 716 | $setting = $this->config->getBool('emprescreate'); |
| 717 | |
| 718 | // allow plugins to override this setting |
| 719 | return (bool) VAPEmployeeAreaManager::override('reservation.create', $setting, $this); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * Checks if an employee can update existing reservations. |
| 724 | * If the reservation ID is provided, checks if the reservation is |
| 725 | * owned by the current employee. |
| 726 | * |
| 727 | * @param integer $id The reservation ID. |
| 728 | * |
| 729 | * @return boolean True if allowed, false otherwise. |
| 730 | * |
| 731 | * @deprecated 1.8 Use manageReservation() instead. |
| 732 | */ |
| 733 | public function resmanage($id = null) |
| 734 | { |
| 735 | return $this->manageReservation($id); |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Checks if an employee can update existing reservations. |
| 740 | * If the reservation ID is provided, checks if the reservation is |
| 741 | * owned by the current employee. |
| 742 | * |
| 743 | * @param integer $id The reservation ID. |
| 744 | * @param boolean $readOnly True to check whether there's a relation. |
| 745 | * |
| 746 | * @return boolean True if allowed, false otherwise. |
| 747 | * |
| 748 | * @since 1.7 |
| 749 | */ |
| 750 | public function manageReservation($id = null, $readOnly = false) |
| 751 | { |
| 752 | if (!$this->isEmployee()) |
| 753 | { |
| 754 | return false; |
| 755 | } |
| 756 | |
| 757 | // check if the employee is the owner |
| 758 | if ($id > 0) |
| 759 | { |
| 760 | $dbo = JFactory::getDbo(); |
| 761 | |
| 762 | $q = $dbo->getQuery(true) |
| 763 | ->select(1) |
| 764 | ->from($dbo->qn('#__vikappointments_reservation')) |
| 765 | ->where(array( |
| 766 | $dbo->qn('id_employee') . ' = ' . $this->id, |
| 767 | $dbo->qn('id') . ' = ' . (int) $id, |
| 768 | )); |
| 769 | |
| 770 | $dbo->setQuery($q, 0, 1); |
| 771 | $dbo->execute(); |
| 772 | |
| 773 | if (!$dbo->getNumRows()) |
| 774 | { |
| 775 | return false; |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * In case of read only, immediately return true without |
| 780 | * checking whether the employee is allowed to manage the |
| 781 | * appointments. |
| 782 | * |
| 783 | * @since 1.7 |
| 784 | */ |
| 785 | if ($readOnly) |
| 786 | { |
| 787 | return true; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | // get default configuration value |
| 792 | $setting = $this->config->getBool('empresmanage'); |
| 793 | |
| 794 | // allow plugins to override this setting |
| 795 | return (bool) VAPEmployeeAreaManager::override('reservation.manage', $setting, $this); |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * Checks if an employee can confirm existing reservations. |
| 800 | * If the reservation ID is provided, checks if the reservation is |
| 801 | * owned by the current employee. |
| 802 | * |
| 803 | * @param integer $id The reservation ID. |
| 804 | * |
| 805 | * @return boolean True if allowed, false otherwise. |
| 806 | * |
| 807 | * @since 1.6 |
| 808 | * @deprecated 1.8 Use confirmReservation() instead. |
| 809 | */ |
| 810 | public function resconfirm($id = null) |
| 811 | { |
| 812 | return $this->confirmReservation($id); |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * Checks if an employee can confirm existing reservations. |
| 817 | * If the reservation ID is provided, checks if the reservation is |
| 818 | * owned by the current employee. |
| 819 | * |
| 820 | * @param integer $id The reservation ID. |
| 821 | * |
| 822 | * @return boolean True if allowed, false otherwise. |
| 823 | * |
| 824 | * @since 1.7 |
| 825 | */ |
| 826 | public function confirmReservation($id = null) |
| 827 | { |
| 828 | if (!$this->isEmployee()) |
| 829 | { |
| 830 | return false; |
| 831 | } |
| 832 | |
| 833 | $status = null; |
| 834 | |
| 835 | // check if the employee is the owner |
| 836 | if ($id > 0) |
| 837 | { |
| 838 | $dbo = JFactory::getDbo(); |
| 839 | |
| 840 | $q = $dbo->getQuery(true) |
| 841 | ->select($dbo->qn('status')) |
| 842 | ->from($dbo->qn('#__vikappointments_reservation')) |
| 843 | ->where(array( |
| 844 | $dbo->qn('id_employee') . ' = ' . $this->id, |
| 845 | $dbo->qn('id') . ' = ' . (int) $id, |
| 846 | )); |
| 847 | |
| 848 | $dbo->setQuery($q, 0, 1); |
| 849 | |
| 850 | // get the reservation status |
| 851 | $status = $dbo->loadResult(); |
| 852 | |
| 853 | if (!$status) |
| 854 | { |
| 855 | return false; |
| 856 | } |
| 857 | |
| 858 | // reservations that are already confirmed are always allowed, |
| 859 | // even if the confirmation rule is turned off |
| 860 | if (JHtml::fetch('vaphtml.status.isapproved', 'appointments', $status)) |
| 861 | { |
| 862 | return true; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | // get default configuration value |
| 867 | $setting = $this->config->getBool('empresconfirm'); |
| 868 | |
| 869 | // allow plugins to override this setting |
| 870 | return (bool) VAPEmployeeAreaManager::override('reservation.confirm', $setting, $this); |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * Checks if an employee can remove existing reservations. |
| 875 | * |
| 876 | * @return boolean True if allowed, false otherwise. |
| 877 | * |
| 878 | * @deprecated 1.8 Use removeReservation() instead. |
| 879 | */ |
| 880 | public function resremove() |
| 881 | { |
| 882 | return $this->removeReservation(); |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * Checks if an employee can remove existing reservations. |
| 887 | * |
| 888 | * @return boolean True if allowed, false otherwise. |
| 889 | * |
| 890 | * @since 1.7 |
| 891 | */ |
| 892 | public function removeReservation() |
| 893 | { |
| 894 | if (!$this->isEmployee()) |
| 895 | { |
| 896 | return false; |
| 897 | } |
| 898 | |
| 899 | // get default configuration value |
| 900 | $setting = $this->config->getBool('empresremove'); |
| 901 | |
| 902 | // allow plugins to override this setting |
| 903 | return (bool) VAPEmployeeAreaManager::override('reservation.remove', $setting, $this); |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Checks if the administrator should be notified every |
| 908 | * time an employee removes a reservation. |
| 909 | * |
| 910 | * @return boolean True if allowed, false otherwise. |
| 911 | */ |
| 912 | public function isNotifyOnReservationDelete() |
| 913 | { |
| 914 | if (!$this->isEmployee()) |
| 915 | { |
| 916 | return false; |
| 917 | } |
| 918 | |
| 919 | // get default configuration value |
| 920 | $setting = $this->config->getBool('empresnotify'); |
| 921 | |
| 922 | // allow plugins to override this setting |
| 923 | return (bool) VAPEmployeeAreaManager::override('reservation.remove.notify', $setting, $this); |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * Helper method used to obtain all the required custom fields that haven't been filled |
| 928 | * in by the currently logged-in employee. |
| 929 | * |
| 930 | * @param array|null $fields The list of custom fields to compare against. |
| 931 | * |
| 932 | * @return string[] A list of missing custom fields (form field names). |
| 933 | * |
| 934 | * @since 1.7.6 |
| 935 | */ |
| 936 | public function getMissingRequiredFields($fields = null) |
| 937 | { |
| 938 | if ($fields === null) |
| 939 | { |
| 940 | // auto-load the custom fields |
| 941 | VAPLoader::import('libraries.customfields.loader'); |
| 942 | |
| 943 | // get relevant custom fields only |
| 944 | $fields = VAPCustomFieldsLoader::getInstance() |
| 945 | ->employees() |
| 946 | ->noRequiredCheckbox() |
| 947 | ->noSeparator() |
| 948 | ->fetch(); |
| 949 | } |
| 950 | |
| 951 | $missing = []; |
| 952 | |
| 953 | foreach ($fields as $field) |
| 954 | { |
| 955 | if (empty($field['required'])) |
| 956 | { |
| 957 | // the field is not mandatory, go ahead |
| 958 | continue; |
| 959 | } |
| 960 | |
| 961 | // get currently specified value |
| 962 | $value = $this->employee['field_' . $field['formname']] ?? null; |
| 963 | |
| 964 | if ($value === null || $value === '') |
| 965 | { |
| 966 | // inject form name within the list of missing fields |
| 967 | $missing[] = $field['formname']; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | return $missing; |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * Checks if a user can register a new account. |
| 976 | * |
| 977 | * @return boolean True if allowed, false otherwise. |
| 978 | * |
| 979 | * @deprecated 1.8 Use VAPEmployeeAreaManager::canRegister() instead. |
| 980 | */ |
| 981 | public function register() |
| 982 | { |
| 983 | return VAPEmployeeAreaManager::canRegister(); |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * Returns the default status of an employee after its registration. |
| 988 | * |
| 989 | * @return string The default status. |
| 990 | * |
| 991 | * @deprecated 1.8 Use VAPEmployeeAreaManager::getSignUpStatus() instead. |
| 992 | */ |
| 993 | public function getSignUpStatus() |
| 994 | { |
| 995 | return VAPEmployeeAreaManager::getSignUpStatus(); |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * Returns the default user group assigned to the employee. |
| 1000 | * |
| 1001 | * @return integer The default user group. |
| 1002 | * |
| 1003 | * @deprecated 1.8 Use VAPEmployeeAreaManager::getSignUpUserGroup() instead. |
| 1004 | */ |
| 1005 | public function getSignUpUserGroup() |
| 1006 | { |
| 1007 | return VAPEmployeeAreaManager::getSignUpUserGroup(); |
| 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * Returns the list of all the services to auto-assign to the employee. |
| 1012 | * |
| 1013 | * @return array The default assigned services. |
| 1014 | * |
| 1015 | * @deprecated 1.8 Use VAPEmployeeAreaManager::getServicesToAssign() instead. |
| 1016 | */ |
| 1017 | public function getServicesToAssign() |
| 1018 | { |
| 1019 | return VAPEmployeeAreaManager::getServicesToAssign(); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * Keep support for the old class name until it gets definitively removed. |
| 1025 | * |
| 1026 | * @deprecated 1.8 Use VAPEmployeeAuth instead. |
| 1027 | */ |
| 1028 | if (!class_exists('EmployeeAuth')) |
| 1029 | { |
| 1030 | class_alias('VAPEmployeeAuth', 'EmployeeAuth'); |
| 1031 | } |
| 1032 |