1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* allows the creation of records from a CSV |
6
|
|
|
* data should be formatted in CSV like this: |
7
|
|
|
* |
8
|
|
|
* NAME (name of ) |
9
|
|
|
* COUNTRY - New Zealand |
10
|
|
|
* COUNTRYCODE - e.g. NZ, AU, US |
11
|
|
|
* TYPE (retailler / agent) |
12
|
|
|
* CITY - e.g. Amsterdam |
13
|
|
|
* WEB - e.g. http://www.mysite.co.nz |
14
|
|
|
* EMAIL |
15
|
|
|
* PHONE e.g. +31 33323321 |
16
|
|
|
* ADDRESS |
17
|
|
|
* |
18
|
|
|
* you can use ANY fields you like... this is just an example |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
abstract class ImportTaskBasics extends BuildTask |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
protected $title = "Extend this class"; |
24
|
|
|
|
25
|
|
|
protected $description = " |
26
|
|
|
Extend this class ... |
27
|
|
|
"; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* excluding base folder |
31
|
|
|
* |
32
|
|
|
* e.g. assets/files/mycsv.csv |
33
|
|
|
* @var String |
34
|
|
|
*/ |
35
|
|
|
protected $fileLocation; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* excluding base folder |
39
|
|
|
* |
40
|
|
|
* e.g. assets/files/mycsv.csv |
41
|
|
|
* @var String |
42
|
|
|
*/ |
43
|
|
|
protected $csvSeparator = ","; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Boolean |
48
|
|
|
*/ |
49
|
|
|
protected $enabled = false; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Boolean |
54
|
|
|
*/ |
55
|
|
|
protected $debug = true; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected static $characters_to_replace = array( |
61
|
|
|
'’' => '\'' |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* the original data from the CVS |
67
|
|
|
* @var Array |
68
|
|
|
*/ |
69
|
|
|
protected $csv = array(); |
70
|
|
|
|
71
|
|
|
public function getDescription() |
72
|
|
|
{ |
73
|
|
|
return $this->description .'<br /> The file to be used is: <strong>'.$this->fileLocation.'</strong>'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function RunFromCode($reset, $run) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if ($reset) { |
79
|
|
|
$_GET["reset"] = 1; |
80
|
|
|
} |
81
|
|
|
if ($run) { |
82
|
|
|
$_GET["run"] = 1; |
83
|
|
|
} |
84
|
|
|
$this->run(null); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
*/ |
90
|
|
|
public function run($request) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
increase_time_limit_to(86400); |
93
|
|
|
set_time_limit(86400); |
94
|
|
|
increase_memory_limit_to('1024M'); |
95
|
|
|
if (isset($_GET["resetonly"])) { |
|
|
|
|
96
|
|
|
//do nothing |
97
|
|
|
} else { |
98
|
|
|
$this->readFile(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (isset($_GET["reset"]) && $_GET["reset"] == 1) { |
102
|
|
|
$this->deleteRecords(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (isset($_GET["run"]) && $_GET["run"] == 1) { |
106
|
|
|
$this->createRecords(); |
107
|
|
|
} |
108
|
|
|
$resetLink = $this->Link(null, false, true); |
109
|
|
|
$runLink = $this->Link(null, true, false); |
110
|
|
|
$allLink = $this->Link(null, true, true); |
111
|
|
|
$this->outputToScreen("================================================"); |
112
|
|
|
$this->outputToScreen($resetLink); |
113
|
|
|
$this->outputToScreen($runLink); |
114
|
|
|
$this->outputToScreen($allLink); |
115
|
|
|
$this->outputToScreen("================================================"); |
116
|
|
|
$this->outputToScreen("================ THE END ======================="); |
117
|
|
|
$this->outputToScreen("================================================"); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function readFile() |
121
|
|
|
{ |
122
|
|
|
$this->outputToScreen(" |
123
|
|
|
================================================ |
124
|
|
|
READING FILE ".ini_get('max_execution_time')." seconds available. ".(ini_get('memory_limit'))."MB available |
125
|
|
|
================================================"); |
126
|
|
|
if (!$this->fileLocation) { |
127
|
|
|
$this->outputToScreen("There is no file to import", "deleted"); |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
$rowCount = 1; |
131
|
|
|
$rows = array(); |
132
|
|
|
|
133
|
|
|
if (strpos($this->fileLocation, Director::baseFolder()) === false) { |
134
|
|
|
$fileLocation = Director::baseFolder()."/".$this->fileLocation; |
135
|
|
|
} else { |
136
|
|
|
$fileLocation = $this->fileLocation; |
137
|
|
|
} |
138
|
|
|
$this->outputToScreen("reading file <strong>$fileLocation </strong>", "altered"); |
139
|
|
|
$replaceFromChars = array_keys($this->Config()->get('characters_to_replace')); |
140
|
|
|
$replaceToChars = array_values($this->Config()->get('characters_to_replace')); |
141
|
|
|
|
142
|
|
|
if (($handle = fopen($fileLocation, "r")) !== false) { |
143
|
|
|
while (($data = fgetcsv($handle, 100000, $this->csvSeparator)) !== false) { |
144
|
|
|
$cleanArray = array(); |
145
|
|
|
foreach ($data as $key => $value) { |
146
|
|
|
$value = str_replace($replaceFromChars, $replaceToChars, $value); |
147
|
|
|
$cleanArray[trim($key)] = trim($value); |
148
|
|
|
} |
149
|
|
|
$rows[] = $cleanArray; |
150
|
|
|
$rowCount++; |
151
|
|
|
} |
152
|
|
|
fclose($handle); |
153
|
|
|
} |
154
|
|
|
//$rows = str_getcsv(file_get_contents(, ",", '"'); |
|
|
|
|
155
|
|
|
|
156
|
|
|
$header = array_shift($rows); |
157
|
|
|
|
158
|
|
|
$this->csv = array(); |
159
|
|
|
$rowCount = 1; |
160
|
|
|
foreach ($rows as $row) { |
161
|
|
|
if (count($header) != count($row)) { |
162
|
|
|
$this->outputToScreen("I am trying to merge ".implode(", ", $header)." with ".implode(", ", $row)." but the column count does not match!", "deleted"); |
163
|
|
|
die("STOPPED"); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
$this->csv[] = array_combine($header, $row); |
166
|
|
|
$rowCount++; |
167
|
|
|
} |
168
|
|
|
$this->outputToScreen("Imported ".count($this->csv)." rows with ".count($header)." cells each"); |
169
|
|
|
$this->outputToScreen("Fields are: ".implode(", ", $header)); |
170
|
|
|
$this->outputToScreen("================================================"); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* created the records |
176
|
|
|
* |
177
|
|
|
*/ |
178
|
|
|
abstract protected function createRecords(); |
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* delete the records |
182
|
|
|
* |
183
|
|
|
*/ |
184
|
|
|
abstract protected function deleteRecords(); |
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* |
188
|
|
|
* |
189
|
|
|
* @param string $message |
190
|
|
|
* @param string $type |
191
|
|
|
*/ |
192
|
|
|
protected function outputToScreen($message, $type = "") |
193
|
|
|
{ |
194
|
|
|
echo " "; |
195
|
|
|
flush(); |
196
|
|
|
ob_end_flush(); |
197
|
|
|
DB::alteration_message($message, $type); |
198
|
|
|
ob_start(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function Link($action = null, $run = false, $reset = false) |
202
|
|
|
{ |
203
|
|
|
$link = |
204
|
|
|
"/dev/tasks/". |
205
|
|
|
$this->class.'/'. |
206
|
|
|
($action ?$action.'/' : ''). |
207
|
|
|
"?run=".($run ? 1 : 0). |
208
|
|
|
"&reset=".($reset ? 1 : 0); |
209
|
|
|
return |
210
|
|
|
'<h3>'.$this->getTitle().': <a href="'.$link.'">'. |
211
|
|
|
($reset ? 'reset' : '').' '.($reset && $run ? 'AND' : '').' '.($run ? 'run' : '').($action ? ' - '.$action : ''). |
212
|
|
|
'</a></h3>'; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* |
217
|
|
|
* |
218
|
|
|
* @param string $message |
219
|
|
|
* @param string $type |
220
|
|
|
*/ |
221
|
|
|
protected function DebugOutput($message, $type = "") |
222
|
|
|
{ |
223
|
|
|
if (!$this->debug) { |
224
|
|
|
return; |
225
|
|
|
} |
226
|
|
|
echo " "; |
227
|
|
|
flush(); |
228
|
|
|
ob_end_flush(); |
229
|
|
|
DB::alteration_message($message, $type); |
230
|
|
|
ob_start(); |
231
|
|
|
echo " "; |
232
|
|
|
flush(); |
233
|
|
|
ob_end_flush(); |
234
|
|
|
ob_start(); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.