Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Used transliterator_transliterate to generate url_key #1631

Merged
merged 10 commits into from
Apr 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ protected function _prepareForm()
'name' => 'identifier',
'label' => Mage::helper('cms')->__('URL Key'),
'title' => Mage::helper('cms')->__('URL Key'),
'required' => true,
'class' => 'validate-identifier',
'note' => Mage::helper('cms')->__('Relative to Website Base URL'),
'disabled' => $isElementDisabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public function saveAction()
);
}

if (empty($data['identifier'])) {
$data['identifier'] = $model->getIdentifier();
}

$this->_getSession()->setFormData($data);
$this->_redirect('*/*/edit', ['page_id' => $this->getRequest()->getParam('page_id')]);
return;
Expand Down
76 changes: 19 additions & 57 deletions app/code/core/Mage/Catalog/Helper/Product/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,61 +28,7 @@ class Mage_Catalog_Helper_Product_Url extends Mage_Core_Helper_Url
*
* @var array
*/
protected $_convertTable = [
'&' => 'and', '@' => 'at', '©' => 'c', '®' => 'r', 'À' => 'a',
'Á' => 'a', 'Â' => 'a', 'Ä' => 'a', 'Å' => 'a', 'Æ' => 'ae','Ç' => 'c',
'È' => 'e', 'É' => 'e', 'Ë' => 'e', 'Ì' => 'i', 'Í' => 'i', 'Î' => 'i',
'Ï' => 'i', 'Ò' => 'o', 'Ó' => 'o', 'Ô' => 'o', 'Õ' => 'o', 'Ö' => 'o',
'Ø' => 'o', 'Ù' => 'u', 'Ú' => 'u', 'Û' => 'u', 'Ü' => 'u', 'Ý' => 'y',
'ß' => 'ss','à' => 'a', 'á' => 'a', 'â' => 'a', 'ä' => 'a', 'å' => 'a',
'æ' => 'ae','ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ò' => 'o', 'ó' => 'o',
'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u',
'û' => 'u', 'ü' => 'u', 'ý' => 'y', 'þ' => 'p', 'ÿ' => 'y', 'Ā' => 'a',
'ā' => 'a', 'Ă' => 'a', 'ă' => 'a', 'Ą' => 'a', 'ą' => 'a', 'Ć' => 'c',
'ć' => 'c', 'Ĉ' => 'c', 'ĉ' => 'c', 'Ċ' => 'c', 'ċ' => 'c', 'Č' => 'c',
'č' => 'c', 'Ď' => 'd', 'ď' => 'd', 'Đ' => 'd', 'đ' => 'd', 'Ē' => 'e',
'ē' => 'e', 'Ĕ' => 'e', 'ĕ' => 'e', 'Ė' => 'e', 'ė' => 'e', 'Ę' => 'e',
'ę' => 'e', 'Ě' => 'e', 'ě' => 'e', 'Ĝ' => 'g', 'ĝ' => 'g', 'Ğ' => 'g',
'ğ' => 'g', 'Ġ' => 'g', 'ġ' => 'g', 'Ģ' => 'g', 'ģ' => 'g', 'Ĥ' => 'h',
'ĥ' => 'h', 'Ħ' => 'h', 'ħ' => 'h', 'Ĩ' => 'i', 'ĩ' => 'i', 'Ī' => 'i',
'ī' => 'i', 'Ĭ' => 'i', 'ĭ' => 'i', 'Į' => 'i', 'į' => 'i', 'İ' => 'i',
'ı' => 'i', 'IJ' => 'ij','ij' => 'ij','Ĵ' => 'j', 'ĵ' => 'j', 'Ķ' => 'k',
'ķ' => 'k', 'ĸ' => 'k', 'Ĺ' => 'l', 'ĺ' => 'l', 'Ļ' => 'l', 'ļ' => 'l',
'Ľ' => 'l', 'ľ' => 'l', 'Ŀ' => 'l', 'ŀ' => 'l', 'Ł' => 'l', 'ł' => 'l',
'Ń' => 'n', 'ń' => 'n', 'Ņ' => 'n', 'ņ' => 'n', 'Ň' => 'n', 'ň' => 'n',
'ʼn' => 'n', 'Ŋ' => 'n', 'ŋ' => 'n', 'Ō' => 'o', 'ō' => 'o', 'Ŏ' => 'o',
'ŏ' => 'o', 'Ő' => 'o', 'ő' => 'o', 'Œ' => 'oe','œ' => 'oe','Ŕ' => 'r',
'ŕ' => 'r', 'Ŗ' => 'r', 'ŗ' => 'r', 'Ř' => 'r', 'ř' => 'r', 'Ś' => 's',
'ś' => 's', 'Ŝ' => 's', 'ŝ' => 's', 'Ş' => 's', 'ş' => 's', 'Š' => 's',
'š' => 's', 'Ţ' => 't', 'ţ' => 't', 'Ť' => 't', 'ť' => 't', 'Ŧ' => 't',
'ŧ' => 't', 'Ũ' => 'u', 'ũ' => 'u', 'Ū' => 'u', 'ū' => 'u', 'Ŭ' => 'u',
'ŭ' => 'u', 'Ů' => 'u', 'ů' => 'u', 'Ű' => 'u', 'ű' => 'u', 'Ų' => 'u',
'ų' => 'u', 'Ŵ' => 'w', 'ŵ' => 'w', 'Ŷ' => 'y', 'ŷ' => 'y', 'Ÿ' => 'y',
'Ź' => 'z', 'ź' => 'z', 'Ż' => 'z', 'ż' => 'z', 'Ž' => 'z', 'ž' => 'z',
'ſ' => 'z', 'Ə' => 'e', 'ƒ' => 'f', 'Ơ' => 'o', 'ơ' => 'o', 'Ư' => 'u',
'ư' => 'u', 'Ǎ' => 'a', 'ǎ' => 'a', 'Ǐ' => 'i', 'ǐ' => 'i', 'Ǒ' => 'o',
'ǒ' => 'o', 'Ǔ' => 'u', 'ǔ' => 'u', 'Ǖ' => 'u', 'ǖ' => 'u', 'Ǘ' => 'u',
'ǘ' => 'u', 'Ǚ' => 'u', 'ǚ' => 'u', 'Ǜ' => 'u', 'ǜ' => 'u', 'Ǻ' => 'a',
'ǻ' => 'a', 'Ǽ' => 'ae','ǽ' => 'ae','Ǿ' => 'o', 'ǿ' => 'o', 'ə' => 'e',
'Ё' => 'jo','Є' => 'e', 'І' => 'i', 'Ї' => 'i', 'А' => 'a', 'Б' => 'b',
'В' => 'v', 'Г' => 'g', 'Д' => 'd', 'Е' => 'e', 'Ж' => 'zh','З' => 'z',
'И' => 'i', 'Й' => 'j', 'К' => 'k', 'Л' => 'l', 'М' => 'm', 'Н' => 'n',
'О' => 'o', 'П' => 'p', 'Р' => 'r', 'С' => 's', 'Т' => 't', 'У' => 'u',
'Ф' => 'f', 'Х' => 'h', 'Ц' => 'c', 'Ч' => 'ch','Ш' => 'sh','Щ' => 'sch',
'Ъ' => '-', 'Ы' => 'y', 'Ь' => '-', 'Э' => 'je','Ю' => 'ju','Я' => 'ja',
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e',
'ж' => 'zh','з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l',
'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's',
'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
'ш' => 'sh','щ' => 'sch','ъ' => '-','ы' => 'y', 'ь' => '-', 'э' => 'je',
'ю' => 'ju','я' => 'ja','ё' => 'jo','є' => 'e', 'і' => 'i', 'ї' => 'i',
'Ґ' => 'g', 'ґ' => 'g', 'א' => 'a', 'ב' => 'b', 'ג' => 'g', 'ד' => 'd',
'ה' => 'h', 'ו' => 'v', 'ז' => 'z', 'ח' => 'h', 'ט' => 't', 'י' => 'i',
'ך' => 'k', 'כ' => 'k', 'ל' => 'l', 'ם' => 'm', 'מ' => 'm', 'ן' => 'n',
'נ' => 'n', 'ס' => 's', 'ע' => 'e', 'ף' => 'p', 'פ' => 'p', 'ץ' => 'C',
'צ' => 'c', 'ק' => 'q', 'ר' => 'r', 'ש' => 'w', 'ת' => 't', '™' => 'tm',
];
protected $_convertTable = ['&' => 'and', '@' => 'at', '©' => 'c', '®' => 'r', '™' => 'tm'];

