Passed
Branch master (560f53)
by Aimeos
13:02
created

Standard   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 418
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 94
c 2
b 0
f 0
dl 0
loc 418
rs 10
wmc 22

7 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 29 5
A csrf() 0 4 1
A getSubClient() 0 76 1
A addData() 0 27 1
A getSubClientNames() 0 3 1
A getHeader() 0 62 5
B getBody() 0 110 8
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020-2021
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Cms\Page;
12
13
14
/**
15
 * Default implementation of cms page section HTML clients.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	/** client/html/cms/page/subparts
25
	 * List of HTML sub-clients rendered within the cms page 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 order of the HTML sub-clients
36
	 * determines the order 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 order of the output by reordering 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 reordering content by a fluid like
51
	 * design.
52
	 *
53
	 * @param array List of sub-client names
54
	 * @since 2021.04
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/cms/page/subparts';
58
	private $subPartNames = ['contact'];
59
60
	private $tags = [];
61
	private $expire;
62
	private $view;
63
64
65
	/**
66
	 * Returns the HTML code for insertion into the body.
67
	 *
68
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
69
	 * @return string HTML code
70
	 */
71
	public function getBody( string $uid = '' ) : string
72
	{
73
		$prefixes = ['path'];
74
		$context = $this->getContext();
75
76
		/** client/html/cms/page/cache
77
		 * Enables or disables caching only for the cms page component
78
		 *
79
		 * Disable caching for components can be useful if you would have too much
80
		 * entries to cache or if the component contains non-cacheable parts that
81
		 * can't be replaced using the modifyBody() and modifyHeader() methods.
82
		 *
83
		 * @param boolean True to enable caching, false to disable
84
		 * @category Developer
85
		 * @category User
86
		 * @see client/html/cms/page/cache
87
		 * @see client/html/cms/filter/cache
88
		 * @see client/html/cms/lists/cache
89
		 */
90
91
		/** client/html/cms/page
92
		 * All parameters defined for the cms page component and its subparts
93
		 *
94
		 * This returns all settings related to the page component.
95
		 * Please refer to the single settings for pages.
96
		 *
97
		 * @param array Associative list of name/value settings
98
		 * @category Developer
99
		 * @see client/html/cms#page
100
		 */
101
		$confkey = 'client/html/cms/page';
102
103
		if( ( $html = $this->getCached( 'body', $uid, $prefixes, $confkey ) ) === null )
104
		{
105
			$view = $this->getView();
106
107
			/** client/html/cms/page/template-body
108
			 * Relative path to the HTML body template of the cms page client.
109
			 *
110
			 * The template file contains the HTML code and processing instructions
111
			 * to generate the result shown in the body of the frontend. The
112
			 * configuration string is the path to the template file relative
113
			 * to the templates directory (usually in client/html/templates).
114
			 *
115
			 * You can overwrite the template file configuration in extensions and
116
			 * provide alternative templates. These alternative templates should be
117
			 * named like the default one but with the string "standard" replaced by
118
			 * an unique name. You may use the name of your project for this. If
119
			 * you've implemented an alternative client class as well, "standard"
120
			 * should be replaced by the name of the new class.
121
			 *
122
			 * @param string Relative path to the template creating code for the HTML page body
123
			 * @since 2021.04
124
			 * @category Developer
125
			 * @see client/html/cms/page/template-header
126
			 */
127
			$tplconf = 'client/html/cms/page/template-body';
128
			$default = 'cms/page/body-standard';
129
130
			try
131
			{
132
				$html = '';
133
134
				if( !isset( $this->view ) ) {
135
					$view = $this->view = $this->getObject()->addData( $view, $this->tags, $this->expire );
136
				}
137
138
				foreach( $this->getSubClients() as $subclient ) {
139
					$html .= $subclient->setView( $view )->getBody( $uid );
140
				}
141
				$view->pageBody = $html;
142
143
				$html = $this->csrf( $view->render( $view->config( $tplconf, $default ) ) );
144
				$this->setCached( 'body', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
145
146
				return $html;
147
			}
148
			catch( \Aimeos\Client\Html\Exception $e )
149
			{
150
				$tplconf = 'client/html/cms/page/template-error';
151
				$error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) );
152
				$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
153
			}
154
			catch( \Aimeos\Controller\Frontend\Exception $e )
155
			{
156
				$tplconf = 'client/html/cms/page/template-error';
157
				$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
158
				$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
159
			}
160
			catch( \Aimeos\MShop\Exception $e )
161
			{
162
				$tplconf = 'client/html/cms/page/template-error';
163
				$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
164
				$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
165
			}
166
			catch( \Exception $e )
167
			{
168
				$error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) );
169
				$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
170
				$this->logException( $e );
171
			}
