AMartinez_CustomImportExport - Version 1.5.001

Version Notes

Magento 1.5 version. Please use UTF-8 csv files (to convert from any charset please use iconv linux command)

Download this release

Release Info

Developer Antonio Martinez
Extension AMartinez_CustomImportExport
Version 1.5.001
Comparing to
See all releases


Version 1.5.001

app/code/local/AMartinez/CustomImportExport/Helper/Data.php ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * @category AMartinez
6
+ * @package AMartinez_CustomImportExport
7
+ * @author Antonio Martinez
8
+ * @copyright Copyright (c) 2011 Antonio Martínez (toniyecla@gmail.com)
9
+ * @license http://opensource.org/licenses/osl-3.0 Open Software License (OSL 3.0)
10
+ */
11
+
12
+ /**
13
+ * Data Helper
14
+ */
15
+ class AMartinez_CustomImportExport_Helper_Data extends Mage_Core_Helper_Abstract
16
+ {
17
+
18
+ }
app/code/local/AMartinez/CustomImportExport/Model/Import.php ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * @category AMartinez
6
+ * @package AMartinez_CustomImportExport
7
+ * @author Antonio Martinez
8
+ * @copyright Copyright (c) 2011 Antonio Martínez (toniyecla@gmail.com)
9
+ * @license http://opensource.org/licenses/osl-3.0 Open Software License (OSL 3.0)
10
+ */
11
+
12
+ /**
13
+ * Import model
14
+ */
15
+ class AMartinez_CustomImportExport_Model_Import extends Mage_ImportExport_Model_Import
16
+ {
17
+ /**
18
+ * Returns source adapter object.
19
+ *
20
+ * @param string $sourceFile Full path to source file
21
+ * @return Mage_ImportExport_Model_Import_Adapter_Abstract
22
+ */
23
+ protected function _getSourceAdapter($sourceFile)
24
+ {
25
+ return AMartinez_CustomImportExport_Model_Import_Adapter::findAdapterFor($sourceFile);
26
+ }
27
+
28
+ /**
29
+ * Validates source file and returns validation result.
30
+ *
31
+ * @param string $sourceFile Full path to source file
32
+ * @return bool
33
+ */
34
+ public function validateSource($sourceFile)
35
+ {
36
+ $result = $this->_getEntityAdapter()
37
+ ->setSource($this->_getSourceAdapter($sourceFile))
38
+ ->isDataValid();
39
+
40
+ return $result;
41
+ }
42
+
43
+ /**
44
+ * Reindex data by all indexers
45
+ *
46
+ * @return array
47
+ */
48
+ public function reindexAll()
49
+ {
50
+ $processes = array();
51
+
52
+ $collection = Mage::getSingleton('index/indexer')->getProcessesCollection();
53
+ foreach ($collection as $process) {
54
+ $processes[] = $process;
55
+ }
56
+
57
+ foreach ($processes as $process) {
58
+ /* @var $process Mage_Index_Model_Process */
59
+ try {
60
+ $process->reindexEverything();
61
+ echo "\t" . $process->getIndexer()->getName() . " index was rebuilt successfully\n";
62
+ } catch (Mage_Core_Exception $e) {
63
+ echo $e->getMessage() . "\n";
64
+ } catch (Exception $e) {
65
+ echo "\t" . $process->getIndexer()->getName() . " index process unknown error:\n";
66
+ echo "\t" . $e . "\n";
67
+ }
68
+ }
69
+
70
+ return $processes;
71
+ }
72
+
73
+ /**
74
+ * Clean cache
75
+ */
76
+ public function cleanCache()
77
+ {
78
+ Mage::app()->cleanCache();
79
+ }
80
+
81
+ /**
82
+ * Flush catalog images cache
83
+ */
84
+ public function flushImages()
85
+ {
86
+ Mage::getModel('catalog/product_image')->clearCache();
87
+ }
88
+ }
app/code/local/AMartinez/CustomImportExport/Model/Import/Adapter.php ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * @category AMartinez
6
+ * @package AMartinez_CustomImportExport
7
+ * @author Antonio Martinez
8
+ * @copyright Copyright (c) 2011 Antonio Martínez (toniyecla@gmail.com)
9
+ * @license http://opensource.org/licenses/osl-3.0 Open Software License (OSL 3.0)
10
+ */
11
+
12
+ /**
13
+ * Import adapter model
14
+ */
15
+ class AMartinez_CustomImportExport_Model_Import_Adapter extends Mage_ImportExport_Model_Import_Adapter
16
+ {
17
+ /**
18
+ * Adapter factory. Checks for availability, loads and create instance of import adapter object.
19
+ *
20
+ * @param string $type Adapter type ('csv', 'xml' etc.)
21
+ * @param mixed $options OPTIONAL Adapter constructor options
22
+ * @throws Exception
23
+ * @return Mage_ImportExport_Model_Import_Adapter_Abstract
24
+ */
25
+ public static function factory($type, $options = null)
26
+ {
27
+ if (!is_string($type) || !$type) {
28
+ Mage::throwException(Mage::helper('importexport')->__('Adapter type must be a non empty string'));
29
+ }
30
+ $adapterClass = __CLASS__ . '_' . ucfirst(strtolower($type));
31
+
32
+ if (!class_exists($adapterClass, false)) {
33
+ $adapterFile = str_replace('_', '/', $adapterClass) . '.php';
34
+ if (!@include_once($adapterFile)) {
35
+ $adapterClass = get_parent_class() . '_' . ucfirst(strtolower($type));
36
+ $adapterFile = str_replace('_', '/', $adapterClass) . '.php';
37
+ if (!@include_once($adapterFile)) {
38
+ Mage::throwException("'{$type}' file extension is not supported");
39
+ }
40
+ }
41
+ if (!class_exists($adapterClass, false)) {
42
+ Mage::throwException("Can not find adapter class {$adapterClass} in adapter file {$adapterFile}");
43
+ }
44
+ }
45
+ $adapter = new $adapterClass($options);
46
+
47
+ if (! $adapter instanceof Mage_ImportExport_Model_Import_Adapter_Abstract) {
48
+ Mage::throwException(
49
+ Mage::helper('importexport')->__('Adapter must be an instance of Mage_ImportExport_Model_Import_Adapter_Abstract')
50
+ );
51
+ }
52
+ return $adapter;
53
+ }
54
+
55
+ /**
56
+ * Create adapter instance for specified source file.
57
+ *
58
+ * @param string $source Source file path.
59
+ * @return Mage_ImportExport_Model_Import_Adapter_Abstract
60
+ */
61
+ public static function findAdapterFor($source)
62
+ {
63
+ return self::factory(pathinfo($source, PATHINFO_EXTENSION), $source);
64
+ }
65
+ }
app/code/local/AMartinez/CustomImportExport/Model/Import/Adapter/Csv1.php ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * @category AMartinez
6
+ * @package AMartinez_CustomImportExport
7
+ * @author Antonio Martinez
8
+ * @copyright Copyright (c) 2011 Antonio Martínez (toniyecla@gmail.com)
9
+ * @license http://opensource.org/licenses/osl-3.0 Open Software License (OSL 3.0)
10
+ */
11
+
12
+ /**
13
+ * CSV import adapter (previous version: all values in a single row, comma separated)
14
+ * e.g.: ..."store1,store2","visibility1,visibility2","CAT1,CAT2,CAT3","ASSOCIATED1,ASSOCIATED2"...
15
+ */
16
+ class AMartinez_CustomImportExport_Model_Import_Adapter_Csv1 extends Mage_ImportExport_Model_Import_Adapter_Abstract
17
+ {
18
+ /**
19
+ * Field delimiter.
20
+ *
21
+ * @var string
22
+ */
23
+ protected $_delimiter = ',';
24
+
25
+ /**
26
+ * Field enclosure character.
27
+ *
28
+ * @var string
29
+ */
30
+ protected $_enclosure = '"';
31
+
32
+ /**
33
+ * Source file handler.
34
+ *
35
+ * @var resource
36
+ */
37
+ protected $_fileHandler;
38
+
39
+ protected $_values = array();
40
+ protected $_currentOffsetKey = 0;
41
+ protected $_maxOffsetKey = 0;
42
+
43
+ /**
44
+ * Object destructor.
45
+ *
46
+ * @return void
47
+ */
48
+ public function __destruct()
49
+ {
50
+ if (is_resource($this->_fileHandler)) {
51
+ fclose($this->_fileHandler);
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Method called as last step of object instance creation. Can be overrided in child classes.
57
+ *
58
+ * @return AMartinez_CustomImportExport_Model_Import_Adapter_Abstract
59
+ */
60
+ protected function _init()
61
+ {
62
+ $this->_fileHandler = fopen($this->_source, 'r');
63
+ $this->rewind();
64
+ return $this;
65
+ }
66
+
67
+ /**
68
+ * Move forward to next element
69
+ *
70
+ * @return void Any returned value is ignored.
71
+ */
72
+ public function next()
73
+ {
74
+ if ($this->_maxOffsetKey > $this->_currentOffsetKey) {
75
+ $this->_currentOffsetKey ++;
76
+ $this->_setValues(array('_store', 'visibility', '_category', '_associated_sku'));
77
+ } else {
78
+ $this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure);
79
+ $this->_getValues(array('_store', 'visibility', '_category', '_associated_sku'));
80
+ }
81
+
82
+ $this->_currentKey = $this->_currentRow ? $this->_currentKey + 1 : null;
83
+ }
84
+
85
+ /**
86
+ * Rewind the Iterator to the first element.
87
+ *
88
+ * @return void Any returned value is ignored.
89
+ */
90
+ public function rewind()
91
+ {
92
+ // rewind resource, reset column names, read first row as current
93
+ rewind($this->_fileHandler);
94
+ $this->_colNames = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure);
95
+ $this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure);
96
+ $this->_getValues(array('_store', 'visibility', '_category', '_associated_sku'));
97
+
98
+ if ($this->_currentRow) {
99
+ $this->_currentKey = 0;
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Seeks to a position.
105
+ *
106
+ * @param int $position The position to seek to.
107
+ * @throws OutOfBoundsException
108
+ * @return void
109
+ */
110
+ public function seek($position)
111
+ {
112
+ if ($position != $this->_currentKey) {
113
+ if (0 == $position) {
114
+ return $this->rewind();
115
+ } elseif ($position > 0) {
116
+ if ($position < $this->_currentKey) {
117
+ $this->rewind();
118
+ }
119
+ while ($this->_currentRow = fgetcsv($this->_fileHandler, null, $this->_delimiter, $this->_enclosure)) {
120
+ $this->_getValues(array('_store', 'visibility', '_category', '_associated_sku'));
121
+ do {
122
+ if (++ $this->_currentKey == $position) {
123
+ $this->_setValues(array('_store', 'visibility', '_category', '_associated_sku'));
124
+ return;
125
+ }
126
+ } while ($this->_maxOffsetKey > $this->_currentOffsetKey ++);
127
+ }
128
+ }
129
+ throw new OutOfBoundsException(Mage::helper('importexport')->__('Invalid seek position'));
130
+ }
131
+ }
132
+
133
+ protected function _getValues($colNames)
134
+ {
135
+ $this->_values = array();
136
+ $this->_currentOffsetKey = 0;
137
+ $this->_maxOffsetKey = 0;
138
+
139
+ foreach ($colNames as $colName)
140
+ {
141
+ $keyId = array_search($colName, $this->_colNames);
142
+ $values = explode(",", $this->_currentRow[$keyId]);
143
+ $this->_values[$colName] = $values;
144
+ $this->_maxOffsetKey = max($this->_maxOffsetKey, count($values) - 1);
145
+ }
146
+
147
+ $this->_setValues($colNames);
148
+ }
149
+
150
+ protected function _setValues($colNames)
151
+ {
152
+ if ($this->_currentOffsetKey > 0) {
153
+ foreach ($this->_currentRow as $key => $value) {
154
+ $this->_currentRow[$key] = "";
155
+ }
156
+ }
157
+
158
+ foreach ($colNames as $colName) {
159
+ if ($this->_currentOffsetKey < count($this->_values[$colName]) && $this->_values[$colName][$this->_currentOffsetKey]) {
160
+ $keyId = array_search($colName, $this->_colNames);
161
+ $this->_currentRow[$keyId] = $this->_values[$colName][$this->_currentOffsetKey];
162
+ }
163
+ }
164
+ }
165
+ }
app/code/local/AMartinez/CustomImportExport/Model/Import/Entity/Product.php ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * @category AMartinez
6
+ * @package AMartinez_CustomImportExport
7
+ * @author Antonio Martinez
8
+ * @copyright Copyright (c) 2011 Antonio Martínez (toniyecla@gmail.com)
9
+ * @license http://opensource.org/licenses/osl-3.0 Open Software License (OSL 3.0)
10
+ */
11
+
12
+ /**
13
+ * Import entity product model
14
+ */
15
+ class AMartinez_CustomImportExport_Model_Import_Entity_Product extends Mage_ImportExport_Model_Import_Entity_Product
16
+ {
17
+ /* Mage_ImportExport_Model_Import_Entity_Abstract */
18
+
19
+ protected $_newAttrParams = array();
20
+
21
+ /**
22
+ * Check one attribute. Can be overridden in child.
23
+ *
24
+ * @param string $attrCode Attribute code
25
+ * @param array $attrParams Attribute params
26
+ * @param array $rowData Row data
27
+ * @param int $rowNum
28
+ * @return boolean
29
+ */
30
+ public function isAttributeValid($attrCode, array $attrParams, array $rowData, $rowNum)
31
+ {
32
+ switch ($attrParams['type']) {
33
+ case 'varchar':
34
+ $val = Mage::helper('core/string')->cleanString($rowData[$attrCode]);
35
+ //// convert to utf-8
36
+ //// $val = iconv('ISO-8859-1', 'UTF-8' . '//IGNORE//TRANSLIT', $val);
37
+ $valid = Mage::helper('core/string')->strlen($val) < self::DB_MAX_VARCHAR_LENGTH;
38
+ break;
39
+ case 'decimal':
40
+ $val = trim($rowData[$attrCode]);
41
+ $valid = (float)$val == $val;
42
+ break;
43
+ case 'select':
44
+ $valid = isset($attrParams['options'][strtolower($rowData[$attrCode])]);
45
+ if (!$valid) {
46
+ if (in_array($attrCode . ":" . $rowData[$attrCode], $this->_newAttrParams))
47
+ {
48
+ return true;
49
+ }
50
+ if ($this->parseAttributeOption($attrCode, $rowData[$attrCode]))
51
+ {
52
+ $this->_newAttrParams[] = $attrCode . ":" . $rowData[$attrCode];
53
+ // $this->addRowError(Mage::helper('importexport')->__("Attribute option added for '%s'"), $rowNum, $attrCode);
54
+ echo ":::: " . Mage::helper('importexport')->__("New attribute option added: ") . $attrCode . " - " . $rowData[$attrCode] . " (first ocurrence in line " . ($rowNum+1) . ") ::::\n";
55
+ $this->addRowError(Mage::helper('importexport')->__("Attribute option added for '%s'"), $rowNum, $attrCode);
56
+ }
57
+ return true;
58
+ }
59
+ break;
60
+ case 'int':
61
+ $val = trim($rowData[$attrCode]);
62
+ $valid = (int)$val == $val;
63
+ break;
64
+ case 'datetime':
65
+ $val = trim($rowData[$attrCode]);
66
+ $valid = strtotime($val)
67
+ || preg_match('/^\d{2}.\d{2}.\d{2,4}(?:\s+\d{1,2}.\d{1,2}(?:.\d{1,2})?)?$/', $val);
68
+ break;
69
+ case 'text':
70
+ $val = Mage::helper('core/string')->cleanString($rowData[$attrCode]);
71
+ //// convert to utf-8
72
+ //// $val = iconv('ISO-8859-1', 'UTF-8' . '//IGNORE//TRANSLIT', $val);
73
+ $valid = Mage::helper('core/string')->strlen($val) < self::DB_MAX_VARCHAR_LENGTH;
74
+ break;
75
+ default:
76
+ $valid = true;
77
+ break;
78
+ }
79
+ if (!$valid) {
80
+ $this->addRowError(Mage::helper('importexport')->__("Invalid value for '%s'"), $rowNum, $attrCode);
81
+ }
82
+ return (bool) $valid;
83
+ }
84
+
85
+ /* Mage_ImportExport_Model_Import_Entity_Product */
86
+
87
+ protected $_entity_type_id;
88
+ protected $eav_entity_setup;
89
+
90
+ /**
91
+ * Add attribute option
92
+ *
93
+ * @return true
94
+ */
95
+ function parseAttributeOption($code, $value)
96
+ {
97
+ if (!$this->_entity_type_id) {
98
+ $entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
99
+ }
100
+
101
+ if (!$this->eav_entity_setup) {
102
+ $eav_entity_setup = new Mage_Eav_Model_Entity_Setup('core_setup');
103
+ }
104
+
105
+ if ( !$id = $eav_entity_setup->getAttribute($entity_type_id, $code, 'attribute_id')) {
106
+ return false;
107
+ }
108
+
109
+ // $attribute = Mage::getModel('catalog/product')->setStoreId(0)->getResource()->getAttribute($id);
110
+ // $options = $this->getAttributeOptions($attribute);
111
+
112
+ $new_option['attribute_id'] = $id;
113
+ $new_option['value']['_custom_'.$value][0] = $value;
114
+ $eav_entity_setup->addAttributeOption($new_option);
115
+
116
+ return true;
117
+ }
118
+
119
+ /**
120
+ * Check product category validity
121
+ *
122
+ * @param array $rowData
123
+ * @param int $rowNum
124
+ * @return bool
125
+ */
126
+ protected function _isProductCategoryValid(array $rowData, $rowNum)
127
+ {
128
+ if (!empty($rowData[self::COL_CATEGORY]) && !isset($this->_categories[$rowData[self::COL_CATEGORY]])) {
129
+ if (!empty($rowData['_custom_option_store']) && isset($this->_storeCodeToId[$rowData['_custom_option_store']])) {
130
+ $storeId = $this->_storeCodeToId[$rowData['_custom_option_store']];
131
+ } else {
132
+ $storeId = $this->_storeCodeToId['default'];
133
+ }
134
+ if ($this->_addCategory($rowData[self::COL_CATEGORY], $storeId))
135
+ {
136
+ echo ":::: " . Mage::helper('importexport')->__("New category created: ") . $rowData[self::COL_CATEGORY] . " (first ocurrence in line " . ($rowNum+1) . ") ::::\n";
137
+ }
138
+ }
139
+ return true;
140
+ }
141
+
142
+ /**
143
+ * Add category as child of store_id root category
144
+ *
145
+ * @return true
146
+ */
147
+ function _addCategory($category, $storeId)
148
+ {
149
+ $store = Mage::app()->getStore($storeId);
150
+ $rootCategoryId = $store->getGroup()->root_category_id;
151
+ $rootCategoryPath = '1/' . $rootCategoryId;
152
+
153
+ $cat = Mage::getModel('catalog/category')
154
+ ->setStoreId(1)
155
+ ->setPath($rootCategoryPath)
156
+ ->setName(trim($category))
157
+ ->setIsAnchor(true)
158
+ ->setIncludeInMenu(true)
159
+ ->setIsActive(true)
160
+ ->save();
161
+
162
+ $this->_categories[$category] = $cat->getId();
163
+
164
+ return true;
165
+ }
166
+ }
app/code/local/AMartinez/CustomImportExport/Model/Import/Entity/Product/Type/Configurable.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * @category AMartinez
6
+ * @package AMartinez_CustomImportExport
7
+ * @author Antonio Martinez
8
+ * @copyright Copyright (c) 2011 Antonio Martínez (toniyecla@gmail.com)
9
+ * @license http://opensource.org/licenses/osl-3.0 Open Software License (OSL 3.0)
10
+ */
11
+
12
+ /**
13
+ * Import entity configurable product type model
14
+ */
15
+ class AMartinez_CustomImportExport_Model_Import_Entity_Product_Type_Configurable
16
+ extends Mage_ImportExport_Model_Import_Entity_Product_Type_Configurable
17
+ {
18
+
19
+ }
app/code/local/AMartinez/CustomImportExport/Model/Import/Entity/Product/Type/Giftcard.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * @category AMartinez
6
+ * @package AMartinez_CustomImportExport
7
+ * @author Antonio Martinez
8
+ * @copyright Copyright (c) 2011 Antonio Martínez (toniyecla@gmail.com)
9
+ * @license http://opensource.org/licenses/osl-3.0 Open Software License (OSL 3.0)
10
+ */
11
+
12
+ /**
13
+ * Import entity gift card product type model
14
+ */
15
+ class AMartinez_CustomImportExport_Model_Import_Entity_Product_Type_Giftcard
16
+ extends Mage_ImportExport_Model_Import_Entity_Product_Type_Giftcard
17
+ {
18
+
19
+ }
app/code/local/AMartinez/CustomImportExport/Model/Import/Entity/Product/Type/Grouped.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * @category AMartinez
6
+ * @package AMartinez_CustomImportExport
7
+ * @author Antonio Martinez
8
+ * @copyright Copyright (c) 2011 Antonio Martínez (toniyecla@gmail.com)
9
+ * @license http://opensource.org/licenses/osl-3.0 Open Software License (OSL 3.0)
10
+ */
11
+
12
+ /**
13
+ * Import entity grouped product type model
14
+ */
15
+ class AMartinez_CustomImportExport_Model_Import_Entity_Product_Type_Grouped
16
+ extends Mage_ImportExport_Model_Import_Entity_Product_Type_Grouped
17
+ {
18
+
19
+ }
app/code/local/AMartinez/CustomImportExport/Model/Import/Entity/Product/Type/Simple.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * @category AMartinez
6
+ * @package AMartinez_CustomImportExport
7
+ * @author Antonio Martinez
8
+ * @copyright Copyright (c) 2011 Antonio Martínez (toniyecla@gmail.com)
9
+ * @license http://opensource.org/licenses/osl-3.0 Open Software License (OSL 3.0)
10
+ */
11
+
12
+ /**
13
+ * Import entity simple product type
14
+ */
15
+ class AMartinez_CustomImportExport_Model_Import_Entity_Product_Type_Simple
16
+ extends Mage_ImportExport_Model_Import_Entity_Product_Type_Simple
17
+ {
18
+
19
+ }
app/code/local/AMartinez/CustomImportExport/etc/config.xml ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * Magento
5
+ *
6
+ * @category AMartinez
7
+ * @package AMartinez_CustomImportExport
8
+ * @author Antonio Martinez
9
+ * @copyright Copyright (c) 2011 Antonio Martínez (toniyecla@gmail.com)
10
+ * @license http://opensource.org/licenses/osl-3.0 Open Software License (OSL 3.0)
11
+ */
12
+ -->
13
+ <config>
14
+
15
+ <modules>
16
+ <AMartinez_CustomImportExport>
17
+ <version>1.5.001</version>
18
+ <depends>
19
+ <!-- no dependencies -->
20
+ </depends>
21
+ </AMartinez_CustomImportExport>
22
+ </modules>
23
+
24
+ <global>
25
+ <models>
26
+ <customimportexport>
27
+ <class>AMartinez_CustomImportExport_Model</class>
28
+ </customimportexport>
29
+ </models>
30
+ <helpers>
31
+ <customimportexport>
32
+ <class>AMartinez_CustomImportExport_Helper</class>
33
+ </customimportexport>
34
+ </helpers>
35
+ <resources />
36
+ <extraconfig />
37
+ <blocks />
38
+ <importexport module="importexport">
39
+ <import_entities>
40
+ <catalog_product translate="label">
41
+ <model_token>customimportexport/import_entity_product</model_token>
42
+ <label>Products</label>
43
+ </catalog_product>
44
+ <customer translate="label">
45
+ <model_token>importexport/import_entity_customer</model_token>
46
+ <label>Customers</label>
47
+ </customer>
48
+ </import_entities>
49
+ <import_product_types>
50
+ <simple>customimportexport/import_entity_product_type_simple</simple>
51
+ <configurable>customimportexport/import_entity_product_type_configurable</configurable>
52
+ <virtual>customimportexport/import_entity_product_type_simple</virtual>
53
+ <grouped>customimportexport/import_entity_product_type_grouped</grouped>
54
+ </import_product_types>
55
+ </importexport>
56
+ </global>
57
+
58
+ <default>
59
+ <customimportexport>
60
+ <products>
61
+ <file>var/customimportexport/productsimport.csv</file>
62
+ </products>
63
+ </customimportexport>
64
+ </default>
65
+
66
+ </config>
app/code/local/AMartinez/CustomImportExport/shell/run.php ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento
4
+ *
5
+ * @category AMartinez
6
+ * @package AMartinez_CustomImportExport
7
+ * @author Antonio Martinez
8
+ * @copyright Copyright (c) 2011 Antonio Martínez (toniyecla@gmail.com)
9
+ * @license http://opensource.org/licenses/osl-3.0 Open Software License (OSL 3.0)
10
+ */
11
+
12
+ /**
13
+ * Require the Magento abstract class for cli scripts
14
+ */
15
+ require_once dirname(__FILE__) . '/../../../../../../shell/abstract.php';
16
+
17
+ class AMartinez_CustomImportExport extends Mage_Shell_Abstract
18
+ {
19
+
20
+ /**
21
+ * Trigger the import
22
+ */
23
+ public function run()
24
+ {
25
+ Mage::setIsDeveloperMode(true);
26
+ ini_set('display_errors', 1);
27
+ // ini_set("memory_limit","2048M");
28
+
29
+ $file = $this->getFile();
30
+ echo "Found $file\n";
31
+ Mage::log("Starting import $file", Zend_Log::DEBUG);
32
+
33
+ $import = $this->processData();
34
+ $validationResult = $import->validateSource($file);
35
+ $processedRowsCount = $import->getProcessedRowsCount();
36
+
37
+ if ($processedRowsCount > 0)
38
+ {
39
+ // if type 'select' attribute options added, revalidate source (not necessary for categories)
40
+ foreach ($import->getErrors() as $type => $lines)
41
+ {
42
+ if (strpos($type, "added"))
43
+ {
44
+ $import = $this->processData();
45
+ $validationResult = $import->validateSource($file);
46
+ $processedRowsCount = $import->getProcessedRowsCount();
47
+ break;
48
+ }
49
+ }
50
+
51
+ if (!$validationResult)
52
+ {
53
+ $message = sprintf("File %s contains %s corrupt records (from a total of %s)",
54
+ $file, $import->getInvalidRowsCount(), $processedRowsCount
55
+ );
56
+ foreach ($import->getErrors() as $type => $lines)
57
+ {
58
+ $message .= "\n:::: " . $type . " ::::\nIn Line(s) " . implode(", ", $lines) . "\n";
59
+ }
60
+ Mage::throwException($message);
61
+ }
62
+
63
+ $import->importSource();
64
+ }
65
+ echo "Processed rows count: " . $processedRowsCount . "\n";
66
+
67
+ if ($this->getArg('r') || $this->getArg('reindex'))
68
+ {
69
+ echo "Reindexing data...\n";
70
+ try
71
+ {
72
+ $import->reindexAll();
73
+ }
74
+ catch (Exception $e) {}
75
+ }
76
+
77
+ if ($this->getArg('c') || $this->getArg('cleancache'))
78
+ {
79
+ echo "Cleaning cache...\n";
80
+ try
81
+ {
82
+ $import->cleanCache();
83
+ }
84
+ catch (Exception $e) {}
85
+ }
86
+
87
+ if ($this->getArg('f') || $this->getArg('flushimages'))
88
+ {
89
+ echo "Erasing catalog images cache...\n";
90
+ try
91
+ {
92
+ $import->flushImages();
93
+ }
94
+ catch (Exception $e) {} }
95
+ }
96
+
97
+ /**
98
+ * Return the specified source file
99
+ *
100
+ * @return string
101
+ */
102
+ public function getFile()
103
+ {
104
+ if ($file = $this->getArg('s'))
105
+ {
106
+ if(file_exists($file)) {
107
+ return $file;
108
+ }
109
+ else
110
+ {
111
+ echo "Skipping file $file\n";
112
+ }
113
+ }
114
+ if ($file = $this->getArg('source'))
115
+ {
116
+ if(file_exists($file)) {
117
+ return $file;
118
+ }
119
+ else
120
+ {
121
+ echo "Skipping file $file\n";
122
+ }
123
+ }
124
+ if ($file = Mage::getStoreConfig('customimportexport/products/file'))
125
+ {
126
+ if(file_exists($file)) {
127
+ return $file;
128
+ }
129
+ else
130
+ {
131
+ echo "Skipping file $file\n";
132
+ }
133
+ }
134
+ die($this->usageHelp());
135
+ }
136
+
137
+ /**
138
+ * Initialize vars and verify source file
139
+ *
140
+ * @return AMartinez_CustomImportExport_Model_Import
141
+ */
142
+ public function processData()
143
+ {
144
+ $import = Mage::getModel('customimportexport/import');
145
+ $import->setEntity('catalog_product');
146
+ $import->setBehavior('replace');
147
+
148
+ return $import;
149
+ }
150
+
151
+ /**
152
+ * Retrieve usage help message
153
+ */
154
+ public function usageHelp()
155
+ {
156
+ return <<<HELP
157
+
158
+ AMartinez_CustomImportExport script
159
+
160
+ NAME
161
+ run.php
162
+
163
+ SYNOPSIS
164
+ php -f app/code/local/AMartinez/CustomImportExport/shell/run.php
165
+ php -f app/code/local/AMartinez/CustomImportExport/shell/run.php [-- [OPTIONS...]]
166
+
167
+ DESCRIPTION
168
+ Import products to Magento database from CSV file, creating categories and attribute options automatically.
169
+ (more options soon...)
170
+
171
+ OPTIONS
172
+ -s, --source csv file to import, if not passed uses the defined in config.xml
173
+ -r, --reindex reindex data by all indexers (attributes, prices, etc)
174
+ -c, --cleancache clean cache storage (html output, etc)
175
+ -f, --flushimages flush catalog images cache (pregenerated product images files)
176
+
177
+ EXAMPLES
178
+ php -f app/code/local/AMartinez/CustomImportExport/shell/run.php -- -source var/importexport/products.csv
179
+ php -f app/code/local/AMartinez/CustomImportExport/shell/run.php -- -reindex
180
+ php -f app/code/local/AMartinez/CustomImportExport/shell/run.php -- -s var/importexport/products.csv -r -c
181
+
182
+
183
+ HELP;
184
+ }
185
+
186
+ }
187
+
188
+ $main = new AMartinez_CustomImportExport();
189
+ $main->run();
package.xml ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>AMartinez_CustomImportExport</name>
4
+ <version>1.5.001</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://opensource.org/licenses/osl-3.0">OSL v3.0</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Import products to Magento database from CSV file, creating categories and attribute options automatically.</summary>
10
+ <description>This extension can import products to Magento database from pregenerated CSV file.&#xD;
11
+ &#xD;
12
+ It extends Mage_ImportExport class -so it's FAST- creating CATEGORIES and ATTRIBUTE OPTIONS automatically.&#xD;
13
+ &#xD;
14
+ You can use old-style csv files too, as used in previous magento versions (all values in a single row, comma separated)&#xD;
15
+ &#xD;
16
+ e.g., fie named "myproducts.csv1":&#xD;
17
+ &#xD;
18
+ ..."store1,store2","visibility1,visibility2","CAT1,CAT2,CAT3","ASSOCIATED1,ASSOCIATED2"..&#xD;
19
+ &#xD;
20
+ To daily automatize imports, you must call "run.php" via crontab or similar.&#xD;
21
+ &#xD;
22
+ USE:&#xD;
23
+ &#xD;
24
+ php -f app/code/local/AMartinez/CustomImportExport/shell/run.php&#xD;
25
+ php -f app/code/local/AMartinez/CustomImportExport/shell/run.php [-- [OPTIONS...]]&#xD;
26
+ &#xD;
27
+ OPTIONS&#xD;
28
+ -s, --source csv file to import, if not passed uses the defined in config.xml&#xD;
29
+ -r, --reindex reindex data by all indexers (attributes, prices, etc)&#xD;
30
+ -c, --cleancache clean cache storage (html output, etc)&#xD;
31
+ -f, --flushimages flush catalog images cache (pregenerated product images files)&#xD;
32
+ &#xD;
33
+ EXAMPLES&#xD;
34
+ php -f app/code/local/AMartinez/CustomImportExport/shell/run.php -- -source var/importexport/products.csv&#xD;
35
+ php -f app/code/local/AMartinez/CustomImportExport/shell/run.php -- -reindex&#xD;
36
+ php -f app/code/local/AMartinez/CustomImportExport/shell/run.php -- -s var/importexport/products.csv -r -c&#xD;
37
+ &#xD;
38
+ &#xD;
39
+ &#xD;
40
+ More options is coming soon!&#xD;
41
+ &#xD;
42
+ &lt;form action="https://www.paypal.com/cgi-bin/webscr" method="post"&gt;&#xD;
43
+ &lt;input type="hidden" name="cmd" value="_s-xclick"&gt;&#xD;
44
+ &lt;input type="hidden" name="hosted_button_id" value="2YW7S23YH44UU"&gt;&#xD;
45
+ &lt;input type="image" src="https://www.paypalobjects.com/WEBSCR-640-20110401-1/es_ES/ES/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal. La forma r&#xE1;pida y segura de pagar en Internet."&gt;&#xD;
46
+ &lt;img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110401-1/es_ES/i/scr/pixel.gif" width="1" height="1"&gt;&#xD;
47
+ &lt;/form&gt;</description>
48
+ <notes>Magento 1.5 version. Please use UTF-8 csv files (to convert from any charset please use iconv linux command)</notes>
49
+ <authors><author><name>Antonio Martinez</name><user>amartinez</user><email>toniyecla@gmail.com</email></author></authors>
50
+ <date>2011-04-26</date>
51
+ <time>09:01:46</time>
52
+ <contents><target name="magelocal"><dir name="AMartinez"><dir name="CustomImportExport"><dir name="Helper"><file name="Data.php" hash="0c8b64920ba9cf893513573bbbb55549"/></dir><dir name="Model"><dir name="Import"><dir name="Adapter"><file name="Csv1.php" hash="fa713ee0bc9034befd91eee758fc2d0a"/></dir><file name="Adapter.php" hash="6d7d568cc3dfe002763638513bc04b7f"/><dir name="Entity"><dir name="Product"><dir name="Type"><file name="Configurable.php" hash="3614bf1abe10bb500219c787c2a6ebcc"/><file name="Giftcard.php" hash="26353aaf786f8a91ba880f3c1f692bd7"/><file name="Grouped.php" hash="b7c054303353d88e5d0c9fd303670ef5"/><file name="Simple.php" hash="48cdfdfc5c2e42b011e83483f50690c4"/></dir></dir><file name="Product.php" hash="e0b2ee2eade739d0a9c1c2d96f2f3c57"/></dir></dir><file name="Import.php" hash="ec248cde0e06ec76076f8be90e633d30"/></dir><dir name="etc"><file name="config.xml" hash="3be3626acad3c9cb4afaa2a08d41565c"/></dir><dir name="shell"><file name="run.php" hash="6e21c74b8f49869b9c4b37e561471265"/></dir></dir></dir></target></contents>
53
+ <compatible/>
54
+ <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
55
+ </package>