Completed
Push — master ( d7c61c...ea7970 )
by Aimeos
03:00
created

Standard::getAddressString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 40
rs 9.6
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019-2020
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Account\Profile\Address;
12
13
14
/**
15
 * Default implementation of acount profile address HTML client.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Common\Client\Summary\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	/** client/html/account/profile/address/standard/subparts
25
	 * List of HTML sub-clients rendered within the account profile address section
26
	 *
27
	 * The output of the frontend is composed of the code generated by the HTML
28
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
29
	 * that are responsible for rendering certain sub-parts of the output. The
30
	 * sub-clients can contain HTML clients themselves and therefore a
31
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
32
	 * the output that is placed inside the container of its parent.
33
	 *
34
	 * At first, always the HTML code generated by the parent is printed, then
35
	 * the HTML code of its sub-clients. The address of the HTML sub-clients
36
	 * determines the address of the output of these sub-clients inside the parent
37
	 * container. If the configured list of clients is
38
	 *
39
	 *  array( "subclient1", "subclient2" )
40
	 *
41
	 * you can easily change the address of the output by readdressing the subparts:
42
	 *
43
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
44
	 *
45
	 * You can also remove one or more parts if they shouldn't be rendered:
46
	 *
47
	 *  client/html/<clients>/subparts = array( "subclient1" )
48
	 *
49
	 * As the clients only generates structural HTML, the layout defined via CSS
50
	 * should support adding, removing or readdressing content by a fluid like
51
	 * design.
52
	 *
53
	 * @param array List of sub-client names
54
	 * @since 2019.07
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/account/profile/address/standard/subparts';
58
	private $subPartNames = [];
59
60
61
	/**
62
	 * Returns the HTML code for insertion into the body.
63
	 *
64
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
65
	 * @return string HTML code
66
	 */
67
	public function getBody( string $uid = '' ) : string
68
	{
69
		$view = $this->getView();
70
71
		$html = '';
72
		foreach( $this->getSubClients() as $subclient ) {
73
			$html .= $subclient->setView( $view )->getBody( $uid );
74
		}
75
		$view->addressBody = $html;
76
77
		/** client/html/account/profile/address/standard/template-body
78
		 * Relative path to the HTML body template of the account profile address client.
79
		 *
80
		 * The template file contains the HTML code and processing instructions
81
		 * to generate the result shown in the body of the frontend. The
82
		 * configuration string is the path to the template file relative
83
		 * to the templates directory (usually in client/html/templates).
84
		 *
85
		 * You can overwrite the template file configuration in extensions and
86
		 * provide alternative templates. These alternative templates should be
87
		 * named like the default one but with the string "standard" replaced by
88
		 * an unique name. You may use the name of your project for this. If
89
		 * you've implemented an alternative client class as well, "standard"
90
		 * should be replaced by the name of the new class.
91
		 *
92
		 * @param string Relative path to the template creating code for the HTML page body
93
		 * @since 2019.07
94
		 * @category Developer
95
		 * @see client/html/account/profile/address/standard/template-header
96
		 */
97
		$tplconf = 'client/html/account/profile/address/standard/template-body';
98
		$default = 'account/profile/address-body-standard';
99
100
		return $view->render( $view->config( $tplconf, $default ) );
101
	}
102
103
104
	/**
105
	 * Returns the sub-client given by its name.
106
	 *
107
	 * @param string $type Name of the client type
108
	 * @param string|null $name Name of the sub-client (Default if null)
109
	 * @return \Aimeos\Client\Html\Iface Sub-client object
110
	 */
