Completed
Push — master ( 451020...47a117 )
by Aimeos
02:48
created

Standard::checkEntry()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 8.8571
cc 6
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Media;
12
13
14
/**
15
 * Media 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/media/name
25
	 * Name of the media processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Processor\Media\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/media/listtypes
51
		 * Names of the product list types for media that are updated or removed
52
		 *
53
		 * If you want to associate media 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 media
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/product/listtypes
66
		 * @see controller/common/product/import/csv/processor/price/listtypes
67
		 * @see controller/common/product/import/csv/processor/text/listtypes
68
		 */
69
		$this->listTypes = $context->getConfig()->get( 'controller/common/product/import/csv/processor/media/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
		$context = $this->getContext();
83
		$manager = \Aimeos\MShop\Factory::createManager( $context, 'media' );
84
		$listManager = \Aimeos\MShop\Factory::createManager( $context, 'product/lists' );
85
		$separator = $context->getConfig()->get( 'controller/common/product/import/csv/separator', "\n" );
86
87
		$manager->begin();
88
89
		try
90
		{
91
			$map = $this->getMappedChunk( $data );
92
			$listItems = $product->getListItems( 'media' );
93
94
			foreach( $map as $pos => $list )
95
			{
96
				if( $this->checkEntry( $list ) === false ) {
97
					continue;
98
				}
99
100
				$urls = explode( $separator, $list['media.url'] );
101
				$type = ( isset( $list['media.type'] ) ? $list['media.type'] : 'default' );
102
				$typecode = ( isset( $list['product.lists.type'] ) ? $list['product.lists.type'] : 'default' );
103
				$langid = ( isset( $list['media.languageid'] ) && $list['media.languageid'] !== '' ? $list['media.languageid'] : null );
104
105
				foreach( $urls as $url )
106
				{
107
					if( ( $listItem = array_shift( $listItems ) ) !== null ) {
108
						$refItem = $listItem->getRefItem();
109
					} else {
110
						$listItem = $listManager->createItem();
111
						$refItem = $manager->createItem();
112
					}
113
114
					$list['media.typeid'] = $this->getTypeId( 'media/type', 'product', $type );
115
					$list['media.languageid'] = $langid;
116
					$list['media.domain'] = 'product';
117
					$list['media.url'] = $url;
118
119
					$refItem->fromArray( $this->addItemDefaults( $list ) );
120
					$manager->saveItem( $refItem );
121
122
					$list['product.lists.typeid'] = $this->getTypeId( 'product/lists/type', 'media', $typecode );
123
					$list['product.lists.parentid'] = $product->getId();
124
					$list['product.lists.refid'] = $refItem->getId();
125
					$list['product.lists.domain'] = 'media';
126
127
					$listItem->fromArray( $this->addListItemDefaults( $list, $pos++ ) );
128
					$listManager->saveItem( $listItem );
129
				}
130
			}
131
132
			foreach( $listItems as $listItem )
133
			{
134
				$manager->deleteItem( $listItem->getRefItem()->getId() );
135
				$listManager->deleteItem( $listItem->getId() );
136
			}
137
138
			$remaining = $this->getObject()->process( $product, $data );
139
140
			$manager->commit();
141
		}
142
		catch( \Exception $e )
143
		{
144
			$manager->rollback();
145
			throw $e;
146
		}
147
148
		return $remaining;
149
	}
150
151
152
	/**
153
	 * Adds the text item default values and returns the resulting array
154
	 *
155
	 * @param array $list Associative list of domain item keys and their values, e.g. "media.status" => 1
156
	 * @return array Given associative list enriched by default values if they were not already set
157
	 */
158
	protected function addItemDefaults( array $list )
159
	{
160
		if( !isset( $list['media.label'] ) ) {
161
			$list['media.label'] = $list['media.url'];
162
		}
163
164
		if( !isset( $list['media.preview'] ) ) {
165
			$list['media.preview'] = $list['media.url'];
166
		}
167
168
		if( !isset( $list['media.status'] ) ) {
169
			$list['media.status'] = 1;
170
		}
171
172
		return $list;
173
	}
174
175
176
	/**
177
	 * Checks if an entry can be used for updating a media item
178
	 *
179
	 * @param array $list Associative list of key/value pairs from the mapping
180
	 * @return boolean True if valid, false if not
181
	 */
182
	protected function checkEntry( array $list )
183
	{
184
		if( !isset( $list['media.url'] ) || $list['media.url'] === '' || isset( $list['product.lists.type'] )
185
				&& $this->listTypes !== null && !in_array( $list['product.lists.type'], (array) $this->listTypes )
186
		) {
187
			return false;
188
		}
189
190
		return true;
191
	}
192
}
193