Passed
Push — master ( 3ca382...ddd5fe )
by Aimeos
03:12
created

Standard::copy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2023
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product\Related;
12
13
sprintf( 'related' ); // for translation
14
15
16
/**
17
 * Default implementation of product related JQAdm client.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
class Standard
23
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
24
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
25
{
26
	/** admin/jqadm/product/related/name
27
	 * Name of the related subpart used by the JQAdm product implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Related\Myname".
30
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
31
	 *
32
	 * @param string Last part of the JQAdm class name
33
	 * @since 2017.03
34
	 */
35
36
37
	/**
38
	 * Adds the required data used in the template
39
	 *
40
	 * @param \Aimeos\Base\View\Iface $view View object
41
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
42
	 */
43
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
44
	{
45
		return $view->set( 'relatedTypes', $this->getTypes() );
46
	}
47
48
49
	/**
50
	 * Copies a resource
51
	 *
52
	 * @return string|null HTML output
53
	 */
54
	public function copy() : ?string
55
	{
56
		$view = $this->object()->data( $this->view() );
57
58
		$view->relatedData = $this->toArray( $view->item, true );
59
		$view->relatedBody = parent::copy();
60
61
		return $this->render( $view );
62
	}
63
64
65
	/**
66
	 * Creates a new resource
67
	 *
68
	 * @return string|null HTML output
69
	 */
70
	public function create() : ?string
71
	{
72
		$view = $this->object()->data( $this->view() );
73
		$siteid = $this->context()->locale()->getSiteId();
74
		$data = $view->param( 'related', [] );
75
76
		foreach( $view->value( $data, 'product.lists.id', [] ) as $idx => $value ) {
77
			$data['product.lists.siteid'][$idx] = $siteid;
78
		}
79
80
		$view->relatedData = $data;
81
		$view->relatedBody = parent::create();
82
83
		return $this->render( $view );
84
	}
85
86
87
	/**
88
	 * Returns a single resource
89
	 *
90
	 * @return string|null HTML output
91
	 */
92
	public function get() : ?string
93
	{
94
		$view = $this->object()->data( $this->view() );
95
		$view->relatedData = $this->toArray( $view->item );
96
		$view->relatedBody = parent::get();
97
98
		return $this->render( $view );
99
	}
100
101
102
	/**
103
	 * Saves the data
104
	 *
105
	 * @return string|null HTML output
106
	 */
107
	public function save() : ?string
108
	{
109
		$view = $this->view();
110
111
		$this->fromArray( $view->item, $view->param( 'related', [] ) );
112
		$view->relatedBody = parent::save();
113
114
		return null;
115
	}
116
117
118
	/**
119
	 * Returns the sub-client given by its name.
120
	 *
121
	 * @param string $type Name of the client type
122
	 * @param string|null $name Name of the sub-client (Default if null)
123
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
124
	 */
