PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Dataflow/Model/Convert/Mapper/Column.php

https://github.com/FiveDigital/magento2
PHP | 151 lines | 71 code | 19 blank | 61 comment | 8 complexity | 313bac7e248a878acd2f508bbb7334cf MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Dataflow
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Convert column mapper
  28. *
  29. * @category Mage
  30. * @package Mage_Dataflow
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Dataflow_Model_Convert_Mapper_Column extends Mage_Dataflow_Model_Convert_Mapper_Abstract
  34. {
  35. /**
  36. * Dataflow batch model
  37. *
  38. * @var Mage_Dataflow_Model_Batch
  39. */
  40. protected $_batch;
  41. /**
  42. * Dataflow batch export model
  43. *
  44. * @var Mage_Dataflow_Model_Batch_Export
  45. */
  46. protected $_batchExport;
  47. /**
  48. * Dataflow batch import model
  49. *
  50. * @var Mage_Dataflow_Model_Batch_Import
  51. */
  52. protected $_batchImport;
  53. /**
  54. * Retrieve Batch model singleton
  55. *
  56. * @return Mage_Dataflow_Model_Batch
  57. */
  58. public function getBatchModel()
  59. {
  60. if (is_null($this->_batch)) {
  61. $this->_batch = Mage::getSingleton('Mage_Dataflow_Model_Batch');
  62. }
  63. return $this->_batch;
  64. }
  65. /**
  66. * Retrieve Batch export model
  67. *
  68. * @return Mage_Dataflow_Model_Batch_Export
  69. */
  70. public function getBatchExportModel()
  71. {
  72. if (is_null($this->_batchExport)) {
  73. $object = Mage::getModel('Mage_Dataflow_Model_Batch_Export');
  74. $this->_batchExport = Varien_Object_Cache::singleton()->save($object);
  75. }
  76. return Varien_Object_Cache::singleton()->load($this->_batchExport);
  77. }
  78. /**
  79. * Retrieve Batch import model
  80. *
  81. * @return Mage_Dataflow_Model_Import_Export
  82. */
  83. public function getBatchImportModel()
  84. {
  85. if (is_null($this->_batchImport)) {
  86. $object = Mage::getModel('Mage_Dataflow_Model_Batch_Import');
  87. $this->_batchImport = Varien_Object_Cache::singleton()->save($object);
  88. }
  89. return Varien_Object_Cache::singleton()->load($this->_batchImport);
  90. }
  91. public function map()
  92. {
  93. $batchModel = $this->getBatchModel();
  94. $batchExport = $this->getBatchExportModel();
  95. $batchExportIds = $batchExport
  96. ->setBatchId($this->getBatchModel()->getId())
  97. ->getIdCollection();
  98. $onlySpecified = (bool)$this->getVar('_only_specified') === true;
  99. if (!$onlySpecified) {
  100. foreach ($batchExportIds as $batchExportId) {
  101. $batchExport->load($batchExportId);
  102. $batchModel->parseFieldList($batchExport->getBatchData());
  103. }
  104. return $this;
  105. }
  106. if ($this->getVar('map') && is_array($this->getVar('map'))) {
  107. $attributesToSelect = $this->getVar('map');
  108. }
  109. else {
  110. $attributesToSelect = array();
  111. }
  112. if (!$attributesToSelect) {
  113. $this->getBatchExportModel()
  114. ->setBatchId($this->getBatchModel()->getId())
  115. ->deleteCollection();
  116. throw new Exception(Mage::helper('Mage_Dataflow_Helper_Data')->__('Error in field mapping: field list for mapping is not defined.'));
  117. }
  118. foreach ($batchExportIds as $batchExportId) {
  119. $batchExport = $this->getBatchExportModel()->load($batchExportId);
  120. $row = $batchExport->getBatchData();
  121. $newRow = array();
  122. foreach ($attributesToSelect as $field => $mapField) {
  123. $newRow[$mapField] = isset($row[$field]) ? $row[$field] : null;
  124. }
  125. $batchExport->setBatchData($newRow)
  126. ->setStatus(2)
  127. ->save();
  128. $this->getBatchModel()->parseFieldList($batchExport->getBatchData());
  129. }
  130. return $this;
  131. }
  132. }