Completed
Push — master ( a3e1ac...484d40 )
by Aimeos
02:36
created

Standard::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Text;
12
13
14
/**
15
 * Text processor for CSV imports
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Product\Import\Csv\Processor\Base
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	/** controller/common/product/import/csv/processor/text/name
25
	 * Name of the text processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Processor\Text\Myname".
28
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
29
	 *
30
	 * @param string Last part of the processor class name
31
	 * @since 2015.10
32
	 * @category Developer
33
	 */
34
35
	private $listTypes;
36
37
38
	/**
39
	 * Initializes the object
40
	 *
41
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
42
	 * @param array $mapping Associative list of field position in CSV as key and domain item key as value
43
	 * @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor
44
	 */
45
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping,
46
			\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null )
47
	{
48
		parent::__construct( $context, $mapping, $object );
49
50
		/** controller/common/product/import/csv/processor/text/listtypes
51
		 * Names of the product list types for texts that are updated or removed
52
		 *
53
		 * If you want to associate text items manually via the administration
54
		 * interface to products and don't want these to be touched during the
55
		 * import, you can specify the product list types for these texts
56
		 * that shouldn't be updated or removed.
57
		 *
58
		 * @param array|null List of product list type names or null for all
59
		 * @since 2015.05
60
		 * @category Developer
61
		 * @category User
62
		 * @see controller/common/product/import/csv/domains
63
		 * @see controller/common/product/import/csv/processor/attribute/listtypes
64
		 * @see controller/common/product/import/csv/processor/catalog/listtypes
65
		 * @see controller/common/product/import/csv/processor/media/listtypes
66
		 * @see controller/common/product/import/csv/processor/price/listtypes
67
		 * @see controller/common/product/import/csv/processor/product/listtypes
68
		 */
69
		$this->listTypes = $context->getConfig()->get( 'controller/common/product/import/csv/processor/text/listtypes' );
70
	}
71
72
73
	/**
74
	 * Saves the product related data to the storage
75
	 *
76
	 * @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items
77
	 * @param array $data List of CSV fields with position as key and data as value
78
	 * @return array List of data which hasn't been imported
79
	 */
80
	public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data )
81
	{
82
		$listManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'product/lists' );
83
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'text' );
84
		$manager->begin();
85
86
		try
87
		{
88
			$delete = $listMap = array();
89
			$map = $this->getMappedChunk( $data );
90
			$listItems = $product->getListItems( 'text', $this->listTypes );
91
92
			foreach( $listItems as $listItem )
93
			{
94
				if( ( $refItem = $listItem->getRefItem() ) !== null ) {
95
					$listMap[ $refItem->getContent() ][ $refItem->getType() ][ $listItem->getType() ] = $listItem;
96
				}
97
			}
98
99
			foreach( $map as $pos => $list )
100
			{
101
				if( $this->checkEntry( $list ) === false ) {
102
					continue;
103
				}
104
105
				$type = ( isset( $list['text.type'] ) ? $list['text.type'] : 'name' );
106
				$typecode = ( isset( $list['product.lists.type'] ) ? $list['product.lists.type'] : 'default' );
107
108
				if( isset( $listMap[ $list['text.content'] ][$type][$typecode] ) )
109
				{
110
					$listItem = $listMap[ $list['text.content'] ][$type][$typecode];
111
					$refItem = $listItem->getRefItem();
112
					unset( $listItems[ $listItem->getId() ] );
113
				}
114
				else
115
				{
116
					$listItem = $listManager->createItem();
117
					$refItem = $manager->createItem();
118
				}
119
120
				$list['text.typeid'] = $this->getTypeId( 'text/type', 'product', $type );
121
				$list['text.domain'] = 'product';
122
123
				$refItem->fromArray( $this->addItemDefaults( $list ) );
124
				$manager->saveItem( $refItem );
125
126
				$list['product.lists.typeid'] = $this->getTypeId( 'product/lists/type', 'text', $typecode );
127
				$list['product.lists.parentid'] = $product->getId();
128
				$list['product.lists.refid'] = $refItem->getId();
129
				$list['product.lists.domain'] = 'text';
130
131
				$listItem->fromArray( $this->addListItemDefaults( $list, $pos ) );
132
				$listManager->saveItem( $listItem );
133
			}
134
135
			foreach( $listItems as $listItem ) {
136
				$delete[] = $listItem->getRefId();
137
			}
138
139
			$manager->deleteItems( $delete );
140
			$listManager->deleteItems( array_keys( $listItems ) );
141
142
			$remaining = $this->getObject()->process( $product, $data );
143
144
			$manager->commit();
145
		}
146
		catch( \Exception $e )
147
		{
148
			$manager->rollback();
149
			throw $e;
150
		}
151
152
		return $remaining;
153
	}
154
155
156
	/**
157
	 * Adds the text item default values and returns the resulting array
158
	 *
159
	 * @param array $list Associative list of domain item keys and their values, e.g. "text.status" => 1
160
	 * @return array Given associative list enriched by default values if they were not already set
161
	 */
162
	protected function addItemDefaults( array $list )
163
	{
164
		if( !isset( $list['text.label'] ) ) {
165
			$list['text.label'] = mb_strcut( $list['text.content'], 0, 255 );
166
		}
167
168
		if( !isset( $list['text.languageid'] ) ) {
169
			$list['text.languageid'] = $this->getContext()->getLocale()->getLanguageId();
170
		}
171
172
		if( !isset( $list['text.status'] ) ) {
173
			$list['text.status'] = 1;
174
		}
175
176
		return $list;
177
	}
178
179
180
	/**
181
	 * Checks if an entry can be used for updating a media item
182
	 *
183
	 * @param array $list Associative list of key/value pairs from the mapping
184
	 * @return boolean True if valid, false if not
185
	 */
186
	protected function checkEntry( array $list )
187
	{
188
		if( !isset( $list['text.content'] ) || $list['text.content'] === '' || isset( $list['product.lists.type'] )
189
			&& $this->listTypes !== null && !in_array( $list['product.lists.type'], (array) $this->listTypes )
190
		) {
191
			return false;
192
		}
193
194
		return true;
195
	}
196
}
197