|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Pods |
|
4
|
|
|
*/ |
|
5
|
|
|
class PodsMigrate { |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @var null|string |
|
9
|
|
|
*/ |
|
10
|
|
|
public $type = 'php'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
public $types = array( 'php', 'json', 'sv', 'xml' ); |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
public $mimes = array( |
|
21
|
|
|
'json' => 'application/json', |
|
22
|
|
|
'csv' => 'text/csv', |
|
23
|
|
|
'tsv' => 'text/tsv', |
|
24
|
|
|
'xml' => 'text/xml', |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var null|string |
|
29
|
|
|
*/ |
|
30
|
|
|
public $delimiter = ','; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var null |
|
34
|
|
|
*/ |
|
35
|
|
|
public $data = array( |
|
36
|
|
|
'items' => array(), |
|
37
|
|
|
'columns' => array(), |
|
38
|
|
|
'fields' => array(), |
|
39
|
|
|
'single' => false, |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var null |
|
44
|
|
|
*/ |
|
45
|
|
|
public $input; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var |
|
49
|
|
|
*/ |
|
50
|
|
|
public $parsed; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var |
|
54
|
|
|
*/ |
|
55
|
|
|
public $built; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Migrate Data to and from Pods |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $type Export Type (php, json, sv, xml) |
|
61
|
|
|
* @param string $delimiter Delimiter for export type 'sv' |
|
62
|
|
|
* @param array $data Array of data settings |
|
63
|
|
|
* |
|
64
|
|
|
* @return \PodsMigrate |
|
|
|
|
|
|
65
|
|
|
* |
|
66
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
|
67
|
|
|
* @since 2.0 |
|
68
|
|
|
*/ |
|
69
|
|
|
function __construct ( $type = null, $delimiter = null, $data = null ) { |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
if ( ! empty( $type ) ) { |
|
72
|
|
|
if ( 'csv' === $type ) { |
|
73
|
|
|
$type = 'sv'; |
|
74
|
|
|
|
|
75
|
|
|
if ( null === $delimiter ) { |
|
76
|
|
|
$delimiter = ','; |
|
77
|
|
|
} |
|
78
|
|
|
} elseif ( 'tsv' === $type ) { |
|
79
|
|
|
$type = 'sv'; |
|
80
|
|
|
|
|
81
|
|
|
if ( null === $delimiter ) { |
|
82
|
|
|
$delimiter = "\t"; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ( in_array( $type, $this->types, true ) ) { |
|
87
|
|
|
$this->type = $type; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ( ! empty( $delimiter ) ) |
|
92
|
|
|
$this->delimiter = $delimiter; |
|
93
|
|
|
|
|
94
|
|
|
if ( !empty( $data ) ) |
|
95
|
|
|
$this->set_data( $data ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
function set_data ( $data ) { |
|
|
|
|
|
|
99
|
|
|
$defaults = array( |
|
100
|
|
|
'items' => array(), |
|
101
|
|
|
'columns' => array(), |
|
102
|
|
|
'fields' => array() |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
$this->data = array_merge( $defaults, (array) $data ); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Importing / Parsing / Validating Code |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $data Array of data |
|
112
|
|
|
* @param string $type Export Type (php, json, sv, xml) |
|
113
|
|
|
* @param string $delimiter Delimiter for export type 'sv' |
|
114
|
|
|
*/ |
|
115
|
|
|
function import ( $data = null, $type = null, $delimiter = null ) { |
|
|
|
|
|
|
116
|
|
|
$this->parse( $data, $type, $delimiter ); |
|
117
|
|
|
|
|
118
|
|
|
return $this->import_pod_items(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param array $data Array of data |
|
123
|
|
|
* @param string $type Export Type (php, json, sv, xml) |
|
124
|
|
|
*/ |
|
125
|
|
|
public function import_pod_items ( $data = null, $type = null ) { |
|
126
|
|
|
if ( !empty( $data ) ) |
|
127
|
|
|
$this->input = $data; |
|
128
|
|
|
|
|
129
|
|
|
if ( !empty( $type ) && in_array( $type, $this->types ) ) |
|
130
|
|
|
$this->type = $type; |
|
131
|
|
|
|
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param array $data Array of data |
|
137
|
|
|
* @param string $type Parse Type (php, json, sv, xml) |
|
138
|
|
|
* @param string $delimiter Delimiter for export type 'sv' |
|
139
|
|
|
* |
|
140
|
|
|
* @return null |
|
141
|
|
|
*/ |
|
142
|
|
View Code Duplication |
public function parse ( $data = null, $type = null, $delimiter = null ) { |
|
|
|
|
|
|
143
|
|
|
if ( !empty( $data ) ) |
|
144
|
|
|
$this->input = $data; |
|
145
|
|
|
|
|
146
|
|
|
if ( !empty( $type ) && in_array( $type, $this->types ) ) |
|
147
|
|
|
$this->type = $type; |
|
148
|
|
|
|
|
149
|
|
|
if ( !empty( $delimiter ) ) |
|
150
|
|
|
$this->delimiter = $delimiter; |
|
151
|
|
|
|
|
152
|
|
|
if ( method_exists( $this, "parse_{$this->type}" ) ) |
|
153
|
|
|
return call_user_func( array( $this, 'parse_' . $this->type ) ); |
|
154
|
|
|
|
|
155
|
|
|
return $this->parsed; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param array $data Array of data |
|
160
|
|
|
* |
|
161
|
|
|
* @return bool |
|
162
|
|
|
*/ |
|
163
|
|
|
public function parse_json ( $data = null ) { |
|
164
|
|
|
if ( !empty( $data ) ) |
|
165
|
|
|
$this->input = $data; |
|
166
|
|
|
|
|
167
|
|
|
$items = @json_decode( $this->input, true ); |
|
168
|
|
|
|
|
169
|
|
|
if ( !is_array( $items ) ) |
|
170
|
|
|
return false; |
|
171
|
|
|
|
|
172
|
|
|
// Only export to a basic object if building for a single item. |
|
173
|
|
|
if ( ! empty( $this->data['single'] ) ) { |
|
174
|
|
|
$data = $items; |
|
175
|
|
|
} else { |
|
176
|
|
|
$data = array( |
|
177
|
|
|
'columns' => array(), |
|
178
|
|
|
'items' => array(), |
|
179
|
|
|
'fields' => array() |
|
180
|
|
|
); |
|
181
|
|
|
|
|
182
|
|
|
foreach ( $items as $key => $item ) { |
|
183
|
|
|
if ( ! is_array( $item ) ) { |
|
184
|
|
|
continue; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
foreach ( $item as $column => $value ) { |
|
188
|
|
|
if ( ! in_array( $column, $data['columns'] ) ) { |
|
189
|
|
|
$data['columns'][] = $column; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$data['items'][ $key ] = $item; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$this->parsed = $data; |
|
198
|
|
|
|
|
199
|
|
|
return $this->parsed; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param array $data Array of data |
|
204
|
|
|
* @param string $delimiter Delimiter for export type 'sv' |
|
205
|
|
|
* |
|
206
|
|
|
* @return bool |
|
207
|
|
|
*/ |
|
208
|
|
|
public function parse_sv ( $data = null, $delimiter = null ) { |
|
209
|
|
|
if ( !empty( $data ) ) |
|
210
|
|
|
$this->input = $data; |
|
211
|
|
|
|
|
212
|
|
|
if ( !empty( $delimiter ) ) |
|
213
|
|
|
$this->delimiter = $delimiter; |
|
214
|
|
|
|
|
215
|
|
|
$rows = $this->str_getcsv( $this->input, '\n' ); |
|
216
|
|
|
|
|
217
|
|
|
if ( empty( $rows ) || 2 > count( $rows ) ) |
|
218
|
|
|
return false; |
|
219
|
|
|
|
|
220
|
|
|
$data = array( |
|
221
|
|
|
'columns' => array(), |
|
222
|
|
|
'items' => array() |
|
223
|
|
|
); |
|
224
|
|
|
|
|
225
|
|
|
foreach ( $rows as $key => $row ) { |
|
226
|
|
|
if ( 0 == $key ) |
|
227
|
|
|
$data[ 'columns' ] = $this->str_getcsv( $row, $this->delimiter ); |
|
228
|
|
|
else { |
|
229
|
|
|
$row = $this->str_getcsv( $row, $this->delimiter ); |
|
230
|
|
|
|
|
231
|
|
|
$data[ 'items' ][ $key ] = array(); |
|
232
|
|
|
|
|
233
|
|
|
foreach ( $data[ 'columns' ] as $ckey => $column ) { |
|
234
|
|
|
$data[ 'items' ][ $key ][ $column ] = ( isset( $row[ $ckey ] ) ? $row[ $ckey ] : '' ); |
|
235
|
|
|
|
|
236
|
|
|
if ( 'NULL' === $data[ 'items' ][ $key ][ $column ] ) |
|
237
|
|
|
$data[ 'items' ][ $key ][ $column ] = null; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
$this->parsed = $data; |
|
243
|
|
|
|
|
244
|
|
|
return $this->parsed; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Handle str_getcsv for cases where it's not set |
|
249
|
|
|
* |
|
250
|
|
|
* @param $line |
|
251
|
|
|
* @param string $delimiter |
|
252
|
|
|
* @param string $enclosure |
|
253
|
|
|
* @param string $escape |
|
254
|
|
|
* |
|
255
|
|
|
* @return array|mixed |
|
256
|
|
|
*/ |
|
257
|
|
|
public function str_getcsv ( $line, $delimiter = ',', $enclosure = '"', $escape = '\\' ) { |
|
258
|
|
|
$line = str_replace( "\r\n", "\n", $line ); |
|
259
|
|
|
$line = str_replace( "\r", "\n", $line ); |
|
260
|
|
|
|
|
261
|
|
|
if ( '\n' != $delimiter && function_exists( 'str_getcsv' ) ) |
|
262
|
|
|
return str_getcsv( $line, $delimiter, $enclosure, $escape ); |
|
263
|
|
|
|
|
264
|
|
|
$delimiter = str_replace( '/', '\/', $delimiter ); |
|
265
|
|
|
$enclosure = preg_quote( $enclosure, '/' ); |
|
266
|
|
|
|
|
267
|
|
|
$split = "/{$delimiter}(?=(?:[^{$enclosure}]*{$enclosure}[^{$enclosure}]*{$enclosure})*(?![^{$enclosure}]*{$enclosure}))/"; |
|
268
|
|
|
|
|
269
|
|
|
$data = preg_split( $split, trim( $line ), -1, PREG_SPLIT_NO_EMPTY ); |
|
270
|
|
|
|
|
271
|
|
|
if ( '\n' != $delimiter ) |
|
272
|
|
|
$data = preg_replace( "/^{$enclosure}(.*){$enclosure}$/s", "$1", $data ); |
|
273
|
|
|
|
|
274
|
|
|
return $data; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @param array $data Array of data |
|
279
|
|
|
* |
|
280
|
|
|
* @return bool |
|
281
|
|
|
*/ |
|
282
|
|
|
public function parse_xml ( $data = null ) { |
|
283
|
|
|
if ( !empty( $data ) ) |
|
284
|
|
|
$this->input = $data; |
|
285
|
|
|
|
|
286
|
|
|
$xml = new SimpleXMLElement( $this->input ); |
|
287
|
|
|
|
|
288
|
|
|
if ( !isset( $xml->items ) ) |
|
289
|
|
|
return false; |
|
290
|
|
|
|
|
291
|
|
|
$data = array( 'columns' => array(), 'items' => array() ); |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @var $child SimpleXMLElement |
|
295
|
|
|
* @var $item_child SimpleXMLElement |
|
296
|
|
|
* @var $data_child SimpleXMLElement |
|
297
|
|
|
*/ |
|
298
|
|
|
|
|
299
|
|
|
if ( isset( $xml->columns ) ) { |
|
300
|
|
|
foreach ( $xml->columns->children() as $child ) { |
|
301
|
|
|
$sub = $child->getName(); |
|
302
|
|
|
|
|
303
|
|
|
if ( empty( $sub ) || 'column' != $sub ) |
|
304
|
|
|
continue; |
|
305
|
|
|
|
|
306
|
|
|
if ( isset( $child->name ) ) { |
|
307
|
|
|
if ( is_array( $child->name ) ) |
|
308
|
|
|
$column = $child->name[ 0 ]; |
|
309
|
|
|
else |
|
310
|
|
|
$column = $child->name; |
|
311
|
|
|
|
|
312
|
|
|
$data[ 'columns' ][] = $column; |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
foreach ( $xml->items->children() as $child ) { |
|
318
|
|
|
$sub = $child->getName(); |
|
319
|
|
|
|
|
320
|
|
|
if ( empty( $sub ) || 'item' != $sub ) |
|
321
|
|
|
continue; |
|
322
|
|
|
|
|
323
|
|
|
$item = array(); |
|
324
|
|
|
|
|
325
|
|
|
$attributes = $child->attributes(); |
|
326
|
|
|
|
|
327
|
|
|
if ( !empty( $attributes ) ) { |
|
328
|
|
|
foreach ( $attributes as $column => $value ) { |
|
329
|
|
|
if ( !in_array( $column, $data[ 'columns' ] ) ) |
|
330
|
|
|
$data[ 'columns' ][] = $column; |
|
331
|
|
|
|
|
332
|
|
|
$item[ $column ] = $value; |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
$item_child = $child->children(); |
|
337
|
|
|
|
|
338
|
|
|
if ( !empty( $item_child ) ) { |
|
339
|
|
|
foreach ( $item_child->children() as $data_child ) { |
|
340
|
|
|
$column = $data_child->getName(); |
|
341
|
|
|
|
|
342
|
|
|
if ( !in_array( $column, $data[ 'columns' ] ) ) |
|
343
|
|
|
$data[ 'columns' ][] = $column; |
|
344
|
|
|
|
|
345
|
|
|
$item[ $column ] = $item_child->$column; |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
if ( !empty( $item ) ) |
|
350
|
|
|
$data[ 'items' ][] = $item; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
$this->parsed = $data; |
|
354
|
|
|
|
|
355
|
|
|
return $this->parsed; |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* @param array $data Array of data |
|
360
|
|
|
* |
|
361
|
|
|
* @return mixed |
|
362
|
|
|
* |
|
363
|
|
|
* @todo For much much later |
|
364
|
|
|
*/ |
|
365
|
|
|
public function parse_sql ( $data = null ) { |
|
366
|
|
|
if ( !empty( $data ) ) |
|
367
|
|
|
$this->input = $data; |
|
368
|
|
|
|
|
369
|
|
|
$this->parsed = $data; |
|
370
|
|
|
|
|
371
|
|
|
return $this->parsed; |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Exporting / Building Code |
|
376
|
|
|
* |
|
377
|
|
|
* @param array $data Array of data |
|
378
|
|
|
* @param string $type Export Type (php, json, sv, xml) |
|
379
|
|
|
* @param string $delimiter Delimiter for export type 'sv' |
|
380
|
|
|
* |
|
381
|
|
|
* @return mixed |
|
382
|
|
|
*/ |
|
383
|
|
View Code Duplication |
public function export ( $data = null, $type = null, $delimiter = null ) { |
|
|
|
|
|
|
384
|
|
|
if ( !empty( $data ) ) |
|
385
|
|
|
$this->set_data( $data ); |
|
386
|
|
|
|
|
387
|
|
|
if ( !empty( $type ) && in_array( $type, $this->types ) ) |
|
388
|
|
|
$this->type = $type; |
|
389
|
|
|
|
|
390
|
|
|
if ( !empty( $delimiter ) ) |
|
391
|
|
|
$this->delimiter = $delimiter; |
|
392
|
|
|
|
|
393
|
|
|
if ( method_exists( $this, "build_{$this->type}" ) ) |
|
394
|
|
|
call_user_func( array( $this, 'build_' . $this->type ) ); |
|
395
|
|
|
|
|
396
|
|
|
return $this->built; |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* @param array $data Array of data |
|
401
|
|
|
*/ |
|
402
|
|
|
public function export_pod_items ( $data = null ) { |
|
403
|
|
|
if ( !empty( $data ) ) |
|
404
|
|
|
$this->set_data( $data ); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* @param array $data Array of data |
|
409
|
|
|
* @param string $type Export Type (php, json, sv, xml) |
|
410
|
|
|
* |
|
411
|
|
|
* @return null |
|
412
|
|
|
*/ |
|
413
|
|
|
public function build ( $data = null, $type = null ) { |
|
414
|
|
|
if ( !empty( $data ) ) |
|
415
|
|
|
$this->set_data( $data ); |
|
416
|
|
|
|
|
417
|
|
|
if ( !empty( $type ) && in_array( $type, $this->types ) ) |
|
418
|
|
|
$this->type = $type; |
|
419
|
|
|
|
|
420
|
|
|
if ( method_exists( $this, "build_{$this->type}" ) ) |
|
421
|
|
|
call_user_func( array( $this, 'build_' . $this->type ) ); |
|
422
|
|
|
|
|
423
|
|
|
return $this->data; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
/** |
|
427
|
|
|
* @param array $data Array of data |
|
428
|
|
|
* |
|
429
|
|
|
* @return bool |
|
430
|
|
|
*/ |
|
431
|
|
|
public function build_json ( $data = null ) { |
|
432
|
|
|
if ( !empty( $data ) ) |
|
433
|
|
|
$this->set_data( $data ); |
|
434
|
|
|
|
|
435
|
|
|
if ( empty( $this->data ) || !is_array( $this->data ) ) |
|
436
|
|
|
return false; |
|
437
|
|
|
|
|
438
|
|
|
// Only export to a basic object if building for a single item. |
|
439
|
|
|
if ( ! empty( $this->data['single'] ) ) { |
|
440
|
|
|
$data = $this->data['items']; |
|
441
|
|
|
} else { |
|
442
|
|
|
$data = array( |
|
443
|
|
|
'items' => array( |
|
444
|
|
|
'count' => count( $this->data['items'] ), |
|
445
|
|
|
'item' => array() |
|
446
|
|
|
) |
|
447
|
|
|
); |
|
448
|
|
|
|
|
449
|
|
|
foreach ( $this->data['items'] as $item ) { |
|
450
|
|
|
$row = array(); |
|
451
|
|
|
|
|
452
|
|
|
foreach ( $this->data['columns'] as $column => $label ) { |
|
453
|
|
View Code Duplication |
if ( is_numeric( $column ) && ( ( is_object( $item ) && ! isset( $item->$column ) ) || ( is_array( $item ) && ! isset( $item[ $column ] ) ) ) ) { |
|
454
|
|
|
$column = $label; |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
$value = ''; |
|
458
|
|
|
|
|
459
|
|
View Code Duplication |
if ( is_object( $item ) ) { |
|
460
|
|
|
if ( ! isset( $item->$column ) ) { |
|
461
|
|
|
$item->$column = ''; |
|
462
|
|
|
} |
|
463
|
|
|
|
|
464
|
|
|
$value = $item->$column; |
|
465
|
|
|
} elseif ( is_array( $item ) ) { |
|
466
|
|
|
if ( ! isset( $item[ $column ] ) ) { |
|
467
|
|
|
$item[ $column ] = ''; |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
$value = $item[ $column ]; |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
$row[ $column ] = $value; |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
$data['items']['item'][] = $row; |
|
477
|
|
|
} |
|
478
|
|
|
} |
|
479
|
|
|
|
|
480
|
|
|
$this->built = @json_encode( $data ); |
|
481
|
|
|
|
|
482
|
|
|
return $this->built; |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
/** |
|
486
|
|
|
* @param array $data Array of data |
|
487
|
|
|
* @param string $delimiter Delimiter for export type 'sv' |
|
488
|
|
|
* |
|
489
|
|
|
* @return bool |
|
490
|
|
|
*/ |
|
491
|
|
|
public function build_sv ( $data = null, $delimiter = null ) { |
|
492
|
|
|
if ( !empty( $data ) ) |
|
493
|
|
|
$this->set_data( $data ); |
|
494
|
|
|
|
|
495
|
|
|
if ( !empty( $delimiter ) ) |
|
496
|
|
|
$this->delimiter = $delimiter; |
|
497
|
|
|
|
|
498
|
|
|
if ( empty( $this->data ) || !is_array( $this->data ) ) |
|
499
|
|
|
return false; |
|
500
|
|
|
|
|
501
|
|
|
$head = $lines = ''; |
|
502
|
|
|
|
|
503
|
|
|
foreach ( $this->data[ 'columns' ] as $column => $label ) { |
|
504
|
|
|
$head .= '"' . $label . '"' . $this->delimiter; |
|
505
|
|
|
} |
|
506
|
|
|
|
|
507
|
|
|
$head = substr( $head, 0, -1 ); |
|
508
|
|
|
|
|
509
|
|
|
foreach ( $this->data[ 'items' ] as $item ) { |
|
510
|
|
|
$line = ''; |
|
511
|
|
|
|
|
512
|
|
|
foreach ( $this->data[ 'columns' ] as $column => $label ) { |
|
513
|
|
View Code Duplication |
if ( is_numeric( $column ) && ( ( is_object( $item ) && !isset( $item->$column ) ) || ( is_array( $item ) && !isset( $item[ $column ] ) ) ) ) |
|
514
|
|
|
$column = $label; |
|
515
|
|
|
|
|
516
|
|
|
$value = ''; |
|
517
|
|
|
|
|
518
|
|
View Code Duplication |
if ( is_object( $item ) ) { |
|
519
|
|
|
if ( !isset( $item->$column ) ) |
|
520
|
|
|
$item->$column = ''; |
|
521
|
|
|
|
|
522
|
|
|
$value = $item->$column; |
|
523
|
|
|
} |
|
524
|
|
|
elseif ( is_array( $item ) ) { |
|
525
|
|
|
if ( !isset( $item[ $column ] ) ) |
|
526
|
|
|
$item[ $column ] = ''; |
|
527
|
|
|
|
|
528
|
|
|
$value = $item[ $column ]; |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
if ( is_array( $value ) || is_object( $value ) ) |
|
532
|
|
|
$value = pods_serial_comma( $value, array( 'field' => $column, 'fields' => pods_var_raw( $column, $this->data[ 'fields' ] ), 'and' => '' ) ); |
|
533
|
|
|
|
|
534
|
|
|
$value = str_replace( array( '"', "\r\n", "\r", "\n" ), array( '\\"', "\n", "\n", '\n' ), $value ); |
|
535
|
|
|
|
|
536
|
|
|
$line .= '"' . $value . '"' . $this->delimiter; |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
$lines .= substr( $line, 0, -1 ) . "\n"; |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
if ( !empty( $lines ) ) |
|
543
|
|
|
$lines = "\n" . substr( $lines, 0, -1 ); |
|
544
|
|
|
|
|
545
|
|
|
$this->built = $head . $lines; |
|
546
|
|
|
|
|
547
|
|
|
return $this->built; |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
/** |
|
551
|
|
|
* @param array $data Array of data |
|
552
|
|
|
* |
|
553
|
|
|
* @return bool |
|
554
|
|
|
*/ |
|
555
|
|
|
public function build_xml ( $data = null ) { |
|
556
|
|
|
if ( !empty( $data ) ) |
|
557
|
|
|
$this->set_data( $data ); |
|
558
|
|
|
|
|
559
|
|
|
if ( empty( $this->data ) || !is_array( $this->data ) ) |
|
560
|
|
|
return false; |
|
561
|
|
|
|
|
562
|
|
|
$head = '<' . '?' . 'xml version="1.0" encoding="utf-8" ' . '?' . '>' . "\r\n<items count=\"" . count( $this->data[ 'items' ] ) . "\">\r\n"; |
|
563
|
|
|
$lines = ''; |
|
564
|
|
|
|
|
565
|
|
|
foreach ( $this->data[ 'items' ] as $item ) { |
|
566
|
|
|
$line = "\t<item>\r\n"; |
|
567
|
|
|
|
|
568
|
|
|
foreach ( $this->data[ 'columns' ] as $column => $label ) { |
|
569
|
|
View Code Duplication |
if ( is_numeric( $column ) && ( ( is_object( $item ) && !isset( $item->$column ) ) || ( is_array( $item ) && !isset( $item[ $column ] ) ) ) ) |
|
570
|
|
|
$column = $label; |
|
571
|
|
|
|
|
572
|
|
|
$line .= $this->build_xml_level( $item, $column ); |
|
573
|
|
|
} |
|
574
|
|
|
|
|
575
|
|
|
$line .= "\t</item>\r\n"; |
|
576
|
|
|
$lines .= $line; |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
$foot = '</items>'; |
|
580
|
|
|
|
|
581
|
|
|
$this->built = $head . $lines . $foot; |
|
582
|
|
|
|
|
583
|
|
|
return $this->built; |
|
584
|
|
|
} |
|
585
|
|
|
|
|
586
|
|
|
public function build_xml_level ( $item, $column, $level = 2, $column_name = '' ) { |
|
587
|
|
|
$column = pods_clean_name( $column, false, false ); |
|
588
|
|
|
|
|
589
|
|
|
$line = ''; |
|
590
|
|
|
|
|
591
|
|
|
$value = ''; |
|
592
|
|
|
|
|
593
|
|
View Code Duplication |
if ( is_object( $item ) ) { |
|
594
|
|
|
if ( !isset( $item->$column ) ) |
|
595
|
|
|
$item->$column = ''; |
|
596
|
|
|
|
|
597
|
|
|
$value = $item->$column; |
|
598
|
|
|
} |
|
599
|
|
|
elseif ( is_array( $item ) ) { |
|
600
|
|
|
if ( !isset( $item[ $column ] ) ) |
|
601
|
|
|
$item[ $column ] = ''; |
|
602
|
|
|
|
|
603
|
|
|
$value = $item[ $column ]; |
|
604
|
|
|
} |
|
605
|
|
|
|
|
606
|
|
|
if ( !empty( $column_name ) ) |
|
607
|
|
|
$column = $column_name; |
|
608
|
|
|
|
|
609
|
|
|
$tabs = str_repeat( "\t", $level ); |
|
610
|
|
|
|
|
611
|
|
|
$line .= $tabs . "<{$column}>"; |
|
612
|
|
|
|
|
613
|
|
|
if ( is_array( $value ) || is_object( $value ) ) { |
|
614
|
|
|
if ( is_object( $value ) ) |
|
615
|
|
|
$value = get_object_vars( $value ); |
|
616
|
|
|
|
|
617
|
|
|
foreach ( $value as $k => $v ) { |
|
618
|
|
|
if ( is_int( $k ) ) |
|
619
|
|
|
$line .= $this->build_xml_level( $value, $k, $level + 1, 'value' ); |
|
620
|
|
|
else |
|
621
|
|
|
$line .= $this->build_xml_level( $value, $k, $level + 1 ); |
|
622
|
|
|
} |
|
623
|
|
|
} |
|
624
|
|
|
elseif ( false !== strpos( $value, '<' ) ) { |
|
625
|
|
|
$value = str_replace( array( '<![CDATA[', ']]>' ), array( '<![CDATA[', ']]>' ), $value ); |
|
626
|
|
|
|
|
627
|
|
|
$line .= "<![CDATA[" . $value . "]]>"; |
|
628
|
|
|
} |
|
629
|
|
|
else |
|
630
|
|
|
$line .= str_replace( '&', '&', $value ); |
|
631
|
|
|
|
|
632
|
|
|
$line .= "</{$column}>\r\n"; |
|
633
|
|
|
|
|
634
|
|
|
return $line; |
|
635
|
|
|
} |
|
636
|
|
|
|
|
637
|
|
|
/** |
|
638
|
|
|
* @param array $data Array of data |
|
639
|
|
|
* |
|
640
|
|
|
* @return mixed |
|
641
|
|
|
*/ |
|
642
|
|
|
public function build_sql ( $data = null ) { |
|
643
|
|
|
if ( !empty( $data ) ) |
|
644
|
|
|
$this->set_data( $data ); |
|
645
|
|
|
|
|
646
|
|
|
$this->built = $data; |
|
647
|
|
|
|
|
648
|
|
|
return $this->built; |
|
649
|
|
|
} |
|
650
|
|
|
|
|
651
|
|
|
/** |
|
652
|
|
|
* Save export to a file. |
|
653
|
|
|
* |
|
654
|
|
|
* @param array $params Additional options for saving. |
|
655
|
|
|
* |
|
656
|
|
|
* @return string The URL of the saved file, a path if not attached. |
|
657
|
|
|
*/ |
|
658
|
|
|
public function save( $params = array() ) { |
|
659
|
|
|
|
|
660
|
|
|
$defaults = array( |
|
661
|
|
|
'file' => null, |
|
662
|
|
|
'path' => null, |
|
663
|
|
|
'attach' => false, |
|
664
|
|
|
); |
|
665
|
|
|
|
|
666
|
|
|
$params = array_merge( $defaults, $params ); |
|
667
|
|
|
|
|
668
|
|
|
$extension = 'txt'; |
|
669
|
|
|
|
|
670
|
|
|
if ( ! empty( $params['file'] ) ) { |
|
671
|
|
|
$export_file = $params['file']; |
|
672
|
|
|
|
|
673
|
|
|
if ( false !== strpos( $export_file, '.' ) ) { |
|
674
|
|
|
$extension = explode( '.', $export_file ); |
|
675
|
|
|
$extension = end( $extension ); |
|
676
|
|
|
} |
|
677
|
|
|
} else { |
|
678
|
|
|
if ( 'sv' === $this->type ) { |
|
679
|
|
|
if ( ',' === $this->delimiter ) { |
|
680
|
|
|
$extension = 'csv'; |
|
681
|
|
|
} elseif ( "\t" === $this->delimiter ) { |
|
682
|
|
|
$extension = 'tsv'; |
|
683
|
|
|
} |
|
684
|
|
|
} else { |
|
685
|
|
|
$extension = $this->type; |
|
686
|
|
|
} |
|
687
|
|
|
|
|
688
|
|
|
$export_file = sprintf( |
|
689
|
|
|
'pods_export_%s.%s', |
|
690
|
|
|
wp_create_nonce( date_i18n( 'm-d-Y_h-i-sa' ) ), |
|
691
|
|
|
$extension |
|
692
|
|
|
); |
|
693
|
|
|
} |
|
694
|
|
|
|
|
695
|
|
|
if ( ! empty( $params['path'] ) ) { |
|
696
|
|
|
$new_file = sprintf( |
|
697
|
|
|
'%s/%s', |
|
698
|
|
|
untrailingslashit( $params['path'] ), |
|
699
|
|
|
$export_file |
|
700
|
|
|
); |
|
701
|
|
|
|
|
702
|
|
|
$filename = $export_file; |
|
703
|
|
|
} else { |
|
704
|
|
|
$uploads = wp_upload_dir( current_time( 'mysql' ) ); |
|
705
|
|
|
|
|
706
|
|
|
if ( ! $uploads || false === $uploads['error'] ) { |
|
707
|
|
|
return pods_error( __( 'There was an issue saving the export file in your uploads folder.', 'pods' ), true ); |
|
708
|
|
|
} |
|
709
|
|
|
|
|
710
|
|
|
// Generate unique file name |
|
711
|
|
|
$filename = wp_unique_filename( $uploads['path'], $export_file ); |
|
712
|
|
|
|
|
713
|
|
|
// move the file to the uploads dir |
|
714
|
|
|
$new_file = $uploads['path'] . '/' . $filename; |
|
715
|
|
|
} |
|
716
|
|
|
|
|
717
|
|
|
file_put_contents( $new_file, $this->built ); |
|
718
|
|
|
|
|
719
|
|
|
// Set correct file permissions |
|
720
|
|
|
$stat = stat( dirname( $new_file ) ); |
|
721
|
|
|
$perms = $stat['mode'] & 0000666; |
|
722
|
|
|
@chmod( $new_file, $perms ); |
|
723
|
|
|
|
|
724
|
|
|
// Only attach if we want to and don't have a custom path. |
|
725
|
|
|
if ( $params['attach'] && empty( $params['path'] ) ) { |
|
726
|
|
|
// Get the file type |
|
727
|
|
|
$wp_filetype = wp_check_filetype( $filename, $this->mimes ); |
|
728
|
|
|
|
|
729
|
|
|
// construct the attachment array |
|
730
|
|
|
$attachment = array( |
|
731
|
|
|
'post_mime_type' => 'text/' . $extension, |
|
732
|
|
|
'guid' => $uploads['url'] . '/' . $filename, |
|
|
|
|
|
|
733
|
|
|
'post_parent' => null, |
|
734
|
|
|
'post_title' => 'Pods Export (' . $export_file . ')', |
|
735
|
|
|
'post_content' => '', |
|
736
|
|
|
'post_status' => 'private' |
|
737
|
|
|
); |
|
738
|
|
|
|
|
739
|
|
|
if ( $wp_filetype['type'] ) { |
|
740
|
|
|
$attachment['post_mime_type'] = $wp_filetype['type']; |
|
741
|
|
|
} |
|
742
|
|
|
|
|
743
|
|
|
// insert attachment |
|
744
|
|
|
$attachment_id = wp_insert_attachment( $attachment, $new_file ); |
|
745
|
|
|
|
|
746
|
|
|
// error! |
|
747
|
|
|
if ( is_wp_error( $attachment_id ) ) { |
|
748
|
|
|
return pods_error( __( 'There was an issue saving the export file in your uploads folder.', 'pods' ), true ); |
|
749
|
|
|
} |
|
750
|
|
|
|
|
751
|
|
|
$url = $attachment['guid']; |
|
752
|
|
|
} else { |
|
753
|
|
|
$url = $new_file; |
|
754
|
|
|
} |
|
755
|
|
|
|
|
756
|
|
|
return $url; |
|
757
|
|
|
|
|
758
|
|
|
} |
|
759
|
|
|
|
|
760
|
|
|
/* |
|
761
|
|
|
* The real enchilada! |
|
762
|
|
|
*/ |
|
763
|
|
|
/* EXAMPLES |
|
|
|
|
|
|
764
|
|
|
//// minimal import (if your fields match on both your pods and tables) |
|
765
|
|
|
$import = array('my_pod' => array('table' => 'my_table')); // if your table name doesn't match the pod name |
|
766
|
|
|
$import = array('my_pod'); // if your table name matches your pod name |
|
767
|
|
|
|
|
768
|
|
|
//// advanced import |
|
769
|
|
|
$import = array(); |
|
770
|
|
|
$import['my_pod'] = array(); |
|
771
|
|
|
$import['my_pod']['fields']['this_field'] = 'field_name_in_table'; // if the field name doesn't match on table and pod |
|
772
|
|
|
$import['my_pod']['fields'][] = 'that_field'; // if the field name matches on table and pod |
|
773
|
|
|
$import['my_pod']['fields']['this_other_field'] = array('filter' => 'wpautop'); // if you want the value to be different than is provided, set a filter function to use [filter uses = filter_name($value,$rowdata)] |
|
774
|
|
|
$import['my_pod']['fields']['another_field'] = array('field' => 'the_real_field_in_table','filter' => 'my_custom_function'); // if you want the value to be filtered, and the field name doesn't match on the table and pod |
|
775
|
|
|
$import[] = 'my_other_pod'; // if your table name matches your pod name |
|
776
|
|
|
$import['another_pod'] = array('update_on' => 'main_field'); // you can update a pod item if the value of this field is the same on both tables |
|
777
|
|
|
$import['another_pod'] = array('reset' => true); // you can choose to reset all data in a pod before importing |
|
778
|
|
|
|
|
779
|
|
|
//// run import |
|
780
|
|
|
pods_import_it($import); |
|
781
|
|
|
*/ |
|
782
|
|
|
/** |
|
783
|
|
|
* @param $import |
|
784
|
|
|
* @param bool $output |
|
785
|
|
|
*/ |
|
786
|
|
|
public function heres_the_beef ( $import, $output = true ) { |
|
787
|
|
|
global $wpdb; |
|
|
|
|
|
|
788
|
|
|
|
|
789
|
|
|
$api = pods_api(); |
|
790
|
|
|
|
|
791
|
|
|
for ( $i = 0; $i < 40000; $i++ ) { |
|
792
|
|
|
echo " \t"; // extra spaces |
|
793
|
|
|
} |
|
794
|
|
|
|
|
795
|
|
|
$default_data = array( |
|
796
|
|
|
'pod' => null, |
|
797
|
|
|
'table' => null, |
|
798
|
|
|
'reset' => null, |
|
799
|
|
|
'update_on' => null, |
|
800
|
|
|
'where' => null, |
|
801
|
|
|
'fields' => array(), |
|
802
|
|
|
'row_filter' => null, |
|
803
|
|
|
'pre_save' => null, |
|
804
|
|
|
'post_save' => null, |
|
805
|
|
|
'sql' => null, |
|
806
|
|
|
'sort' => null, |
|
807
|
|
|
'limit' => null, |
|
808
|
|
|
'page' => null, |
|
809
|
|
|
'output' => null, |
|
810
|
|
|
'page_var' => 'ipg', |
|
811
|
|
|
'bypass_helpers' => false |
|
812
|
|
|
); |
|
813
|
|
|
|
|
814
|
|
|
$default_field_data = array( 'field' => null, 'filter' => null ); |
|
815
|
|
|
|
|
816
|
|
|
if ( !is_array( $import ) ) |
|
817
|
|
|
$import = array( $import ); |
|
818
|
|
|
elseif ( empty( $import ) ) |
|
819
|
|
|
die( '<h1 style="color:red;font-weight:bold;">ERROR: No imports configured</h1>' ); |
|
820
|
|
|
|
|
821
|
|
|
$import_counter = 0; |
|
822
|
|
|
$total_imports = count( $import ); |
|
823
|
|
|
$paginated = false; |
|
824
|
|
|
$avg_time = -1; |
|
825
|
|
|
$total_time = 0; |
|
826
|
|
|
$counter = 0; |
|
827
|
|
|
$avg_unit = 100; |
|
828
|
|
|
$avg_counter = 0; |
|
829
|
|
|
|
|
830
|
|
|
foreach ( $import as $datatype => $data ) { |
|
831
|
|
|
$import_counter++; |
|
832
|
|
|
|
|
833
|
|
|
flush(); |
|
834
|
|
|
@ob_end_flush(); |
|
835
|
|
|
usleep( 50000 ); |
|
836
|
|
|
|
|
837
|
|
|
if ( !is_array( $data ) ) { |
|
838
|
|
|
$datatype = $data; |
|
839
|
|
|
$data = array( 'table' => $data ); |
|
840
|
|
|
} |
|
841
|
|
|
|
|
842
|
|
|
if ( isset( $data[ 0 ] ) ) |
|
843
|
|
|
$data = array( 'table' => $data[ 0 ] ); |
|
844
|
|
|
|
|
845
|
|
|
$data = array_merge( $default_data, $data ); |
|
846
|
|
|
|
|
847
|
|
|
if ( null === $data[ 'pod' ] ) |
|
848
|
|
|
$data[ 'pod' ] = array( 'name' => $datatype ); |
|
849
|
|
|
|
|
850
|
|
|
if ( false !== $output ) |
|
851
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - <strong>Loading Pod: " . $data[ 'pod' ][ 'name' ] . "</strong>\n"; |
|
852
|
|
|
|
|
853
|
|
|
if ( 2 > count( $data[ 'pod' ] ) ) |
|
854
|
|
|
$data[ 'pod' ] = $api->load_pod( array( 'name' => $data[ 'pod' ][ 'name' ] ) ); |
|
855
|
|
|
|
|
856
|
|
|
if ( empty( $data[ 'pod' ][ 'fields' ] ) ) |
|
857
|
|
|
continue; |
|
858
|
|
|
|
|
859
|
|
|
if ( null === $data[ 'table' ] ) |
|
860
|
|
|
$data[ 'table' ] = $data[ 'pod' ][ 'name' ]; |
|
861
|
|
|
|
|
862
|
|
|
if ( $data[ 'reset' ] === true ) { |
|
863
|
|
View Code Duplication |
if ( false !== $output ) |
|
864
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - <strong style='color:blue;'>Resetting Pod: " . $data[ 'pod' ][ 'name' ] . "</strong>\n"; |
|
865
|
|
|
|
|
866
|
|
|
$api->reset_pod( array( 'id' => $data[ 'pod' ][ 'id' ], 'name' => $data[ 'pod' ][ 'name' ] ) ); |
|
867
|
|
|
} |
|
868
|
|
|
|
|
869
|
|
|
if ( null === $data[ 'sort' ] && null !== $data[ 'update_on' ] && isset( $data[ 'fields' ][ $data[ 'update_on' ] ] ) ) { |
|
870
|
|
|
if ( isset( $data[ 'fields' ][ $data[ 'update_on' ] ][ 'field' ] ) ) |
|
871
|
|
|
$data[ 'sort' ] = $data[ 'fields' ][ $data[ 'update_on' ] ][ 'field' ]; |
|
872
|
|
|
else |
|
873
|
|
|
$data[ 'sort' ] = $data[ 'update_on' ]; |
|
874
|
|
|
} |
|
875
|
|
|
|
|
876
|
|
|
$page = 1; |
|
877
|
|
|
|
|
878
|
|
|
if ( false !== $data[ 'page_var' ] && isset( $_GET[ $data[ 'page_var' ] ] ) ) |
|
879
|
|
|
$page = absval( $_GET[ $data[ 'page_var' ] ] ); |
|
880
|
|
|
|
|
881
|
|
|
if ( null === $data[ 'sql' ] ) |
|
882
|
|
|
$data[ 'sql' ] = "SELECT * FROM {$data['table']}" . ( null !== $data[ 'where' ] ? " WHERE {$data['where']}" : '' ) . ( null !== $data[ 'sort' ] ? " ORDER BY {$data['sort']}" : '' ) . ( null !== $data[ 'limit' ] ? " LIMIT " . ( 1 < $page ? ( ( $page - 1 ) * $data[ 'limit' ] ) . ',' : '' ) . "{$data['limit']}" : '' ); |
|
883
|
|
|
|
|
884
|
|
View Code Duplication |
if ( false !== $output ) |
|
885
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Getting Results: " . $data[ 'pod' ][ 'name' ] . "\n"; |
|
886
|
|
|
|
|
887
|
|
View Code Duplication |
if ( false !== $output ) |
|
888
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Using Query: <small><code>" . $data[ 'sql' ] . "</code></small>\n"; |
|
889
|
|
|
|
|
890
|
|
|
$result = $wpdb->get_results( $data[ 'sql' ], ARRAY_A ); |
|
891
|
|
|
|
|
892
|
|
|
if ( false !== $output ) |
|
893
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Results Found: " . count( $result ) . "\n"; |
|
894
|
|
|
|
|
895
|
|
|
$avg_time = -1; |
|
896
|
|
|
$total_time = 0; |
|
897
|
|
|
$counter = 0; |
|
898
|
|
|
$avg_unit = 100; |
|
899
|
|
|
$avg_counter = 0; |
|
900
|
|
|
$result_count = count( $result ); |
|
901
|
|
|
$paginated = false; |
|
902
|
|
|
|
|
903
|
|
|
if ( false !== $data[ 'page_var' ] && $result_count == $data[ 'limit' ] ) |
|
904
|
|
|
$paginated = "<input type=\"button\" onclick=\"document.location=\'" . pods_ui_var_update( array( $data[ 'page_var' ] => $page + 1 ), false, false ) . "\';\" value=\" Continue Import » \" />"; |
|
905
|
|
|
|
|
906
|
|
|
if ( $result_count < $avg_unit && 5 < $result_count ) |
|
907
|
|
|
$avg_unit = number_format( $result_count / 5, 0, '', '' ); |
|
908
|
|
|
elseif ( 2000 < $result_count && 10 < count( $data[ 'pod' ][ 'fields' ] ) ) |
|
909
|
|
|
$avg_unit = 40; |
|
910
|
|
|
|
|
911
|
|
|
$data[ 'count' ] = $result_count; |
|
912
|
|
|
timer_start(); |
|
913
|
|
|
|
|
914
|
|
|
if ( false !== $output && 1 == $import_counter ) |
|
915
|
|
|
echo "<div style='width:50%;background-color:navy;padding:10px 10px 30px 10px;color:#FFF;position:absolute;top:10px;left:25%;text-align:center;'><p id='progress_status' align='center'>" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Running Importer..</p><br /><small>This will automatically update every " . $avg_unit . " rows</small></div>\n"; |
|
916
|
|
|
|
|
917
|
|
|
foreach ( $result as $k => $row ) { |
|
918
|
|
|
flush(); |
|
919
|
|
|
@ob_end_flush(); |
|
920
|
|
|
usleep( 50000 ); |
|
921
|
|
|
|
|
922
|
|
View Code Duplication |
if ( false !== $output ) |
|
923
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Processing Row #" . ( $k + 1 ) . "\n"; |
|
924
|
|
|
|
|
925
|
|
View Code Duplication |
if ( null !== $data[ 'row_filter' ] && function_exists( $data[ 'row_filter' ] ) ) { |
|
926
|
|
|
if ( false !== $output ) |
|
927
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Filtering <strong>" . $data[ 'row_filter' ] . "</strong> on Row #" . ( $k + 1 ) . "\n"; |
|
928
|
|
|
|
|
929
|
|
|
$row = $data[ 'row_filter' ]( $row, $data ); |
|
930
|
|
|
} |
|
931
|
|
|
|
|
932
|
|
|
if ( !is_array( $row ) ) |
|
933
|
|
|
continue; |
|
934
|
|
|
|
|
935
|
|
|
$params = array( |
|
936
|
|
|
'datatype' => $data[ 'pod' ][ 'name' ], |
|
937
|
|
|
'columns' => array(), |
|
938
|
|
|
'bypass_helpers' => $data[ 'bypass_helpers' ] |
|
939
|
|
|
); |
|
940
|
|
|
|
|
941
|
|
|
foreach ( $data[ 'pod' ][ 'fields' ] as $fk => $field_info ) { |
|
942
|
|
|
$field = $field_info[ 'name' ]; |
|
943
|
|
|
|
|
944
|
|
|
if ( !empty( $data[ 'fields' ] ) && !isset( $data[ 'fields' ][ $field ] ) && !in_array( $field, $data[ 'fields' ] ) ) |
|
945
|
|
|
continue; |
|
946
|
|
|
|
|
947
|
|
|
if ( isset( $data[ 'fields' ][ $field ] ) ) { |
|
948
|
|
|
if ( is_array( $data[ 'fields' ][ $field ] ) ) |
|
949
|
|
|
$field_data = $data[ 'fields' ][ $field ]; |
|
950
|
|
|
else |
|
951
|
|
|
$field_data = array( 'field' => $data[ 'fields' ][ $field ] ); |
|
952
|
|
|
} |
|
953
|
|
|
else |
|
954
|
|
|
$field_data = array(); |
|
955
|
|
|
|
|
956
|
|
|
if ( !is_array( $field_data ) ) { |
|
957
|
|
|
$field = $field_data; |
|
958
|
|
|
$field_data = array(); |
|
959
|
|
|
} |
|
960
|
|
|
|
|
961
|
|
|
$field_data = array_merge( $default_field_data, $field_data ); |
|
962
|
|
|
|
|
963
|
|
|
if ( null === $field_data[ 'field' ] ) |
|
964
|
|
|
$field_data[ 'field' ] = $field; |
|
965
|
|
|
|
|
966
|
|
|
$data[ 'fields' ][ $field ] = $field_data; |
|
967
|
|
|
$value = ''; |
|
968
|
|
|
|
|
969
|
|
|
if ( isset( $row[ $field_data[ 'field' ] ] ) ) |
|
970
|
|
|
$value = $row[ $field_data[ 'field' ] ]; |
|
971
|
|
|
|
|
972
|
|
View Code Duplication |
if ( null !== $field_data[ 'filter' ] ) { |
|
973
|
|
|
if ( function_exists( $field_data[ 'filter' ] ) ) { |
|
974
|
|
|
if ( false !== $output ) |
|
975
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Filtering <strong>" . $field_data[ 'filter' ] . "</strong> on Field: " . $field . "\n"; |
|
976
|
|
|
|
|
977
|
|
|
$value = $field_data[ 'filter' ]( $value, $row, $data ); |
|
978
|
|
|
} |
|
979
|
|
|
else |
|
980
|
|
|
$value = ''; |
|
981
|
|
|
} |
|
982
|
|
|
|
|
983
|
|
|
if ( 1 > strlen( $value ) && 1 == $field_info[ 'required' ] ) |
|
984
|
|
|
die( '<h1 style="color:red;font-weight:bold;">ERROR: Field Required for <strong>' . $field . '</strong></h1>' ); |
|
985
|
|
|
|
|
986
|
|
|
$params[ 'columns' ][ $field ] = $value; |
|
987
|
|
|
|
|
988
|
|
|
unset( $value ); |
|
989
|
|
|
unset( $field_data ); |
|
990
|
|
|
unset( $field_info ); |
|
991
|
|
|
unset( $fk ); |
|
992
|
|
|
} |
|
993
|
|
|
|
|
994
|
|
|
if ( empty( $params[ 'columns' ] ) ) |
|
995
|
|
|
continue; |
|
996
|
|
|
|
|
997
|
|
|
$params[ 'columns' ] = pods_sanitize( $params[ 'columns' ] ); |
|
998
|
|
|
|
|
999
|
|
|
if ( null !== $data[ 'update_on' ] && isset( $params[ 'columns' ][ $data[ 'update_on' ] ] ) ) { |
|
1000
|
|
|
if ( false !== $output ) |
|
1001
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Checking for Existing Item\n"; |
|
1002
|
|
|
|
|
1003
|
|
|
$check = new Pod( $data[ 'pod' ][ 'name' ] ); |
|
|
|
|
|
|
1004
|
|
|
$check->findRecords( array( |
|
|
|
|
|
|
1005
|
|
|
'orderby' => 't.id', |
|
1006
|
|
|
'limit' => 1, |
|
1007
|
|
|
'where' => "t.{$data['update_on']} = '{$params['columns'][$data['update_on']]}'", |
|
1008
|
|
|
'search' => false, |
|
1009
|
|
|
'page' => 1 |
|
1010
|
|
|
) ); |
|
1011
|
|
|
|
|
1012
|
|
|
if ( 0 < $check->getTotalRows() ) { |
|
|
|
|
|
|
1013
|
|
|
$check->fetchRecord(); |
|
|
|
|
|
|
1014
|
|
|
|
|
1015
|
|
|
$params[ 'tbl_row_id' ] = $check->get_field( 'id' ); |
|
|
|
|
|
|
1016
|
|
|
$params[ 'pod_id' ] = $check->get_pod_id(); |
|
|
|
|
|
|
1017
|
|
|
|
|
1018
|
|
View Code Duplication |
if ( false !== $output ) |
|
1019
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Found Existing Item w/ ID: " . $params[ 'tbl_row_id' ] . "\n"; |
|
1020
|
|
|
|
|
1021
|
|
|
unset( $check ); |
|
1022
|
|
|
} |
|
1023
|
|
|
|
|
1024
|
|
View Code Duplication |
if ( !isset( $params[ 'tbl_row_id' ] ) && false !== $output ) |
|
1025
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Existing item not found - Creating New\n"; |
|
1026
|
|
|
} |
|
1027
|
|
|
|
|
1028
|
|
View Code Duplication |
if ( null !== $data[ 'pre_save' ] && function_exists( $data[ 'pre_save' ] ) ) { |
|
1029
|
|
|
if ( false !== $output ) |
|
1030
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Running Pre Save <strong>" . $data[ 'pre_save' ] . "</strong> on " . $data[ 'pod' ][ 'name' ] . "\n"; |
|
1031
|
|
|
|
|
1032
|
|
|
$params = $data[ 'pre_save' ]( $params, $row, $data ); |
|
1033
|
|
|
} |
|
1034
|
|
|
|
|
1035
|
|
|
$id = $api->save_pod_item( $params ); |
|
1036
|
|
|
|
|
1037
|
|
|
if ( false !== $output ) |
|
1038
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - <strong>Saved Row #" . ( $k + 1 ) . " w/ ID: " . $id . "</strong>\n"; |
|
1039
|
|
|
|
|
1040
|
|
|
$params[ 'tbl_row_id' ] = $id; |
|
1041
|
|
|
|
|
1042
|
|
View Code Duplication |
if ( null !== $data[ 'post_save' ] && function_exists( $data[ 'post_save' ] ) ) { |
|
1043
|
|
|
if ( false !== $output ) |
|
1044
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - Running Post Save <strong>" . $data[ 'post_save' ] . "</strong> on " . $data[ 'pod' ][ 'name' ] . "\n"; |
|
1045
|
|
|
|
|
1046
|
|
|
$data[ 'post_save' ]( $params, $row, $data ); |
|
1047
|
|
|
} |
|
1048
|
|
|
|
|
1049
|
|
|
unset( $params ); |
|
1050
|
|
|
unset( $result[ $k ] ); |
|
1051
|
|
|
unset( $row ); |
|
1052
|
|
|
|
|
1053
|
|
|
wp_cache_flush(); |
|
1054
|
|
|
$wpdb->queries = array(); |
|
1055
|
|
|
|
|
1056
|
|
|
$avg_counter++; |
|
1057
|
|
|
$counter++; |
|
1058
|
|
|
|
|
1059
|
|
|
if ( $avg_counter == $avg_unit && false !== $output ) { |
|
1060
|
|
|
$avg_counter = 0; |
|
1061
|
|
|
$avg_time = timer_stop( 0, 10 ); |
|
1062
|
|
|
$total_time += $avg_time; |
|
1063
|
|
|
$rows_left = $result_count - $counter; |
|
1064
|
|
|
$estimated_time_left = ( ( $total_time / $counter ) * $rows_left ) / 60; |
|
1065
|
|
|
$percent_complete = 100 - ( ( $rows_left * 100 ) / $result_count ); |
|
1066
|
|
|
|
|
1067
|
|
|
echo "<script type='text/javascript'>document.getElementById('progress_status').innerHTML = '" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em><br /><strong>" . $percent_complete . "% Complete</strong><br /><strong>Estimated Time Left:</strong> " . $estimated_time_left . " minute(s) or " . ( $estimated_time_left / 60 ) . " hours(s)<br /><strong>Time Spent:</strong> " . ( $total_time / 60 ) . " minute(s)<br /><strong>Rows Done:</strong> " . ( $result_count - $rows_left ) . "/" . $result_count . "<br /><strong>Rows Left:</strong> " . $rows_left . "';</script>\n"; |
|
1068
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - <strong>Updated Status:</strong> " . $percent_complete . "% Complete</strong>\n"; |
|
1069
|
|
|
} |
|
1070
|
|
|
} |
|
1071
|
|
|
|
|
1072
|
|
|
if ( false !== $output ) { |
|
1073
|
|
|
$avg_counter = 0; |
|
1074
|
|
|
$avg_time = timer_stop( 0, 10 ); |
|
1075
|
|
|
$total_time += $avg_time; |
|
1076
|
|
|
$rows_left = $result_count - $counter; |
|
1077
|
|
|
$estimated_time_left = ( ( $total_time / $counter ) * $rows_left ) / 60; |
|
1078
|
|
|
$percent_complete = 100 - ( ( $rows_left * 100 ) / $result_count ); |
|
1079
|
|
|
|
|
1080
|
|
|
echo "<script type='text/javascript'>document.getElementById('progress_status').innerHTML = '" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em><br /><strong style=\'color:green;\'>100% Complete</strong><br /><br /><strong>Time Spent:</strong> " . ( $total_time / 60 ) . " minute(s)<br /><strong>Rows Imported:</strong> " . $result_count . ( false !== $paginated ? "<br /><br />" . $paginated : '' ) . "';</script>\n"; |
|
1081
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <em>" . $data[ 'pod' ][ 'name' ] . "</em> - <strong style='color:green;'>Done Importing: " . $data[ 'pod' ][ 'name' ] . "</strong>\n"; |
|
1082
|
|
|
} |
|
1083
|
|
|
|
|
1084
|
|
|
unset( $result ); |
|
1085
|
|
|
unset( $import[ $datatype ] ); |
|
1086
|
|
|
unset( $datatype ); |
|
1087
|
|
|
unset( $data ); |
|
1088
|
|
|
|
|
1089
|
|
|
wp_cache_flush(); |
|
1090
|
|
|
$wpdb->queries = array(); |
|
1091
|
|
|
} |
|
1092
|
|
|
|
|
1093
|
|
|
if ( false !== $output ) { |
|
1094
|
|
|
$avg_counter = 0; |
|
1095
|
|
|
$avg_time = timer_stop( 0, 10 ); |
|
1096
|
|
|
$total_time += $avg_time; |
|
1097
|
|
|
$rows_left = $result_count - $counter; |
|
|
|
|
|
|
1098
|
|
|
|
|
1099
|
|
|
echo "<script type='text/javascript'>document.getElementById('progress_status').innerHTML = '" . date( 'Y-m-d h:i:sa' ) . " - <strong style=\'color:green;\'>Import Complete</strong><br /><br /><strong>Time Spent:</strong> " . ( $total_time / 60 ) . " minute(s)<br /><strong>Rows Imported:</strong> " . $result_count . ( false !== $paginated ? "<br /><br />" . $paginated : '' ) . "';</script>\n"; |
|
1100
|
|
|
echo "<br />" . date( 'Y-m-d h:i:sa' ) . " - <strong style='color:green;'>Import Complete</strong>\n"; |
|
1101
|
|
|
} |
|
1102
|
|
|
} |
|
1103
|
|
|
|
|
1104
|
|
|
/** |
|
1105
|
|
|
* Export data to a file. |
|
1106
|
|
|
* |
|
1107
|
|
|
* @param string $file File to export to. |
|
1108
|
|
|
* @param array $data Data to export. |
|
1109
|
|
|
* @param bool $single Whether this is a single item export. |
|
1110
|
|
|
* |
|
1111
|
|
|
* @return mixed |
|
1112
|
|
|
*/ |
|
1113
|
|
|
public static function export_data_to_file( $file, $data, $single = false ) { |
|
1114
|
|
|
|
|
1115
|
|
|
$path = ABSPATH; |
|
1116
|
|
|
|
|
1117
|
|
|
// Detect path if it is set in the file param. |
|
1118
|
|
View Code Duplication |
if ( false !== strpos( $file, '/' ) ) { |
|
1119
|
|
|
$path = dirname( $file ); |
|
1120
|
|
|
$file = basename( $file ); |
|
1121
|
|
|
} |
|
1122
|
|
|
|
|
1123
|
|
|
$format = 'json'; |
|
1124
|
|
|
|
|
1125
|
|
|
// Detect the export format. |
|
1126
|
|
View Code Duplication |
if ( false !== strpos( $file, '.' ) ) { |
|
1127
|
|
|
$format = explode( '.', $file ); |
|
1128
|
|
|
$format = end( $format ); |
|
1129
|
|
|
} |
|
1130
|
|
|
|
|
1131
|
|
|
$migrate_data = array( |
|
1132
|
|
|
'items' => array( $data ), |
|
1133
|
|
|
'single' => $single, |
|
1134
|
|
|
); |
|
1135
|
|
|
|
|
1136
|
|
|
$migrate = new self( $format, null, $migrate_data ); |
|
1137
|
|
|
|
|
1138
|
|
|
// Handle processing the data into the format needed. |
|
1139
|
|
|
$migrate->export(); |
|
1140
|
|
|
|
|
1141
|
|
|
$save_params = array( |
|
1142
|
|
|
'path' => $path, |
|
1143
|
|
|
'file' => $file, |
|
1144
|
|
|
'attach' => true, |
|
1145
|
|
|
); |
|
1146
|
|
|
|
|
1147
|
|
|
return $migrate->save( $save_params ); |
|
1148
|
|
|
|
|
1149
|
|
|
} |
|
1150
|
|
|
|
|
1151
|
|
|
/** |
|
1152
|
|
|
* Get data from a file. |
|
1153
|
|
|
* |
|
1154
|
|
|
* @param string $file File to get data from. |
|
1155
|
|
|
* @param bool $single Whether this is a single item. |
|
1156
|
|
|
* |
|
1157
|
|
|
* @return mixed |
|
1158
|
|
|
*/ |
|
1159
|
|
|
public static function get_data_from_file( $file, $single = false ) { |
|
1160
|
|
|
|
|
1161
|
|
|
$path = ABSPATH; |
|
1162
|
|
|
|
|
1163
|
|
|
// Detect path if it is set in the file param. |
|
1164
|
|
View Code Duplication |
if ( false !== strpos( $file, '/' ) ) { |
|
1165
|
|
|
$path = dirname( $file ); |
|
1166
|
|
|
$file = basename( $file ); |
|
1167
|
|
|
} |
|
1168
|
|
|
|
|
1169
|
|
|
$format = 'json'; |
|
1170
|
|
|
|
|
1171
|
|
|
// Detect the export format. |
|
1172
|
|
View Code Duplication |
if ( false !== strpos( $file, '.' ) ) { |
|
1173
|
|
|
$format = explode( '.', $file ); |
|
1174
|
|
|
$format = end( $format ); |
|
1175
|
|
|
} |
|
1176
|
|
|
|
|
1177
|
|
|
$migrate_data = array( |
|
1178
|
|
|
'single' => $single, |
|
1179
|
|
|
); |
|
1180
|
|
|
|
|
1181
|
|
|
$migrate = new self( $format, null, $migrate_data ); |
|
1182
|
|
|
|
|
1183
|
|
|
$raw_data = file_get_contents( $file ); |
|
1184
|
|
|
|
|
1185
|
|
|
// Handle processing the raw data from the format needed. |
|
1186
|
|
|
$data = $migrate->parse( $raw_data ); |
|
|
|
|
|
|
1187
|
|
|
|
|
1188
|
|
|
return $data; |
|
1189
|
|
|
|
|
1190
|
|
|
} |
|
1191
|
|
|
|
|
1192
|
|
|
} |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.