111
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
112
	{
113
		/** client/html/account/profile/address/decorators/excludes
114
		 * Excludes decorators added by the "common" option from the account profile address html client
115
		 *
116
		 * Decorators extend the functionality of a class by adding new aspects
117
		 * (e.g. log what is currently done), executing the methods of the underlying
118
		 * class only in certain conditions (e.g. only for logged in users) or
119
		 * modify what is returned to the caller.
120
		 *
121
		 * This option allows you to remove a decorator added via
122
		 * "client/html/common/decorators/default" before they are wrapped
123
		 * around the html client.
124
		 *
125
		 *  client/html/account/profile/address/decorators/excludes = array( 'decorator1' )
126
		 *
127
		 * This would remove the decorator named "decorator1" from the list of
128
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
129
		 * "client/html/common/decorators/default" to the html client.
130
		 *
131
		 * @param array List of decorator names
132
		 * @since 2019.07
133
		 * @category Developer
134
		 * @see client/html/common/decorators/default
135
		 * @see client/html/account/profile/address/decorators/global
136
		 * @see client/html/account/profile/address/decorators/local
137
		 */
138
139
		/** client/html/account/profile/address/decorators/global
140
		 * Adds a list of globally available decorators only to the account profile address html 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\Client\Html\Common\Decorator\*") around the html client.
149
		 *
150
		 *  client/html/account/profile/address/decorators/global = array( 'decorator1' )
151
		 *
152
		 * This would add the decorator named "decorator1" defined by
153
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
154
		 *
155
		 * @param array List of decorator names
156
		 * @since 2019.07
157
		 * @category Developer
158
		 * @see client/html/common/decorators/default
159
		 * @see client/html/account/profile/address/decorators/excludes
160
		 * @see client/html/account/profile/address/decorators/local
161
		 */
162
163
		/** client/html/account/profile/address/decorators/local
164
		 * Adds a list of local decorators only to the account profile address html 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 local decorators
172
		 * ("\Aimeos\Client\Html\Account\Decorator\*") around the html client.
173
		 *
174
		 *  client/html/account/profile/address/decorators/local = array( 'decorator2' )
175
		 *
176
		 * This would add the decorator named "decorator2" defined by
177
		 * "\Aimeos\Client\Html\Account\Decorator\Decorator2" only to the html client.
178
		 *
179
		 * @param array List of decorator names
180
		 * @since 2019.07
181
		 * @category Developer
182
		 * @see client/html/common/decorators/default
183
		 * @see client/html/account/profile/address/decorators/excludes
184
		 * @see client/html/account/profile/address/decorators/global
185
		 */
186
187
		return $this->createSubClient( 'account/profile/address/' . $type, $name );
188
	}
189
190
191
	/**
192
	 * Processes the input, e.g. store given values.
193
	 *
194
	 * A view must be available and this method doesn't generate any output
195
	 * besides setting view variables if necessary.
196
	 */
197
	public function process()
198
	{
199
		$view = $this->getView();
200
201
		if( !$view->param( 'address/save' ) && !$view->param( 'address/delete' ) ) {
202
			return parent::process();
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::process() targeting Aimeos\Client\Html\Base::process() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
203
		}
204
205
		$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
206
		$addrItems = $cntl->uses( ['customer/address'] )->get()->getAddressItems();
207
		$cntl->add( $view->param( 'address/payment', [] ) );
208
		$map = [];
209
210
		foreach( $view->param( 'address/delivery/customer.address.id', [] ) as $pos => $id )
211
		{
212
			foreach( $view->param( 'address/delivery', [] ) as $key => $list )
213
			{
214
				if( array_key_exists( $pos, $list ) ) {
215
					$map[$pos][$key] = $list[$pos];
216
				}
217
			}
218
		}
219
220
		if( $pos = $view->param( 'address/delete' ) ) {
221
			unset( $map[$pos] );
222
		}
223
224
		foreach( $map as $pos => $data )
225
		{
226
			$addrItem = $addrItems->get( $pos ) ?: $cntl->createAddressItem();
227
			$cntl->addAddressItem( $addrItem->fromArray( $data ), $pos );
228
			$addrItems->remove( $pos );
229
		}
230
231
		foreach( $addrItems as $addrItem ) {
232
			$cntl->deleteAddressItem( $addrItem );
233
		}
234
235
		$cntl->store();
236
237
		parent::process();
238
	}
