/** * Retrieve attribute field name * * * @param Mage_Eav_Model_Entity_Attribute $attribute * @return string */ public function getAttributeFieldName($attribute) { $name = $attribute->getAttributeCode(); if ($suffix = $this->getForm()->getFieldNameSuffix()) { $name = $this->getForm()->addSuffixToName($name, $suffix); } return $name; }
/** * Add Join with option value for collection select * * @param Mage_Eav_Model_Entity_Collection_Abstract $collection * @param Mage_Eav_Model_Entity_Attribute $attribute * @param Zend_Db_Expr $valueExpr * * @return Mage_Eav_Model_Mysql4_Entity_Attribute_Option */ public function addOptionValueToCollection($collection, $attribute, $valueExpr) { $attributeCode = $attribute->getAttributeCode(); $optionTable1 = $attributeCode . '_option_value_t1'; $optionTable2 = $attributeCode . '_option_value_t2'; $collection->getSelect()->joinLeft(array($optionTable1 => $this->getTable('eav/attribute_option_value')), "`{$optionTable1}`.`option_id`={$valueExpr}" . " AND `{$optionTable1}`.`store_id`='0'", array())->joinLeft(array($optionTable2 => $this->getTable('eav/attribute_option_value')), "`{$optionTable2}`.`option_id`={$valueExpr}" . " AND `{$optionTable1}`.`store_id`='{$collection->getStoreId()}'", array($attributeCode => "IFNULL(`{$optionTable2}`.`value`, `{$optionTable1}`.`value`)")); return $this; }
private function saveAttribute() { if ($this->attributeObj->getId()) { return array('result' => true, 'obj' => $this->attributeObj, 'code' => $this->attributeObj->getAttributeCode()); } if (!$this->validate()) { return array('result' => false, 'error' => 'Attribute builder. Validation failed.'); } $data = $this->params; $data['attribute_code'] = $this->code; $data['frontend_label'] = array(Mage_Core_Model_App::ADMIN_STORE_ID => $this->primaryLabel); $data['frontend_input'] = $this->inputType; $data['source_model'] = Mage::helper('catalog/product')->getAttributeSourceModelByInputType($this->inputType); $data['backend_model'] = Mage::helper('catalog/product')->getAttributeBackendModelByInputType($this->inputType); !isset($data['is_global']) && ($data['is_global'] = self::SCOPE_STORE); !isset($data['is_configurable']) && ($data['is_configurable'] = 0); !isset($data['is_filterable']) && ($data['is_filterable'] = 0); !isset($data['is_filterable_in_search']) && ($data['is_filterable_in_search'] = 0); $this->attributeObj = Mage::getModel('catalog/resource_eav_attribute'); if (is_null($this->attributeObj->getIsUserDefined()) || $this->attributeObj->getIsUserDefined() != 0) { $data['backend_type'] = $this->attributeObj->getBackendTypeByInput($this->inputType); } // default value if (empty($data['default_value'])) { unset($data['default_value']); } // --------------------------------------- !isset($data['apply_to']) && ($data['apply_to'] = array()); // prepare options foreach ($this->options as $optionValue) { $code = 'option_' . substr(sha1($optionValue), 0, 6); $data['option']['value'][$code] = array(Mage_Core_Model_App::ADMIN_STORE_ID => $optionValue); } // --------------------------------------- $this->attributeObj->addData($data); $this->attributeObj->setEntityTypeId($this->entityTypeId); $this->attributeObj->setIsUserDefined(1); try { $this->attributeObj->save(); } catch (Exception $e) { return array('result' => false, 'error' => $e->getMessage()); } return array('result' => true, 'obj' => $this->attributeObj, 'code' => $this->attributeObj->getAttributeCode()); }
/** * Add Join with option value for collection select * * @param Mage_Eav_Model_Entity_Collection_Abstract $collection * @param Mage_Eav_Model_Entity_Attribute $attribute * @param Zend_Db_Expr $valueExpr * * @return Mage_Eav_Model_Mysql4_Entity_Attribute_Option */ public function addOptionValueToCollection($collection, $attribute, $valueExpr) { $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID; $attributeCode = $attribute->getAttributeCode(); $optionTable1 = $attributeCode . '_option_value_t1'; $optionTable2 = $attributeCode . '_option_value_t2'; $collection->getSelect()->joinLeft(array($optionTable1 => $this->getTable('eav/attribute_option_value')), "`{$optionTable1}`.`option_id`={$valueExpr}" . " AND `{$optionTable1}`.`store_id`='{$adminStore}'", $collection->getStoreId() != $adminStore ? array() : array($attributeCode . '_value' => "{$optionTable1}.value")); if ($collection->getStoreId() != $adminStore) { $collection->getSelect()->joinLeft(array($optionTable2 => $this->getTable('eav/attribute_option_value')), "`{$optionTable2}`.`option_id`={$valueExpr}" . " AND `{$optionTable1}`.`store_id`='{$collection->getStoreId()}'", array($attributeCode . '_value' => "IFNULL(`{$optionTable2}`.`value`, `{$optionTable1}`.`value`)")); } return $this; }
/** * Add Join with option value for collection select * * @param Mage_Eav_Model_Entity_Collection_Abstract $collection * @param Mage_Eav_Model_Entity_Attribute $attribute * @param Zend_Db_Expr $valueExpr * @return Mage_Eav_Model_Resource_Entity_Attribute_Option */ public function addOptionValueToCollection($collection, $attribute, $valueExpr) { $adapter = $this->_getReadAdapter(); $attributeCode = $attribute->getAttributeCode(); $optionTable1 = $attributeCode . '_option_value_t1'; $optionTable2 = $attributeCode . '_option_value_t2'; $tableJoinCond1 = "{$optionTable1}.option_id={$valueExpr} AND {$optionTable1}.store_id=0"; $tableJoinCond2 = $adapter->quoteInto("{$optionTable2}.option_id={$valueExpr} AND {$optionTable2}.store_id=?", $collection->getStoreId()); $valueExpr = $adapter->getCheckSql("{$optionTable2}.value_id IS NULL", "{$optionTable1}.value", "{$optionTable2}.value"); $collection->getSelect()->joinLeft(array($optionTable1 => $this->getTable('eav/attribute_option_value')), $tableJoinCond1, array())->joinLeft(array($optionTable2 => $this->getTable('eav/attribute_option_value')), $tableJoinCond2, array($attributeCode => $valueExpr)); return $this; }
/** * Update attribute flat data * * @param Mage_Eav_Model_Entity_Attribute $attribute * @param int $store * @param int|array $productIds update only product(s) * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Flat_Indexer */ public function updateAttribute($attribute, $store, $productIds = null) { if ($attribute->getBackend()->getType() == 'static') { $select = $this->_getWriteAdapter()->select()->join(array('main_table' => $this->getTable('catalog/product')), 'main_table.entity_id=e.entity_id ', array($attribute->getAttributeCode() => 'main_table.' . $attribute->getAttributeCode())); if ($this->getFlatHelper()->isAddChildData()) { $select->where("e.is_child=?", 0); } if (!is_null($productIds)) { $select->where('main_table.entity_id IN(?)', $productIds); } $sql = $select->crossUpdateFromSelect(array('e' => $this->getFlatTableName($store))); $this->_getWriteAdapter()->query($sql); } else { $select = $attribute->getFlatUpdateSelect($store); if ($select instanceof Varien_Db_Select) { if (!is_null($productIds)) { $select->where('e.entity_id IN(?)', $productIds); } $sql = $select->crossUpdateFromSelect(array('e' => $this->getFlatTableName($store))); $this->_getWriteAdapter()->query($sql); } } return $this; }
/** * Update attribute flat data * * @param Mage_Eav_Model_Entity_Attribute $attribute * @param int $storeId * @param int|array $productIds update only product(s) * @return Mage_Catalog_Model_Resource_Product_Flat_Indexer */ public function updateAttribute($attribute, $storeId, $productIds = null) { if (!$this->_isFlatTableExists($storeId)) { return $this; } $adapter = $this->_getWriteAdapter(); $flatTableName = $this->getFlatTableName($storeId); $describe = $adapter->describeTable($flatTableName); if ($attribute->getBackend()->getType() == 'static') { if (!isset($describe[$attribute->getAttributeCode()])) { return $this; } $select = $adapter->select()->join(array('main_table' => $this->getTable('catalog/product')), 'main_table.entity_id = e.entity_id', array($attribute->getAttributeCode() => 'main_table.' . $attribute->getAttributeCode())); if ($this->getFlatHelper()->isAddChildData()) { $select->where('e.is_child = ?', 0); } if ($productIds !== null) { $select->where('main_table.entity_id IN(?)', $productIds); } $sql = $select->crossUpdateFromSelect(array('e' => $flatTableName)); $adapter->query($sql); } else { $columns = $attribute->getFlatColumns(); if (!$columns) { return $this; } foreach (array_keys($columns) as $columnName) { if (!isset($describe[$columnName])) { return $this; } } $select = $attribute->getFlatUpdateSelect($storeId); if ($select instanceof Varien_Db_Select) { if ($productIds !== null) { $select->where('e.entity_id IN(?)', $productIds); } $sql = $select->crossUpdateFromSelect(array('e' => $flatTableName)); $adapter->query($sql); } } return $this; }
/** * Create Field and Return it * * @param Varien_Data_Form_Element_Abstract $fieldset * @param Varien_Object $e * @param Mage_Eav_Model_Entity_Attribute $attribute * @param string $fieldStep * * @return Varien_Data_Form_Element_Abstract */ protected function _createField(Varien_Data_Form_Element_Abstract $fieldset, Varien_Object $e, Mage_Eav_Model_Entity_Attribute $attribute, $fieldStep = 'standard') { // Get Config Data $configData = $this->getConfigData(); if ('' == $attribute->getFrontendLabel()) { return false; } // Define Attribute Code $attributeCode = $attribute->getAttributeCode(); $attributeCode = $fieldStep == 'newfield' ? "{$attributeCode}_dynamic_new" : $attributeCode; // Get Attribute Data and Inheritance $path = $this->_configPath . $attributeCode; if (isset($configData[$path])) { $data = $configData[$path]; $inherit = false; } else { $data = (string) Mage::getConfig()->getNode(null, $this->getForm()->getScope(), $this->getForm()->getScopeCode())->descend($path); $inherit = true; } // Get field Renderer if ($e->frontend_model) { $fieldRenderer = Mage::getBlockSingleton((string) $e->frontend_model); } else { $fieldRenderer = $this->_getFieldRenderer(); } // Define Type, Name, and Label $fieldType = (string) $e->frontend_type ? (string) $e->frontend_type : 'text'; $name = str_replace('_attrCode_', $attributeCode, $this->_fieldNameTemplate); $label = $fieldStep == 'newfield' ? "" : $attribute->getFrontendLabel(); // Pass through backend model in case it needs to modify value if ($e->backend_model) { $model = Mage::getModel((string) $e->backend_model); if (!$model instanceof Mage_Core_Model_Config_Data) { Mage::throwException('Invalid config field backend model: ' . (string) $e->backend_model); } $model->setPath($path)->setValue($data)->afterLoad(); $data = $model->getValue(); } // Pre-populate New field with label text if ('newfield' == $fieldStep && '' == $data) { $data = $attribute->getFrontendLabel(); } // Select Field for Existing attributes. $field = $fieldset->addField($attributeCode, $fieldType, array('name' => $name, 'label' => $label, 'value' => $data === 0 ? '' : $data, 'inherit' => $fieldStep == 'newfield' ? false : $inherit, 'field_config' => $e, 'scope' => $this->getForm()->getScope(), 'scopeId' => $this->getForm()->getScopeId(), 'scope_label' => '[STORE VIEW]', 'can_use_default_value' => $this->getForm()->canUseDefaultValue((int) $e->show_in_default), 'can_use_website_value' => $this->getForm()->canUseWebsiteValue((int) $e->show_in_website), 'can_use_store_value' => $this->getForm()->canUseWebsiteValue((int) $e->show_in_store))); // Add Validation if ($e->validate) { $field->addClass($e->validate); } // Determine if value can be empty if (isset($e->frontend_type) && 'multiselect' === (string) $e->frontend_type && isset($e->can_be_empty)) { $field->setCanBeEmpty(true); } // Set Field Renderer $field->setRenderer($fieldRenderer); // Use Source Model to define available options if ($e->source_model) { $sourceModel = Mage::getSingleton((string) $e->source_model); if ($sourceModel instanceof Varien_Object) { $sourceModel->setPath($path); } $field->setValues($sourceModel->toOptionArray()); } return $field; }
/** * Select field filter HTML. * * @param Mage_Eav_Model_Entity_Attribute $attribute * @return string */ protected function _getSelectHtml(Mage_Eav_Model_Entity_Attribute $attribute) { if ($attribute->getFilterOptions()) { $options = array(); foreach ($attribute->getFilterOptions() as $value => $label) { $options[] = array('value' => $value, 'label' => $label); } } else { $options = $attribute->getSource()->getAllOptions(false); } if ($size = count($options)) { // add empty vaue option $firstOption = reset($options); if ('' === $firstOption['value']) { $options[key($options)]['label'] = ''; } else { array_unshift($options, array('value' => '', 'label' => '')); } $selectBlock = new Mage_Core_Block_Html_Select(array('name' => $this->getFilterElementName($attribute->getAttributeCode()), 'id' => $this->getFilterElementId($attribute->getAttributeCode()), 'class' => 'select', 'extra_params' => 'style="width:280px"')); return $selectBlock->setOptions($options)->getHtml(); } else { return $this->_helper->__('Attribute does not has options, so filtering is impossible'); } }
/** * Retrieves scope dependent value from fixture value, i.e, * store view or * website attribute value * * * @param array $row * @param Mage_Eav_Model_Entity_Attribute $attribute * @param string $scopeCode * @param string $scopeType * @return mixed|null */ protected function _getGwsValue($row, $attribute, $scopeCode, $scopeType = self::SCOPE_TYPE_STORE) { if (!isset($row['/' . $scopeType][$scopeCode]) || !is_array($row['/' . $scopeType][$scopeCode]) || !isset($row['/' . $scopeType][$scopeCode][$attribute->getAttributeCode()])) { return null; } return $this->_getAttributeValue($row['/' . $scopeType][$scopeCode], $attribute); }
/** * add product eav Attribute to document * * @param int $storeId * @param Mage_Eav_Model_Entity_Attribute $attribute * @param array $documents * @return array */ public function _updateAttribute($storeId, $attribute, $documents = null) { $fields = $this->getMappings($storeId); $adapter = $this->_getReadAdapter(); if ($attribute->getBackend()->getType() == 'static') { var_dump('not implemented yet'); return $this; if (false === isset($describe[$attribute->getAttributeCode()])) { return $this; } $select = $adapter->select()->join(array('main_table' => $this->getTable('catalog/product')), 'main_table.entity_id = e.entity_id', array($attribute->getAttributeCode() => 'main_table.' . $attribute->getAttributeCode())); $select->where('main_table.entity_id IN(?)', array_map('intval', array_keys($documents))); $sql = $select->crossUpdateFromSelect(array('e' => $flatTableName)); var_dump($sql); die; $adapter->query($sql); } else { //non static attributes $columns = $attribute->getFlatColumns(); if (!$columns) { return $this; } foreach (array_keys($columns) as $columnName) { if (false === isset($fields[$columnName])) { return $this; } } $select = $attribute->getFlatUpdateSelect($storeId); $select->from(array('e' => $this->getTable('catalog/product')), 'entity_id'); if ($select instanceof Varien_Db_Select) { $select->where('e.entity_id IN(?)', array_map('intval', array_keys($documents))); foreach ($adapter->query($select)->fetchAll() as $data) { $documentId = $data['entity_id']; //remove entity id Field unset($data['entity_id']); if (true === isset($documents[$documentId])) { if (true === is_array($data) && count($data) > 0) { foreach ($data as $field => $value) { $documents[$documentId]->set($field, $value); } } } } } } return $documents; }
/** * Set attribute model to filter * * @param Mage_Eav_Model_Entity_Attribute $attribute * @return Mage_Catalog_Model_Layer_Filter_Abstract */ public function setAttributeModel($attribute) { $this->setRequestVar($attribute->getAttributeCode()); $this->setData('attribute_model', $attribute); return $this; }
/** * Create filter fields for 'Filter' column. * * @param mixed $value * @param Mage_Eav_Model_Entity_Attribute $row * @param Varien_Object $column * @param boolean $isExport * @return string */ public function decorateFilter($value, Mage_Eav_Model_Entity_Attribute $row, Varien_Object $column, $isExport) { $value = null; $values = $column->getValues(); if (is_array($values) && isset($values[$row->getAttributeCode()])) { $value = $values[$row->getAttributeCode()]; } switch (Mage_ImportExport_Model_Export::getAttributeFilterType($row)) { case Mage_ImportExport_Model_Export::FILTER_TYPE_SELECT: $cell = $this->_getSelectHtmlWithValue($row, $value); break; case Mage_ImportExport_Model_Export::FILTER_TYPE_INPUT: $cell = $this->_getInputHtmlWithValue($row, $value); break; case Mage_ImportExport_Model_Export::FILTER_TYPE_DATE: $cell = $this->_getDateFromToHtmlWithValue($row, $value); break; case Mage_ImportExport_Model_Export::FILTER_TYPE_NUMBER: $cell = $this->_getNumberFromToHtmlWithValue($row, $value); break; default: $cell = $this->_helper->__('Unknown attribute filter type'); } return $cell; }
/** * @param Mage_Eav_Model_Entity_Attribute $attribute * @param string $optionLabel */ protected function _createAttributeOption($attribute, $optionLabel) { $option = array('value' => array(array('0' => $optionLabel)), 'order' => array(0), 'delete' => array('')); $attribute->setOption($option); $attribute->save(); $this->_attributeOptions[$attribute->getAttributeCode()][] = $optionLabel; $this->_initTypeModels(); }
/** * Lock a specific category or product attribute so that it can not be editted through the interface. * @param Mage_Eav_Model_Entity_Attribute $attribute * @param Mage_Catalog_Model_Abstract $model * @param $profile */ protected function _lockAttribute(Mage_Eav_Model_Entity_Attribute $attribute, Mage_Catalog_Model_Abstract $model, $profile) { $note = $attribute->getNote() ? $attribute->getNote() : ''; if ($attribute->getAttributeCode() == 'ho_import_profile') { return; } if ($note) { $note .= "<br />\n"; } $note .= Mage::helper('ho_import')->__("Locked by import: <i>%s</i>", $profile); $model->lockAttribute($attribute->getAttributeCode()); $attribute->setNote($note); }
/** * Filter the collection by an EAV attribute option * * @param $products * @param Mage_Eav_Model_Entity_Attribute $attribute * @param int|array $value * @param Fishpig_AttributeSplashPro_Model_Page $page * @param string $cond * @return $this */ protected function _addEavAttributeOptionFilterToProductCollection($products, $attribute, $value, $page, $cond = '= ?') { $alias = $attribute->getAttributeCode() . '_index'; if (!is_array($value)) { $alias .= '_' . $value; } $products->getSelect()->distinct()->join(array($alias => $this->getTable('catalog/product_index_eav')), "`{$alias}`.`entity_id` = `e`.`entity_id`" . $this->_getReadAdapter()->quoteInto(" AND `{$alias}`.`attribute_id` = ? ", $attribute->getAttributeId()) . $this->_getReadAdapter()->quoteInto(" AND `{$alias}`.`store_id` = ? ", $page->getStoreId()) . $this->_getReadAdapter()->quoteInto(" AND `{$alias}`.`value` " . $cond, $value), ''); return $this; }
/** * Generate an object representing the suggestion. * * @param Mage_Eav_Model_Entity_Attribute $attribute Suggested attribute. * @param string $value Suggested value. * @param float $score Suggestion score. * * @return Varien_Object */ protected function _createSuggestion($attribute, $value, $score) { $urlParams = array($this->_requestVar => $value, $attribute->getAttributeCode() => $value); $url = Mage::getUrl('catalogsearch/result/index', array('_query' => $urlParams)); return new Varien_Object(array('label' => $value, 'url' => $url, 'score' => $score, 'attribute' => $attribute)); }
public function setAttributeObj(Mage_Eav_Model_Entity_Attribute $obj) { $this->attributeObj = $obj; $this->code = $obj->getAttributeCode(); return $this; }
/** * Retrieves attribute value * * @param array $row * @param Mage_Eav_Model_Entity_Attribute $attribute * @return mixed|null */ protected function _getAttributeValue($row, $attribute) { if (isset($row[$attribute->getAttributeCode()]) && !is_array($row[$attribute->getAttributeCode()])) { $value = $row[$attribute->getAttributeCode()]; } elseif ($attribute->getIsRequired() && $attribute->getDefaultValue() !== null && $attribute->getDefaultValue() !== '' && !is_array($attribute->getDefaultValue())) { $value = $attribute->getDefaultValue(); } else { $value = null; } if ($attribute->usesSource() && $value !== null) { if ($attribute->getSource() instanceof Mage_Eav_Model_Entity_Attribute_Source_Abstract) { $value = $attribute->getSource()->getOptionId($value); } else { $value = $this->_getOptionIdNonAttributeSource($attribute->getSource()->getAllOptions(), $value); } } return $value; }
/** * Check whether attribute reserved or not * * @param Mage_Eav_Model_Entity_Attribute $attribute Attribute model object * @return boolean */ public function isReservedAttribute($attribute) { return $attribute->getIsUserDefined() && in_array($attribute->getAttributeCode(), $this->getReservedAttributes()); }
/** * @param Mage_Eav_Model_Entity_Attribute $attribute * @return $this */ protected function _cacheAttribute($attribute) { $this->_cachedAttributes[$attribute->getAttributeCode()] = $attribute; return $this; }