fixer.php
333 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments - Libraries |
| 4 | * @subpackage update |
| 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.update.adapter'); |
| 15 | |
| 16 | /** |
| 17 | * Implements the abstract methods to fix an update. |
| 18 | * |
| 19 | * Never use exit() and die() functions to stop the flow. |
| 20 | * Return false instead to break process safely. |
| 21 | * |
| 22 | * @since 1.0 |
| 23 | */ |
| 24 | class VikAppointmentsUpdateFixer |
| 25 | { |
| 26 | /** |
| 27 | * The current version. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $version; |
| 32 | |
| 33 | /** |
| 34 | * Class constructor. |
| 35 | */ |
| 36 | public function __construct($version) |
| 37 | { |
| 38 | $this->version = $version; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * This method is called before the SQL installation. |
| 43 | * |
| 44 | * @return boolean True to proceed with the update, otherwise false to stop. |
| 45 | */ |
| 46 | public function beforeInstallation() |
| 47 | { |
| 48 | if (version_compare($this->version, '1.2', '<') && !VikAppointmentsLiteManager::guessPro()) |
| 49 | { |
| 50 | $dbo = JFactory::getDbo(); |
| 51 | // truncate the payment gateways table |
| 52 | $dbo->setQuery("TRUNCATE TABLE `#__vikappointments_gpayments`"); |
| 53 | $dbo->execute(); |
| 54 | } |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * This method is called after the SQL installation. |
| 61 | * |
| 62 | * @return boolean True to proceed with the update, otherwise false to stop. |
| 63 | */ |
| 64 | public function afterInstallation() |
| 65 | { |
| 66 | /** |
| 67 | * Unpublish overrides and obtain tracking list. |
| 68 | * |
| 69 | * @since 1.2.13 |
| 70 | */ |
| 71 | $track = $this->deactivateBreakingOverrides(); |
| 72 | |
| 73 | // register breaking changes, if any |
| 74 | VikAppointmentsInstaller::registerBreakingChanges($track); |
| 75 | |
| 76 | if (version_compare($this->version, '1.1.3', '<')) |
| 77 | { |
| 78 | $this->fix_1_1_3(); |
| 79 | } |
| 80 | |
| 81 | if (version_compare($this->version, '1.2', '<')) |
| 82 | { |
| 83 | $this->fix_1_2(); |
| 84 | } |
| 85 | |
| 86 | if (version_compare($this->version, '1.2.3', '<')) |
| 87 | { |
| 88 | $this->fix_1_2_3(); |
| 89 | } |
| 90 | |
| 91 | if (version_compare($this->version, '1.2.4', '<')) |
| 92 | { |
| 93 | $this->fix_1_2_4(); |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns a list of possible overrides that may |
| 101 | * break the site for backward compatibility errors. |
| 102 | * |
| 103 | * @return array The list of overrides, grouped by client. |
| 104 | * |
| 105 | * @since 1.2.13 |
| 106 | */ |
| 107 | protected function getBreakingOverrides() |
| 108 | { |
| 109 | // define initial overrides lookup |
| 110 | $lookup = [ |
| 111 | 'admin' => [], |
| 112 | 'site' => [], |
| 113 | 'layouts' => [], |
| 114 | 'widgets' => [], |
| 115 | ]; |
| 116 | |
| 117 | // check whether the current version (before the update) |
| 118 | // was prior than 1.3 version |
| 119 | if (version_compare($this->version, '1.3', '<')) |
| 120 | { |
| 121 | /** |
| 122 | * @todo |
| 123 | */ |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * NOTE: it is possible to use the code below to automatically deactivate all the existing overrides: |
| 128 | * `$lookup = JModel::getInstance('vikappointments', 'overrides', 'admin')->getAllOverrides();` |
| 129 | */ |
| 130 | |
| 131 | return $lookup; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Helper function used to deactivate any overrides that |
| 136 | * may corrupt the system because of breaking changes. |
| 137 | * |
| 138 | * @return array The list of unpublished overrides. |
| 139 | * |
| 140 | * @since 1.2.13 |
| 141 | */ |
| 142 | protected function deactivateBreakingOverrides() |
| 143 | { |
| 144 | // load list of breaking overrides |
| 145 | $lookup = $this->getBreakingOverrides(); |
| 146 | |
| 147 | $track = []; |
| 148 | |
| 149 | // get models to manage the overrides |
| 150 | $listModel = JModel::getInstance('vikappointments', 'overrides', 'admin'); |
| 151 | $itemModel = JModel::getInstance('vikappointments', 'override', 'admin'); |
| 152 | |
| 153 | foreach ($lookup as $client => $files) |
| 154 | { |
| 155 | // do not need to load the whole tree in case |
| 156 | // the client doesn't report any files |
| 157 | if ($files) |
| 158 | { |
| 159 | $tree = $listModel->getTree($client); |
| 160 | |
| 161 | foreach ($files as $file) |
| 162 | { |
| 163 | // clean file path |
| 164 | $file = JPath::clean($file); |
| 165 | |
| 166 | // check whether the specified file is supported |
| 167 | if ($node = $listModel->isSupported($tree, $file)) |
| 168 | { |
| 169 | // skip in case the path has been already unpublished |
| 170 | if (in_array($node['override'], $track[$client] ?? [])) |
| 171 | { |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | // override found, check whether we have an existing |
| 176 | // and published override |
| 177 | if ($node['has'] && $node['published']) |
| 178 | { |
| 179 | // deactivate the override |
| 180 | if ($itemModel->publish($node['override'], 0)) |
| 181 | { |
| 182 | if (!isset($track[$client])) |
| 183 | { |
| 184 | $track[$client] = []; |
| 185 | } |
| 186 | |
| 187 | // track the unpublished file for later use |
| 188 | $track[$client][] = $node['override']; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return $track; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Fix 1.2.4 version. |
| 201 | * |
| 202 | * @return void |
| 203 | */ |
| 204 | protected function fix_1_2_4() |
| 205 | { |
| 206 | // create customizer folder |
| 207 | JFolder::create(VAP_UPLOAD_DIR_PATH . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'customizer'); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Fix 1.2.3 version. |
| 212 | * |
| 213 | * @return void |
| 214 | */ |
| 215 | protected function fix_1_2_3() |
| 216 | { |
| 217 | // use the old notation of the hook used to dispatch the cron jobs |
| 218 | $hook = 'vikappointments_cron_listener'; |
| 219 | |
| 220 | // fetch the next scheduled event |
| 221 | $event = wp_get_scheduled_event($hook); |
| 222 | |
| 223 | if ($event) |
| 224 | { |
| 225 | // unschedule the event |
| 226 | wp_unschedule_event($event->timestamp, $hook); |
| 227 | } |
| 228 | |
| 229 | // create customizer folder |
| 230 | JFolder::create(VAP_UPLOAD_DIR_PATH . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'customizer'); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Fix 1.2 version. |
| 235 | * |
| 236 | * @return void |
| 237 | */ |
| 238 | protected function fix_1_2() |
| 239 | { |
| 240 | JFolder::create(VAPCUSTOMERS_DOCUMENTS); |
| 241 | |
| 242 | // load the update adapter used for 1.7 version in Joomla |
| 243 | VAPLoader::import('libraries.update.adapters.1_7'); |
| 244 | // instantiate the update adapter |
| 245 | $adapter = new VAPUpdateAdapter1_7(); |
| 246 | |
| 247 | // launch all the update methods |
| 248 | $adapter->update($this); |
| 249 | $adapter->finalise($this); |
| 250 | $adapter->afterupdate($this); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Fix 1.1.3 version. |
| 255 | * |
| 256 | * @return void |
| 257 | */ |
| 258 | protected function fix_1_1_3() |
| 259 | { |
| 260 | $dbo = JFactory::getDbo(); |
| 261 | |
| 262 | ////////////////////////////////// |
| 263 | /// FIX SUBSCRIPTIONS ORDERING /// |
| 264 | ////////////////////////////////// |
| 265 | |
| 266 | $q = $dbo->getQuery(true) |
| 267 | ->select('*') |
| 268 | ->from($dbo->qn('#__vikappointments_subscription')) |
| 269 | ->order(array( |
| 270 | $dbo->qn('type') . ' ASC', |
| 271 | $dbo->qn('amount') . ' ASC', |
| 272 | )); |
| 273 | |
| 274 | $dbo->setQuery($q); |
| 275 | $dbo->execute(); |
| 276 | |
| 277 | if ($dbo->getNumRows()) |
| 278 | { |
| 279 | foreach ($dbo->loadObjectList() as $i => $subscr) |
| 280 | { |
| 281 | // update ordering |
| 282 | $subscr->ordering = $i + 1; |
| 283 | |
| 284 | $dbo->updateObject('#__vikappointments_subscription', $subscr, 'id'); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /////////////////////////////////////// |
| 289 | /// FIX SERVICES-EMPLOYEES ORDERING /// |
| 290 | /////////////////////////////////////// |
| 291 | |
| 292 | $q = $dbo->getQuery(true) |
| 293 | ->select($dbo->qn('id')) |
| 294 | ->from($dbo->qn('#__vikappointments_service')); |
| 295 | |
| 296 | $dbo->setQuery($q); |
| 297 | $dbo->execute(); |
| 298 | |
| 299 | if (!$dbo->getNumRows()) |
| 300 | { |
| 301 | // no installed services |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | foreach ($dbo->loadColumn() as $id_service) |
| 306 | { |
| 307 | $q = $dbo->getQuery(true) |
| 308 | ->select($dbo->qn('a.id')) |
| 309 | ->from($dbo->qn('#__vikappointments_ser_emp_assoc', 'a')) |
| 310 | ->leftjoin($dbo->qn('#__vikappointments_employee', 'e') . ' ON ' . $dbo->qn('e.id') . ' = ' . $dbo->qn('a.id_employee')) |
| 311 | ->where($dbo->qn('a.id_service') . ' = ' . $id_service) |
| 312 | ->order($dbo->qn('e.nickname') . ' ASC'); |
| 313 | |
| 314 | $dbo->setQuery($q); |
| 315 | $dbo->execute(); |
| 316 | |
| 317 | if ($dbo->getNumRows()) |
| 318 | { |
| 319 | foreach ($dbo->loadColumn() as $i => $id) |
| 320 | { |
| 321 | $q = $dbo->getQuery(true) |
| 322 | ->update($dbo->qn('#__vikappointments_ser_emp_assoc')) |
| 323 | ->set($dbo->qn('ordering') . ' = ' . ($i + 1)) |
| 324 | ->where($dbo->qn('id') . ' = ' . $id); |
| 325 | |
| 326 | $dbo->setQuery($q); |
| 327 | $dbo->execute(); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 |