Standard::data()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

275
		$listItem = $manager->/** @scrutinizer ignore-call */ createListItem()->setRefId( $id );

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...
276
		$listItems = $products->getListItems( 'supplier', null, null, false )
277
			->flat( 1 )->col( null, 'product.lists.id' );
278
279
		foreach( (array) $prodIds as $idx => $prodId )
280
		{
281
			if( ( $product = $products->get( $prodId ) ) === null ) {
282
				continue;
283
			}
284
285
			$listId = $this->val( $data, 'product.lists.id/' . $idx );
286
			$listItem = $listItems->get( $listId ) ?: clone $listItem;
287
288
			$listItem->setType( $this->val( $data, 'product.lists.type/' . $idx, 'default' ) )
289
				->setConfig( (array) json_decode( $this->val( $data, 'product.lists.config/' . $idx, '{}' ) ) )
290
				->setPosition( (int) $this->val( $data, 'product.lists.position/' . $idx, 0 ) )
291
				->setStatus( (int) $this->val( $data, 'product.lists.status/' . $idx, 1 ) )
292
				->setDateStart( $this->val( $data, 'product.lists.datestart/' . $idx ) )
293
				->setDateEnd( $this->val( $data, 'product.lists.dateend/' . $idx ) );
294
295
			$product->addListItem( 'supplier', $listItem );
296
			$listItems->remove( $listItem->getId() );
297
		}
298
299
		\Aimeos\MShop::create( $context, 'index' )->save( $products );
300
301
		return $item;
302
	}
303
304
305
	/**
306
	 * Returns the rendered template including the view data
307
	 *
308
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
309
	 * @return string HTML output
310
	 */
311
	protected function render( \Aimeos\Base\View\Iface $view ) : string
312
	{
313
		/** admin/jqadm/supplier/product/template-item
314
		 * Relative path to the HTML body template of the product subpart for suppliers.
315
		 *
316
		 * The template file contains the HTML code and processing instructions
317
		 * to generate the result shown in the body of the frontend. The
318
		 * configuration string is the path to the template file relative
319
		 * to the templates directory (usually in templates/admin/jqadm).
320
		 *
321
		 * You can overwrite the template file configuration in extensions and
322
		 * provide alternative templates. These alternative templates should be
323
		 * named like the default one but with the string "default" replaced by
324
		 * an unique name. You may use the name of your project for this. If
325
		 * you've implemented an alternative client class as well, "default"
326
		 * should be replaced by the name of the new class.
327
		 *
328
		 * @param string Relative path to the template creating the HTML code
329
		 * @since 2016.04
330
		 */
331
		$tplconf = 'admin/jqadm/supplier/product/template-item';
332
		$default = 'supplier/item-product';
333
334
		return $view->render( $view->config( $tplconf, $default ) );
335
	}
336
}
337