1_7
5 days ago
1_7_2
5 days ago
1_6.php
5 days ago
1_6_4.php
5 days ago
1_7.php
5 days ago
1_7_2.php
5 days ago
index.html
5 days ago
1_6.php
362 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 | * Update adapter for com_vikappointments 1.6 version. |
| 16 | * |
| 17 | * This class can include update() and finalise(). |
| 18 | * |
| 19 | * NOTE. do not call exit() or die() because the update won't be finalised correctly. |
| 20 | * Return false instead to stop in anytime the flow without errors. |
| 21 | * |
| 22 | * @since 1.6 |
| 23 | */ |
| 24 | abstract class VAPUpdateAdapter1_6 |
| 25 | { |
| 26 | /** |
| 27 | * Method run during update process. |
| 28 | * |
| 29 | * @param object $parent The parent that calls this method. |
| 30 | * |
| 31 | * @return boolean True on success, otherwise false to stop the flow. |
| 32 | */ |
| 33 | public static function update($parent) |
| 34 | { |
| 35 | try |
| 36 | { |
| 37 | // define alias for services |
| 38 | self::defineRecordAlias('service', 'name'); |
| 39 | // define alias for language services |
| 40 | self::defineRecordAlias('lang_service', 'name'); |
| 41 | // define alias for employees |
| 42 | self::defineRecordAlias('employee', 'nickname'); |
| 43 | // define alias for language employees |
| 44 | self::defineRecordAlias('lang_employee', 'nickname'); |
| 45 | |
| 46 | // insert new ordering modes for employees list |
| 47 | self::addEmployeesOrderingModes(); |
| 48 | |
| 49 | // fix custom fields |
| 50 | self::fixCustomFields($parent); |
| 51 | |
| 52 | // move the employee payments within the global DB table |
| 53 | self::fixEmployeePayments(); |
| 54 | |
| 55 | // find working days parent |
| 56 | self::fixWorkingDays(); |
| 57 | } |
| 58 | catch (Exception $e) |
| 59 | { |
| 60 | JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error'); |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Method run during postflight process. |
| 70 | * |
| 71 | * @param object $parent The parent that calls this method. |
| 72 | * |
| 73 | * @return boolean True on success, otherwise false to stop the flow. |
| 74 | */ |
| 75 | public static function finalise($parent) |
| 76 | { |
| 77 | try |
| 78 | { |
| 79 | // fix router configuration |
| 80 | self::fixRouter($parent); |
| 81 | |
| 82 | // drop employee payments DB table |
| 83 | self::dropEmployeePayments(); |
| 84 | } |
| 85 | catch (Exception $e) |
| 86 | { |
| 87 | JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error'); |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates an alias for the records that belong to the specified type. |
| 97 | * |
| 98 | * @param string $type The entity type. |
| 99 | * @param string $name_col The column containing the record name. |
| 100 | * |
| 101 | * @param boolean True on success, false otherwise. |
| 102 | */ |
| 103 | protected static function defineRecordAlias($type, $name_col) |
| 104 | { |
| 105 | VAPLoader::import('libraries.helpers.alias'); |
| 106 | |
| 107 | $dbo = JFactory::getDbo(); |
| 108 | |
| 109 | $tables = array(); |
| 110 | |
| 111 | $q = $dbo->getQuery(true) |
| 112 | ->select($dbo->qn(array('id', $name_col, 'alias'))) |
| 113 | ->from($dbo->qn('#__vikappointments_' . $type)); |
| 114 | |
| 115 | $dbo->setQuery($q); |
| 116 | $dbo->execute(); |
| 117 | |
| 118 | if (!$dbo->getNumRows()) |
| 119 | { |
| 120 | // no records to fetch |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | $records = $dbo->loadObjectList(); |
| 125 | |
| 126 | foreach ($records as $obj) |
| 127 | { |
| 128 | // use only allowed chars and make sure it is unique |
| 129 | $obj->alias = AliasHelper::getUniqueAlias($obj->{$name_col}, $type); |
| 130 | |
| 131 | $dbo->updateObject('#__vikappointments_' . $type, $obj, 'id'); |
| 132 | } |
| 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Inserts new employees ordering modes: |
| 139 | * - price low to high (7) |
| 140 | * - price high to low (8) |
| 141 | * |
| 142 | * @return boolean True on success, false otherwise. |
| 143 | */ |
| 144 | protected static function addEmployeesOrderingModes() |
| 145 | { |
| 146 | $config = VAPFactory::getConfig(); |
| 147 | $modes = $config->getArray('emplistmode'); |
| 148 | |
| 149 | $modes[7] = 1; |
| 150 | $modes[8] = 1; |
| 151 | |
| 152 | $config->set('emplistmode', $modes); |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Fixes the existing custom fields to support |
| 159 | * the new DB structure. |
| 160 | * |
| 161 | * @param object $parent The parent that calls this method. |
| 162 | * |
| 163 | * @return boolean True on success, false otherwise. |
| 164 | */ |
| 165 | protected static function fixCustomFields($parent) |
| 166 | { |
| 167 | // check if the preflight method (script.php) was |
| 168 | // able to retrieve the existing custom fields |
| 169 | if (!isset($parent->customFields)) |
| 170 | { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | $dbo = JFactory::getDbo(); |
| 175 | |
| 176 | // iterate the custom fields |
| 177 | foreach ($parent->customFields as $field) |
| 178 | { |
| 179 | // find the rule to use |
| 180 | if ($field->isnominative) |
| 181 | { |
| 182 | $field->rule = 1; |
| 183 | } |
| 184 | else if ($field->isemail) |
| 185 | { |
| 186 | $field->rule = 2; |
| 187 | } |
| 188 | else if ($field->isphone) |
| 189 | { |
| 190 | $field->rule = 3; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | $field->rule = 0; |
| 195 | } |
| 196 | |
| 197 | // update only the rule found |
| 198 | $q = $dbo->getQuery(true) |
| 199 | ->update('#__vikappointments_custfields') |
| 200 | ->set($dbo->qn('rule') . ' = ' . $field->rule) |
| 201 | ->where($dbo->qn('id') . ' = ' . $field->id); |
| 202 | |
| 203 | $dbo->setQuery($q); |
| 204 | $dbo->execute(); |
| 205 | } |
| 206 | |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Moves the employee payments within the global |
| 212 | * database table. The record will be assigned to the |
| 213 | * employees using a specific column. |
| 214 | * |
| 215 | * @return boolean True on success, false otherwise. |
| 216 | */ |
| 217 | protected static function fixEmployeePayments() |
| 218 | { |
| 219 | $dbo = JFactory::getDbo(); |
| 220 | |
| 221 | $q = $dbo->getQuery(true) |
| 222 | ->select('*') |
| 223 | ->from($dbo->qn('#__vikappointments_employee_payment')); |
| 224 | |
| 225 | $dbo->setQuery($q); |
| 226 | $dbo->execute(); |
| 227 | |
| 228 | if ($dbo->getNumRows()) |
| 229 | { |
| 230 | foreach ($dbo->loadObjectList() as $payment) |
| 231 | { |
| 232 | // unset primary key as the record needs to be added |
| 233 | unset($payment->id); |
| 234 | |
| 235 | $dbo->insertObject('#__vikappointments_gpayments', $payment, 'id'); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Finds the parent of the existing working days, |
| 244 | * so that the duplicated records (for the services) will |
| 245 | * belong always to the original record. |
| 246 | * |
| 247 | * @return boolean True on success, false otherwise. |
| 248 | */ |
| 249 | protected static function fixWorkingDays() |
| 250 | { |
| 251 | $dbo = JFactory::getDbo(); |
| 252 | |
| 253 | $q = $dbo->getQuery(true) |
| 254 | ->select('*') |
| 255 | ->from($dbo->qn('#__vikappointments_emp_worktime')); |
| 256 | |
| 257 | $dbo->setQuery($q); |
| 258 | $dbo->execute(); |
| 259 | |
| 260 | if (!$dbo->getNumRows()) |
| 261 | { |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | $wdays = $dbo->loadObjectList(); |
| 266 | |
| 267 | foreach ($wdays as $wd) |
| 268 | { |
| 269 | if ($wd->id_service == -1) |
| 270 | { |
| 271 | continue; |
| 272 | } |
| 273 | |
| 274 | foreach ($wdays as $inner) |
| 275 | { |
| 276 | if ($wd->id == $inner->id || $inner->id_service != -1) |
| 277 | { |
| 278 | continue; |
| 279 | } |
| 280 | |
| 281 | if (self::matchWorkingDays($wd, $inner)) |
| 282 | { |
| 283 | $wd->parent = $inner->id; |
| 284 | $dbo->updateObject('#__vikappointments_emp_worktime', $wd, 'id'); |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Checks if 2 working days are matching. |
| 296 | * |
| 297 | * @param object $a The first working day. |
| 298 | * @param object $b The second working day. |
| 299 | * |
| 300 | * @return boolean True if equals, false otherwise. |
| 301 | */ |
| 302 | protected static function matchWorkingDays($a, $b) |
| 303 | { |
| 304 | return ($a->id_employee == $b->id_employee |
| 305 | && $a->day == $b->day |
| 306 | && $a->fromts == $b->fromts |
| 307 | && $a->endts == $b->endts |
| 308 | && $a->ts == $b->ts |
| 309 | && $a->closed == $b->closed); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Fixes the router configuration by adding a new |
| 314 | * dedicated setting and by renaming the _router.php |
| 315 | * into router.php. |
| 316 | * |
| 317 | * @param object $parent The parent that calls this method. |
| 318 | * |
| 319 | * @return boolean True on success, false otherwise. |
| 320 | */ |
| 321 | protected static function fixRouter($parent) |
| 322 | { |
| 323 | $dbo = JFactory::getDbo(); |
| 324 | |
| 325 | $value = (int) $parent->routerEnabled; |
| 326 | |
| 327 | $q = $dbo->getQuery(true) |
| 328 | ->insert($dbo->qn('#__vikappointments_config')) |
| 329 | ->columns($dbo->qn(array('param', 'setting'))) |
| 330 | ->values($dbo->q('router') . ', ' . $value); |
| 331 | |
| 332 | $dbo->setQuery($q); |
| 333 | $dbo->execute(); |
| 334 | |
| 335 | $res = (bool) $dbo->insertid(); |
| 336 | |
| 337 | if (is_file(VAPBASE . DIRECTORY_SEPARATOR . '_router.php')) |
| 338 | { |
| 339 | $res = unlink(VAPBASE . DIRECTORY_SEPARATOR . '_router.php'); |
| 340 | } |
| 341 | |
| 342 | return $res; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Deletes the employee payments table from the database. |
| 347 | * |
| 348 | * @return boolean True on success, false otherwise. |
| 349 | */ |
| 350 | protected static function dropEmployeePayments() |
| 351 | { |
| 352 | $dbo = JFactory::getDbo(); |
| 353 | |
| 354 | $q = "DROP TABLE `#__vikappointments_employee_payment`"; |
| 355 | |
| 356 | $dbo->setQuery($q); |
| 357 | $dbo->execute(); |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | } |
| 362 |