-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathimport.php
More file actions
146 lines (118 loc) · 4.22 KB
/
import.php
File metadata and controls
146 lines (118 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
require_once 'abstract.php';
/**
* @category AvS
* @package AvS_FastSimpleImport
* @license http://opensource.org/licenses/osl-3.0.php Open Software Licence 3.0 (OSL-3.0)
* @author Andreas von Studnitz <avs@avs-webentwicklung.de>
*/
class AvS_FastSimpleImport_Shell_Import extends Mage_Shell_Abstract
{
/**
* Run script
*
*/
public function run()
{
if ($importTypeCode = $this->getArg('type')) {
try {
$importMethods = $this->_getImportMethods();
if (!isset($importMethods[$importTypeCode])) {
Mage::throwException('Please give a valid import type code: product, category, customer or category_product.');
return;
}
//initialize the translations so that we are able to translate things.
Mage::app()->loadAreaPart(
Mage_Core_Model_App_Area::AREA_ADMINHTML,
Mage_Core_Model_App_Area::PART_TRANSLATE
);
$importMethod = $importMethods[$importTypeCode];
$data = $this->_getImportArray();
/** @var $import AvS_FastSimpleImport_Model_Import */
$import = Mage::getModel('fastsimpleimport/import');
$import->$importMethod($data);
echo $import->getEntityAdapter()->getProcessedRowsCount() . ' rows with ' . $import->getEntityAdapter()->getProcessedEntitiesCount() . ' entities have been imported successfully.' . "\n";
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage() . "\n";
}
} else {
echo $this->usageHelp();
}
}
/**
* @return array All available import type codes (keys) with according methods (values)
*/
protected function _getImportMethods()
{
return array(
'product' => 'processProductImport',
'category' => 'processCategoryImport',
'customer' => 'processCustomerImport',
'category_product' => 'processCategoryProductImport',
);
}
/**
* Read import csv file and transform to array format
*
* @return array
*/
protected function _getImportArray()
{
$filename = $this->_getFilename();
$fieldnames = array();
$data = array();
$handle = fopen($filename, 'r');
while (!feof($handle)) {
if (!sizeof($fieldnames)) {
$fieldnames = fgetcsv($handle, null, ',', '"');
} else {
$lineData = fgetcsv($handle, null, ',', '"');
$lineDataWithKeys = array();
foreach($lineData as $key => $value) {
if (!isset($fieldnames[$key])) {
Mage::throwException('Data has more columns than the header.');
}
$lineDataWithKeys[$fieldnames[$key]] = $value;
}
$data[] = $lineDataWithKeys;
}
}
fclose($handle);
return $data;
}
/**
* @return string
*/
protected function _getFilename()
{
if (!($filename = $this->getArg('file'))) {
$filename = Mage::getBaseDir('var') . DS . 'import' . DS . $this->getArg('type') . '.csv';
}
if (!is_file($filename)) {
Mage::throwException('File "' . $filename . '" does not exist.');
}
if (!is_readable($filename)) {
Mage::throwException('File "' . $filename . '" is not readable.');
}
if (!filesize($filename)) {
Mage::throwException('File "' . $filename . '" seems to be empty.');
}
return $filename;
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f import.php -- [options]
php -f import.php -- --type product --file ../var/import/test.csv
--type <code> Import type: product, category, customer or category_product
--file <filename> The relative or absolute filename
help This help
USAGE;
}
}
$shell = new AvS_FastSimpleImport_Shell_Import();
$shell->run();