Completed
Push — master ( b9f1e5...5ff555 )
by Aimeos
11:02
created

Standard   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 331
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 7
dl 0
loc 331
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B getBody() 0 41 5
A getHeader() 0 12 4
B getSubClient() 0 78 1
A getSubClientNames() 0 4 1
B process() 0 51 8
B addData() 0 55 9
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Checkout\Standard\Delivery;
13
14
15
// Strings for translation
16
sprintf( 'delivery' );
17
18
19
/**
20
 * Default implementation of checkout delivery HTML client.
21
 *
22
 * @package Client
23
 * @subpackage Html
24
 */
25
class Standard
26
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
27
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
28
{
29
	/** client/html/checkout/standard/delivery/subparts
30
	 * List of HTML sub-clients rendered within the checkout standard delivery section
31
	 *
32
	 * The output of the frontend is composed of the code generated by the HTML
33
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
34
	 * that are responsible for rendering certain sub-parts of the output. The
35
	 * sub-clients can contain HTML clients themselves and therefore a
36
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
37
	 * the output that is placed inside the container of its parent.
38
	 *
39
	 * At first, always the HTML code generated by the parent is printed, then
40
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
41
	 * determines the order of the output of these sub-clients inside the parent
42
	 * container. If the configured list of clients is
43
	 *
44
	 *  array( "subclient1", "subclient2" )
45
	 *
46
	 * you can easily change the order of the output by reordering the subparts:
47
	 *
48
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
49
	 *
50
	 * You can also remove one or more parts if they shouldn't be rendered:
51
	 *
52
	 *  client/html/<clients>/subparts = array( "subclient1" )
53
	 *
54
	 * As the clients only generates structural HTML, the layout defined via CSS
55
	 * should support adding, removing or reordering content by a fluid like
56
	 * design.
57
	 *
58
	 * @param array List of sub-client names
59
	 * @since 2014.03
60
	 * @category Developer
61
	 */
62
	private $subPartPath = 'client/html/checkout/standard/delivery/subparts';
63
	private $subPartNames = [];
64
65
66
	/**
67
	 * Returns the HTML code for insertion into the body.
68
	 *
69
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
70
	 * @return string HTML code
71
	 */
72
	public function getBody( string $uid = '' ) : string
73
	{
74
		$view = $this->getView();
75
		$step = $view->get( 'standardStepActive' );
76
		$onepage = $view->config( 'client/html/checkout/standard/onepage', [] );
77
78
		if( $step != 'delivery' && !( in_array( 'delivery', $onepage ) && in_array( $step, $onepage ) ) ) {
79
			return '';
80
		}
81
82
		$html = '';
83
		foreach( $this->getSubClients() as $subclient ) {
84
			$html .= $subclient->setView( $view )->getBody( $uid );
85
		}
86
		$view->deliveryBody = $html;
87
88
		/** client/html/checkout/standard/delivery/template-body
89
		 * Relative path to the HTML body template of the checkout standard delivery client.
90
		 *
91
		 * The template file contains the HTML code and processing instructions
92
		 * to generate the result shown in the body of the frontend. The
93
		 * configuration string is the path to the template file relative
94
		 * to the templates directory (usually in client/html/templates).
95
		 *
96
		 * You can overwrite the template file configuration in extensions and
97
		 * provide alternative templates. These alternative templates should be
98
		 * named like the default one but with the string "standard" replaced by
99
		 * an unique name. You may use the name of your project for this. If
100
		 * you've implemented an alternative client class as well, "standard"
101
		 * should be replaced by the name of the new class.
102
		 *
103
		 * @param string Relative path to the template creating code for the HTML page body
104
		 * @since 2014.03
105
		 * @category Developer
106
		 * @see client/html/checkout/standard/delivery/template-header
107
		 */
108
		$tplconf = 'client/html/checkout/standard/delivery/template-body';
109
		$default = 'checkout/standard/delivery-body-standard';
110
111
		return $view->render( $view->config( $tplconf, $default ) );
112
	}
113
114
115
	/**
116
	 * Returns the HTML string for insertion into the header.
117
	 *
118
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
119
	 * @return string|null String including HTML tags for the header on error
120
	 */
121
	public function getHeader( string $uid = '' ) : ?string
122
	{
123
		$view = $this->getView();
124
		$step = $view->get( 'standardStepActive' );
125
		$onepage = $view->config( 'client/html/checkout/standard/onepage', [] );
126
127
		if( $step != 'delivery' && !( in_array( 'delivery', $onepage ) && in_array( $step, $onepage ) ) ) {
128
			return '';
129
		}
130
131
		return parent::getHeader( $uid );
132
	}
133
134
135
	/**
136
	 * Returns the sub-client given by its name.
137
	 *
138
	 * @param string $type Name of the client type
139
	 * @param string|null $name Name of the sub-client (Default if null)
140
	 * @return \Aimeos\Client\Html\Iface Sub-client object
141
	 */
142
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
143
	{
144
		/** client/html/checkout/standard/delivery/decorators/excludes
145
		 * Excludes decorators added by the "common" option from the checkout standard delivery html client
146
		 *
147
		 * Decorators extend the functionality of a class by adding new aspects
148
		 * (e.g. log what is currently done), executing the methods of the underlying
149
		 * class only in certain conditions (e.g. only for logged in users) or
150
		 * modify what is returned to the caller.
151
		 *
152
		 * This option allows you to remove a decorator added via
153
		 * "client/html/common/decorators/default" before they are wrapped
154
		 * around the html client.
155
		 *
156
		 *  client/html/checkout/standard/delivery/decorators/excludes = array( 'decorator1' )
157
		 *
158
		 * This would remove the decorator named "decorator1" from the list of
159
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
160
		 * "client/html/common/decorators/default" to the html client.
161
		 *
162
		 * @param array List of decorator names
163
		 * @since 2015.08
164
		 * @category Developer
165
		 * @see client/html/common/decorators/default
166
		 * @see client/html/checkout/standard/delivery/decorators/global
167
		 * @see client/html/checkout/standard/delivery/decorators/local
168
		 */
169
170
		/** client/html/checkout/standard/delivery/decorators/global
171
		 * Adds a list of globally available decorators only to the checkout standard delivery html client
172
		 *
173
		 * Decorators extend the functionality of a class by adding new aspects
174
		 * (e.g. log what is currently done), executing the methods of the underlying
175
		 * class only in certain conditions (e.g. only for logged in users) or
176
		 * modify what is returned to the caller.
177
		 *
178
		 * This option allows you to wrap global decorators
179
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
180
		 *
181
		 *  client/html/checkout/standard/delivery/decorators/global = array( 'decorator1' )
182
		 *
183
		 * This would add the decorator named "decorator1" defined by
184
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
185
		 *
186
		 * @param array List of decorator names
187
		 * @since 2015.08
188
		 * @category Developer
189
		 * @see client/html/common/decorators/default
190
		 * @see client/html/checkout/standard/delivery/decorators/excludes
191
		 * @see client/html/checkout/standard/delivery/decorators/local
192
		 */
193
194
		/** client/html/checkout/standard/delivery/decorators/local
195
		 * Adds a list of local decorators only to the checkout standard delivery html client
196
		 *
197
		 * Decorators extend the functionality of a class by adding new aspects
198
		 * (e.g. log what is currently done), executing the methods of the underlying
199
		 * class only in certain conditions (e.g. only for logged in users) or
200
		 * modify what is returned to the caller.
201
		 *
202
		 * This option allows you to wrap local decorators
203
		 * ("\Aimeos\Client\Html\Checkout\Decorator\*") around the html client.
204
		 *
205
		 *  client/html/checkout/standard/delivery/decorators/local = array( 'decorator2' )
206
		 *
207
		 * This would add the decorator named "decorator2" defined by
208
		 * "\Aimeos\Client\Html\Checkout\Decorator\Decorator2" only to the html client.
209
		 *
210
		 * @param array List of decorator names
211
		 * @since 2015.08
212
		 * @category Developer
213
		 * @see client/html/common/decorators/default
214
		 * @see client/html/checkout/standard/delivery/decorators/excludes
215
		 * @see client/html/checkout/standard/delivery/decorators/global
216
		 */
217
218
		return $this->createSubClient( 'checkout/standard/delivery/' . $type, $name );
219
	}
220
221
222
	/**
223
	 * Processes the input, e.g. store given values.
224
	 *
225
	 * A view must be available and this method doesn't generate any output
226
	 * besides setting view variables.
227
	 */
228
	public function process()
229
	{
230
		$view = $this->getView();
231
232
		try
233
		{
234
			$context = $this->getContext();
235
			$basketCtrl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
236
			$servCtrl = \Aimeos\Controller\Frontend::create( $context, 'service' )->uses( ['media', 'price', 'text'] );
237
238
			// only start if there's something to do
239
			if( ( $serviceIds = $view->param( 'c_deliveryoption', null ) ) !== null )
240
			{
241
				$basketCtrl->deleteService( 'delivery' );
242
243
				foreach( (array) $serviceIds as $idx => $id )
244
				{
245
					try
246
					{
247
						$basketCtrl->addService( $servCtrl->get( $id ), $view->param( 'c_delivery/' . $id, [] ), $idx );
248
					}
249
					catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
250
					{
251
						$view->deliveryError = $e->getErrors();
252
						$view->standardErrorList = array_merge( $view->get( 'standardErrorList', [] ), $e->getErrors() );
253
254
						throw $e;
255
					}
256
				}
257
			}
258
259
260
			parent::process();
261
262
263
			// Test if delivery service is available
264
			$services = $basketCtrl->get()->getServices( 'delivery' );
265
266
			if( !isset( $view->standardStepActive ) && $services->isEmpty()
267
				&& count( $servCtrl->getProviders( 'delivery' ) ) > 0
268
			) {
269
				$view->standardStepActive = 'delivery';
270
				return;
271
			}
272
		}
273
		catch( \Exception $e )
274
		{
275
			$view->standardStepActive = 'delivery';
276
			throw $e;
277
		}
278
	}
279
280
281
	/**
282
	 * Returns the list of sub-client names configured for the client.
283
	 *
284
	 * @return array List of HTML client names
285
	 */
286
	protected function getSubClientNames() : array
287
	{
288
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
289
	}
290
291
292
	/**
293
	 * Sets the necessary parameter values in the view.
294
	 *
295
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
296
	 * @param array &$tags Result array for the list of tags that are associated to the output
297
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
298
	 * @return \Aimeos\MW\View\Iface Modified view object
299
	 */
300
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
301
	{
302
		$context = $this->getContext();
303
		$domains = ['media', 'price', 'text'];
304
305
		/** client/html/checkout/standard/delivery/domains
306
		 * List of domain names whose items should be available in the checkout payment templates
307
		 *
308
		 * The templates rendering checkout delivery related data usually add the
309
		 * images, prices and texts associated to each item. If you want to display
310
		 * additional content like the attributes, you can configure your own list
311
		 * of domains (attribute, media, price, text, etc. are domains) whose items
312
		 * are fetched from the storage.
313
		 *
314
		 * @param array List of domain names
315
		 * @since 2019.04
316
		 * @category Developer
317
		 * @see client/html/checkout/standard/payment/domains
318
		 */
319
		$domains = $context->getConfig()->get( 'client/html/checkout/standard/delivery/domains', $domains );
320
321
		$basketCntl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
322
		$serviceCntl = \Aimeos\Controller\Frontend::create( $context, 'service' );
323
324
		$services = [];
325
		$basket = $basketCntl->get();
326
		$providers = $serviceCntl->uses( $domains )->type( 'delivery' )->getProviders();
327
		$orderServices = map( $basket->getService( 'delivery' ) )->col( null, 'order.base.service.serviceid' );
328
329
		foreach( $providers as $id => $provider )
330
		{
331
			if( $provider->isAvailable( $basket ) === true )
332
			{
333
				$attr = $provider->getConfigFE( $basket );
334
335
				if( $oservice = $orderServices->get( $id ) )
336
				{
337
					foreach( $attr as $key => $item )
338
					{
339
						$value = is_array( $item->getDefault() ) ? key( $item->getDefault() ) : $item->getDefault();
340
						$value = $oservice->getAttribute( $key, 'delivery' ) ?: $value;
341
						$item->value = $oservice->getAttribute( $key . '/hidden', 'delivery' ) ?: $value;
342
					}
343
				}
344
345
				$services[$id] = $provider->getServiceItem()->set( 'attributes', $attr )
346
					->set( 'price', $provider->calcPrice( $basket ) );
347
			}
348
		}
349
350
		$view->deliveryServices = $services;
351
		$view->deliveryOption = $view->param( 'c_deliveryoption', $orderServices->firstKey() ?: $providers->firstKey() );
352
353
		return parent::addData( $view, $tags, $expire );
354
	}
355
}
356