Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Oct 27, 2024
1 parent 150fe53 commit 0bad5c8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function beforeSave($object)
$urlKey = $object->getName();
}

if (method_exists($object, 'setLocale')) {
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $object->getStoreId());
$object->setLocale($locale);
}

$object->setData($attributeName, $object->formatUrlKey($urlKey));

return $this;
Expand Down
9 changes: 5 additions & 4 deletions app/code/core/Mage/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,10 @@ public function getUrlModel()
public function getCategoryIdUrl()
{
Varien_Profiler::start('REGULAR: ' . __METHOD__);
$urlKey = $this->getUrlKey() ? $this->getUrlKey() : $this->formatUrlKey($this->getName());
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $this->getStoreId());
$urlKey = $this->getUrlKey() ? $this->getUrlKey() : $this->setLocale($locale)->formatUrlKey($this->getName());
$url = $this->getUrlInstance()->getUrl('catalog/category/view', [
's' => $urlKey,
's' => $urlKey,
'id' => $this->getId(),
]);
Varien_Profiler::stop('REGULAR: ' . __METHOD__);
Expand All @@ -520,15 +521,15 @@ public function getCategoryIdUrl()
*/
public function formatUrlKey($str)
{
return $this->getUrlModel()->formatUrlKey($str);
return $this->getUrlModel()->setLocale($this->getLocale())->formatUrlKey($str);
}

public function getLocale(): ?string
{
return $this->locale;
}

public function setLocale(string $locale)
public function setLocale(?string $locale)
{
$this->locale = $locale;
return $this;
Expand Down
4 changes: 2 additions & 2 deletions app/code/core/Mage/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1681,15 +1681,15 @@ public function getUrlInStore($params = [])
*/
public function formatUrlKey($str)
{
return $this->getUrlModel()->formatUrlKey($str);
return $this->getUrlModel()->setLocale($this->getLocale())->formatUrlKey($str);
}

public function getLocale(): ?string
{
return $this->locale;
}

public function setLocale(string $locale)
public function setLocale(?string $locale)
{
$this->locale = $locale;
return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function beforeSave($object)
$urlKey = $object->getName();
}

if (method_exists($object, 'setLocale')) {
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $object->getStoreId());
$object->setLocale($locale);
}
$object->setData($attributeName, $object->formatUrlKey($urlKey));

return $this;
Expand Down
72 changes: 31 additions & 41 deletions app/code/core/Mage/Catalog/Model/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,8 @@ protected function _refreshCategoryRewrites(Varien_Object $category, $parentPath
{
if ($category->getId() != $this->getStores($category->getStoreId())->getRootCategoryId()) {
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $category->getStoreId());
if ($category->getUrlKey() == '') {
$urlKey = $this->getCategoryModel()->setLocale($locale)->formatUrlKey($category->getName());
} else {
$urlKey = $this->getCategoryModel()->setLocale($locale)->formatUrlKey($category->getUrlKey());
}
$urlKey = $category->getUrlKey() == '' ? $category->getName() : $category->getUrlKey();
$urlKey = $this->getCategoryModel()->setLocale($locale)->formatUrlKey($urlKey);

$idPath = $this->generatePath('id', null, $category);
$targetPath = $this->generatePath('target', null, $category);
Expand Down Expand Up @@ -381,11 +378,8 @@ protected function _refreshProductRewrite(Varien_Object $product, Varien_Object
}

$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $product->getStoreId());
if ($product->getUrlKey() == '') {
$urlKey = $this->getProductModel()->setLocale($locale)->formatUrlKey($product->getName());
} else {
$urlKey = $this->getProductModel()->setLocale($locale)->formatUrlKey($product->getUrlKey());
}
$urlKey = $product->getUrlKey() == '' ? $product->getName() : $product->getUrlKey();
$urlKey = $this->getProductModel()->setLocale($locale)->formatUrlKey($urlKey);

$idPath = $this->generatePath('id', $product, $category);
$targetPath = $this->generatePath('target', $product, $category);
Expand Down Expand Up @@ -772,11 +766,9 @@ public function getCategoryRequestPath($category, $parentPath)
$existingRequestPath = $this->_rewrites[$idPath]->getRequestPath();
}

if ($category->getUrlKey() == '') {
$urlKey = $this->getCategoryModel()->formatUrlKey($category->getName());
} else {
$urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey());
}
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $category->getStoreId());
$urlKey = $category->getUrlKey() == '' ? $category->getName() : $category->getUrlKey();
$urlKey = $this->getCategoryModel()->setLocale($locale)->formatUrlKey($urlKey);