172
173
			$html = $view->render( $view->config( $tplconf, $default ) );
174
		}
175
		else
176
		{
177
			$html = $this->modifyBody( $html, $uid );
178
		}
179
180
		return $html;
181
	}
182
183
184
	/**
185
	 * Returns the HTML string for insertion into the header.
186
	 *
187
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
188
	 * @return string|null String including HTML tags for the header on error
189
	 */
190
	public function getHeader( string $uid = '' ) : ?string
191
	{
192
		$prefixes = ['page'];
193
		$confkey = 'client/html/cms/page';
194
195
		if( ( $html = $this->getCached( 'header', $uid, $prefixes, $confkey ) ) === null )
196
		{
197
			$view = $this->getView();
198
199
			/** client/html/cms/page/template-header
200
			 * Relative path to the HTML header template of the cms page client.
201
			 *
202
			 * The template file contains the HTML code and processing instructions
203
			 * to generate the HTML code that is inserted into the HTML page header
204
			 * of the rendered page in the frontend. The configuration string is the
205
			 * path to the template file relative to the templates directory (usually
206
			 * in client/html/templates).
207
			 *
208
			 * You can overwrite the template file configuration in extensions and
209
			 * provide alternative templates. These alternative templates should be
210
			 * named like the default one but with the string "standard" replaced by
211
			 * an unique name. You may use the name of your project for this. If
212
			 * you've implemented an alternative client class as well, "standard"
213
			 * should be replaced by the name of the new class.
214
			 *
215
			 * @param string Relative path to the template creating code for the HTML page head
216
			 * @since 2021.04
217
			 * @category Developer
218
			 * @see client/html/cms/page/template-body
219
			 */
220
			$tplconf = 'client/html/cms/page/template-header';
221
			$default = 'cms/page/header-standard';
222
223
			try
224
			{
225
				$html = '';
226
227
				if( !isset( $this->view ) ) {
228
					$view = $this->view = $this->getObject()->addData( $view, $this->tags, $this->expire );
229
				}
230
231
				foreach( $this->getSubClients() as $subclient ) {
232
					$html .= $subclient->setView( $view )->getHeader( $uid );
233
				}
234
				$view->pageHeader = $html;
235
236
				$html = $view->render( $view->config( $tplconf, $default ) );
237
				$this->setCached( 'header', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
238
239
				return $html;
240
			}
241
			catch( \Exception $e )
242
			{
243
				$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), [$e->getMessage()] );
244
			}
245
		}
246
		else
247
		{
248
			$html = $this->modifyHeader( $html, $uid );
249
		}
250
251
		return $html;
252
	}
253
254
255
	/**
256
	 * Returns the sub-client given by its name.
257
	 *
258
	 * @param string $type Name of the client type
259
	 * @param string|null $name Name of the sub-client (Default if null)
260
	 * @return \Aimeos\Client\Html\Iface Sub-client object
261
	 */
262
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
263
	{
264
		/** client/html/cms/page/decorators/excludes
265
		 * Excludes decorators added by the "common" option from the cms page html client
266
		 *
267
		 * Decorators extend the functionality of a class by adding new aspects
268
		 * (e.g. log what is currently done), executing the methods of the underlying
269
		 * class only in certain conditions (e.g. only for logged in users) or
270
		 * modify what is returned to the caller.
271
		 *
272
		 * This option allows you to remove a decorator added via
273
		 * "client/html/common/decorators/default" before they are wrapped
274
		 * around the html client.
275
		 *
276
		 *  client/html/cms/page/decorators/excludes = array( 'decorator1' )
277
		 *
278
		 * This would remove the decorator named "decorator1" from the list of
279
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
280
		 * "client/html/common/decorators/default" to the html client.
281
		 *
282
		 * @param array List of decorator names
283
		 * @since 2021.04
284
		 * @category Developer
285
		 * @see client/html/common/decorators/default
286
		 * @see client/html/cms/page/decorators/global
287
		 * @see client/html/cms/page/decorators/local
288
		 */
289
290
		/** client/html/cms/page/decorators/global
291
		 * Adds a list of globally available decorators only to the cms page html client
292
		 *
293
		 * Decorators extend the functionality of a class by adding new aspects
294
		 * (e.g. log what is currently done), executing the methods of the underlying
295
		 * class only in certain conditions (e.g. only for logged in users) or
296
		 * modify what is returned to the caller.
297
		 *
298
		 * This option allows you to wrap global decorators
299
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
300
		 *
301
		 *  client/html/cms/page/decorators/global = array( 'decorator1' )
302
		 *
303
		 * This would add the decorator named "decorator1" defined by
304
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
305
		 *
306
		 * @param array List of decorator names
307
		 * @since 2021.04
308
		 * @category Developer
309
		 * @see client/html/common/decorators/default
310
		 * @see client/html/cms/page/decorators/excludes
311
		 * @see client/html/cms/page/decorators/local
312
		 */
313
314
		/** client/html/cms/page/decorators/local
315
		 * Adds a list of local decorators only to the cms page html client
316
		 *
317
		 * Decorators extend the functionality of a class by adding new aspects
318
		 * (e.g. log what is currently done), executing the methods of the underlying
319
		 * class only in certain conditions (e.g. only for logged in users) or
320
		 * modify what is returned to the caller.
321
		 *
322
		 * This option allows you to wrap local decorators
323
		 * ("\Aimeos\Client\Html\Cms\Decorator\*") around the html client.
324
		 *
325
		 *  client/html/cms/page/decorators/local = array( 'decorator2' )
326
		 *
327
		 * This would add the decorator named "decorator2" defined by
328
		 * "\Aimeos\Client\Html\Cms\Decorator\Decorator2" only to the html client.
329
		 *
330
		 * @param array List of decorator names
331
		 * @since 2021.04
332
		 * @category Developer
333
		 * @see client/html/common/decorators/default
334
		 * @see client/html/cms/page/decorators/excludes
335
		 * @see client/html/cms/page/decorators/global
336
		 */
337
		return $this->createSubClient( 'cms/page/' . $type, $name );
338
	}