125
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
126
	{
127
		/** admin/jqadm/product/related/decorators/excludes
128
		 * Excludes decorators added by the "common" option from the product JQAdm client
129
		 *
130
		 * Decorators extend the functionality of a class by adding new aspects
131
		 * (e.g. log what is currently done), executing the methods of the underlying
132
		 * class only in certain conditions (e.g. only for logged in users) or
133
		 * modify what is returned to the caller.
134
		 *
135
		 * This option allows you to remove a decorator added via
136
		 * "admin/jqadm/common/decorators/default" before they are wrapped
137
		 * around the JQAdm client.
138
		 *
139
		 *  admin/jqadm/product/related/decorators/excludes = array( 'decorator1' )
140
		 *
141
		 * This would remove the decorator named "decorator1" from the list of
142
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
143
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
144
		 *
145
		 * @param array List of decorator names
146
		 * @since 2016.01
147
		 * @see admin/jqadm/common/decorators/default
148
		 * @see admin/jqadm/product/related/decorators/global
149
		 * @see admin/jqadm/product/related/decorators/local
150
		 */
151
152
		/** admin/jqadm/product/related/decorators/global
153
		 * Adds a list of globally available decorators only to the product JQAdm client
154
		 *
155
		 * Decorators extend the functionality of a class by adding new aspects
156
		 * (e.g. log what is currently done), executing the methods of the underlying
157
		 * class only in certain conditions (e.g. only for logged in users) or
158
		 * modify what is returned to the caller.
159
		 *
160
		 * This option allows you to wrap global decorators
161
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
162
		 *
163
		 *  admin/jqadm/product/related/decorators/global = array( 'decorator1' )
164
		 *
165
		 * This would add the decorator named "decorator1" defined by
166
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
167
		 *
168
		 * @param array List of decorator names
169
		 * @since 2016.01
170
		 * @see admin/jqadm/common/decorators/default
171
		 * @see admin/jqadm/product/related/decorators/excludes
172
		 * @see admin/jqadm/product/related/decorators/local
173
		 */
174
175
		/** admin/jqadm/product/related/decorators/local
176
		 * Adds a list of local decorators only to the product JQAdm client
177
		 *
178
		 * Decorators extend the functionality of a class by adding new aspects
179
		 * (e.g. log what is currently done), executing the methods of the underlying
180
		 * class only in certain conditions (e.g. only for logged in users) or
181
		 * modify what is returned to the caller.
182
		 *
183
		 * This option allows you to wrap local decorators
184
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
185
		 *
186
		 *  admin/jqadm/product/related/decorators/local = array( 'decorator2' )
187
		 *
188
		 * This would add the decorator named "decorator2" defined by
189
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
190
		 *
191
		 * @param array List of decorator names
192
		 * @since 2016.01
193
		 * @see admin/jqadm/common/decorators/default
194
		 * @see admin/jqadm/product/related/decorators/excludes
195
		 * @see admin/jqadm/product/related/decorators/global
196
		 */
197
		return $this->createSubClient( 'product/related/' . $type, $name );
198
	}
199
200
201
	/**
202
	 * Returns the list of sub-client names configured for the client.
203
	 *
204
	 * @return array List of JQAdm client names
205
	 */
206
	protected function getSubClientNames() : array
207
	{
208
		/** admin/jqadm/product/related/subparts
209
		 * List of JQAdm sub-clients rendered within the product related section
210
		 *
211
		 * The output of the frontend is composed of the code generated by the JQAdm
212
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
213
		 * that are responsible for rendering certain sub-parts of the output. The
214
		 * sub-clients can contain JQAdm clients themselves and therefore a
215
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
216
		 * the output that is placed inside the container of its parent.
217
		 *
218
		 * At first, always the JQAdm code generated by the parent is printed, then
219
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
220
		 * determines the order of the output of these sub-clients inside the parent
221
		 * container. If the configured list of clients is
222
		 *
223
		 *  array( "subclient1", "subclient2" )
224
		 *
225
		 * you can easily change the order of the output by reordering the subparts:
226
		 *
227
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
228
		 *
229
		 * You can also remove one or more parts if they shouldn't be rendered:
230
		 *
231
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
232
		 *
233
		 * As the clients only generates structural JQAdm, the layout defined via CSS
234
		 * should support adding, removing or reordering content by a fluid like
235
		 * design.
236
		 *
237
		 * @param array List of sub-client names
238
		 * @since 2016.01
239
		 */
240
		return $this->context()->config()->get( 'admin/jqadm/product/related/subparts', [] );
241
	}
242
243
244
	/**
245
	 * Returns the product list types to update
246
	 *
247
	 * @return \Aimeos\Map Associative map with type codes as keys and type labels as values
248
	 */
249
	protected function getTypes() : \Aimeos\Map
