Browse code

Initial commit

Benjamin Roth authored on26/11/2015 10:33:32
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,749 @@
1
+<?php
2
+/**
3
+ * OXID CSV Export
4
+ *
5
+ * Exportscript für das exportieren von Artikeldaten in eine CSV-Datei.
6
+ * 
7
+ * @author Benjamin Roth [benjamin@esales-media.de]
8
+ * @version 0.1.11.1.25
9
+ * @copyright 2011 eSales Media [www.esales-media.de]
10
+ * @package OXID-Export
11
+ */
12
+
13
+/* CONFIGURATION START */
14
+
15
+define('ES_CSV_EXPORT_HASH', '07fb834c7f9a5c5ff300bbe5b8ad8e58'); // MD5::weisenbach
16
+ 
17
+/**
18
+ * DB Settings
19
+ */
20
+ 
21
+$DB = array
22
+(
23
+	'host'     => 'db596917193.db.1and1.com',
24
+	'database' => 'db596917193',
25
+	'username' => 'dbo596917193',
26
+	'password' => 'h7skE3B'
27
+);
28
+
29
+/**
30
+ * Load configuration or exit
31
+ */
32
+if (!isset($_GET['config']) && !isset($_SERVER['argv'][1]))
33
+	die('No configuration file specified');
34
+
35
+if (isset($_GET['config']))
36
+{
37
+	$config = basename($_GET['config']);
38
+} else {
39
+	$config = basename($_SERVER['argv'][1]);
40
+}
41
+
42
+if (!strlen($config) || !file_exists(dirname(__FILE__).'/conf/'.$config))
43
+	die('Configuration file not found');
44
+
45
+require_once(dirname(__FILE__).'/conf/'.$config);
46
+
47
+if (!isset($CONFIG) || !isset($EXPORT))
48
+	die('Missing configuration');
49
+
50
+/* CONFIGURATION END */
51
+
52
+/* OXID INIT START */
53
+
54
+/**
55
+ * Returns false.
56
+ *
57
+ * @return boolean
58
+ */
59
+error_reporting( E_ALL ^ E_NOTICE );
60
+ini_set('display_errors',true);
61
+
62
+define('OX_BASE_PATH', dirname(__FILE__) .'/../' ); 
63
+
64
+// custom functions file 
65
+require OX_BASE_PATH . 'bootstrap.php'; 
66
+
67
+// error reporting level
68
+error_reporting(E_ALL ^ E_NOTICE);
69
+
70
+// Generic utility method file including autoloading definition
71
+require_once OX_BASE_PATH . 'core/oxfunctions.php';
72
+
73
+/* OXID INIT END */
74
+
75
+
76
+/**
77
+ * Export Class
78
+ *
79
+ * @package OXID-Export
80
+ * @author  Benjamin Roth
81
+ */
82
+class Export extends oxUBase {
83
+	
84
+	protected $_aArticles = array();
85
+	
86
+	protected $_aCategories = array();
87
+	
88
+	protected $db;
89
+	
90
+	protected $oOxidConfig;
91
+	
92
+	protected $currentArticle = array();
93
+
94
+	protected $arrDefaultSettings = array
95
+	(
96
+		'field_encloser'     => '"',
97
+		'field_terminator'   => ';',
98
+		'escape_char'        => '\\',
99
+		'filename'           => 'esm_export/csv/export.csv',
100
+		'encoding'           => 'UTF-8',
101
+		'stripHTML'          => true,
102
+		'strictStrip'        => true,
103
+		'doublets'           => true,
104
+		'languageid'         => 0,
105
+		'includeHeader'      => true,
106
+		'activeOnly'         => true,
107
+		'activeArticleField' => 'OXACTIVE',
108
+		'activeCatField'     => 'OXACTIVE',
109
+		'onStockOnly'        => false,
110
+		'skipLastTerminator' => true,
111
+		'noVariants'         => false,
112
+		'excludeParent'      => true,
113
+		'catPathSeparator'   => ">",
114
+		'customSqlCondition' => '',
115
+		'eol'                => 'LF',
116
+	);
117
+
118
+	protected $arrConfig = array();
119
+
120
+	protected $blnSkipTrim = false;
121
+
122
+	public function __construct()
123
+	{
124
+		// Get shop config object
125
+		$this->oOxidConfig = $this->getConfig();
126
+
127
+		// Set job config
128
+		$this->arrConfig = array_merge($this->arrDefaultSettings,(is_array($GLOBALS['CONFIG']) ? $GLOBALS['CONFIG'] : array()));
129
+		
130
+		// DB initialization
131
+		$this->db = oxDb::getDb();
132
+		$this->db->SetFetchMode(ADODB_FETCH_ASSOC);
133
+		
134
+		// Check config vars
135
+		$blnActive = ($this->arrConfig['activeOnly'] === true ? true : false);
136
+		$blnOnStockOnly = ($this->arrConfig['onStockOnly'] === true ? true : false);
137
+		$blnCustomSqlCond = ($this->arrConfig['customSqlCondition'] ? true : false);
138
+
139
+		$cat_where = '';
140
+		if (count($GLOBALS['EXPORT']['categories']['exclude_ids']) > 0)
141
+		{
142
+			$cat_excludes_array = array();
143
+			foreach($GLOBALS['EXPORT']['categories']['exclude_ids'] as $cat)
144
+			{
145
+				$cat_excludes_array[] = preg_replace('/(^|$)/',"'",$cat);
146
+			}
147
+			$cat_excludes = implode(',',$cat_excludes_array);
148
+			$cat_where .= ' AND oac.OXCATNID NOT IN ('.$cat_excludes.')';
149
+		}
150
+		if (count($GLOBALS['EXPORT']['categories']['include_ids']) > 0)
151
+		{
152
+			$cat_includes_array = array();
153
+			foreach($GLOBALS['EXPORT']['categories']['include_ids'] as $cat)
154
+			{
155
+				$cat_includes_array[] = preg_replace('/(^|$)/',"'",$cat);
156
+			}
157
+			$cat_includes = implode(',',$cat_includes_array);
158
+			$cat_where .= ' AND oac.OXCATNID IN ('.$cat_includes.')';
159
+		}
160
+		if ($blnOnStockOnly)
161
+		{
162
+			$cat_where .= ' AND oa.OXSTOCK > 0';
163
+		}
164
+		if ($blnActive)
165
+		{
166
+			$cat_where .= ' AND oa.'.$this->arrConfig['activeArticleField'].' = 1 AND oc.'.$this->arrConfig['activeCatField'].' = 1';
167
+		}
168
+		if ($blnCustomSqlCond)
169
+		{
170
+			$cat_where .= ' AND '.$this->arrConfig['customSqlCondition'];
171
+		}
172
+		
173
+		$sql = "SELECT oa.OXID, oc.OXID AS COXID FROM oxarticles AS oa, oxobject2category as oac, oxcategories as oc WHERE oac.OXOBJECTID=oa.OXID AND oc.OXID=oac.OXCATNID AND oa.OXPARENTID = '' ".$cat_where." ".(!$this->arrConfig['doublets'] ? 'GROUP BY oa.OXID' : '')." ORDER BY oac.OXPOS, oc.OXSORT, oa.OXSORT";
174
+		$rs = $this->db->execute($sql);
175
+
176
+		while(!$rs->EOF)
177
+		{
178
+			$arrRow = $rs->fetchRow();
179
+			$oArticle = oxNew('oxarticle');
180
+			$oArticle->setLanguage($this->arrConfig['languageid']);
181
+			$oArticle->load($arrRow['OXID']);
182
+			$Variants = $oArticle->getVariants();
183
+			if (!$this->arrConfig['noVariants'] && $Variants instanceof oxArticleList && $Variants->count())
184
+			{
185
+				if (!$this->arrConfig['excludeParent'])
186
+				{
187
+					$this->_aArticles[] = array('category' => $arrRow['COXID'], 'article' => $oArticle->oxarticles__oxid->value, 'hasVariants' => true);
188
+				}
189
+				foreach($Variants->getArray() as $variant)
190
+				{
191
+					if (!$blnOnStockOnly || ($blnOnStockOnly && $variant->oxarticles__oxstock->value > 0))
192
+					{
193
+						$this->_aArticles[] = array('category' => $arrRow['COXID'], 'article' => $variant->oxarticles__oxid->value, 'hasVariants' => false);
194
+					}
195
+				}
196
+			} else {
197
+				$this->_aArticles[] = array('category' => $arrRow['COXID'], 'article' => $oArticle->oxarticles__oxid->value, 'hasVariants' => false);
198
+			}
199
+		}
200
+		
201
+	}
202
+
203
+	function __call($name, $arguments)
204
+	{
205
+		switch ($name)
206
+		{
207
+			case 'getConfigParam':
208
+				return (array_key_exists($arguments[0],$this->arrConfig) ? $this->arrConfig[$arguments[0]] : false);
209
+			break;
210
+
211
+			case 'getJobConfig':
212
+				return $this->arrConfig;
213
+			break;
214
+
215
+			default:
216
+				throw new BadFunctionCallException('Method "'.$name.'" does not exist');
217
+		}
218
+	}
219
+
220
+
221
+	/**
222
+	 * function getCategory
223
+	 *
224
+	 * @return object
225
+	 */
226
+	function getCategory($oxid)
227
+	{
228
+		if (isset($this->_aCategories[$oxid]) && is_object($this->_aCategories[$oxid]))
229
+			return $this->_aCategories[$oxid];
230
+		
231
+		$oCategory = oxNew('oxcategory');
232
+		$oCategory->setLanguage($this->arrConfig['languageid']);
233
+		$oCategory->load($oxid);
234
+
235
+		$this->_aCategories[$oxid] = $oCategory;
236
+		return $this->_aCategories[$oxid];
237
+	}
238
+
239
+	function getCategoryPath($oxid)
240
+	{
241
+		$arrPath = array();
242
+		if (isset($this->_aCategories[$oxid]) && is_object($this->_aCategories[$oxid]))
243
+		{
244
+			$oCategory = &$this->_aCategories[$oxid];
245
+		} else {
246
+			$oCategory = oxNew('oxcategory');
247
+			$oCategory->setLanguage($this->arrConfig['languageid']);
248
+			$oCategory->load($oxid);
249
+
250
+			$this->_aCategories[$oxid] = $oCategory;
251
+
252
+		}
253
+
254
+		$arrPath[] = trim($oCategory->oxcategories__oxtitle->value);
255
+
256
+		if ($oCategory->oxcategories__oxparentid->value != 'oxrootid')
257
+		{
258
+			$arrPath = array_merge($arrPath,$this->getCategoryPath($oCategory->oxcategories__oxparentid->value));
259
+		}
260
+
261
+		return array_reverse($arrPath);
262
+	}
263
+
264
+	/**
265
+	 * function replaceTags
266
+	 *
267
+	 * @return string
268
+	 */
269
+	protected function replaceTags($strBuffer)
270
+	{
271
+		//$tags = preg_split('/{{([^}]+)}}/', $strBuffer, -1, PREG_SPLIT_DELIM_CAPTURE);
272
+		$tags = preg_split('/\{\{(([^\{\}]*|(?R))*)\}\}/', $strBuffer, -1, PREG_SPLIT_DELIM_CAPTURE);
273
+
274
+		$strBuffer = '';
275
+		$arrCache = array();
276
+
277
+		for($_rit=0, $_cnt=count($tags); $_rit<$_cnt; $_rit+=3)
278
+		{
279
+			/*if (!isset($tags[$_rit+1]))
280
+			{
281
+				continue;
282
+			}*/
283
+
284
+			$strBuffer .= $tags[$_rit];
285
+			$strTag = (isset($tags[$_rit+1]) ? $tags[$_rit+1] : '');
286
+
287
+			// Skip empty tags
288
+			if (!strlen($strTag))
289
+			{
290
+				continue;
291
+			}
292
+
293
+			// Load value from cache array
294
+			if (isset($arrCache[$strTag]))
295
+			{
296
+				$strBuffer .= $arrCache[$strTag];
297
+				continue;
298
+			}
299
+
300
+			// Run the replacement again if there are more tags (see #4402)
301
+			if (strpos($strTag, '{{') !== false)
302
+			{
303
+				$strTag = $this->replaceTags($strTag);
304
+			}
305
+
306
+			$elements = explode('::', $strTag);
307
+
308
+			$arrCache[$strTag] = '';
309
+
310
+			// Replace tag
311
+			switch (strtolower($elements[0]))
312
+			{
313
+				case 'parent':
314
+					if (is_null($this->currentArticle['parent']))
315
+					{
316
+						$this->currentArticle['parent'] = $this->currentArticle['article']->getParentArticle();
317
+					}
318
+					$arrCache[$strTag] = $this->currentArticle['parent']->{'oxarticles__'.strtolower($elements[1])}->value;
319
+					break;
320
+
321
+				case 'article':
322
+					$arrCache[$strTag] = $this->currentArticle['article']->{'oxarticles__'.strtolower($elements[1])}->value;
323
+					break;
324
+
325
+				case 'inheritance':
326
+					if (!$this->currentArticle['article']->{'oxarticles__'.strtolower($elements[1])}->value)
327
+					{
328
+						if (is_null($this->currentArticle['parent']))
329
+						{
330
+							$this->currentArticle['parent'] = $this->currentArticle['article']->getParentArticle();
331
+						}
332
+						$arrCache[$strTag] = (!$this->currentArticle['parent'] ? '' : $this->currentArticle['parent']->{'oxarticles__'.strtolower($elements[1])}->value);
333
+					} else {
334
+						$arrCache[$strTag] = $this->currentArticle['article']->{'oxarticles__'.strtolower($elements[1])}->value;
335
+					}
336
+					break;
337
+
338
+				case 'category':
339
+					$arrCache[$strTag] = $this->currentArticle['category']->{'oxcategories__'.strtolower($elements[1])}->value;
340
+					break;
341
+
342
+				case 'categorypath':
343
+					$arrCache[$strTag] = implode($this->arrConfig['catPathSeparator'],$this->getCategoryPath($this->currentArticle['category']->oxcategories__oxid->value));
344
+					break;
345
+
346
+				case 'crossselling':
347
+					$oCrossSelling = $this->currentArticle['article']->getCrossSelling();
348
+					$arrCrossSelling = array();
349
+					if (!is_null($oCrossSelling) && $intLimit = $oCrossSelling->count())
350
+					{
351
+						if (isset($elements[1]))
352
+						{
353
+							$intLimit = intval($elements[1]);
354
+						}
355
+
356
+						$i=0;
357
+						while ($oCrossSelling->valid() && $i < $intLimit)
358
+						{
359
+							if ($oCrossSelling->current()->getVariants())
360
+							{
361
+								$arrCrossSelling[] = $oCrossSelling->current()->oxarticles__oxartnum->value.'__parent';
362
+							} else {
363
+								$arrCrossSelling[] = $oCrossSelling->current()->oxarticles__oxartnum->value;
364
+							}
365
+							$oCrossSelling->next();
366
+							$i++;
367
+						}
368
+					}
369
+					$arrCache[$strTag] = implode(',',$arrCrossSelling);
370
+					break;
371
+					
372
+				case 'db_foreign':
373
+					if (isset($elements[1]) && isset($elements[2]) && ($chunks = explode('=',$elements[1])))
374
+					{
375
+						list($foreign_table, $foreign_field) = explode('.',$chunks[0]);
376
+						$foreign_keyValue = $this->currentArticle['article']->{'oxarticles__'.strtolower($chunks[1])}->value;
377
+						if (($sViewName = getViewName($foreign_table, $this->arrConfig['languageid'])))
378
+						{
379
+							$sDbValue = $this->db->getOne("select ".$elements[2]." from {$sViewName} where ".$foreign_field." = " . $this->db->quote($foreign_keyValue));
380
+
381
+						} else {
382
+							$sDbValue = $this->db->getOne("select ".$elements[2]." from {$foreign_table} where ".$foreign_field." = " . $this->db->quote($foreign_keyValue));
383
+
384
+						}/* else {
385
+							$sql = "SELECT * FROM ".$foreign_table." WHERE ".$foreign_field."='".$foreign_keyValue."'";
386
+
387
+							$resSql = $this->db->execute($sql);
388
+
389
+							$arrCache[$strTag] = ($resSql->recordCount() < 1) ? '' : $resSql->fields($elements[2]);
390
+						}*/
391
+						if ($sDbValue)
392
+						{
393
+							$oForeign = new oxField();
394
+							$oForeign->setValue($sDbValue, oxField::T_RAW);
395
+
396
+							$arrCache[$strTag] = $oForeign->value;
397
+						}
398
+
399
+
400
+						unset($resSql,$oForeign);
401
+					}
402
+					break;
403
+
404
+				case 'link':
405
+					switch(strtolower($elements[1]))
406
+					{
407
+						case 'article':
408
+							if ( oxRegistry::getUtils()->seoIsActive() )
409
+							{
410
+								$arrCache[$strTag] = preg_replace('/(?<=\.html)\?.*$/', '', $this->currentArticle['article']->getMainLink());
411
+							} else {
412
+								$arrCache[$strTag] = $this->currentArticle['article']->getMainLink();
413
+							}
414
+							break;
415
+						case 'category':
416
+							if ( oxRegistry::getUtils()->seoIsActive() )
417
+							{
418
+								$arrCache[$strTag] = preg_replace('/(?<=\.html)\?.*$/', '', $this->currentArticle['category']->getMainLink());
419
+							} else {
420
+								$arrCache[$strTag] = $this->currentArticle['category']->getMainLink();
421
+							}
422
+							break;
423
+					}
424
+					break;
425
+
426
+				case 'image':
427
+					switch(strtolower($elements[1]))
428
+					{
429
+						case 'article':
430
+							switch(strtolower($elements[2]))
431
+							{
432
+								case 'thumb':
433
+									$arrCache[$strTag] = $this->currentArticle['article']->getThumbnailUrl();
434
+									break;
435
+								case 'icon':
436
+									$arrCache[$strTag] = $this->currentArticle['article']->getIconUrl();
437
+									break;
438
+								default:
439
+									$arrCache[$strTag] = ($this->currentArticle['article']->{'oxarticles__oxpic'.$elements[2]}->value ? $this->currentArticle['article']->getZoomPictureUrl(intval($elements[2])) : '');
440
+									break;
441
+							}
442
+							break;
443
+						case 'category':
444
+							$arrCache[$strTag] = '';
445
+							break;
446
+					}
447
+					break;
448
+
449
+				case 'db_if':
450
+					if (isset($elements[1]) && isset($elements[2]) && isset($elements[3]) && isset($elements[4]))
451
+					{
452
+						
453
+						$db_value = $this->currentArticle['article']->{'oxarticles__'.strtolower($elements[1])}->value;
454
+						if (isset($elements[5]))
455
+						{
456
+							$varVal = $elements[4];
457
+							$varAltVal = $elements[5];
458
+						} else {
459
+							$varVal = $db_value;
460
+							$varAltVal = $elements[4];
461
+						}
462
+						switch($elements[2])
463
+						{
464
+							case '>':
465
+								$arrCache[$strTag] = ($db_value > $elements[3]) ? $varVal : $varAltVal;
466
+								break;
467
+							case '>=':
468
+								$arrCache[$strTag] = ($db_value >= $elements[3]) ? $varVal : $varAltVal;
469
+								break;
470
+							case '<':
471
+								$arrCache[$strTag] = ($db_value < $elements[3]) ? $varVal : $varAltVal;
472
+								break;
473
+							case '<=':
474
+								$arrCache[$strTag] = ($db_value <= $elements[3]) ? $varVal : $varAltVal;
475
+								break;
476
+							case '=':
477
+								$arrCache[$strTag] = ($db_value == $elements[3]) ? $varVal : $varAltVal;
478
+								break;
479
+							case '!=':
480
+								$arrCache[$strTag] = ($db_value != $elements[3]) ? $varVal : $varAltVal;
481
+								break;
482
+						}
483
+					}
484
+					break;
485
+
486
+				case 'not_empty':
487
+					if (isset($elements[1]) && isset($elements[2]))
488
+					{
489
+						if ($elements[1])
490
+						{
491
+							$arrCache[$strTag] = $elements[2];
492
+						} else {
493
+							$arrCache[$strTag] = '';
494
+						}
495
+					}
496
+					break;
497
+
498
+				case 'fixval':
499
+					$arrCache[$strTag] = $elements[1];
500
+					break;
501
+
502
+				case 'has_variants':
503
+					if (isset($elements[1]) && isset($elements[2]))
504
+					{
505
+						$arrCache[$strTag] = ($this->currentArticle['hasVariants'] ? $elements[1] : $elements[2]);
506
+					}
507
+					break;
508
+
509
+				case 'parse_smarty':
510
+					$arrCache[$strTag] = oxRegistry::get("oxUtilsView")->parseThroughSmarty($elements[1], basename($this->arrConfig['filename']) . $this->arrConfig['languageid'], null, true);
511
+					break;
512
+
513
+				case 'fstring':
514
+					if (isset($elements[1]) && isset($elements[2]))
515
+					{
516
+						$this->blnSkipTrim = true;
517
+						//$strFstring = call_user_func_array("sprintf",array_slice($elements,1,2));
518
+						$strFstring = sprintf($elements[1],$this->convert($elements[2]));
519
+						if (isset($elements[3]))
520
+						{
521
+							$strFstring = substr($strFstring,0,$elements[3]);
522
+						}
523
+						$arrCache[$strTag] = $this->convert($strFstring,'utf-8');
524
+					}
525
+					break;
526
+
527
+				case 'fdate':
528
+					if (isset($elements[1]) && isset($elements[2]))
529
+					{
530
+						$oDate = new DateTime($elements[2]);
531
+						if (is_object($oDate))
532
+						$arrCache[$strTag] = $oDate->format($elements[1]);
533
+					}
534
+					break;
535
+
536
+				case 'price':
537
+					if (isset($elements[1]))
538
+					{
539
+						$oPrice = oxNew( 'oxPrice' );
540
+						$blPriceNet = (bool) $this->getConfig()->getConfigParam('blShowNetPrice');
541
+						if ($blPriceNet)
542
+						{
543
+							$oPrice->setNettoPriceMode();
544
+						} else {
545
+							$oPrice->setBruttoPriceMode();
546
+						}
547
+
548
+						$oPrice->setPrice(doubleval($elements[1]));
549
+
550
+						if (isset($elements[2]))
551
+						{
552
+							switch ($elements[2])
553
+							{
554
+								case 'net':
555
+								case 'netto':
556
+									$arrCache[$strTag] = $oPrice->getNettoPrice();
557
+									break;
558
+
559
+								case 'gross':
560
+								case 'brutto':
561
+									$arrCache[$strTag] = $oPrice->getBruttoPrice();
562
+									break;
563
+
564
+								default:
565
+									$arrCache[$strTag] = $oPrice->getPrice();
566
+									break;
567
+							}
568
+						}
569
+					}
570
+					break;
571
+
572
+				case 'translate':
573
+					if (isset($elements[1]) && isset($elements[2]))
574
+					{
575
+						if (isset($GLOBALS['EXPORT']['translate'][$elements[1]][$elements[2]]))
576
+						{
577
+							$arrCache[$strTag] = $GLOBALS['EXPORT']['translate'][$elements[1]][$elements[2]];
578
+						}
579
+					}
580
+					break;
581
+			}
582
+
583
+			$strBuffer .= $arrCache[$strTag];
584
+		}
585
+
586
+		return $strBuffer;
587
+	}
588
+	
589
+	protected function parseLine($arrArticle)
590
+	{
591
+
592
+		$oArticle = oxNew('oxarticle');
593
+		$oArticle->setLanguage($this->arrConfig['languageid']);
594
+		$oArticle->load($arrArticle['article']);
595
+
596
+		$this->currentArticle = array
597
+		(
598
+			'category'    => $this->getCategory($arrArticle['category']),
599
+			'article'     => $oArticle,
600
+			'hasVariants' => $arrArticle['hasVariants'],
601
+			'parent'      => null
602
+		);
603
+
604
+		$this->blnSkipTrim = false;
605
+		$strLine = '';
606
+		$index = 0;
607
+		foreach($GLOBALS['EXPORT']['fields'] as $field)
608
+		{
609
+			$field = $this->replaceTags($field);
610
+			$field = $this->encapsulate($field, ($this->arrConfig['skipLastTerminator'] && ++$index == count($GLOBALS['EXPORT']['fields']) ? true : false));
611
+//			$field = mb_convert_encoding($field, strtoupper($this->arrConfig['encoding']), 'UTF-8');
612
+			$strLine .= $this->convert($field);
613
+		}
614
+		return $strLine.(strtoupper($this->arrConfig['eol']) == 'CRLF' ? "\r\n" : "\n");
615
+	}
616
+	
617
+	public function encapsulate($strVal, $blnSkipTerminator=false)
618
+	{
619
+		$strSrcEncoding = mb_detect_encoding($strVal,"UTF-8,".$this->arrConfig['encoding'].",cp1252,cp1251,cp1250,iso-8859-1,iso-8859-15",true);
620
+
621
+		if (!$this->blnSkipTrim)
622
+		{
623
+			$strVal = trim($strVal);
624
+		}
625
+		$strVal = preg_replace('/[\\n\\r]/', '<br />', $strVal);
626
+		if ($this->arrConfig['stripHTML'])
627
+		{
628
+			if ($this->arrConfig['strictStrip']) {
629
+				$strVal = preg_replace('/(<br\s*(|\/)\s*>)+/i', ' ', $strVal);
630
+				$strVal = preg_replace('/(<p\s*(|\/)\s*>)+/i', ' ', $strVal);
631
+				$strVal = strip_tags($strVal);
632
+//				$strVal = html_entity_decode($strVal, ENT_QUOTES, 'UTF-8');
633
+				$strVal = mb_convert_encoding($strVal, 'HTML-ENTITIES', $strSrcEncoding);
634
+				if (!$this->blnSkipTrim)
635
+				{
636
+					$strVal = trim(preg_replace('/[\xa0\s]+/', ' ', $strVal));
637
+				}
638
+				$strVal = str_ireplace('&nbsp;','',$strVal);
639
+			} else {
640
+				$strVal = strip_tags($strVal,'<br><p>');
641
+				$strVal = preg_replace('/(<br\s*(|\/)\s*>){2,}/', '<br>', $strVal);
642
+			}
643
+		}
644
+		$strVal = str_replace($this->arrConfig['field_encloser'],$this->arrConfig['escape_char'].$this->arrConfig['field_encloser'],$strVal);
645
+		if (strlen($strVal))
646
+		{
647
+			$strVal = preg_replace('/(^|$)/',$this->arrConfig['field_encloser'],$strVal);
648
+		} else {
649
+			$strVal = $this->arrConfig['field_encloser'].$this->arrConfig['field_encloser'];
650
+		}
651
+		
652
+		return $strVal.($blnSkipTerminator ? '' : $this->arrConfig['field_terminator']);
653
+	}
654
+	
655
+	protected function convert($strValue, $strCharset = null)
656
+	{
657
+		if (is_null($strCharset))
658
+		{
659
+			$strCharset = strtoupper($this->arrConfig['encoding']);
660
+		} else {
661
+			$strCharset = strtoupper($strCharset);
662
+		}
663
+
664
+		$strSrcEncoding = mb_detect_encoding($strValue,"UTF-8,".$this->arrConfig['encoding'].",cp1252,cp1251,cp1250,iso-8859-1,iso-8859-15",true);
665
+		return ($strSrcEncoding == $strCharset) ? $strValue : mb_convert_encoding($strValue, $strCharset, $strSrcEncoding);
666
+	}
667
+
668
+	protected function getHeader()
669
+	{
670
+		$strHeader = '';
671
+		$index = 0;
672
+		foreach(array_keys($GLOBALS['EXPORT']['fields']) as $field)
673
+		{
674
+			$field = $this->encapsulate($field, ($this->arrConfig['skipLastTerminator'] && ++$index == count($GLOBALS['EXPORT']['fields']) ? true : false));
675
+			$strHeader .= $this->convert($field);
676
+		}
677
+		return $strHeader."\n";
678
+	}
679
+	
680
+	public function getCSV()
681
+	{
682
+		// echo 'Datensätze: '.count($this->arrDB)."\n";
683
+		$strBuffer = ($this->arrConfig['includeHeader']) ? $this->getHeader() : '';
684
+		foreach($this->arrDB as $this->arrRow)
685
+		{
686
+			$strBuffer .= $this->parseLine($this->arrRow);
687
+		}
688
+		return $strBuffer;
689
+	}
690
+	
691
+	public function saveCSV()
692
+	{
693
+		$tmpname = sys_get_temp_dir().'/'.hash('crc32', uniqid(microtime()));
694
+
695
+		$fh = fopen($tmpname,"w");
696
+		// $fh = fopen(getShopBasePath() . $this->arrConfig['filename'],"w");
697
+		
698
+		fputs($fh,(($this->arrConfig['includeHeader']) ? $this->getHeader() : ''));
699
+		foreach($this->_aArticles as $article)
700
+		{
701
+			// echo $article['article']->oxarticles__oxtitle->value."\n";
702
+			// echo $article['category']->oxcategories__oxtitle->value."\n\n";
703
+			fputs($fh,$this->parseLine($article));
704
+		}
705
+		
706
+		fclose($fh);
707
+		if (!rename($tmpname, getShopBasePath() . $this->arrConfig['filename']))
708
+			die('Export file could not be created');
709
+			
710
+		return count($this->_aArticles);
711
+	}
712
+}
713
+
714
+
715
+$Export = new Export();
716
+$intArticles = $Export->saveCSV();
717
+
718
+header('Content-Type: text/plain; charset='.$Export->getConfigParam['encoding']);
719
+echo "Items exported: " . $intArticles."\n";
720
+
721
+$intMemory = memory_get_peak_usage(true);
722
+$hrsize = function($intMemory) {
723
+	$kilobyte = 1024;
724
+	$megabyte = $kilobyte * 1024;
725
+	$gigabyte = $megabyte * 1024;
726
+	$terabyte = $gigabyte * 1024;
727
+	$precision = 2;
728
+	$bytes = $intMemory;
729
+
730
+	if (($bytes >= 0) && ($bytes < $kilobyte)) {
731
+		return $bytes . ' B';
732
+
733
+	} elseif (($bytes >= $kilobyte) && ($bytes < $megabyte)) {
734
+		return round($bytes / $kilobyte, $precision) . ' KB';
735
+
736
+	} elseif (($bytes >= $megabyte) && ($bytes < $gigabyte)) {
737
+		return round($bytes / $megabyte, $precision) . ' MB';
738
+
739
+	} elseif (($bytes >= $gigabyte) && ($bytes < $terabyte)) {
740
+		return round($bytes / $gigabyte, $precision) . ' GB';
741
+
742
+	} elseif ($bytes >= $terabyte) {
743
+		return round($bytes / $terabyte, $precision) . ' TB';
744
+	} else {
745
+		return $bytes . ' B';
746
+	}
747
+};
748
+
749
+echo "Memory used for export: " . $hrsize($intMemory)."\n";
0 750
\ No newline at end of file