Passed
Push — master ( b7b940...6b1153 )
by Aimeos
03:39
created

Standard::data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

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