339
340
341
	/**
342
	 * Processes the input, e.g. store given values.
343
	 *
344
	 * A view must be available and this method doesn't generate any output
345
	 * besides setting view variables if necessary.
346
	 */
347
	public function process()
348
	{
349
		$view = $this->getView();
350
		$context = $this->getContext();
351
352
		try
353
		{
354
			parent::process();
355
		}
356
		catch( \Aimeos\Client\Html\Exception $e )
357
		{
358
			$error = array( $context->getI18n()->dt( 'client', $e->getMessage() ) );
359
			$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
360
		}
361
		catch( \Aimeos\Controller\Frontend\Exception $e )
362
		{
363
			$error = array( $context->getI18n()->dt( 'controller/frontend', $e->getMessage() ) );
364
			$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
365
		}
366
		catch( \Aimeos\MShop\Exception $e )
367
		{
368
			$error = array( $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
369
			$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
370
		}
371
		catch( \Exception $e )
372
		{
373
			$error = array( $context->getI18n()->dt( 'client', 'A non-recoverable error occured' ) );
374
			$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
375
			$this->logException( $e );
376
		}
377
	}
378
379
380
	/**
381
	 * Sets the necessary parameter values in the view.
382
	 *
383
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
384
	 * @param array &$tags Result array for the list of tags that are associated to the output
385
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
386
	 * @return \Aimeos\MW\View\Iface Modified view object
387
	 */
388
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
389
	{
390
		$context = $this->getContext();
391
		$controller = \Aimeos\Controller\Frontend::create( $context, 'cms' );
392
393
		/** client/html/cms/page/domains
394
		 * A list of domain names whose items should be available in the cms page view template
395
		 *
396
		 * The templates rendering the cms page section use the texts and
397
		 * maybe images and attributes associated to the categories. You can
398
		 * configure your own list of domains (attribute, media, price, product,
399
		 * text, etc. are domains) whose items are fetched from the storage.
400
		 * Please keep in mind that the more domains you add to the configuration,
401
		 * the more time is required for fetching the content!
402
		 *
403
		 * @param array List of domain names
404
		 * @since 2021.04
405
		 */
406
		$domains = $context->getConfig()->get( 'client/html/cms/page/domains', ['media', 'text'] );
407
408
		$page = $controller->uses( $domains )->find( '/' . trim( $view->param( 'path' ), '/' ) );
0 ignored issues
show
Bug introduced by
The method uses() 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

408
		$page = $controller->/** @scrutinizer ignore-call */ uses( $domains )->find( '/' . trim( $view->param( 'path' ), '/' ) );
Loading history...
409
410
		$this->addMetaItems( $page, $expire, $tags );
411
412
		$view->pageCmsItem = $page;
413
414
		return parent::addData( $view, $tags, $expire );
415
	}
416
417
418
	/**
419
	 * Returns the list of sub-client names configured for the client.
420
	 *
421
	 * @return array List of HTML client names
422
	 */
423
	protected function getSubClientNames() : array
424
	{
425
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
426
	}
427
428
429
	/**
430
	 * Returns the passed HTML code with CSRF tokens replaced
431
	 *
432
	 * @return string HTML code
433
	 */
434
	protected function csrf( string $html ) : string
435
	{
436
		$csrf = $this->getView()->csrf();
437
		return str_replace( ['%csrf.name%', '%csrf.value%'], [$csrf->name(), $csrf->value()], $html );
438
	}
439
}
440