8000 Add attribute option images to be used as label by carco · Pull Request #4 · tzyganu/Switcher · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add attribute option images to be used as label #4

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Easylife_Switcher extension
*
* NOTICE OF LICENSE
*
* This source file is subject to the MIT License
* that is bundled with this package in the file LICENSE_EASYLIFE_SWITCHER.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/mit-license.php
*
* @category Easylife
* @package Easylife_Switcher
* @copyright Copyright (c) 2014
* @license http://opensource.org/licenses/mit-license.php MIT License
*/


/**
* Attribute option Images tab
*
* @category Easylife
* @package Easylife_Switcher
* @author Emil [carco] Sirbu <emil.sirbu@gmail.com>
*/

class Easylife_Switcher_Block_Adminhtml_Catalog_Product_Attribute_Edit_Tab_Optimage extends Mage_Adminhtml_Block_Template implements Mage_Adminhtml_Block_Widget_Tab_Interface
{

/**
* Attribute option collection, with image/hexa code attached
*
*/

protected $_optCollection = null;


/**
* Return Tab label
*
* @return string
*/
public function getTabLabel() {
return Mage::helper('easylife_switcher/optimage')->__('Manage Option Images');
}

/**
* Return Tab title
*
* @return string
*/
public function getTabTitle() {
return Mage::helper('easylife_switcher/optimage')->__('Manage Option Images');
}

/**
* Can show tab in tabs
*
* @return boolean
*/
public function canShowTab() {

$attributes = Mage::helper('easylife_switcher/optimage')->getColorImageAttributes();
if(!$attributes) {
return false;
}
$attribute = Mage::registry('entity_attribute');

return (
$attribute &&
$attribute->getAttributeCode() &&
$attribute->getIsConfigurable() &&
$attribute->getFrontendInput() == 'select' &&
in_array($attribute->getAttributeCode(),$attributes)
);
}

/**
* Tab is hidden?
*
* @return boolean
*/
public function isHidden() {
return false;
}

/**
* Get attribute option collection with image/hexa code attached
*
* @return Varien_Collection|Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection
*/
public function getOptionsCollection()
{
if(!$this->_optCollection) {
$attribute = Mage::registry('entity_attribute');
if(!$attribute || !$attribute->getId()) {
$this->_optCollection = new Varien_Collection();
} else {
$this->_optCollection = Mage::getModel('easylife_switcher/optimage')->getOptionCollection($attribute->getId());
}
}
return $this->_optCollection;
}


/**
* Get upload action
*
* @return string
*/
public function getUploadImageUrl()
{
return $this->getUrl('*/attribute_image/upload',array('_current'=>true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class Easylife_Switcher_Block_Catalog_Product_View_Type_Configurable_Config
* config path to use images
*/
const XML_USE_IMAGES_PATH = 'easylife_switcher/settings/use_images';
/**
* config path to use option images
*/
const XML_USE_OPTION_IMAGES_PATH= 'easylife_switcher/settings/use_option_images';
/**
* config path to image attributes
*/
Expand All @@ -51,6 +55,10 @@ class Easylife_Switcher_Block_Catalog_Product_View_Type_Configurable_Config
* config path to image resize
*/
const XML_IMAGE_RESIZE = 'easylife_switcher/settings/image_resize';
/**
* config path to labels / options image resize
*/
const XML_OPTIONS_IMAGE_RESIZE = 'easylife_switcher/settings/options_image_resize';
/**
* config pat to change image callback
*/
Expand Down Expand Up @@ -107,6 +115,7 @@ public function getJsonAdditionalConfig(){
$config['stock'] = $this->getStockOptions();
$config['switch_attributes'] = $this->getSwitchAttributes();
$config['images'] = $this->getImages();
$config['option_images'] = $this->getOptionImages();

if (!$this->getProduct()->hasPreconfiguredValues()){
if ($this->getDefaultValues()){
Expand Down Expand Up @@ -175,7 +184,7 @@ public function getConfigurableBlock(){
*/
public function getSwitchAttributes($path = self::XML_USE_IMAGES_PATH){
if (!isset($this->_switchAttribtues[$path])){
$allowedString = Mage::getStoreConfig($path);
$allowedString = trim(Mage::getStoreConfig($path),' ,');
if (!$allowedString){
$this->_switchAttribtues[$path] = array();
}
Expand All @@ -195,6 +204,56 @@ public function getSwitchAttributes($path = self::XML_USE_IMAGES_PATH){
return $this->_switchAttribtues[$path];
}


/**
* get attribute option images to use for labels
* @access public
* @return array
* @author Emil [carco] Sirbu <emil.sirbu@gmail.com>
*/
public function getOptionImages(){


$attributes = $this->getSwitchAttributes(self::XML_USE_OPTION_IMAGES_PATH);
if (!$attributes) {
return array();
}

$simpleProducts = $this->getSimpleProducts();
if(!$simpleProducts) {
return array();
}

$optIDs = array();
foreach ($simpleProducts as $product){
foreach($attributes as $attrId=>$attrCode) {
if($attrCode && $product->getData($attrCode)) {
$optIDs[$product->getData($attrCode)] = $attrId;
}
}
}
if(!$optIDs) {
return array();
}

$images = array();
$options = Mage::getModel('easylife_switcher/optimage')->getOptionCollection(array_keys($optIDs),$byOptIds = true);
foreach ($options as $option){
if($option->getOptimage()) {
$image = Mage::helper('easylife_switcher/optimage')->init($option,'optimage');
$dimensions = $this->_getImageDimensions(self::XML_OPTIONS_IMAGE_RESIZE);
if (!empty($dimensions)){
$image->resize($dimensions[0], $dimensions[1]);
}
$images[$option->getAttributeId()][$option->getId()]['image_url'] = (string)$image;
} elseif($option->getHexaCode()) {
$images[$option->getAttributeId()][$option->getId()]['hexa_code'] = $option->getHexaCode();
}
}

return $images;
}

/**
* get images to use for labels
* @access public
Expand All @@ -217,7 +276,12 @@ public function getImages(){
foreach ($this->getSwitchAttributes() as $id=>$code){
foreach ($simpleProducts as $product){
if ($product->getData($imageAttribute) != '' && $product->getData($imageAttribute) != 'no_selection'){
$images[$id][$product->getId()] = (string)Mage::helper('catalog/image')->init($product, $this->getImageAttribute())->resize(40);
$image = Mage::helper('catalog/image')->init($product, $this->getImageAttribute());
$dimensions = $this->_getImageDimensions(self::XML_OPTIONS_IMAGE_RESIZE);
if (!empty($dimensions)){
$image->resize($dimensions[0], $dimensions[1]);
}
$images[$id][$product->getId()] = (string)$image;
}
}
}
Expand Down Expand Up @@ -349,15 +413,20 @@ public function getSwitchMedia(){
* @return array|bool
* @author Marius Strajeru <marius.strajeru@gmail.com>
*/
protected function _getImageDimensions(){
$value = Mage::getStoreConfig(self::XML_IMAGE_RESIZE);
protected function _getImageDimensions($path = self::XML_IMAGE_RESIZE){
$value = Mage::getStoreConfig($path);
81DB if (!$value){
return false;
}
$dimensions = explode('x', $value, 2);
if (!isset($dimensions[1])){
$dimensions[1] = $dimensions[0];
}
$dimensions[0] = (int)$dimensions[0];
$dimensions[1] = (int)$dimensions[1];
if($dimensions[0]<=0 || $dimensions[1]<=0) {
return false;
}
return $dimensions;
}

Expand Down Expand Up @@ -388,4 +457,5 @@ protected function _getMediaBlockTemplate(){
}
return $template;
}

}
Loading
0