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

Add more characters to be replaced for image filenames #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
29 changes: 15 additions & 14 deletions app/code/community/Netzarbeiter/NicerImageNames/Helper/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ public function init(Mage_Catalog_Model_Product $product, $attributeName, $image
if (Mage::getStoreConfig("catalog/nicerimagenames/generate_labels")) {
$this->_setNicerImageLabels($attributeName);
}

return $this;
}

/**
* Set the label on the product for the given image type, and also on the gallery images
*
*
* @param string $attributeName One of 'image' or 'thumbnail' or 'small_image'
*/
protected function _setNicerImageLabels($attributeName) {
$map = $this->_getNiceLabelMap();
$label = $this->_getGeneratedNameForImageAttribute($attributeName, $map, false);

// Set it on the product (used for the main product image in the view media template)
$key = $attributeName . '_label';
if (! $this->getProduct()->getData($key)) {
Expand All @@ -84,7 +84,7 @@ protected function _setNicerImageLabels($attributeName) {

/**
* Return the label name template according to the config settings
*
*
* @return string
*/
protected function _getNiceLabelMap()
Expand All @@ -102,7 +102,7 @@ protected function _getNiceLabelMap()
*
* @param string $attributeName One of 'image', 'small_image' or 'thumbnail'
* @param string $map The template to use to generate the value
* @param bool $forFiles Should the returned value be usable as a file name
* @param bool $forFiles Should the returned value be usable as a file name
* @return string
*/
protected function _getGeneratedNameForImageAttribute($attributeName, $map = null, $forFiles = true)
Expand All @@ -128,7 +128,7 @@ protected function _getGeneratedNameForImageAttribute($attributeName, $map = nul
if (Mage::getStoreConfigFlag("catalog/nicerimagenames/append_file_hash")) {
$map .= '_' . sha1_file($this->__toString());
}

// Replace multiple spaces or - with one of it's kind
$value = preg_replace('/([ -]){2,}/', '$1', $map);

Expand All @@ -154,7 +154,7 @@ public function getAttributePlaceholdersFromMap($map)

/**
* Prepare a string so it may be used as part of a a file name
*
*
* @param string $value
* @param bool $forFiles
* @return mixed
Expand All @@ -165,13 +165,14 @@ protected function _prepareValue($value, $forFiles = true)
$value = strtr($value, array(
'&' => 'and',
));

if ($forFiles) {
// not needed if this is for image labels
$value = strtr($value, array(
'%' => '', ' ' => '-', '#' => '-', '"' => '-', '<' => '-',
"'" => '-', ':' => '-', '..' => '_', '/' => '-', '_' => '-',
'>' => '-',
'>' => '-', '‒' => '-', '–' => '-', '—' => '-', '―' => '-',
',' => '-', ';' => '-'
));
$value = Mage::helper('catalog/product_url')->format($value);
$value = strtr($value, array('ã' => 'a'));
Expand All @@ -182,7 +183,7 @@ protected function _prepareValue($value, $forFiles = true)
'"' => '&quot;', '>' => '&gt;', '<' => '&lt;', "'" => ''
));
}

return $value;
}

Expand Down Expand Up @@ -227,18 +228,18 @@ protected function _getProductAttributeValue($attributeCode, $_sentry = false)
if (!is_scalar($value)) {
return $attributeCode;
}

return $value;
}

/**
* Load a single attribute value onto a product.
*
* Load a single attribute value onto a product.
*
* This method is not nice. I only keep it because if it reduces
* the number of support requests, when people specify attributes
* in the template string but don't have them loaded on product
* collections.
*
*
* @param Mage_Catalog_Model_Product $product
* @param string $attributeCode
* @return $this
Expand Down