| @@ -89,8 +89,12 @@ | ||
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | if (version_compare($this->version, '1.6.0', '<')) |
| 92 | 92 | { |
| 93 | + if (!class_exists('VikBooking')) { | |
| 94 | + require_once VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'lib.vikbooking.php'; | |
| 95 | + } | |
| 96 | + | |
| 93 | 97 | // normalize translation records table name |
| 94 | 98 | VikBooking::getTranslator()->normalizeTnTableNames(); |
| 95 | 99 | } |
| 96 | 100 | |
| @@ -103,8 +107,20 @@ | ||
| 103 | 107 | * @return boolean True to proceed with the update, otherwise false to stop. |
| 104 | 108 | */ |
| 105 | 109 | public function afterInstallation() |
| 106 | 110 | { |
| 111 | + $dbo = JFactory::getDbo(); | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Unpublish overrides and obtain tracking list. | |
| 115 | + * | |
| 116 | + * @since 1.6.5 | |
| 117 | + */ | |
| 118 | + $track = $this->deactivateBreakingOverrides(); | |
| 119 | + | |
| 120 | + // register breaking changes, if any | |
| 121 | + VikBookingInstaller::registerBreakingChanges($track); | |
| 122 | + | |
| 107 | 123 | if (version_compare($this->version, '1.6.1', '<')) |
| 108 | 124 | { |
| 109 | 125 | // resolve conflicting update checks for older VCM versions (1.8.4) |
| 110 | 126 | if (defined('VIKCHANNELMANAGER_SOFTWARE_VERSION') && version_compare(VIKCHANNELMANAGER_SOFTWARE_VERSION, '1.8.5', '<')) |
| @@ -114,7 +130,189 @@ | ||
| 114 | 130 | file_put_contents(VCM_ADMIN_PATH . DIRECTORY_SEPARATOR . 'controller.php', $old_vcm_data); |
| 115 | 131 | } |
| 116 | 132 | } |
| 117 | 133 | |
| 134 | + if (version_compare($this->version, '1.7.0', '<')) | |
| 135 | + { | |
| 136 | + /** | |
| 137 | + * Install the new "tinyurl" Shortcode. | |
| 138 | + */ | |
| 139 | + $model = JModel::getInstance('vikbooking', 'shortcode', 'admin'); | |
| 140 | + | |
| 141 | + $shortcode = new stdClass; | |
| 142 | + $shortcode->createdby = JFactory::getUser()->id; | |
| 143 | + $shortcode->createdon = JFactory::getDate()->toSql(); | |
| 144 | + $shortcode->title = 'COM_VIKBOOKING_TINYURL_VIEW_DEFAULT_TITLE'; | |
| 145 | + $shortcode->name = 'Tiny URL'; | |
| 146 | + $shortcode->type = 'tinyurl'; | |
| 147 | + $shortcode->shortcode = '[vikbooking view="tinyurl" lang="*"]'; | |
| 148 | + | |
| 149 | + try { | |
| 150 | + $sh_id = $model->save($shortcode); | |
| 151 | + | |
| 152 | + if ($sh_id) { | |
| 153 | + // assign the shortcode to a new page | |
| 154 | + $model->addPage($sh_id); | |
| 155 | + } | |
| 156 | + } catch (Throwable $e) { | |
| 157 | + // do nothing, but prevent fatal errors | |
| 158 | + } | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * Normalize invoice and check-in document names for OTA bookings. | |
| 162 | + * | |
| 163 | + * @since 1.7.0 | |
| 164 | + */ | |
| 165 | + $fixing_invoices = JFolder::files(implode(DIRECTORY_SEPARATOR, [VBO_SITE_PATH, 'helpers', 'invoices', 'generated']), '[0-9]+_\.pdf$', $recursive = false, $fullpath = true); | |
| 166 | + $fixing_checkdocs = JFolder::files(implode(DIRECTORY_SEPARATOR, [VBO_SITE_PATH, 'helpers', 'checkins', 'generated']), '[0-9]+_\.pdf$', $recursive = false, $fullpath = true); | |
| 167 | + foreach (array_merge($fixing_invoices, $fixing_checkdocs) as $fix_doc) { | |
| 168 | + $doc_bid = (int) preg_replace('/[^0-9]/', '', basename($fix_doc)); | |
| 169 | + if (!$doc_bid) { | |
| 170 | + continue; | |
| 171 | + } | |
| 172 | + // whether it's an invoice or a check-in document | |
| 173 | + $is_invoice = strpos($fix_doc, 'invoices') !== false; | |
| 174 | + // get the OTA booking ID | |
| 175 | + $dbo->setQuery( | |
| 176 | + $dbo->getQuery(true) | |
| 177 | + ->select($dbo->qn('idorderota')) | |
| 178 | + ->from($dbo->qn('#__vikbooking_orders')) | |
| 179 | + ->where($dbo->qn('id') . ' = ' . $doc_bid) | |
| 180 | + , 0, 1); | |
| 181 | + $idorderota = $dbo->loadResult(); | |
| 182 | + if (!$idorderota) { | |
| 183 | + // delete file for security reason if the booking does not exist | |
| 184 | + JFile::delete($fix_doc); | |
| 185 | + continue; | |
| 186 | + } | |
| 187 | + // build new safe document name | |
| 188 | + $safe_fname = $doc_bid . '_' . $idorderota . '.pdf'; | |
| 189 | + $safe_fpath = str_replace(basename($fix_doc), $safe_fname, $fix_doc); | |
| 190 | + // rename document file | |
| 191 | + rename($fix_doc, $safe_fpath); | |
| 192 | + // update db reference | |
| 193 | + if ($is_invoice) { | |
| 194 | + // invoice | |
| 195 | + $q = $dbo->getQuery(true) | |
| 196 | + ->update($dbo->qn('#__vikbooking_invoices')) | |
| 197 | + ->set($dbo->qn('file_name') . ' = ' . $dbo->q($safe_fname)) | |
| 198 | + ->where($dbo->qn('idorder') . ' = ' . $doc_bid) | |
| 199 | + ->where($dbo->qn('file_name') . ' = ' . $dbo->q(basename($fix_doc))); | |
| 200 | + } else { | |
| 201 | + // check-in document | |
| 202 | + $q = $dbo->getQuery(true) | |
| 203 | + ->update($dbo->qn('#__vikbooking_customers_orders')) | |
| 204 | + ->set($dbo->qn('checkindoc') . ' = ' . $dbo->q($safe_fname)) | |
| 205 | + ->where($dbo->qn('idorder') . ' = ' . $doc_bid) | |
| 206 | + ->where($dbo->qn('checkindoc') . ' = ' . $dbo->q(basename($fix_doc))); | |
| 207 | + } | |
| 208 | + $dbo->setQuery($q); | |
| 209 | + $dbo->execute(); | |
| 210 | + if (VBOPlatformDetection::isWordPress()) { | |
| 211 | + // trigger files mirroring | |
| 212 | + VikBookingUpdateManager::triggerUploadBackup($safe_fpath); | |
| 213 | + } | |
| 214 | + } | |
| 215 | + } | |
| 216 | + | |
| 118 | 217 | return true; |
| 218 | + } | |
| 219 | + | |
| 220 | + /** | |
| 221 | + * Returns a list of possible overrides that may | |
| 222 | + * break the site for backward compatibility errors. | |
| 223 | + * | |
| 224 | + * @return array The list of overrides, grouped by client. | |
| 225 | + * | |
| 226 | + * @since 1.6.5 | |
| 227 | + */ | |
| 228 | + protected function getBreakingOverrides() | |
| 229 | + { | |
| 230 | + // define initial overrides lookup | |
| 231 | + $lookup = [ | |
| 232 | + 'admin' => [], | |
| 233 | + 'site' => [], | |
| 234 | + 'layouts' => [], | |
| 235 | + 'widgets' => [], | |
| 236 | + ]; | |
| 237 | + | |
| 238 | + // check whether the current version (before the update) | |
| 239 | + // was prior than 1.6.5 version | |
| 240 | + if (version_compare($this->version, '1.6.5', '<')) | |
| 241 | + { | |
| 242 | + // multitasking layout must be updated to support the Service Worker | |
| 243 | + $lookup['layouts'][] = VBO_ADMIN_PATH . '/layouts/sidepanel/multitasking.php'; | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 247 | + * NOTE: it is possible to use the code below to automatically deactivate all the existing overrides: | |
| 248 | + * `$lookup = JModel::getInstance('vikbooking', 'overrides', 'admin')->getAllOverrides();` | |
| 249 | + */ | |
| 250 | + | |
| 251 | + return $lookup; | |
| 252 | + } | |
| 253 | + | |
| 254 | + /** | |
| 255 | + * Helper function used to deactivate any overrides that | |
| 256 | + * may corrupt the system because of breaking changes. | |
| 257 | + * | |
| 258 | + * @return array The list of unpublished overrides. | |
| 259 | + * | |
| 260 | + * @since 1.6.5 | |
| 261 | + */ | |
| 262 | + protected function deactivateBreakingOverrides() | |
| 263 | + { | |
| 264 | + // load list of breaking overrides | |
| 265 | + $lookup = $this->getBreakingOverrides(); | |
| 266 | + | |
| 267 | + $track = []; | |
| 268 | + | |
| 269 | + // get models to manage the overrides | |
| 270 | + $listModel = JModel::getInstance('vikbooking', 'overrides', 'admin'); | |
| 271 | + $itemModel = JModel::getInstance('vikbooking', 'override', 'admin'); | |
| 272 | + | |
| 273 | + foreach ($lookup as $client => $files) | |
| 274 | + { | |
| 275 | + // do not need to load the whole tree in case | |
| 276 | + // the client doesn't report any files | |
| 277 | + if ($files) | |
| 278 | + { | |
| 279 | + $tree = $listModel->getTree($client); | |
| 280 | + | |
| 281 | + foreach ($files as $file) | |
| 282 | + { | |
| 283 | + // clean file path | |
| 284 | + $file = JPath::clean($file); | |
| 285 | + | |
| 286 | + // check whether the specified file is supported | |
| 287 | + if ($node = $listModel->isSupported($tree, $file)) | |
| 288 | + { | |
| 289 | + // skip in case the path has been already unpublished | |
| 290 | + if (in_array($node['override'], isset($track[$client]) ? $track[$client] : [])) | |
| 291 | + { | |
| 292 | + continue; | |
| 293 | + } | |
| 294 | + | |
| 295 | + // override found, check whether we have an existing | |
| 296 | + // and published override | |
| 297 | + if ($node['has'] && $node['published']) | |
| 298 | + { | |
| 299 | + // deactivate the override | |
| 300 | + if ($itemModel->publish($node['override'], 0)) | |
| 301 | + { | |
| 302 | + if (!isset($track[$client])) | |
| 303 | + { | |
| 304 | + $track[$client] = []; | |
| 305 | + } | |
| 306 | + | |
| 307 | + // track the unpublished file for later use | |
| 308 | + $track[$client][] = $node['override']; | |
| 309 | + } | |
| 310 | + } | |
| 311 | + } | |
| 312 | + } | |
| 313 | + } | |
| 314 | + } | |
| 315 | + | |
| 316 | + return $track; | |
| 119 | 317 | } |
| 120 | 318 | } |