239
240
241
	/**
242
	 * Sets the necessary parameter values in the view.
243
	 *
244
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
245
	 * @param array &$tags Result array for the list of tags that are associated to the output
246
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
247
	 * @return \Aimeos\MW\View\Iface Modified view object
248
	 */
249
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
250
	{
251
		$context = $this->getContext();
252
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'customer' );
0 ignored issues
show
Unused Code introduced by
The assignment to $cntl is dead and can be removed.
Loading history...
253
254
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
255
		$languages = $localeManager->searchItems( $localeManager->createSearch( true ) )
256
			->col( 'locale.languageid', 'locale.languageid' );
257
258
259
		$deliveries = [];
260
		$addr = $view->profileCustomerItem->getPaymentAddress();
261
262
		$billing = $addr->toArray();
263
		$billing['string'] = $this->getAddressString( $view, $addr );
264
265
		foreach( $view->profileCustomerItem->getAddressItems() as $pos => $address )
266
		{
267
			$delivery = $address->toArray();
268
			$delivery['string'] = $this->getAddressString( $view, $address );
269
			$deliveries[$pos] = $delivery;
270
		}
271
272
273
		$view->addressBilling = $billing;
274
		$view->addressDelivery = $deliveries;
275
		$view->addressCountries = $view->config( 'client/html/checkout/standard/address/countries', [] );
276
		$view->addressStates = $view->config( 'client/html/checkout/standard/address/states', [] );
277
		$view->addressSalutations = array( 'company', 'mr', 'mrs' );
278
		$view->addressLanguages = $languages;
279
280
		return parent::addData( $view, $tags, $expire );
281
	}
282
283
284
	/**
285
	 * Returns the address as string
286
	 *
287
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
288
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $addr Order address item
289
	 * @return string Address as string
290
	 */
291
	protected function getAddressString( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Common\Item\Address\Iface $addr )
292
	{
293
		return preg_replace( "/\n+/m", "\n", trim( sprintf(
294
			/// Address format with company (%1$s), salutation (%2$s), title (%3$s), first name (%4$s), last name (%5$s),
295
			/// address part one (%6$s, e.g street), address part two (%7$s, e.g house number), address part three (%8$s, e.g additional information),
296
			/// postal/zip code (%9$s), city (%10$s), state (%11$s), country (%12$s), language (%13$s),
297
			/// e-mail (%14$s), phone (%15$s), facsimile/telefax (%16$s), web site (%17$s), vatid (%18$s)
298
			$view->translate( 'client', '%1$s
299
%2$s %3$s %4$s %5$s
300
%6$s %7$s
301
%8$s
302
%9$s %10$s
303
%11$s
304
%12$s
305
%13$s
306
%14$s
307
%15$s
308
%16$s
309
%17$s
310
%18$s
311
'
312
			),
313
			$addr->getCompany(),
314
			$view->translate( 'mshop/code', (string) $addr->getSalutation() ),
315
			$addr->getTitle(),
316
			$addr->getFirstName(),
317
			$addr->getLastName(),
318
			$addr->getAddress1(),
319
			$addr->getAddress2(),
320
			$addr->getAddress3(),
321
			$addr->getPostal(),
322
			$addr->getCity(),
323
			$addr->getState(),
324
			$view->translate( 'country', (string) $addr->getCountryId() ),
325
			$view->translate( 'language', (string) $addr->getLanguageId() ),
326
			$addr->getEmail(),
327
			$addr->getTelephone(),
328
			$addr->getTelefax(),
329
			$addr->getWebsite(),
330
			$addr->getVatID()
331
		) ) );
332
	}
333
334
335
	/**
336
	 * Returns the list of sub-client names configured for the client.
337
	 *
338
	 * @return array List of HTML client names
339
	 */
340
	protected function getSubClientNames() : array
341
	{
342
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
343
	}
344
}
345