helper.php
104 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage mod_vikappointments_services |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2023 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 to this file |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | VAPLoader::import('libraries.helpers.module'); |
| 15 | |
| 16 | /** |
| 17 | * Helper class used by the Services module. |
| 18 | * |
| 19 | * @since 1.2 |
| 20 | */ |
| 21 | class VikAppointmentsServicesModuleHelper |
| 22 | { |
| 23 | /** |
| 24 | * Use methods defined by modules trait for a better reusability. |
| 25 | * |
| 26 | * @see VAPModuleHelper |
| 27 | * |
| 28 | * @since 1.4 |
| 29 | */ |
| 30 | use VAPModuleHelper; |
| 31 | |
| 32 | /** |
| 33 | * Returns the list of services that should be displayed |
| 34 | * depending on the configuration of the module. |
| 35 | * |
| 36 | * @param JRegistry $params The configuration registry. |
| 37 | * |
| 38 | * @return array The services list. |
| 39 | */ |
| 40 | public static function getServices($params) |
| 41 | { |
| 42 | $dbo = JFactory::getDbo(); |
| 43 | $user = JFactory::getUser(); |
| 44 | |
| 45 | $group_filter = $params->get('groupfilter'); |
| 46 | $service_filter = $params->get('servicefilter'); |
| 47 | |
| 48 | $services = array(); |
| 49 | |
| 50 | $q = $dbo->getQuery(true) |
| 51 | ->select($dbo->qn(array('id', 'name', 'description', 'duration', 'price', 'image'))) |
| 52 | ->from($dbo->qn('#__vikappointments_service')) |
| 53 | ->where($dbo->qn('published') . ' = 1') |
| 54 | ->order($dbo->qn('ordering') . ' ASC'); |
| 55 | |
| 56 | if ($group_filter) |
| 57 | { |
| 58 | $count = count($group_filter); |
| 59 | |
| 60 | if ($count == 1) |
| 61 | { |
| 62 | $q->where($dbo->qn('id_group') . ' = ' . (int) $group_filter[0]); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | $q->where($dbo->qn('id_group') . ' IN (' . implode(', ', array_map('intval', $group_filter)) . ')'); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if ($service_filter) |
| 71 | { |
| 72 | $count = count($service_filter); |
| 73 | |
| 74 | if ($count == 1) |
| 75 | { |
| 76 | $q->where($dbo->qn('id') . ' = ' . (int) $service_filter[0]); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | $q->where($dbo->qn('id') . ' IN (' . implode(', ', array_map('intval', $service_filter)) . ')'); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // retrieve only the services that belong to the view |
| 85 | // access level of the current user |
| 86 | $levels = $user->getAuthorisedViewLevels(); |
| 87 | |
| 88 | if ($levels) |
| 89 | { |
| 90 | $q->where($dbo->qn('level') . ' IN (' . implode(', ', $levels) . ')'); |
| 91 | } |
| 92 | |
| 93 | $dbo->setQuery($q); |
| 94 | $services = $dbo->loadAssocList(); |
| 95 | |
| 96 | if ($services) |
| 97 | { |
| 98 | VikAppointments::translateServices($services); |
| 99 | } |
| 100 | |
| 101 | return $services; |
| 102 | } |
| 103 | } |
| 104 |