Standard::copy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

248
				/** @scrutinizer ignore-call */ 
249
    $addrItem = $manager->createAddressItem();

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...
249
			}
250
251
			$addrItem->fromArray( $entry, true );
252
			$item->addAddressItem( $addrItem );
253
		}
254
255
		return $item->deleteAddressItems( $addrItems );
256
	}
257
258
259
	/**
260
	 * Constructs the data array for the view from the given item
261
	 *
262
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object including referenced domain items
263
	 * @param bool $copy True if items should be copied, false if not
264
	 * @return string[] Multi-dimensional associative list of item data
265
	 */
266
	protected function toArray( \Aimeos\MShop\Supplier\Item\Iface $item, bool $copy = false ) : array
267
	{
268
		$siteId = $this->context()->locale()->getSiteId();
269
		$data = [];
270
271
		foreach( $item->getAddressItems() as $addrItem )
272
		{
273
			$list = $addrItem->toArray( true );
274
275
			if( $copy === true )
276
			{
277
				$list['supplier.address.siteid'] = $siteId;
278
				$list['supplier.address.id'] = '';
279
			}
280
281
			$data[] = $list;
282
		}
283
284
		return $data;
285
	}
286
287
288
	/**
289
	 * Returns the rendered template including the view data
290
	 *
291
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
292
	 * @return string HTML output
293
	 */
294
	protected function render( \Aimeos\Base\View\Iface $view ) : string
295
	{
296
		/** admin/jqadm/supplier/address/template-item
297
		 * Relative path to the HTML body template of the address subpart for suppliers.
298
		 *
299
		 * The template file contains the HTML code and processing instructions
300
		 * to generate the result shown in the body of the frontend. The
301
		 * configuration string is the path to the template file relative
302
		 * to the templates directory (usually in templates/admin/jqadm).
303
		 *
304
		 * You can overwrite the template file configuration in extensions and
305
		 * provide alternative templates. These alternative templates should be
306
		 * named like the default one but with the string "default" replaced by
307
		 * an unique name. You may use the name of your project for this. If
308
		 * you've implemented an alternative client class as well, "default"
309
		 * should be replaced by the name of the new class.
310
		 *
311
		 * @param string Relative path to the template creating the HTML code
312
		 * @since 2017.10
313
		 */
314
		$tplconf = 'admin/jqadm/supplier/address/template-item';
315
		$default = 'supplier/item-address';
316
317
		return $view->render( $view->config( $tplconf, $default ) );
318
	}
319
}
320