Standard::get()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Customer\Address;
12
13
sprintf( 'address' ); // for translation
14
15
16
/**
17
 * Default implementation of customer 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/customer/address/name
27
	 * Name of the address 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
	 * 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]['customer.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/customer/address/decorators/excludes
115
		 * Excludes decorators added by the "common" option from the customer 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/customer/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 2016.01
134
		 * @see admin/jqadm/common/decorators/default
135
		 * @see admin/jqadm/customer/address/decorators/global
136
		 * @see admin/jqadm/customer/address/decorators/local
137
		 */
138
139
		/** admin/jqadm/customer/address/decorators/global
140
		 * Adds a list of globally available decorators only to the customer 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/customer/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 2016.01
157
		 * @see admin/jqadm/common/decorators/default
158
		 * @see admin/jqadm/customer/address/decorators/excludes
159
		 * @see admin/jqadm/customer/address/decorators/local
160
		 */
161
162
		/** admin/jqadm/customer/address/decorators/local
163
		 * Adds a list of local decorators only to the customer 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\Customer\Decorator\*") around the JQAdm client.
172
		 *
173
		 *  admin/jqadm/customer/address/decorators/local = array( 'decorator2' )
174
		 *
175
		 * This would add the decorator named "decorator2" defined by
176
		 * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client.
177
		 *
178
		 * @param array List of decorator names
179
		 * @since 2016.01
180
		 * @see admin/jqadm/common/decorators/default
181
		 * @see admin/jqadm/customer/address/decorators/excludes
182
		 * @see admin/jqadm/customer/address/decorators/global
183
		 */
184
		return $this->createSubClient( 'customer/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/customer/address/subparts
196
		 * List of JQAdm sub-clients rendered within the customer 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 2016.01
226
		 */
227
		return $this->context()->config()->get( 'admin/jqadm/customer/address/subparts', [] );
228
	}
229
230
231
	/**
232
	 * Creates new and updates existing items using the data array
233
	 *
234
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object without referenced domain items
235
	 * @param array $data Data array
236
	 * @return \Aimeos\MShop\Customer\Item\Iface Modified coupon item
237
	 */
238
	protected function fromArray( \Aimeos\MShop\Customer\Item\Iface $item, array $data ) : \Aimeos\MShop\Customer\Item\Iface
239
	{
240
		$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
241
242
		$addrItems = $item->getAddressItems();
243
244
		foreach( $data as $entry )
245
		{
246
			if( $addrItem = $addrItems->get( $entry['customer.address.id'] ) ) {
247
				$addrItems->remove( $entry['customer.address.id'] );
248
			} else {
249
				$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

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