Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion install.zasilkovna.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ public function preflight($route, $adapter) {
if ($route === 'update') {
$media_path = JPATH_ROOT . DS . 'media' . DS . 'com_zasilkovna';
recurse_delete($media_path);

$this->removeObsoleteShippingPaymentRestrictionsFromConfig();
$this->removeAdministratorFiles();
$this->removeAlzaboxFromShipmentParams();
}
}

Expand Down Expand Up @@ -598,5 +599,66 @@ private function createCronToken() {

$model->updateConfig($config);
}

/**
* @return void
*/
private function removeAlzaboxFromShipmentParams() {
$db = JFactory::getDBO();
$db->setQuery('SELECT `virtuemart_shipmentmethod_id`, `shipment_params` FROM `#__virtuemart_shipmentmethods` WHERE `shipment_element` = "zasilkovna" AND `shipment_params` LIKE "%alzabox%"');
$methods = $db->loadObjectList();

foreach ($methods as $method) {
$newParams = $this->removeAlzaboxFromShipmentParamsString($method->shipment_params);
$updateQuery = sprintf(
'UPDATE `#__virtuemart_shipmentmethods` SET `shipment_params` = %s WHERE `virtuemart_shipmentmethod_id` = %d',
$db->quote($newParams),
(int)$method->virtuemart_shipmentmethod_id
);

$db->setQuery($updateQuery);
$db->execute();
}
}

/**
* @param string $shipmentParamsString
* @return string
*/
private function removeAlzaboxFromShipmentParamsString($shipmentParamsString) {
// Split the string into JSON part and the rest
list($jsonPart, $rest) = explode("|", $shipmentParamsString, 2);

// Remove 'delivery_settings=' from the start of the JSON part
$jsonPart = str_replace('delivery_settings=', '', $jsonPart);

// Check if the string contains "alzabox"
if (strpos($jsonPart, 'alzabox') !== false) {
$arr = json_decode($jsonPart, true);

// Remove "alzabox" from the vendor_groups array
if (($key = array_search('alzabox', $arr['vendor_groups'], true)) !== false) {
unset($arr['vendor_groups'][$key]);
$arr['vendor_groups'] = array_values($arr['vendor_groups']);
}

$jsonPart = json_encode($arr);
}

return 'delivery_settings=' . $jsonPart . "|" . $rest;
}

private function removeObsoleteShippingPaymentRestrictionsFromConfig() {
/** @var VirtueMartModelZasilkovna $zasilkovnaModel */
$zasilkovnaModel = VmModel::getModel('zasilkovna');
$config = $zasilkovnaModel->loadConfig();

foreach ($config as $key => $value) {
if (strpos($key, 'zasilkovna_combination') === 0) {
unset($config[$key]);
}
}
$zasilkovnaModel->updateConfig($config);
}
}