forked from DivanteLtd/magento1-vsbridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate product mapping by attribute data from magento instead of dy…
…namic generating DivanteLtd#7
- Loading branch information
1 parent
68e6763
commit 4d89f04
Showing
5 changed files
with
272 additions
and
178 deletions.
There are no files selected for viewing
118 changes: 104 additions & 14 deletions
118
...o1-module/app/code/local/Divante/VueStorefrontBridge/controllers/AttributesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,128 @@ | ||
<?php | ||
require_once('AbstractController.php'); | ||
|
||
class Divante_VueStorefrontBridge_AttributesController extends Divante_VueStorefrontBridge_AbstractController | ||
{ | ||
const ES_DATA_TYPE_STRING = 'text'; | ||
const ES_DATA_TYPE_FLOAT = 'float'; | ||
const ES_DATA_TYPE_INT = 'integer'; | ||
const ES_DATA_TYPE_DATE = 'date'; | ||
const ES_DATA_TYPE_OBJECT = 'object'; | ||
const ES_DATA_TYPE_KEYWORD = 'keyword'; | ||
const ES_DATA_TYPE_BOOLEAN = 'boolean'; | ||
|
||
/** | ||
* elasticsearch <=> magento type mappings | ||
* @var array | ||
*/ | ||
protected $_mapAttributeType = array( | ||
'varchar' => self::ES_DATA_TYPE_STRING, | ||
'text' => self::ES_DATA_TYPE_STRING, | ||
'decimal' => self::ES_DATA_TYPE_FLOAT, | ||
'int' => self::ES_DATA_TYPE_INT, | ||
'smallint' => self::ES_DATA_TYPE_INT, | ||
'timestamp' => self::ES_DATA_TYPE_DATE, | ||
'datetime' => self::ES_DATA_TYPE_DATE, | ||
'static' => self::ES_DATA_TYPE_STRING, | ||
); | ||
|
||
public function indexAction() | ||
{ | ||
if($this->_authorize($this->getRequest())) { | ||
|
||
$params = $this->_processParams($this->getRequest()); | ||
if ($this->_authorize($this->getRequest())) { | ||
$attrList = array(); | ||
$params = $this->_processParams($this->getRequest()); | ||
$productAttrs = Mage::getResourceModel('catalog/product_attribute_collection'); | ||
|
||
$attrList = array(); | ||
foreach ($productAttrs as $productAttr) { | ||
$options = array(); | ||
$productAttrDTO = $productAttr->getData(); | ||
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $productAttr */ | ||
$attribute = Mage::getSingleton('eav/config') | ||
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $productAttr->getAttributeCode()); | ||
->getAttribute( | ||
Mage_Catalog_Model_Product::ENTITY, | ||
$productAttr->getAttributeCode() | ||
); | ||
|
||
$options = array(); | ||
if ($attribute->usesSource()) { | ||
$options = $attribute->getSource()->getAllOptions(false); | ||
} | ||
|
||
$productAttrDTO = $productAttr->getData(); | ||
if (in_array($productAttrDTO['source_model'], array('core/design_source_design'))) { | ||
continue; | ||
} // exception - this attribute has string typed values; this is not acceptable by VS | ||
|
||
if (in_array($productAttrDTO['source_model'], array('core/design_source_design'))) continue; // exception - this attribute has string typed values; this is not acceptable by VS | ||
$productAttrDTO['id'] = intval($productAttr->getAttributeId()); | ||
$productAttrDTO['options'] = $options; | ||
$productAttrDTO['default_value'] = (string)$productAttrDTO['default_value']; | ||
$productAttrDTO = $this->_filterDTO($productAttrDTO); | ||
$attrList[] = $productAttrDTO; | ||
} | ||
$this->_result(200, $attrList); | ||
} | ||
} | ||
|
||
$productAttrDTO['id'] = intval($productAttr->attribute_id); | ||
$productAttrDTO['options'] = $options; | ||
/** | ||
* Prepare attribute data for mappings | ||
*/ | ||
public function attributeInfoAction() | ||
{ | ||
if ($this->_authorize($this->getRequest())) { | ||
$result = array(); | ||
$attributeMapping = array(); | ||
$productAttributes = Mage::getResourceModel('catalog/product_attribute_collection'); | ||
foreach ($productAttributes as $productAttribute) { | ||
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $productAttr */ | ||
$attribute = Mage::getSingleton('eav/config') | ||
->getAttribute( | ||
Mage_Catalog_Model_Product::ENTITY, | ||
$productAttribute->getAttributeCode() | ||
); | ||
|
||
$productAttrDTO = $this->_filterDTO($productAttrDTO); | ||
$backendType = $attribute->getBackendType(); | ||
$attributeCode = $productAttribute->getAttributeCode(); | ||
if (strstr($attributeCode, 'is_') || strstr($attributeCode, 'has_')) { | ||
$fieldType = self::ES_DATA_TYPE_BOOLEAN; | ||
} else { | ||
$fieldType = $this->_getElasticsearchTypeByMagentoType($backendType); | ||
} | ||
|
||
$attributeMapping[$attributeCode]['type'] = $fieldType; | ||
if ($backendType == 'timestamp' || $backendType == 'datetime') { | ||
$attributeMapping[$attributeCode]['format'] = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"; | ||
} | ||
|
||
$attrList[] = $productAttrDTO; | ||
if (($attribute->getUsedForSortBy() | ||
|| $attribute->getIsFilterableInSearch() | ||
|| $attribute->getIsFilterable()) | ||
&& $fieldType == self::ES_DATA_TYPE_STRING | ||
) { | ||
$attributeMapping[$attributeCode]['fielddata'] = true; | ||
} | ||
} | ||
$this->_result(200, $attrList); | ||
$attributeMapping['final_price']['type'] = self::ES_DATA_TYPE_FLOAT; | ||
$attributeMapping['configurable_children']['properties'] = $attributeMapping; | ||
|
||
unset($attributeMapping['created_at']); | ||
unset($attributeMapping['updated_at']); | ||
|
||
$result['properties'] = $attributeMapping; | ||
$this->_result(200, $result); | ||
} | ||
} | ||
|
||
/** | ||
* Elasticsearch <=> magento type mapping | ||
* | ||
* @param $magentoType | ||
* | ||
* @return mixed | ||
*/ | ||
protected function _getElasticsearchTypeByMagentoType($magentoType) | ||
{ | ||
if (!isset($this->_mapAttributeType[$magentoType])) { | ||
return $magentoType; | ||
} | ||
|
||
return $this->_mapAttributeType[$magentoType]; | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.