$categoryUrlSuffix = $this->getCategoryUrlSuffix($storeId);
if ($parentPath === null) {
Expand Down Expand Up @@ -828,11 +820,10 @@ protected function _deleteOldTargetPath($requestPath, $idPath, $storeId)
*/
public function getProductRequestPath($product, $category)
{
if ($product->getUrlKey() == '') {
$urlKey = $this->getProductModel()->formatUrlKey($product->getName());
} else {
$urlKey = $this->getProductModel()->formatUrlKey($product->getUrlKey());
}
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $product->getStoreId());
$urlKey = $product->getUrlKey() == '' ? $product->getName() : $product->getUrlKey();
$urlKey = $this->getProductModel()->setLocale($locale)->formatUrlKey($urlKey);

$storeId = $category->getStoreId();
$suffix = $this->getProductUrlSuffix($storeId);
$idPath = $this->generatePath('id', $product, $category);
Expand Down Expand Up @@ -943,11 +934,9 @@ public function generatePath($type = 'target', $product = null, $category = null
if ($type === 'request') {
// for category
if (!$product) {
if ($category->getUrlKey() == '') {
$urlKey = $this->getCategoryModel()->formatUrlKey($category->getName());
} else {
$urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey());
}
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $category->getStoreId());
$urlKey = $category->getUrlKey() == '' ? $category->getName() : $category->getUrlKey();
$urlKey = $this->getCategoryModel()->setLocale($locale)->formatUrlKey($urlKey);

$categoryUrlSuffix = $this->getCategoryUrlSuffix($category->getStoreId());
if ($parentPath === null) {
Expand All @@ -974,11 +963,10 @@ public function generatePath($type = 'target', $product = null, $category = null
Mage::throwException(Mage::helper('core')->__('A category object is required for determining the product request path.')); // why?
}

if ($product->getUrlKey() == '') {
$urlKey = $this->getProductModel()->formatUrlKey($product->getName());
} else {
$urlKey = $this->getProductModel()->formatUrlKey($product->getUrlKey());
}
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $product->getStoreId());
$urlKey = $product->getUrlKey() == '' ? $product->getName() : $product->getUrlKey();
$urlKey = $this->getProductModel()->setLocale($locale)->formatUrlKey($urlKey);

$productUrlSuffix = $this->getProductUrlSuffix($category->getStoreId());
if ($category->getLevel() > 1) {
// To ensure, that category has url path either from attribute or generated now
Expand Down Expand Up @@ -1060,7 +1048,7 @@ public function formatUrlKey($str)

public function getSlugger(): AsciiSlugger
{
$locale = $this->getLocale() ?: 'en_US';
$locale = $this->getLocale();

if (is_null($this->slugger) || !array_key_exists($locale, $this->slugger)) {
$config = $this->getSluggerConfig($locale);
Expand All @@ -1073,7 +1061,7 @@ public function getSlugger(): AsciiSlugger
return $this->slugger[$locale];
}

public function getSluggerConfig(string $locale): array
public function getSluggerConfig(?string $locale): array
{
$config = [
'@' => 'at',
Expand All @@ -1083,15 +1071,17 @@ public function getSluggerConfig(string $locale): array
];

// @todo better config
$configNodes = Mage::getConfig()->getNode('global/slugger/' . $locale . '/replacements');
if ($configNodes instanceof Mage_Core_Model_Config_Element) {
$custom = [];
/** @var Mage_Core_Model_Config_Element $configNode */
foreach ($configNodes->children() as $configNode) {
$configNode = $configNode->asArray();
$custom[$configNode['f']] = $configNode['t'];
if ($locale) {
$configNodes = Mage::getConfig()->getNode('global/slugger/' . $locale . '/replacements');
if ($configNodes instanceof Mage_Core_Model_Config_Element) {
$custom = [];
/** @var Mage_Core_Model_Config_Element $configNode */
foreach ($configNodes->children() as $configNode) {
$configNode = $configNode->asArray();
$custom[$configNode['from']] = $configNode['to'];
}
$config = [$locale => $config + $custom];
}
$config = [$locale => $config + $custom];
}

return $config;
Expand All @@ -1105,7 +1095,7 @@ public function getLocale(): ?string
/**
* @return $this
*/
public function setLocale(string $locale)
public function setLocale(?string $locale)
{
$this->locale = $locale;
return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,8 @@ protected function _prepareAttributes($rowData, $rowScope, $attributes, $rowSku,
$attrValue = gmdate(Varien_Date::DATETIME_PHP_FORMAT, strtotime($attrValue));
} elseif ($attribute->getAttributeCode() === 'url_key') {
if (empty($attrValue)) {
$attrValue = $product->formatUrlKey($product->getName());
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $product->getStoreId());
$attrValue = $product->setLocale($locale)->formatUrlKey($product->getName());
}
} elseif ($backModel) {
$attribute->getBackend()->beforeSave($product);
Expand Down

0 comments on commit 0bad5c8

Please sign in to comment.