Passed
Push — master ( 0a6ee6...3ca382 )
by Aimeos
04:14
created

Standard::data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
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), 2020-2023
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product\Supplier;
12
13
sprintf( 'supplier' ); // for translation
14
15
16
/**
17
 * Default implementation of product supplier 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/supplier/name
27
	 * Name of the supplier subpart used by the JQAdm product implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Supplier\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 2020.04
34
	 * @supplier Developer
35
	 */
36
37
38
	/**
39
	 * Adds the required data used in the template
40
	 *
41
	 * @param \Aimeos\Base\View\Iface $view View object
42
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
43
	 */
44
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
45
	{
46
		$manager = \Aimeos\MShop::create( $this->context(), 'product/lists/type' );
47
		$filter = $manager->filter( true )->add( 'product.lists.type.domain', '==', 'supplier' )
48
			->order( 'product.lists.type.position', 'product.lists.type.code' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Base\Criteria\Iface::order() has too many arguments starting with 'product.lists.type.code'. ( Ignorable by Annotation )

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

48
			->/** @scrutinizer ignore-call */ order( 'product.lists.type.position', 'product.lists.type.code' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
49
50
		$view->supplierTypes = $manager->search( $filter )->col( 'product.lists.type.label', 'product.lists.type.code' );
51
		return $view;
52
	}
53
54
55
	/**
56
	 * Copies a resource
57
	 *
58
	 * @return string|null HTML output
59
	 */
60
	public function copy() : ?string
61
	{
62
		$view = $this->object()->data( $this->view() );
63
64
		$view->supplierData = $this->toArray( $view->item, true );
65
		$view->supplierBody = parent::copy();
66
67
		return $this->render( $view );
68
	}
69
70
71
	/**
72
	 * Creates a new resource
73
	 *
74
	 * @return string|null HTML output
75
	 */
76
	public function create() : ?string
77
	{
78
		$view = $this->object()->data( $this->view() );
79
		$siteid = $this->context()->locale()->getSiteId();
80
		$data = $view->param( 'supplier', [] );
81
82
		foreach( $view->value( $data, 'product.lists.id', [] ) as $idx => $value ) {
83
			$data['product.lists.siteid'][$idx] = $siteid;
84
		}
85
86
		$view->supplierData = $data;
87
		$view->supplierBody = parent::create();
88
89
		return $this->render( $view );
90
	}
91
92
93
	/**
94
	 * Returns a single resource
95
	 *
96
	 * @return string|null HTML output
97
	 */
98
	public function get() : ?string
99
	{
100
		$view = $this->object()->data( $this->view() );
101
		$view->supplierData = $this->toArray( $view->item );
102
		$view->supplierBody = parent::get();
103
104
		return $this->render( $view );
105
	}
106
107
108
	/**
109
	 * Saves the data
110
	 *
111
	 * @return string|null HTML output
112
	 */
113
	public function save() : ?string
114
	{
115
		$view = $this->view();
116
117
		$this->fromArray( $view->item, $view->param( 'supplier', [] ) );
118
		$view->supplierBody = parent::save();
119
120
		return null;
121
	}
122
123
124
	/**
125
	 * Returns the sub-client given by its name.
126
	 *
127
	 * @param string $type Name of the client type
128
	 * @param string|null $name Name of the sub-client (Default if null)
129
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
130
	 */
131
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
132
	{
133
		/** admin/jqadm/product/supplier/decorators/excludes
134
		 * Excludes decorators added by the "common" option from the product JQAdm client
135
		 *
136
		 * Decorators extend the functionality of a class by adding new aspects
137
		 * (e.g. log what is currently done), executing the methods of the underlying
138
		 * class only in certain conditions (e.g. only for logged in users) or
139
		 * modify what is returned to the caller.
140
		 *
141
		 * This option allows you to remove a decorator added via
142
		 * "admin/jqadm/common/decorators/default" before they are wrapped
143
		 * around the JQAdm client.
144
		 *
145
		 *  admin/jqadm/product/supplier/decorators/excludes = array( 'decorator1' )
146
		 *
147
		 * This would remove the decorator named "decorator1" from the list of
148
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
149
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
150
		 *
151
		 * @param array List of decorator names
152
		 * @since 2020.04
153
		 * @supplier Developer
154
		 * @see admin/jqadm/common/decorators/default
155
		 * @see admin/jqadm/product/supplier/decorators/global
156
		 * @see admin/jqadm/product/supplier/decorators/local
157
		 */
158
159
		/** admin/jqadm/product/supplier/decorators/global
160
		 * Adds a list of globally available decorators only to the product JQAdm client
161
		 *
162
		 * Decorators extend the functionality of a class by adding new aspects
163
		 * (e.g. log what is currently done), executing the methods of the underlying
164
		 * class only in certain conditions (e.g. only for logged in users) or
165
		 * modify what is returned to the caller.
166
		 *
167
		 * This option allows you to wrap global decorators
168
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
169
		 *
170
		 *  admin/jqadm/product/supplier/decorators/global = array( 'decorator1' )
171
		 *
172
		 * This would add the decorator named "decorator1" defined by
173
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
174
		 *
175
		 * @param array List of decorator names
176
		 * @since 2020.04
177
		 * @supplier Developer
178
		 * @see admin/jqadm/common/decorators/default
179
		 * @see admin/jqadm/product/supplier/decorators/excludes
180
		 * @see admin/jqadm/product/supplier/decorators/local
181
		 */
182
183
		/** admin/jqadm/product/supplier/decorators/local
184
		 * Adds a list of local decorators only to the product JQAdm client
185
		 *
186
		 * Decorators extend the functionality of a class by adding new aspects
187
		 * (e.g. log what is currently done), executing the methods of the underlying
188
		 * class only in certain conditions (e.g. only for logged in users) or
189
		 * modify what is returned to the caller.
190
		 *
191
		 * This option allows you to wrap local decorators
192
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
193
		 *
194
		 *  admin/jqadm/product/supplier/decorators/local = array( 'decorator2' )
195
		 *
196
		 * This would add the decorator named "decorator2" defined by
197
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
198
		 *
199
		 * @param array List of decorator names
200
		 * @since 2020.04
201
		 * @supplier Developer
202
		 * @see admin/jqadm/common/decorators/default
203
		 * @see admin/jqadm/product/supplier/decorators/excludes
204
		 * @see admin/jqadm/product/supplier/decorators/global
205
		 */
206
		return $this->createSubClient( 'product/supplier/' . $type, $name );
207
	}
208
209
210
	/**
211
	 * Returns the list of sub-client names configured for the client.
212
	 *
213
	 * @return array List of JQAdm client names
214
	 */
215
	protected function getSubClientNames() : array
216
	{
217
		/** admin/jqadm/product/supplier/subparts
218
		 * List of JQAdm sub-clients rendered within the product supplier section
219
		 *
220
		 * The output of the frontend is composed of the code generated by the JQAdm
221
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
222
		 * that are responsible for rendering certain sub-parts of the output. The
223
		 * sub-clients can contain JQAdm clients themselves and therefore a
224
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
225
		 * the output that is placed inside the container of its parent.
226
		 *
227
		 * At first, always the JQAdm code generated by the parent is printed, then
228
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
229
		 * determines the order of the output of these sub-clients inside the parent
230
		 * container. If the configured list of clients is
231
		 *
232
		 *  array( "subclient1", "subclient2" )
233
		 *
234
		 * you can easily change the order of the output by reordering the subparts:
235
		 *
236
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
237
		 *
238
		 * You can also remove one or more parts if they shouldn't be rendered:
239
		 *
240
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
241
		 *
242
		 * As the clients only generates structural JQAdm, the layout defined via CSS
243
		 * should support adding, removing or reordering content by a fluid like
244
		 * design.
245
		 *
246
		 * @param array List of sub-client names
247
		 * @since 2020.04
248
		 * @supplier Developer
249
		 */
250
		return $this->context()->config()->get( 'admin/jqadm/product/supplier/subparts', [] );
251
	}
252
253
254
	/**
255
	 * Creates new and updates existing items using the data array
256
	 *
257
	 * @param \Aimeos\MShop\Product\Item\Iface $item Product item object without referenced domain items
258
	 * @param array $data Data array
259
	 * @return \Aimeos\MShop\Product\Item\Iface Modified product item
260
	 */
261
	protected function fromArray( \Aimeos\MShop\Product\Item\Iface $item, array $data ) : \Aimeos\MShop\Product\Item\Iface
262
	{
263
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
264
		$listItems = $item->getListItems( 'supplier' );
265
		$idx = 0;
266
267
		foreach( $data as $entry )
268
		{
269
			$listid = $this->val( $entry, 'product.lists.id' );
270
			$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

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