/** * Retrieves associative list of attribute tables and their columns * * @param Mage_Eav_Model_Entity_Type $entityTypeModel * @return array */ protected function _getAttributeTablesColumnList($entityTypeModel) { $tableNames = array_unique($entityTypeModel->getAttributeCollection()->walk('getBackendTable')); $columnsByTable = array(); foreach ($tableNames as $table) { if ($table) { $columnsByTable[$table] = $this->_getWriteAdapter()->describeTable($table); } } return $columnsByTable; }
/** * Collect required EAV attributes, validate applicable attributes and validate source attributes values * * @param array $data * @param Mage_Eav_Model_Entity_Type $productEntity * @return array */ protected function _validateAttributes($data, $productEntity) { if (!isset($data['attribute_set_id']) || empty($data['attribute_set_id'])) { $this->_critical('Missing "attribute_set_id" in request.', Mage_Api2_Model_Server::HTTP_BAD_REQUEST); } if (!isset($data['type_id']) || empty($data['type_id'])) { $this->_critical('Missing "type_id" in request.', Mage_Api2_Model_Server::HTTP_BAD_REQUEST); } // Validate weight if (isset($data['weight']) && !empty($data['weight']) && $data['weight'] > 0 && !Zend_Validate::is($data['weight'], 'Between', array(0, self::MAX_DECIMAL_VALUE))) { $this->_addError('The "weight" value is not within the specified range.'); } // msrp_display_actual_price_type attribute values needs to be a string to pass validation // see Mage_Catalog_Model_Product_Attribute_Source_Msrp_Type_Price::getAllOptions() if (isset($data['msrp_display_actual_price_type'])) { $data['msrp_display_actual_price_type'] = (string) $data['msrp_display_actual_price_type']; } $requiredAttributes = array('attribute_set_id'); $positiveNumberAttributes = array('weight', 'price', 'special_price', 'msrp'); /** @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */ foreach ($productEntity->getAttributeCollection($data['attribute_set_id']) as $attribute) { $attributeCode = $attribute->getAttributeCode(); $value = false; $isSet = false; if (isset($data[$attribute->getAttributeCode()])) { $value = $data[$attribute->getAttributeCode()]; $isSet = true; } $applicable = false; if (!$attribute->getApplyTo() || in_array($data['type_id'], $attribute->getApplyTo())) { $applicable = true; } if (!$applicable && !$attribute->isStatic() && $isSet) { $productTypes = Mage_Catalog_Model_Product_Type::getTypes(); $this->_addError(sprintf('Attribute "%s" is not applicable for product type "%s"', $attributeCode, $productTypes[$data['type_id']]['label'])); } if ($applicable && $isSet) { // Validate dropdown attributes if ($attribute->usesSource() && !(empty($value) && $attribute->getIsRequired())) { $allowedValues = $this->_getAttributeAllowedValues($attribute->getSource()->getAllOptions()); if (!is_array($value)) { // make validation of select and multiselect identical $value = array($value); } foreach ($value as $selectValue) { $useStrictMode = !is_numeric($selectValue); if (!in_array($selectValue, $allowedValues, $useStrictMode) && !$this->_isConfigValueUsed($data, $attributeCode)) { $this->_addError(sprintf('Invalid value "%s" for attribute "%s".', $selectValue, $attributeCode)); } } } // Validate datetime attributes if ($attribute->getBackendType() == 'datetime') { try { $attribute->getBackend()->formatDate($value); } catch (Zend_Date_Exception $e) { $this->_addError(sprintf('Invalid date in the "%s" field.', $attributeCode)); } } // Validate positive number required attributes if (in_array($attributeCode, $positiveNumberAttributes) && (!empty($value) && $value !== 0) && (!is_numeric($value) || $value < 0)) { $this->_addError(sprintf('Please enter a number 0 or greater in the "%s" field.', $attributeCode)); } } if ($applicable && $attribute->getIsRequired() && $attribute->getIsVisible()) { if (!in_array($attributeCode, $positiveNumberAttributes) || $value !== 0) { $requiredAttributes[] = $attribute->getAttributeCode(); } } } foreach ($requiredAttributes as $key) { if (!array_key_exists($key, $data)) { if (!$this->_isUpdate()) { $this->_addError(sprintf('Missing "%s" in request.', $key)); continue; } } else { if (!is_numeric($data[$key]) && empty($data[$key])) { $this->_addError(sprintf('Empty value for "%s" in request.', $key)); } } } }
/** * Generates records for catalog_product_super_attribute and catalog_product_super_link tables * * @param array $row * @param Mage_Eav_Model_Entity_Type $entityTypeModel * @throws Exception * @return array */ protected function _getProductSuperRelations($row, $entityTypeModel) { $result = array(); if (isset($row['super_attributes']) && is_array($row['super_attributes'])) { $records = array(); $attributeCodes = $entityTypeModel->getAttributeCollection(); foreach ($row['super_attributes'] as $attributeCode) { $attribute = $attributeCodes->getItemByColumnValue('attribute_code', $attributeCode); if (!$attribute) { throw new Exception('Super attribute not found with code: ' . $attributeCode); } $attributeId = $attribute->getId(); $records[] = array('product_id' => $row[$this->_getEntityIdField($entityTypeModel)], 'attribute_id' => $attributeId); } if ($records) { $result += array('catalog/product_super_attribute' => $records); } } if (isset($row['configurable_children']) && is_array($row['configurable_children'])) { $records = array(); foreach ($row['configurable_children'] as $childId) { $records[] = array('parent_id' => $row[$this->_getEntityIdField($entityTypeModel)], 'product_id' => $childId); } if ($records) { $result += array('catalog/product_super_link' => $records); } } return $result; }