Passed
Branch master (4f99e3)
by Aimeos
04:48
created

Standard::getBody()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 40
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 3
nop 1
dl 0
loc 40
rs 9.5555
c 0
b 0
f 0
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-2018
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/standard/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/standard/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( $uid = '' )
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/standard/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/standard/template-header
107
		 */
108
		$tplconf = 'client/html/checkout/standard/delivery/standard/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( $uid = '' )
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( $type, $name = null )
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
	 * A view must be available and this method doesn't generate any output
225
	 * besides setting view variables.
226
	 */
227
	public function process()
228
	{
229
		$view = $this->getView();
230
231
		try
232
		{
233
			$context = $this->getContext();
234
			$basketCtrl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
235
			$serviceCtrl = \Aimeos\Controller\Frontend::create( $context, 'service' );
236
237
			// only start if there's something to do
238
			if( ( $serviceIds = $view->param( 'c_deliveryoption', null ) ) !== null )
239
			{
240
				$basketCtrl->deleteService( 'delivery' );
1 ignored issue
show
Bug introduced by
The method deleteService() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

240
				$basketCtrl->/** @scrutinizer ignore-call */ 
241
                 deleteService( 'delivery' );
Loading history...
241
242
				foreach( (array) $serviceIds as $serviceId )
243
				{
244
					$attributes = $view->param( 'c_delivery/' . $serviceId, [] );
245
					$errors = $serviceCtrl->checkAttributes( $serviceId, $attributes );
1 ignored issue
show
Bug introduced by
The method checkAttributes() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

245
					/** @scrutinizer ignore-call */ 
246
     $errors = $serviceCtrl->checkAttributes( $serviceId, $attributes );
Loading history...
246
					$view->deliveryError = $errors;
247
248
					if( count( $errors ) > 0 )
249
					{
250
						$view->standardErrorList = $view->get( 'standardErrorList', [] ) + $errors;
251
						throw new \Aimeos\Client\Html\Exception( sprintf( 'Please recheck your delivery choice' ) );
252
					}
253
					else
254
					{
255
						$basketCtrl->addService( 'delivery', $serviceId, $attributes );
1 ignored issue
show
Bug introduced by
The method addService() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

255
						$basketCtrl->/** @scrutinizer ignore-call */ 
256
                   addService( 'delivery', $serviceId, $attributes );
Loading history...
256
					}
257
				}
258
			}
259
260
261
			parent::process();
262
263
264
			// Test if delivery service is available
265
			$services = $basketCtrl->get()->getServices();
1 ignored issue
show
Bug introduced by
The method get() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

265
			$services = $basketCtrl->/** @scrutinizer ignore-call */ get()->getServices();
Loading history...
266
267
			if( !isset( $view->standardStepActive ) && ( !isset( $services['delivery'] ) || empty( $services['delivery'] ) )
268
				&& count( $serviceCtrl->getProviders( 'delivery' ) ) > 0
1 ignored issue
show
Bug introduced by
The method getProviders() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

268
				&& count( $serviceCtrl->/** @scrutinizer ignore-call */ getProviders( 'delivery' ) ) > 0
Loading history...
269
			) {
270
				$view->standardStepActive = 'delivery';
271
				return false;
272
			}
273
		}
274
		catch( \Exception $e )
275
		{
276
			$view->standardStepActive = 'delivery';
277
			throw $e;
278
		}
279
	}
280
281
282
	/**
283
	 * Returns the list of sub-client names configured for the client.
284
	 *
285
	 * @return array List of HTML client names
286
	 */
287
	protected function getSubClientNames()
288
	{
289
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
290
	}
291
292
293
	/**
294
	 * Sets the necessary parameter values in the view.
295
	 *
296
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
297
	 * @param array &$tags Result array for the list of tags that are associated to the output
298
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
299
	 * @return \Aimeos\MW\View\Iface Modified view object
300
	 */
301
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], &$expire = null )
302
	{
303
		$context = $this->getContext();
304
305
		$basketCntl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
306
		$serviceCntl = \Aimeos\Controller\Frontend::create( $context, 'service' );
307
308
		$basket = $basketCntl->get();
309
		$services = $attributes = $prices = [];
310
		$providers = $serviceCntl->getProviders( 'delivery' );
311
312
		foreach( $providers as $id => $provider )
313
		{
314
			if( $provider->isAvailable( $basket ) === true )
315
			{
316
				$services[$id] = $provider->getServiceItem();
317
				$prices[$id] = $provider->calcPrice( $basket );
318
				$attributes[$id] = $provider->getConfigFE( $basket );
319
			}
320
		}
321
322
		$view->deliveryServices = $services;
323
		$view->deliveryServicePrices = $prices;
324
		$view->deliveryServiceAttributes = $attributes;
325
326
		return parent::addData( $view, $tags, $expire );
327
	}
328
}