helper.php
268 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage mod_vikappointments_search |
| 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 Search module. |
| 18 | * |
| 19 | * @since 1.3 |
| 20 | */ |
| 21 | class VikAppointmentsSearchHelper |
| 22 | { |
| 23 | /** |
| 24 | * Use methods defined by modules trait for a better reusability. |
| 25 | * |
| 26 | * @see VAPModuleHelper |
| 27 | * |
| 28 | * @since 1.5 |
| 29 | */ |
| 30 | use VAPModuleHelper; |
| 31 | |
| 32 | /** |
| 33 | * Returns an associative array containing the data set |
| 34 | * in the request. |
| 35 | * |
| 36 | * @param JRegistry $params The module parameters (@since 1.6). |
| 37 | * |
| 38 | * @return array The data array. |
| 39 | */ |
| 40 | public static function getViewHtmlReferences($params) |
| 41 | { |
| 42 | $app = JFactory::getApplication(); |
| 43 | $dbo = JFactory::getDbo(); |
| 44 | $user = JFactory::getUser(); |
| 45 | $config = VAPFactory::getConfig(); |
| 46 | |
| 47 | $data = array( |
| 48 | 'date' => $app->input->getString('date', null), |
| 49 | 'id_ser' => $app->input->getUint('id_ser', 0), |
| 50 | 'id_emp' => $app->input->getUint('id_emp', 0), |
| 51 | ); |
| 52 | |
| 53 | // get user timezone |
| 54 | $tz = VikAppointments::getUserTimezone()->getName(); |
| 55 | |
| 56 | if (VAPDateHelper::isNull($data['date'])) |
| 57 | { |
| 58 | // use the current date when not specified |
| 59 | $data['date'] = JHtml::fetch('date', 'now', $config->get('dateformat'), $tz); |
| 60 | } |
| 61 | |
| 62 | $max_sys_date = static::getMaxTimeStamp(); |
| 63 | |
| 64 | // get services |
| 65 | |
| 66 | $group_ids = $service_ids = array(); |
| 67 | |
| 68 | $services = array(); |
| 69 | |
| 70 | $q = $dbo->getQuery(true) |
| 71 | ->select($dbo->qn(array('s.id', 's.name', 's.choose_emp', 's.random_emp', 's.id_group', 'mindate', 'maxdate'))) |
| 72 | ->select($dbo->qn('g.id', 'gid')) |
| 73 | ->select($dbo->qn('g.name', 'gname')) |
| 74 | ->from($dbo->qn('#__vikappointments_service', 's')) |
| 75 | ->leftjoin($dbo->qn('#__vikappointments_group', 'g') . ' ON ' . $dbo->qn('s.id_group') . ' = ' . $dbo->qn('g.id')) |
| 76 | ->where($dbo->qn('s.published') . ' = 1') |
| 77 | ->order(array( |
| 78 | $dbo->qn('g.ordering') . ' ASC', |
| 79 | $dbo->qn('s.ordering') . ' ASC', |
| 80 | $dbo->qn('s.name') . ' ASC', |
| 81 | )); |
| 82 | |
| 83 | // retrieve only the services that belong to the view |
| 84 | // access level of the current user |
| 85 | $levels = $user->getAuthorisedViewLevels(); |
| 86 | |
| 87 | if ($levels) |
| 88 | { |
| 89 | $q->where($dbo->qn('s.level') . ' IN (' . implode(', ', $levels) . ')'); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Take only the services assigned to the selected employee. |
| 94 | * |
| 95 | * @since 1.6 |
| 96 | */ |
| 97 | if ($employeeId = $params->get('id_employee')) |
| 98 | { |
| 99 | $q->innerjoin($dbo->qn('#__vikappointments_ser_emp_assoc', 'a') . ' ON ' . $dbo->qn('a.id_service') . ' = ' . $dbo->qn('s.id')); |
| 100 | $q->where($dbo->qn('a.id_employee') . ' = ' . (int) $employeeId); |
| 101 | } |
| 102 | |
| 103 | $dbo->setQuery($q); |
| 104 | |
| 105 | foreach ($dbo->loadAssocList() as $r) |
| 106 | { |
| 107 | if (!isset($services[$r['id_group']])) |
| 108 | { |
| 109 | $services[$r['id_group']] = array( |
| 110 | 'id' => $r['gid'], |
| 111 | 'name' => $r['gname'], |
| 112 | 'list' => array(), |
| 113 | ); |
| 114 | |
| 115 | if ($r['id_group']) |
| 116 | { |
| 117 | $group_ids[] = $r['id_group']; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if ($r['maxdate'] == -1) |
| 122 | { |
| 123 | // get maximum number of days from configuration |
| 124 | $r['maxdate'] = $config->getUint('maxdate'); |
| 125 | } |
| 126 | |
| 127 | if ($r['maxdate'] > 0) |
| 128 | { |
| 129 | $modifier = '+' . $r['maxdate'] . ' days'; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | // use the system maximum date |
| 134 | $modifier = $max_sys_date; |
| 135 | } |
| 136 | |
| 137 | // calculate the maximum date |
| 138 | $r['maxdate'] = JHtml::fetch('date', $modifier, 'Y-m-d\T00:00:00', $tz); |
| 139 | |
| 140 | if ($r['mindate'] == -1) |
| 141 | { |
| 142 | // get minimum number of days from configuration |
| 143 | $r['mindate'] = $config->getUint('mindate'); |
| 144 | } |
| 145 | |
| 146 | if ($r['mindate'] > 0) |
| 147 | { |
| 148 | $modifier = '+' . $r['mindate'] . ' days'; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | // use the current date |
| 153 | $modifier = 'now'; |
| 154 | } |
| 155 | |
| 156 | // calculate the minimum date |
| 157 | $r['mindate'] = JHtml::fetch('date', $modifier, 'Y-m-d\T00:00:00', $tz); |
| 158 | |
| 159 | $services[$r['id_group']]['list'][] = array( |
| 160 | 'id' => $r['id'], |
| 161 | 'name' => $r['name'], |
| 162 | 'choose_emp' => $r['choose_emp'], |
| 163 | 'random_emp' => $r['random_emp'], |
| 164 | 'id_group' => $r['id_group'], |
| 165 | 'mindate' => $r['mindate'], |
| 166 | 'maxdate' => $r['maxdate'], |
| 167 | ); |
| 168 | |
| 169 | $service_ids[] = $r['id']; |
| 170 | } |
| 171 | |
| 172 | $translator = VAPFactory::getTranslator(); |
| 173 | |
| 174 | $lang = JFactory::getLanguage()->getTag(); |
| 175 | |
| 176 | // preload translations |
| 177 | $serLang = $translator->load('service', array_unique($service_ids), $lang); |
| 178 | $grpLang = $translator->load('group', array_unique($group_ids), $lang); |
| 179 | |
| 180 | foreach ($services as $id_group => $group) |
| 181 | { |
| 182 | // translate record for the given language |
| 183 | $tx = $grpLang->getTranslation($id_group, $lang); |
| 184 | |
| 185 | if ($tx) |
| 186 | { |
| 187 | $group['name'] = $tx->name; |
| 188 | } |
| 189 | |
| 190 | foreach ($group['list'] as $i => $service) |
| 191 | { |
| 192 | // translate record for the given language |
| 193 | $tx = $serLang->getTranslation($service['id'], $lang); |
| 194 | |
| 195 | if ($tx) |
| 196 | { |
| 197 | $group['list'][$i]['name'] = $tx->name; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // update group |
| 202 | $services[$id_group] = $group; |
| 203 | } |
| 204 | |
| 205 | // always append services without group at the end of the list |
| 206 | if (isset($services[0])) |
| 207 | { |
| 208 | $tmp = $services[0]; |
| 209 | unset($services[0]); |
| 210 | $services[0] = $tmp; |
| 211 | } |
| 212 | |
| 213 | $group = reset($services); |
| 214 | |
| 215 | // fetch the first available service when not specified |
| 216 | if (!$data['id_ser'] && $services && $group['list']) |
| 217 | { |
| 218 | $data['id_ser'] = $group['list'][0]['id']; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Include the folder containing all the VikAppointments models for the site client. |
| 223 | * |
| 224 | * @since 1.5.2 |
| 225 | */ |
| 226 | JModelLegacy::addIncludePath(VAPBASE . DIRECTORY_SEPARATOR . 'models'); |
| 227 | |
| 228 | /** |
| 229 | * Take only the employees that should be listed. |
| 230 | * |
| 231 | * @since 1.4.3 |
| 232 | */ |
| 233 | $employees = JModelVAP::getInstance('service')->getEmployees($data['id_ser'], $strict = true); |
| 234 | |
| 235 | if ($employees) |
| 236 | { |
| 237 | // convert to array for backward compatibility |
| 238 | $employees = array_map(function($elem) |
| 239 | { |
| 240 | return (array) $elem; |
| 241 | }, $employees); |
| 242 | |
| 243 | VikAppointments::translateEmployees($employees, $lang); |
| 244 | } |
| 245 | |
| 246 | return array( |
| 247 | 'lastValues' => $data, |
| 248 | 'services' => $services, |
| 249 | 'employees' => $employees, |
| 250 | ); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Returns the maximum timestamp that can be used within the |
| 255 | * jQuery Datepicker. |
| 256 | * |
| 257 | * @return JDate The maximum date threshold. |
| 258 | */ |
| 259 | public static function getMaxTimeStamp() |
| 260 | { |
| 261 | $date = JFactory::getDate(); |
| 262 | $date->modify('+' . VAPFactory::getConfig()->getUint('numcals') . ' months'); |
| 263 | $date->modify($date->format('Y') . '-' . $date->format('m') . '-' . $date->format('t')); |
| 264 | |
| 265 | return $date; |
| 266 | } |
| 267 | } |
| 268 |