|
1
|
|
|
<?php |
|
2
|
|
|
class pinp_csv { |
|
3
|
|
|
|
|
4
|
|
|
public function _init($settings = "") { |
|
5
|
|
|
return csv::init($settings); |
|
6
|
|
|
} |
|
7
|
|
|
|
|
8
|
|
|
public function _load($fileName = "file", $fileNameNls = "", $settings = "") { |
|
9
|
|
|
return csv::load($fileName, $fileNameNls, $settings); |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
public function _read($fileName = "file", $settings = array() ) { |
|
13
|
|
|
return csv::read($fileName, $settings); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function _readFromArray($csvArray, $settings = array() ) { |
|
17
|
|
|
return csv::readFromArray($csvArray, $settings); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function _writeToString($csvFeed, $settings = array() ) { |
|
21
|
|
|
return csv::writeToString($csvFeed, $settings); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
class csv { |
|
27
|
|
|
|
|
28
|
|
|
public static function init($settings = "") { |
|
29
|
|
|
$context = pobject::getContext(); |
|
30
|
|
|
$me = $context["arCurrentObject"]; |
|
31
|
|
|
|
|
32
|
|
|
return new csvFeed($me, $settings); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public static function load($fileName = "file", $fileNameNls = "", $settings = array()) { |
|
36
|
|
|
return $this->read($fileName, array_merge( array('nls' => $fileNameNls), $settings)); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public static function read($fileName = "file", $settings = array()) { |
|
40
|
|
|
$context = pobject::getContext(); |
|
41
|
|
|
$me = $context["arCurrentObject"]; |
|
42
|
|
|
|
|
43
|
|
|
$csv = new csvFeed($me, $settings); |
|
44
|
|
|
$csv->read($fileName, $settings); |
|
45
|
|
|
return $csv; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/* |
|
49
|
|
|
function readFromString($csv, $settings = array()) { |
|
50
|
|
|
$context = pobject::getContext(); |
|
51
|
|
|
$me = $context["arCurrentObject"]; |
|
52
|
|
|
|
|
53
|
|
|
$csv = new csvFeed($me, $settings); |
|
54
|
|
|
$csv->readFromString($csv, $settings); |
|
55
|
|
|
return $csv; |
|
56
|
|
|
} |
|
57
|
|
|
*/ |
|
58
|
|
|
|
|
59
|
|
|
public function readFromArray($csvArray, $settings = array()) { |
|
60
|
|
|
$context = pobject::getContext(); |
|
61
|
|
|
$me = $context["arCurrentObject"]; |
|
62
|
|
|
|
|
63
|
|
|
$csv = new csvFeed($me, $settings); |
|
64
|
|
|
$result = $csv->readFromArray($csvArray, $settings); |
|
65
|
|
|
if ($result && ar_error::isError($result)) { |
|
66
|
|
|
return $result; |
|
67
|
|
|
} else { |
|
68
|
|
|
return $csv; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function _readFromArray($csvArray, $settings = array() ) { |
|
73
|
|
|
return $this->readFromArray($csvArray, $settings); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function writeToString($csvFeed, $settings = array()) { |
|
77
|
|
|
return $csvFeed->writeToString($settings); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function _writeToString($csvFeed, $settings = array()) { |
|
81
|
|
|
return $csvFeed->writeToString($settings); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
class csvFeed { |
|
87
|
|
|
protected $settings; |
|
88
|
|
|
protected $object; |
|
89
|
|
|
protected $readMode; |
|
90
|
|
|
protected $fp; |
|
91
|
|
|
|
|
92
|
|
|
public function __construct($object, $settings = array()) { |
|
93
|
|
|
$default = Array( |
|
94
|
|
|
"seperator" => ",", |
|
95
|
|
|
"quotation" => "\"", |
|
96
|
|
|
"charset" => "utf-8", |
|
97
|
|
|
"keyRow" => null, |
|
98
|
|
|
"keySelection" => null, |
|
99
|
|
|
"bufferLength" => 4096 * 4, |
|
100
|
|
|
"lineEnd" => "\n" |
|
101
|
|
|
); |
|
102
|
|
|
if (!$settings) { |
|
|
|
|
|
|
103
|
|
|
$settings = Array(); |
|
104
|
|
|
} |
|
105
|
|
|
$settings = $settings + $default; |
|
106
|
|
|
if (!isset($settings["escape"])) { |
|
107
|
|
|
$settings["escape"] = $settings["quotation"]; |
|
108
|
|
|
} |
|
109
|
|
|
$this->settings = $settings; |
|
110
|
|
|
$this->object = $object; |
|
111
|
|
|
$this->readMode = false; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
public function load($fileName = "file", $fileNameNls = "") { |
|
116
|
|
|
return $this->read($fileName, array('nls' => $fileNameNls)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function _load($fileName = "file", $fileNameNls = "") { |
|
120
|
|
|
return $this->load($fileName, $fileNameNls); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function read($fileName = "file", $settings = "") { |
|
124
|
|
|
$object = $this->object; |
|
125
|
|
|
|
|
126
|
|
|
$files = $object->store->get_filestore("files"); |
|
127
|
|
|
if (!$fileName) { |
|
128
|
|
|
$fileName = "file"; |
|
129
|
|
|
} |
|
130
|
|
|
if (!$settings['nls']) { |
|
131
|
|
|
$settings['nls'] = $object->reqnls; |
|
132
|
|
|
} |
|
133
|
|
|
if ($files->exists($object->id, $settings['nls'].'_'.$fileName)) { |
|
134
|
|
|
$fileName = $settings['nls'].'_'.$fileName; |
|
135
|
|
|
} |
|
136
|
|
|
$tempDir = $object->store->get_config("files")."temp/"; |
|
137
|
|
|
$tempFile = tempnam($tempDir, "csvexport"); |
|
138
|
|
|
$files->copy_from_store($tempFile, $object->id, $fileName); |
|
139
|
|
|
|
|
140
|
|
|
$this->readMode = "fp"; |
|
141
|
|
|
$this->fp = fopen($tempFile, "r"); |
|
142
|
|
|
$this->reset(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function _read($fileName = "file", $settings = array()) { |
|
146
|
|
|
return $this->read($fileName, $settings); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function readFromArray($csvArray, $settings = array() ) { |
|
|
|
|
|
|
150
|
|
|
if (!is_array($csvArray)) { |
|
151
|
|
|
$error = ar_error::raiseError('mod_csv: readFromArray, input is not an array', 1); |
|
152
|
|
|
return $error; |
|
153
|
|
|
} |
|
154
|
|
|
$this->readMode = 'array'; |
|
155
|
|
|
$this->csvArray = $csvArray; |
|
|
|
|
|
|
156
|
|
|
$this->reset(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function _readFromArray($csvArray, $settings = array() ) { |
|
160
|
|
|
return $this->readFromArray($csvArray, $settings); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function reset() { |
|
164
|
|
|
switch ($this->readMode) { |
|
165
|
|
|
case "array": |
|
166
|
|
|
reset($this->csvArray); |
|
167
|
|
|
break; |
|
168
|
|
|
default: |
|
169
|
|
|
case "fp": |
|
|
|
|
|
|
170
|
|
|
fseek($this->fp, 0); |
|
171
|
|
|
if (isset($this->settings['keyRow']) && $this->settings['keyRow'] !== "") { // csv lib saves defaults as "" |
|
172
|
|
|
$this->keys = array(); |
|
|
|
|
|
|
173
|
|
|
for ($i = 0; $i <= $this->settings['keyRow']; $i++) { |
|
174
|
|
|
$keys = $this->next(); |
|
175
|
|
|
} |
|
176
|
|
|
$this->keys = $keys; |
|
177
|
|
|
if (!$this->settings['keySelection']) { |
|
178
|
|
|
$this->settings['keySelection'] = $keys; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
$this->readLine = ""; |
|
|
|
|
|
|
182
|
|
|
break; |
|
183
|
|
|
} |
|
184
|
|
|
$this->next(); // set pointer to first item for current() |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
public function next() { |
|
189
|
|
|
switch ($this->readMode) { |
|
190
|
|
|
case 'array': |
|
191
|
|
|
$result = current($this->csvArray); |
|
192
|
|
|
next($this->csvArray); |
|
193
|
|
|
break; |
|
194
|
|
|
default: |
|
195
|
|
|
case "fp": |
|
|
|
|
|
|
196
|
|
|
if (feof($this->fp)) { |
|
197
|
|
|
$result = Array(); |
|
198
|
|
|
} else { |
|
199
|
|
|
$result = fgetcsv($this->fp, $this->settings['bufferLength'], $this->settings['seperator'], $this->settings['quotation']); |
|
200
|
|
|
if (is_array($result) && strtolower($this->settings['charset']) != "utf-8") { |
|
201
|
|
|
if (!function_exists("iconv")) { |
|
202
|
|
|
global $store; |
|
203
|
|
|
include_once($store->get_config("code")."modules/mod_unicode.php"); |
|
204
|
|
|
foreach ($result as $item => $resultItem) { |
|
205
|
|
|
$result[$item] = unicode::convertToUTF8($this->settings["charset"], $result[$item]); |
|
206
|
|
|
} |
|
207
|
|
|
} else { |
|
208
|
|
|
foreach ($result as $item => $resultItem) { |
|
209
|
|
|
$result[$item] = iconv($this->settings["charset"], "utf-8", $result[$item]); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
break; |
|
215
|
|
|
} |
|
216
|
|
|
if ($result && $this->keys && $this->settings['keySelection']) { |
|
217
|
|
|
$hashResult = Array(); |
|
218
|
|
|
foreach ($this->keys as $i => $key) { |
|
219
|
|
|
if (in_array($key, $this->settings['keySelection'])) { |
|
220
|
|
|
$hashResult[$key] = $result[$i]; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
$result = $hashResult; |
|
224
|
|
|
} |
|
225
|
|
|
$this->readLine = $result; |
|
226
|
|
|
return $result; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
public function current() { |
|
231
|
|
|
return $this->readLine; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
View Code Duplication |
public function call($template, $args=Array()) { |
|
235
|
|
|
$current = $this->current(); |
|
236
|
|
|
if ($current) { |
|
237
|
|
|
$args['item'] = $current; |
|
238
|
|
|
$result = $this->object->call($template, $args); |
|
239
|
|
|
} |
|
240
|
|
|
return $result; |
|
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function count() { |
|
244
|
|
|
$this->reset(); |
|
245
|
|
|
$i = 0; |
|
246
|
|
|
while ($this->current()) { $i++; $this->next(); }; |
|
247
|
|
|
return $i; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public function ls($template, $args='', $limit=0, $offset=0) { |
|
251
|
|
|
global $ARBeenHere; |
|
252
|
|
|
$ARBeenHere = Array(); |
|
253
|
|
|
$this->reset(); |
|
254
|
|
|
if ($offset) { |
|
255
|
|
|
while ($offset) { |
|
256
|
|
|
$this->next(); |
|
257
|
|
|
$offset--; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
if( $limit == 0 ) { $limit = -1; } |
|
261
|
|
|
while($this->current() && $limit ) { |
|
262
|
|
|
$ARBeenHere = Array(); |
|
263
|
|
|
$args["item"] = $this->current(); |
|
264
|
|
|
$this->call($template, $args); |
|
265
|
|
|
$limit--; |
|
266
|
|
|
$this->next(); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
public function _getArray($limit=0, $offset=0) { |
|
271
|
|
|
return $this->getArray($limit,$offset); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
public function getArray($limit=0, $offset=0) { |
|
275
|
|
|
$result=Array(); |
|
276
|
|
|
$this->reset(); |
|
277
|
|
|
if ($offset) { |
|
278
|
|
|
while ($offset) { |
|
279
|
|
|
$this->next(); |
|
280
|
|
|
$offset--; |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
if( $limit == 0 ) { $limit = -1; } |
|
284
|
|
|
while( $this->current() && $limit ) { |
|
285
|
|
|
$result[]=$this->current(); |
|
286
|
|
|
$limit--; |
|
287
|
|
|
$this->next(); |
|
288
|
|
|
} |
|
289
|
|
|
return $result; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
public function writeToString($settings = array()) { |
|
293
|
|
|
$settings = array_merge($this->settings, $settings); |
|
294
|
|
|
$result = ''; |
|
295
|
|
|
if ($settings['keyRow'] && $settings['keySelection']) { |
|
296
|
|
|
foreach ($settings['keySelection'] as $key) { |
|
297
|
|
|
$result .= $this->quoteValue($key, $settings).$settings['seperator']; |
|
298
|
|
|
} |
|
299
|
|
|
$result = substr($result, 0, -(strlen($settings['seperator']))) . $settings['lineEnd']; |
|
300
|
|
|
} |
|
301
|
|
|
$limit = $settings['limit']; |
|
302
|
|
|
if (!$limit) { |
|
303
|
|
|
$limit = -1; |
|
304
|
|
|
} |
|
305
|
|
|
while (($values = $this->current()) && $limit) { |
|
306
|
|
|
$limit--; |
|
307
|
|
|
$result .= $this->quoteValues($values, $settings); |
|
308
|
|
|
$this->next(); |
|
309
|
|
|
} |
|
310
|
|
|
return $result; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
public function _writeToString($settings = array() ) { |
|
314
|
|
|
return $this->writeToString($settings); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
public function quoteValue($value, $settings = array()) { |
|
318
|
|
|
$settings = array_merge($this->settings, $settings); |
|
319
|
|
|
return $settings['quotation'] . AddCSlashes($value, $settings['escape']) . $settings['quotation']; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
public function quoteValues($values, $settings = array()) { |
|
323
|
|
|
$settings = array_merge($this->settings, $settings); |
|
324
|
|
|
if (!is_array($values)) { |
|
325
|
|
|
return ar_error::raiseError('mod_csv: quoteValues: values argument is not an array', 2); |
|
326
|
|
|
} |
|
327
|
|
|
$result = ''; |
|
328
|
|
|
if ($settings['keySelection']) { |
|
329
|
|
|
foreach($settings['keySelection'] as $key) { |
|
330
|
|
|
$result .= $this->quoteValue($values[$key], $settings) . $settings['seperator']; |
|
331
|
|
|
} |
|
332
|
|
|
} else { |
|
333
|
|
|
foreach ($values as $value) { |
|
334
|
|
|
$result .= $this->quoteValue($value, $settings) . $settings['seperator']; |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
$result = substr($result, 0, -strlen($settings['seperator'])) . $settings['lineEnd']; |
|
338
|
|
|
|
|
339
|
|
|
return $result; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
public function _reset() { |
|
343
|
|
|
return $this->reset(); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
public function _next() { |
|
347
|
|
|
return $this->next(); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
public function _count() { |
|
351
|
|
|
return $this->count(); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
public function _current() { |
|
355
|
|
|
return $this->current(); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
public function _ls($template, $args='') { |
|
359
|
|
|
return $this->ls($template, $args); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
} |
|
363
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.