public function catalogProductImportFinishBefore($observer) { $adapter = $observer->getEvent()->getAdapter(); Mage_ImportExport_Model_Import::getDataSourceModel()->getIterator()->rewind(); $model = Mage::getModel('catalog/product'); $skus = array(); while ($bunch = $adapter->getNextBunch()) { foreach ($bunch as $rowData) { if (null !== $rowData['sku']) { $skus[] = $rowData['sku']; } } } if (!empty($skus)) { $resource = Mage::getResourceModel('jr_stockhistory/stock_history'); $productIds = $resource->getProductsIdBySku($skus); if (!empty($productIds)) { $stock = Mage::getSingleton('cataloginventory/stock'); $stocks = Mage::getResourceModel('cataloginventory/stock')->getProductsStock($stock, $productIds); $stocksHistory = array(); $datetime = Varien_Date::formatDate(time()); foreach ($stocks as $stockData) { $stocksHistory[] = array('item_id' => $stockData['item_id'], 'user' => $this->_getUsername(), 'user_id' => $this->_getUserId(), 'qty' => $stockData['qty'], 'is_in_stock' => (int) $stockData['is_in_stock'], 'message' => 'Product import', 'created_at' => $datetime); } if (!empty($stocksHistory)) { $resource->insertStocksHistory($stocksHistory); } } } }
/** * Add behaviour fieldsets to expected fieldsets * * @static */ public static function setUpBeforeClass() { $uniqueBehaviors = Mage_ImportExport_Model_Import::getUniqueEntityBehaviors(); foreach (array_keys($uniqueBehaviors) as $behavior) { self::$_expectedFieldsets[] = $behavior . '_fieldset'; } }
public function catalogProductImportFinishBefore($observer) { $productIds = array(); $adapter = $observer->getEvent()->getAdapter(); $resource = Mage::getResourceModel('bubble_stockmovements/stock_movement'); if ($adapter instanceof Mage_Catalog_Model_Convert_Adapter_Product) { $productIds = $adapter->getAffectedEntityIds(); } else { Mage_ImportExport_Model_Import::getDataSourceModel()->getIterator()->rewind(); $skus = array(); while ($bunch = $adapter->getNextBunch()) { foreach ($bunch as $rowData) { if (null !== $rowData['sku']) { $skus[] = $rowData['sku']; } } } if (!empty($skus)) { $productIds = $resource->getProductsIdBySku($skus); } } if (!empty($productIds)) { $stock = Mage::getSingleton('cataloginventory/stock'); $stocks = Mage::getResourceModel('cataloginventory/stock')->getProductsStock($stock, $productIds); $stocksMovements = array(); $datetime = Varien_Date::formatDate(time()); foreach ($stocks as $stockData) { $stocksMovements[] = array('item_id' => $stockData['item_id'], 'user' => $this->_getUsername(), 'user_id' => $this->_getUserId(), 'qty' => $stockData['qty'], 'is_in_stock' => (int) $stockData['is_in_stock'], 'message' => 'Product import', 'created_at' => $datetime); } if (!empty($stocksMovements)) { $resource->insertStocksMovements($stocksMovements); } } }
/** * Retrive model behavior * * @return string */ public function getBehavior() { if (is_null($this->_behavior)) { $this->_behavior = Mage_ImportExport_Model_Import::getDataSourceModel()->getBehavior(); } return $this->_behavior; }
/** * Adds bundle price type to internal attributes. * * @return Danslo_ApiImport_Model_Import_Entity_Product_Type_Bundle */ public function _initAttributes() { parent::_initAttributes(); // Price type does not live in an attribute set, so it is not picked up by abstract _initAttributes method. We add it here manually. $attribute = Mage::getResourceModel('catalog/eav_attribute')->load('price_type', 'attribute_code'); foreach (array_keys($this->_attributes) as $attrSetName) { $this->_addAttributeParams($attrSetName, array('id' => $attribute->getId(), 'code' => $attribute->getAttributeCode(), 'for_configurable' => $attribute->getIsConfigurable(), 'is_global' => $attribute->getIsGlobal(), 'is_required' => $attribute->getIsRequired(), 'is_unique' => $attribute->getIsUnique(), 'frontend_label' => $attribute->getFrontendLabel(), 'is_static' => $attribute->isStatic(), 'apply_to' => $attribute->getApplyTo(), 'type' => Mage_ImportExport_Model_Import::getAttributeType($attribute), 'default_value' => strlen($attribute->getDefaultValue()) ? $attribute->getDefaultValue() : null, 'options' => $this->_entityModel->getAttributeOptions($attribute, $this->_indexValueAttributes))); } return $this; }
/** * Initialize attributes parameters for all attributes' sets. * * @return Mage_ImportExport_Model_Import_Entity_Product_Type_Abstract */ protected function _initAttributes() { // temporary storage for attributes' parameters to avoid double querying inside the loop $attributesCache = array(); foreach (Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($this->_entityModel->getEntityTypeId()) as $attributeSet) { foreach (Mage::getResourceModel('catalog/product_attribute_collection')->setAttributeSetFilter($attributeSet->getId()) as $attribute) { $attributeCode = $attribute->getAttributeCode(); $attributeId = $attribute->getId(); if ($attribute->getIsVisible() || in_array($attributeCode, $this->_forcedAttributesCodes)) { if (!isset($attributesCache[$attributeId])) { $attributesCache[$attributeId] = array('id' => $attributeId, 'code' => $attributeCode, 'for_configurable' => $attribute->getIsConfigurable(), 'is_global' => $attribute->getIsGlobal(), 'is_required' => $attribute->getIsRequired(), 'frontend_label' => $attribute->getFrontendLabel(), 'frontend_input' => $attribute->getFrontendInput(), 'is_static' => $attribute->isStatic(), 'apply_to' => $attribute->getApplyTo(), 'type' => Mage_ImportExport_Model_Import::getAttributeType($attribute), 'default_value' => strlen($attribute->getDefaultValue()) ? $attribute->getDefaultValue() : null, 'options' => $this->_entityModel->getAttributeOptions($attribute, $this->_indexValueAttributes)); } $this->_addAttributeParams($attributeSet->getAttributeSetName(), $attributesCache[$attributeId]); } } } return $this; }
/** * Test for method _saveValidatedBunches() */ public function testSaveValidatedBunches() { $source = new Mage_ImportExport_Model_Import_Adapter_Csv(__DIR__ . '/Entity/Eav/_files/customers_for_validation_test.csv'); $expected = $source->current(); /** @var $model Mage_ImportExport_Model_Import_EntityAbstract|PHPUnit_Framework_MockObject_MockObject */ $model = $this->getMockForAbstractClass('Mage_ImportExport_Model_Import_EntityAbstract'); $model->expects($this->any())->method('validateRow')->will($this->returnValue(true)); $model->expects($this->any())->method('getEntityTypeCode')->will($this->returnValue('customer')); $model->setSource($source); $method = new ReflectionMethod($model, '_saveValidatedBunches'); $method->setAccessible(true); $method->invoke($model); $dataSourceModel = Mage_ImportExport_Model_Import::getDataSourceModel(); $this->assertCount(1, $dataSourceModel->getIterator()); $bunch = $dataSourceModel->getNextBunch(); $this->assertEquals($expected, $bunch[0]); //Delete created bunch from DB $dataSourceModel->cleanBunches(); }
/** * Add fieldsets * * @return Mage_ImportExport_Block_Adminhtml_Import_Edit_Form */ protected function _prepareForm() { $helper = Mage::helper('Mage_ImportExport_Helper_Data'); $form = new Varien_Data_Form(array('id' => 'edit_form', 'action' => $this->getUrl('*/*/validate'), 'method' => 'post', 'enctype' => 'multipart/form-data')); // base fieldset /** @var $importEntity Mage_ImportExport_Model_Source_Import_Entity */ $importEntity = Mage::getModel('Mage_ImportExport_Model_Source_Import_Entity'); $fieldsets['base'] = $form->addFieldset('base_fieldset', array('legend' => $helper->__('Import Settings'))); $fieldsets['base']->addField('entity', 'select', array('name' => 'entity', 'title' => $helper->__('Entity Type'), 'label' => $helper->__('Entity Type'), 'required' => true, 'onchange' => 'varienImport.handleEntityTypeSelector();', 'values' => $importEntity->toOptionArray())); // add behaviour fieldsets $uniqueBehaviors = Mage_ImportExport_Model_Import::getUniqueEntityBehaviors(); foreach ($uniqueBehaviors as $behaviorCode => $behaviorClass) { $fieldsets[$behaviorCode] = $form->addFieldset($behaviorCode . '_fieldset', array('legend' => $helper->__('Import Behavior'), 'style' => 'display:none')); /** @var $behaviorSource Mage_ImportExport_Model_Source_Import_BehaviorAbstract */ $behaviorSource = Mage::getModel($behaviorClass); $fieldsets[$behaviorCode]->addField($behaviorCode, 'select', array('name' => 'behavior', 'title' => $helper->__('Import Behavior'), 'label' => $helper->__('Import Behavior'), 'required' => true, 'disabled' => true, 'values' => $behaviorSource->toOptionArray())); } // fieldset for file uploading $fieldsets['upload'] = $form->addFieldset('upload_file_fieldset', array('legend' => $helper->__('File to Import'), 'style' => 'display:none')); $fieldsets['upload']->addField(Mage_ImportExport_Model_Import::FIELD_NAME_SOURCE_FILE, 'file', array('name' => Mage_ImportExport_Model_Import::FIELD_NAME_SOURCE_FILE, 'label' => $helper->__('Select File to Import'), 'title' => $helper->__('Select File to Import'), 'required' => true)); $form->setUseContainer(true); $this->setForm($form); return parent::_prepareForm(); }
/** * Constructor * * @param array $data */ public function __construct(array $data = array()) { if (isset($data['helpers'])) { $this->_helpers = $data['helpers']; } $this->_dataSourceModel = isset($data['data_source_model']) ? $data['data_source_model'] : Mage_ImportExport_Model_Import::getDataSourceModel(); $this->_connection = isset($data['connection']) ? $data['connection'] : Mage::getSingleton('Mage_Core_Model_Resource')->getConnection('write'); $this->_jsonHelper = isset($data['json_helper']) ? $data['json_helper'] : Mage::helper('Mage_Core_Helper_Data'); $this->_stringHelper = isset($data['string_helper']) ? $data['string_helper'] : Mage::helper('Mage_Core_Helper_String'); $this->_pageSize = isset($data['page_size']) ? $data['page_size'] : (static::XML_PATH_PAGE_SIZE ? (int) Mage::getStoreConfig(static::XML_PATH_PAGE_SIZE) : 0); $this->_maxDataSize = isset($data['max_data_size']) ? $data['max_data_size'] : Mage::getResourceHelper('Mage_ImportExport')->getMaxDataSize(); $this->_bunchSize = isset($data['bunch_size']) ? $data['bunch_size'] : (static::XML_PATH_BUNCH_SIZE ? (int) Mage::getStoreConfig(static::XML_PATH_BUNCH_SIZE) : 0); }
/** * Constructor. * */ public function __construct() { $this->_dataSourceModel = Mage_ImportExport_Model_Import::getDataSourceModel(); $this->_connection = Mage::getSingleton('core/resource')->getConnection('write'); $this->_initCategories(); $this->_entityTable = Mage::getModel('catalog/category')->getResource()->getTable('catalog/category_product'); $this->_productEntityTable = Mage::getModel('catalog/product')->getResource()->getEntityTable(); }
/** * Initialize customer attributes. * * @return Mage_ImportExport_Model_Import_Entity_Customer */ protected function _initAttributes() { $collection = Mage::getResourceModel('customer/attribute_collection')->addSystemHiddenFilterWithPasswordHash(); foreach ($collection as $attribute) { $this->_attributes[$attribute->getAttributeCode()] = array('id' => $attribute->getId(), 'is_required' => $attribute->getIsRequired(), 'is_static' => $attribute->isStatic(), 'rules' => $attribute->getValidateRules() ? unserialize($attribute->getValidateRules()) : null, 'type' => Mage_ImportExport_Model_Import::getAttributeType($attribute), 'options' => $this->getAttributeOptions($attribute)); } return $this; }
/** * Initialize customer address attributes. * * @return Mage_ImportExport_Model_Import_Entity_Customer_Address */ protected function _initAttributes() { $addrCollection = Mage::getResourceModel('customer/address_attribute_collection')->addSystemHiddenFilter()->addExcludeHiddenFrontendFilter(); foreach ($addrCollection as $attribute) { $this->_attributes[self::getColNameForAttrCode($attribute->getAttributeCode())] = array('id' => $attribute->getId(), 'code' => $attribute->getAttributeCode(), 'table' => $attribute->getBackend()->getTable(), 'is_required' => $attribute->getIsRequired(), 'rules' => $attribute->getValidateRules() ? unserialize($attribute->getValidateRules()) : null, 'type' => Mage_ImportExport_Model_Import::getAttributeType($attribute), 'options' => $this->getAttributeOptions($attribute)); } return $this; }
/** * Initialize attribute option values and types. * * @return Mage_ImportExport_Model_Export_Entity_Product */ protected function _initAttributes() { foreach ($this->getAttributeCollection() as $attribute) { $this->_attributeValues[$attribute->getAttributeCode()] = $this->getAttributeOptions($attribute); $this->_attributeTypes[$attribute->getAttributeCode()] = Mage_ImportExport_Model_Import::getAttributeType($attribute); } return $this; }
/** * Initialize source entities and collections * * @param array $data * @return Mage_ImportExport_Model_Import_Entity_Product_Option */ protected function _initSourceEntities(array $data) { if (isset($data['data_source_model'])) { $this->_dataSourceModel = $data['data_source_model']; } else { $this->_dataSourceModel = Mage_ImportExport_Model_Import::getDataSourceModel(); } if (isset($data['product_model'])) { $this->_productModel = $data['product_model']; } else { $this->_productModel = Mage::getModel('Mage_Catalog_Model_Product'); } if (isset($data['option_collection'])) { $this->_optionCollection = $data['option_collection']; } else { $this->_optionCollection = Mage::getResourceModel('Mage_Catalog_Model_Resource_Product_Option_Collection'); } if (isset($data['product_entity'])) { $this->_productEntity = $data['product_entity']; } else { Mage::throwException($this->_helper('Mage_ImportExport_Helper_Data')->__('Option entity must have a parent product entity.')); } if (isset($data['collection_by_pages_iterator'])) { $this->_byPagesIterator = $data['collection_by_pages_iterator']; } else { $this->_byPagesIterator = Mage::getResourceModel('Mage_ImportExport_Model_Resource_CollectionByPagesIterator'); } if (isset($data['page_size'])) { $this->_pageSize = $data['page_size']; } else { $this->_pageSize = self::XML_PATH_PAGE_SIZE ? (int) Mage::getStoreConfig(self::XML_PATH_PAGE_SIZE) : 0; } return $this; }
/** * Initialize entity attributes * * @return Mage_ImportExport_Model_Import_Entity_EavAbstract */ protected function _initAttributes() { /** @var $attribute Mage_Eav_Model_Attribute */ foreach ($this->_attributeCollection as $attribute) { $this->_attributes[$attribute->getAttributeCode()] = array('id' => $attribute->getId(), 'code' => $attribute->getAttributeCode(), 'table' => $attribute->getBackend()->getTable(), 'is_required' => $attribute->getIsRequired(), 'is_static' => $attribute->isStatic(), 'rules' => $attribute->getValidateRules() ? unserialize($attribute->getValidateRules()) : null, 'type' => Mage_ImportExport_Model_Import::getAttributeType($attribute), 'options' => $this->getAttributeOptions($attribute)); } return $this; }
/** * Import behavior getter. * * @return string */ public function getBehavior() { if (!isset($this->_parameters['behavior']) || $this->_parameters['behavior'] != Mage_ImportExport_Model_Import::BEHAVIOR_APPEND && $this->_parameters['behavior'] != Mage_ImportExport_Model_Import::BEHAVIOR_REPLACE && $this->_parameters['behavior'] != Mage_ImportExport_Model_Import::BEHAVIOR_DELETE) { return Mage_ImportExport_Model_Import::getDefaultBehavior(); } return $this->_parameters['behavior']; }
/** * @expectedException Mage_Core_Exception * @expectedExceptionMessage Entity is unknown */ public function testGetEntityEntityIsNotSet() { $this->_model->getEntity(); }
/** * Initialize customer attributes. * * @return $this */ protected function _initAttributes() { $collection = Mage::getResourceModel('catalog/category_attribute_collection'); foreach ($collection as $attribute) { /** @var $attribute Mage_Eav_Model_Entity_Attribute */ $this->_attributes[$attribute->getAttributeCode()] = array('id' => $attribute->getId(), 'is_required' => $attribute->getIsRequired(), 'is_static' => $attribute->isStatic(), 'rules' => $attribute->getValidateRules() ? unserialize($attribute->getValidateRules()) : null, 'type' => Mage_ImportExport_Model_Import::getAttributeType($attribute), 'options' => $this->getAttributeOptions($attribute), 'attribute' => $attribute); } return $this; }
/** * Returns source adapter object. * * @param array $sourceData Array Source Data * * @return AvS_FastSimpleImport_Model_ArrayAdapter */ protected function _getSourceAdapter($sourceData) { if (is_array($sourceData)) { if ($this->getUseNestedArrays()) { return Mage::getModel('fastsimpleimport/nestedArrayAdapter', $sourceData); } else { return Mage::getModel('fastsimpleimport/arrayAdapter', $sourceData); } } return parent::_getSourceAdapter($sourceData); }
<?php /** * Magento * * NOTICE OF LICENSE * * This source file is subject to the Open Software License (OSL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/osl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@magentocommerce.com so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade Magento to newer * versions in the future. If you wish to customize Magento for your * needs please refer to http://www.magentocommerce.com for more information. * * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ $pattern = array('_attribute_set' => 'Default', '_type' => Mage_Catalog_Model_Product_Type::TYPE_SIMPLE, '_product_websites' => 'base', 'name' => 'Product %s', 'short_description' => 'Short desc %s', 'weight' => 1, 'description' => 'Description %s', 'sku' => 'product_dynamic_%s', 'price' => 10, 'visibility' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, 'status' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED, 'tax_class_id' => 0, 'is_in_stock' => 1, 'qty' => 100500, 'use_config_min_qty' => '1', 'use_config_backorders' => '1', 'use_config_min_sale_qty' => '1', 'use_config_max_sale_qty' => '1', 'use_config_notify_stock_qty' => '1', 'use_config_manage_stock' => '1', 'use_config_qty_increments' => '1', 'use_config_enable_qty_inc' => '1', 'stock_id' => Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID); $generator = new Magento_ImportExport_Fixture_Generator($pattern, 100000); $import = new Mage_ImportExport_Model_Import(array('entity' => 'catalog_product', 'behavior' => 'append')); // it is not obvious, but the validateSource() will actually save import queue data to DB $import->validateSource($generator); // this converts import queue into actual entities $import->importSource();