250
	{
251
		$manager = \Aimeos\MShop::create( $this->context(), 'product/lists/type' );
252
		$filter = $manager->filter( true )->add( 'product.lists.type.domain', '==', 'product' )
253
			->order( ['product.lists.type.position', 'product.lists.type.code'] );
254
255
		return $manager->search( $filter )->col( 'product.lists.type.label', 'product.lists.type.code' )->except( 'default' );
0 ignored issues
show
Bug introduced by
'default' of type string is incompatible with the type iterable expected by parameter $keys of Aimeos\Map::except(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

255
		return $manager->search( $filter )->col( 'product.lists.type.label', 'product.lists.type.code' )->except( /** @scrutinizer ignore-type */ 'default' );
Loading history...
256
	}
257
258
259
	/**
260
	 * Creates new and updates existing items using the data array
261
	 *
262
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
263
	 * @param array $data Data array
264
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
265
	 */
266
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
267
	{
268
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
269
		$listItems = $item->getListItems( 'product', $this->getTypes()->keys()->all() );
270
271
		foreach( $data as $entry )
272
		{
273
			$listid = $this->val( $entry, 'product.lists.id' );
274
			$litem = $listItems->pull( $listid ) ?: $manager->createListItem();
0 ignored issues
show
Bug introduced by
The method createListItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

274
			$litem = $listItems->pull( $listid ) ?: $manager->/** @scrutinizer ignore-call */ createListItem();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
275
276
			$litem->setType( $this->val( $entry, 'product.lists.type' ) )
277
				->setRefId( $this->val( $entry, 'product.lists.refid' ) );
278
279
			$item->addListItem( 'product', $litem );
280
		}
281
282
		return $item->deleteListItems( $listItems );
283
	}
284
285
286
	/**
287
	 * Constructs the data array for the view from the given item
288
	 *
289
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object including referenced domain items
290
	 * @param bool $copy True if items should be copied, false if not
291
	 * @return string[] Multi-dimensional associative list of item data
292
	 */
293
	protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array
294
	{
295
		$data = [];
296
		$siteId = $this->context()->locale()->getSiteId();
297
298
		foreach( $item->getListItems( 'product', $this->getTypes()->keys()->all() ) as $listItem )
299
		{
300
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
301
				continue;
302
			}
303
304
			$label = $refItem->getLabel() . ' (' . $refItem->getCode() . ')';
305
			$list = ['product.label' => $label] + $listItem->toArray( true ) + $refItem->toArray( true );
306
307
			if( $copy === true )
308
			{
309
				$list['product.lists.siteid'] = $siteId;
310
				$list['product.lists.id'] = '';
311
			}
312
313
			$data[] = $list;
314
		}
315
316
		return $data;
317
	}
318
319
320
	/**
321
	 * Returns the rendered template including the view data
322
	 *
323
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
324
	 * @return string HTML output
325
	 */
326
	protected function render( \Aimeos\Base\View\Iface $view ) : string
327
	{
328
		/** admin/jqadm/product/related/template-item
329
		 * Relative path to the HTML body template of the related subpart for products.
330
		 *
331
		 * The template file contains the HTML code and processing instructions
332
		 * to generate the result shown in the body of the frontend. The
333
		 * configuration string is the path to the template file relative
334
		 * to the templates directory (usually in templates/admin/jqadm).
335
		 *
336
		 * You can overwrite the template file configuration in extensions and
337
		 * provide alternative templates. These alternative templates should be
338
		 * named like the default one but with the string "default" replaced by
339
		 * an unique name. You may use the name of your project for this. If
340
		 * you've implemented an alternative client class as well, "default"
341
		 * should be replaced by the name of the new class.
342
		 *
343
		 * @param string Relative path to the template creating the HTML code
344
		 * @since 2016.04
345
		 */
346
		$tplconf = 'admin/jqadm/product/related/template-item';
347
		$default = 'product/item-related';
348
349
		return $view->render( $view->config( $tplconf, $default ) );
350
	}
351
}
352