/app/code/core/Mage/Dataflow/Model/Convert/Mapper/Column.php
PHP | 151 lines | 71 code | 19 blank | 61 comment | 8 complexity | 313bac7e248a878acd2f508bbb7334cf MD5 | raw file
Possible License(s): CC-BY-SA-3.0
- <?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.
- *
- * @category Mage
- * @package Mage_Dataflow
- * @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)
- */
- /**
- * Convert column mapper
- *
- * @category Mage
- * @package Mage_Dataflow
- * @author Magento Core Team <core@magentocommerce.com>
- */
- class Mage_Dataflow_Model_Convert_Mapper_Column extends Mage_Dataflow_Model_Convert_Mapper_Abstract
- {
- /**
- * Dataflow batch model
- *
- * @var Mage_Dataflow_Model_Batch
- */
- protected $_batch;
- /**
- * Dataflow batch export model
- *
- * @var Mage_Dataflow_Model_Batch_Export
- */
- protected $_batchExport;
- /**
- * Dataflow batch import model
- *
- * @var Mage_Dataflow_Model_Batch_Import
- */
- protected $_batchImport;
- /**
- * Retrieve Batch model singleton
- *
- * @return Mage_Dataflow_Model_Batch
- */
- public function getBatchModel()
- {
- if (is_null($this->_batch)) {
- $this->_batch = Mage::getSingleton('Mage_Dataflow_Model_Batch');
- }
- return $this->_batch;
- }
- /**
- * Retrieve Batch export model
- *
- * @return Mage_Dataflow_Model_Batch_Export
- */
- public function getBatchExportModel()
- {
- if (is_null($this->_batchExport)) {
- $object = Mage::getModel('Mage_Dataflow_Model_Batch_Export');
- $this->_batchExport = Varien_Object_Cache::singleton()->save($object);
- }
- return Varien_Object_Cache::singleton()->load($this->_batchExport);
- }
- /**
- * Retrieve Batch import model
- *
- * @return Mage_Dataflow_Model_Import_Export
- */
- public function getBatchImportModel()
- {
- if (is_null($this->_batchImport)) {
- $object = Mage::getModel('Mage_Dataflow_Model_Batch_Import');
- $this->_batchImport = Varien_Object_Cache::singleton()->save($object);
- }
- return Varien_Object_Cache::singleton()->load($this->_batchImport);
- }
- public function map()
- {
- $batchModel = $this->getBatchModel();
- $batchExport = $this->getBatchExportModel();
- $batchExportIds = $batchExport
- ->setBatchId($this->getBatchModel()->getId())
- ->getIdCollection();
- $onlySpecified = (bool)$this->getVar('_only_specified') === true;
- if (!$onlySpecified) {
- foreach ($batchExportIds as $batchExportId) {
- $batchExport->load($batchExportId);
- $batchModel->parseFieldList($batchExport->getBatchData());
- }
- return $this;
- }
- if ($this->getVar('map') && is_array($this->getVar('map'))) {
- $attributesToSelect = $this->getVar('map');
- }
- else {
- $attributesToSelect = array();
- }
- if (!$attributesToSelect) {
- $this->getBatchExportModel()
- ->setBatchId($this->getBatchModel()->getId())
- ->deleteCollection();
- throw new Exception(Mage::helper('Mage_Dataflow_Helper_Data')->__('Error in field mapping: field list for mapping is not defined.'));
- }
- foreach ($batchExportIds as $batchExportId) {
- $batchExport = $this->getBatchExportModel()->load($batchExportId);
- $row = $batchExport->getBatchData();
- $newRow = array();
- foreach ($attributesToSelect as $field => $mapField) {
- $newRow[$mapField] = isset($row[$field]) ? $row[$field] : null;
- }
- $batchExport->setBatchData($newRow)
- ->setStatus(2)
- ->save();
- $this->getBatchModel()->parseFieldList($batchExport->getBatchData());
- }
- return $this;
- }
- }