/**
* Check additional instruction for convertation table in configuration
Expand Down Expand Up @@ -111,10 +57,26 @@ public function getConvertTable()
* Process string based on convertation table
*
* @param string $string
* @param null|string $locale
* @param bool $lower
* @return string
*/
public function format($string)
public function format($string, $locale = null, $lower = true)
{
return $string === null ? '' : strtr($string, $this->getConvertTable());
if ($string === null) {
return '';
}

$string = strtr($string, $this->getConvertTable());

if (!empty($locale)) {
$opts = transliterator_list_ids();
$code = str_replace('_', '-', strtolower($locale)) . '_Latn/BGN';
if (in_array($code, $opts)) {
return transliterator_transliterate($code . '; Any-Latin; Latin-ASCII; [^\u001F-\u007f] remove' . ($lower ? '; Lower()' : ''), $string);
}
}

return transliterator_transliterate('Any-Latin; Latin-ASCII; [^\u001F-\u007f] remove' . ($lower ? '; Lower()' : ''), $string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function beforeSave($object)
$urlKey = $object->getName();
}

$object->setData($attributeName, $object->formatUrlKey($urlKey));
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $object->getStoreId());
$object->setData($attributeName, $object->formatUrlKey($urlKey, $locale));

return $this;
}
Expand Down
17 changes: 8 additions & 9 deletions app/code/core/Mage/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,28 +499,27 @@ 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->formatUrlKey($this->getName(), $locale);
$url = $this->getUrlInstance()->getUrl('catalog/category/view', [
's' => $urlKey,
's' => $urlKey,
'id' => $this->getId(),
]);
Varien_Profiler::stop('REGULAR: ' . __METHOD__);
return $url;
}

