|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017 |
|
6
|
|
|
* @package Controller |
|
7
|
|
|
* @subpackage Common |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Common\Coupon\Import\Csv; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Common class for CSV coupon import job controllers and processors. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Controller |
|
18
|
|
|
* @subpackage Common |
|
19
|
|
|
*/ |
|
20
|
|
|
class Base |
|
21
|
|
|
extends \Aimeos\Controller\Jobs\Base |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Returns the coupon code items for the given codes |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $codes List of coupon codes |
|
27
|
|
|
* @return array Associative list of coupon codes as key and coupon code items as value |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function getCouponCodeItems( array $codes ) |
|
30
|
|
|
{ |
|
31
|
|
|
$result = []; |
|
32
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'coupon/code' ); |
|
33
|
|
|
|
|
34
|
|
|
$search = $manager->createSearch(); |
|
35
|
|
|
$search->setConditions( $search->compare( '==', 'coupon.code.code', $codes ) ); |
|
36
|
|
|
$search->setSlice( 0, count( $codes ) ); |
|
37
|
|
|
|
|
38
|
|
|
foreach( $manager->searchItems( $search ) as $item ) { |
|
39
|
|
|
$result[ $item->getCode() ] = $item; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $result; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns the rows from the CSV file up to the maximum count |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Aimeos\MW\Container\Content\Iface $content CSV content object |
|
50
|
|
|
* @param integer $maxcnt Maximum number of rows that should be retrieved at once |
|
51
|
|
|
* @param integer $codePos Column position which contains the unique coupon code (starting from 0) |
|
52
|
|
|
* @return array List of arrays with coupon codes as keys and list of values from the CSV file |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function getData( \Aimeos\MW\Container\Content\Iface $content, $maxcnt, $codePos ) |
|
55
|
|
|
{ |
|
56
|
|
|
$count = 0; |
|
57
|
|
|
$data = []; |
|
58
|
|
|
|
|
59
|
|
|
while( $content->valid() && $count++ < $maxcnt ) |
|
60
|
|
|
{ |
|
61
|
|
|
$row = $content->current(); |
|
62
|
|
|
$data[ $row[$codePos] ] = $row; |
|
63
|
|
|
$content->next(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $data; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns the default mapping for the CSV fields to the domain item keys |
|
72
|
|
|
* |
|
73
|
|
|
* Example: |
|
74
|
|
|
* 'item' => array( |
|
75
|
|
|
* 0 => 'coupon.code.code', // e.g. letters and digits |
|
76
|
|
|
* 1 => 'coupon.code.count', // number of time the code is available |
|
77
|
|
|
* ), |
|
78
|
|
|
* |
|
79
|
|
|
* @return array Associative list of domains as keys and a list of positions and the domain item keys as values |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getDefaultMapping() |
|
82
|
|
|
{ |
|
83
|
|
|
return array( |
|
84
|
|
|
'code' => array( |
|
85
|
|
|
0 => 'coupon.code.code', |
|
86
|
|
|
1 => 'coupon.code.count', |
|
87
|
|
|
2 => 'coupon.code.datestart', |
|
88
|
|
|
3 => 'coupon.code.dateend', |
|
89
|
|
|
), |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns the mapped data from the CSV line |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $data List of CSV fields with position as key and domain item key as value (mapped data is removed) |
|
98
|
|
|
* @param array $mapping List of domain item keys with the CSV field position as key |
|
99
|
|
|
* @return array List of associative arrays containing the chunked properties |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function getMappedChunk( array &$data, array $mapping ) |
|
102
|
|
|
{ |
|
103
|
|
|
$idx = 0; |
|
104
|
|
|
$map = []; |
|
105
|
|
|
|
|
106
|
|
|
foreach( $mapping as $pos => $key ) |
|
107
|
|
|
{ |
|
108
|
|
|
if( isset( $map[$idx][$key] ) ) { |
|
109
|
|
|
$idx++; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if( isset( $data[$pos] ) ) |
|
113
|
|
|
{ |
|
114
|
|
|
$map[$idx][$key] = $data[$pos]; |
|
115
|
|
|
unset( $data[$pos] ); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $map; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns the processor object for saving the coupon related information |
|
125
|
|
|
* |
|
126
|
|
|
* @param array $mappings Associative list of processor types as keys and index/data mappings as values |
|
127
|
|
|
* @return \Aimeos\Controller\Common\Coupon\Import\Csv\Processor\Iface Processor object |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function getProcessors( array $mappings ) |
|
130
|
|
|
{ |
|
131
|
|
|
$context = $this->getContext(); |
|
132
|
|
|
$config = $context->getConfig(); |
|
133
|
|
|
$iface = '\\Aimeos\\Controller\\Common\\Coupon\\Import\\Csv\\Processor\\Iface'; |
|
134
|
|
|
$object = new \Aimeos\Controller\Common\Coupon\Import\Csv\Processor\Done( $context, [] ); |
|
135
|
|
|
|
|
136
|
|
|
foreach( $mappings as $type => $mapping ) |
|
137
|
|
|
{ |
|
138
|
|
|
if( ctype_alnum( $type ) === false ) |
|
139
|
|
|
{ |
|
140
|
|
|
$classname = is_string($type) ? '\\Aimeos\\Controller\\Common\\Coupon\\Import\\Csv\\Processor\\' . $type : '<not a string>'; |
|
141
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$name = $config->get( 'controller/common/coupon/import/csv/processor/' . $type . '/name', 'Standard' ); |
|
145
|
|
|
|
|
146
|
|
|
if( ctype_alnum( $name ) === false ) |
|
147
|
|
|
{ |
|
148
|
|
|
$classname = is_string($name) ? '\\Aimeos\\Controller\\Common\\Coupon\\Import\\Csv\\Processor\\' . $type . '\\' . $name : '<not a string>'; |
|
149
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Invalid characters in class name "%1$s"', $classname ) ); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$classname = '\\Aimeos\\Controller\\Common\\Coupon\\Import\\Csv\\Processor\\' . ucfirst( $type ) . '\\' . $name; |
|
153
|
|
|
|
|
154
|
|
|
if( class_exists( $classname ) === false ) { |
|
155
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Class "%1$s" not found', $classname ) ); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$object = new $classname( $context, $mapping, $object ); |
|
159
|
|
|
|
|
160
|
|
|
if( !( $object instanceof $iface ) ) { |
|
161
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Class "%1$s" does not implement interface "%2$s"', $classname, $iface ) ); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return $object; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|