Standard::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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