/**
* Format URL key from name or defined key
* Format Key for URL
*
* @param string $str
* @param null|string $locale
* @return string
*/
public function formatUrlKey($str)
public function formatUrlKey($str, $locale = null)
{
$str = Mage::helper('catalog/product_url')->format($str);
$urlKey = preg_replace('#[^0-9a-z]+#i', '-', $str);
$urlKey = strtolower($urlKey);
$urlKey = trim($urlKey, '-');
return $urlKey;
$urlKey = preg_replace('#[^0-9a-z]+#i', '-', Mage::helper('catalog/product_url')->format($str, $locale));
return trim($urlKey, '-');
}

/**
Expand Down
5 changes: 3 additions & 2 deletions app/code/core/Mage/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1674,11 +1674,12 @@ public function getUrlInStore($params = [])
* Formats URL key
*
* @param string $str
* @param null|string $locale
* @return string
*/
public function formatUrlKey($str)
public function formatUrlKey($str, $locale = null)
{
return $this->getUrlModel()->formatUrlKey($str);
return $this->getUrlModel()->formatUrlKey($str, $locale);
}

/**
Expand Down
10 changes: 4 additions & 6 deletions app/code/core/Mage/Catalog/Model/Product/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,13 @@ public function getProductUrl($product, $useSid = null)
* Format Key for URL
*
* @param string $str
* @param null|string $locale
* @return string
*/
public function formatUrlKey($str)
public function formatUrlKey($str, $locale = null)
{
$urlKey = preg_replace('#[^0-9a-z]+#i', '-', Mage::helper('catalog/product_url')->format($str));
$urlKey = strtolower($urlKey);
$urlKey = trim($urlKey, '-');

return $urlKey;
$urlKey = preg_replace('#[^0-9a-z]+#i', '-', Mage::helper('catalog/product_url')->format($str, $locale));
return trim($urlKey, '-');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function beforeSave($object)
$urlKey = $object->getName();
}

$object->setData($attributeName, $object->formatUrlKey($urlKey));
$locale = Mage::getStoreConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE, $object->getStoreId());
$object->setData($attributeName, $object->formatUrlKey($urlKey, $locale));

return $this;
}
Expand Down
10 changes: 6 additions & 4 deletions app/code/core/Mage/Catalog/Model/Resource/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,10 @@ public function saveRewriteHistory($rewriteData)
*
* @param Varien_Object|Mage_Catalog_Model_Category $category
* @param string $attributeCode
* @param bool $ignoreDefault
* @return $this
*/
public function saveCategoryAttribute(Varien_Object $category, $attributeCode)
public function saveCategoryAttribute(Varien_Object $category, $attributeCode, $ignoreDefault = false)
{
$adapter = $this->_getWriteAdapter();
if (!isset($this->_categoryAttributes[$attributeCode])) {
Expand Down Expand Up @@ -381,7 +382,7 @@ public function saveCategoryAttribute(Varien_Object $category, $attributeCode)
$adapter->insert($attributeTable, $attributeData);
}

if ($attributeData['store_id'] != 0) {
if (!$ignoreDefault && $attributeData['store_id'] != 0) {
$attributeData['store_id'] = 0;
$select = $adapter->select()
->from($attributeTable)
Expand Down Expand Up @@ -489,9 +490,10 @@ protected function _getCategoryAttribute($attributeCode, $categoryIds, $storeId)
*
* @param Varien_Object|Mage_Catalog_Model_Product $product
* @param string $attributeCode
* @param bool $ignoreDefault
* @return $this
*/
public function saveProductAttribute(Varien_Object $product, $attributeCode)
public function saveProductAttribute(Varien_Object $product, $attributeCode, $ignoreDefault = false)
{
$adapter = $this->_getWriteAdapter();
if (!isset($this->_productAttributes[$attributeCode])) {
Expand Down Expand Up @@ -535,7 +537,7 @@ public function saveProductAttribute(Varien_Object $product, $attributeCode)
$adapter->insert($attributeTable, $attributeData);
}

if ($attributeData['store_id'] != 0) {
if (!$ignoreDefault && $attributeData['store_id'] != 0) {
$attributeData['store_id'] = 0;
$select = $adapter->select()
->from($attributeTable)
Expand Down
Loading
Loading