Passed
Push — master ( e65429...cce4b8 )
by Aimeos
03:27
created

Standard::body()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 45
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 45
rs 9.9
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020-2022
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', 'cataloglist'];
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 body( string $uid = '' ) : string
72
	{
73
		/** client/html/cms/page/cache
74
		 * Enables or disables caching only for the cms page component
75
		 *
76
		 * Disable caching for components can be useful if you would have too much
77
		 * entries to cache or if the component contains non-cacheable parts that
78
		 * can't be replaced using the modify() method.
79
		 *
80
		 * @param boolean True to enable caching, false to disable
81
		 * @category Developer
82
		 * @category User
83
		 * @see client/html/cms/page/cache
84
		 * @see client/html/cms/filter/cache
85
		 * @see client/html/cms/lists/cache
86
		 */
87
88
		/** client/html/cms/page
89
		 * All parameters defined for the cms page component and its subparts
90
		 *
91
		 * This returns all settings related to the page component.
92
		 * Please refer to the single settings for pages.
93
		 *
94
		 * @param array Associative list of name/value settings
95
		 * @category Developer
96
		 * @see client/html/cms#page
97
		 */
98
		$confkey = 'client/html/cms/page';
99
		$prefixes = ['path'];
100
101
		if( $html = $this->cached( 'body', $uid, $prefixes, $confkey ) ) {
102
			return $this->modify( $html, $uid );
103
		}
104
105
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
106
107
		$html = '';
108
		foreach( $this->getSubClients() as $subclient ) {
109
			$html .= $subclient->setView( $view )->body( $uid );
110
		}
111
112
		$template = $this->context()->config()->get( 'client/html/cms/page/template-body', 'cms/page/body' );
113
		$html = $view->set( 'body', $html )->render( $template );
114
115
		return $this->cache( 'body', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
116
	}
117
118
119
	/**
120
	 * Returns the HTML string for insertion into the header.
121
	 *
122
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
123
	 * @return string|null String including HTML tags for the header on error
124
	 */
125
	public function header( string $uid = '' ) : ?string
126
	{
127
		$confkey = 'client/html/cms/page';
128
		$prefixes = ['page'];
129
130
		if( $html = $this->cached( 'header', $uid, $prefixes, $confkey ) ) {
131
			return $this->modify( $html, $uid );
132
		}
133
134
		$view = $this->view = $this->view ?? $this->object()->data( $this->view(), $this->tags, $this->expire );
135
		$template = $this->context()->config()->get( 'client/html/cms/page/template-header', 'cms/page/header' );
136
		$html = $view->render( $template );
137
138
		return $this->cache( 'header', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
139
	}
140
141
142
	/**
143
	 * Returns the sub-client given by its name.
144
	 *
145
	 * @param string $type Name of the client type
146
	 * @param string|null $name Name of the sub-client (Default if null)
147
	 * @return \Aimeos\Client\Html\Iface Sub-client object
148
	 */
149
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
150
	{
151
		/** client/html/cms/page/decorators/excludes
152
		 * Excludes decorators added by the "common" option from the cms page html client
153
		 *
154
		 * Decorators extend the functionality of a class by adding new aspects
155
		 * (e.g. log what is currently done), executing the methods of the underlying
156
		 * class only in certain conditions (e.g. only for logged in users) or
157
		 * modify what is returned to the caller.
158
		 *
159
		 * This option allows you to remove a decorator added via
160
		 * "client/html/common/decorators/default" before they are wrapped
161
		 * around the html client.
162
		 *
163
		 *  client/html/cms/page/decorators/excludes = array( 'decorator1' )
164
		 *
165
		 * This would remove the decorator named "decorator1" from the list of
166
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
167
		 * "client/html/common/decorators/default" to the html client.
168
		 *
169
		 * @param array List of decorator names
170
		 * @since 2021.04
171
		 * @category Developer
172
		 * @see client/html/common/decorators/default
173
		 * @see client/html/cms/page/decorators/global
174
		 * @see client/html/cms/page/decorators/local
175
		 */
176
177
		/** client/html/cms/page/decorators/global
178
		 * Adds a list of globally available decorators only to the cms page html client
179
		 *
180
		 * Decorators extend the functionality of a class by adding new aspects
181
		 * (e.g. log what is currently done), executing the methods of the underlying
182
		 * class only in certain conditions (e.g. only for logged in users) or
183
		 * modify what is returned to the caller.
184
		 *
185
		 * This option allows you to wrap global decorators
186
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
187
		 *
188
		 *  client/html/cms/page/decorators/global = array( 'decorator1' )
189
		 *
190
		 * This would add the decorator named "decorator1" defined by
191
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
192
		 *
193
		 * @param array List of decorator names
194
		 * @since 2021.04
195
		 * @category Developer
196
		 * @see client/html/common/decorators/default
197
		 * @see client/html/cms/page/decorators/excludes
198
		 * @see client/html/cms/page/decorators/local
199
		 */
200
201
		/** client/html/cms/page/decorators/local
202
		 * Adds a list of local decorators only to the cms page html client
203
		 *
204
		 * Decorators extend the functionality of a class by adding new aspects
205
		 * (e.g. log what is currently done), executing the methods of the underlying
206
		 * class only in certain conditions (e.g. only for logged in users) or
207
		 * modify what is returned to the caller.
208
		 *
209
		 * This option allows you to wrap local decorators
210
		 * ("\Aimeos\Client\Html\Cms\Decorator\*") around the html client.
211
		 *
212
		 *  client/html/cms/page/decorators/local = array( 'decorator2' )
213
		 *
214
		 * This would add the decorator named "decorator2" defined by
215
		 * "\Aimeos\Client\Html\Cms\Decorator\Decorator2" only to the html client.
216
		 *
217
		 * @param array List of decorator names
218
		 * @since 2021.04
219
		 * @category Developer
220
		 * @see client/html/common/decorators/default
221
		 * @see client/html/cms/page/decorators/excludes
222
		 * @see client/html/cms/page/decorators/global
223
		 */
224
		return $this->createSubClient( 'cms/page/' . $type, $name );
225
	}
226
227
228
	/**
229
	 * Sets the necessary parameter values in the view.
230
	 *
231
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
232
	 * @param array &$tags Result array for the list of tags that are associated to the output
233
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
234
	 * @return \Aimeos\MW\View\Iface Modified view object
235
	 */
236
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
237
	{
238
		$context = $this->context();
239
		$controller = \Aimeos\Controller\Frontend::create( $context, 'cms' );
240
241
		/** client/html/cms/page/domains
242
		 * A list of domain names whose items should be available in the cms page view template
243
		 *
244
		 * The templates rendering the cms page section use the texts and
245
		 * maybe images and attributes associated to the categories. You can
246
		 * configure your own list of domains (attribute, media, price, product,
247
		 * text, etc. are domains) whose items are fetched from the storage.
248
		 * Please keep in mind that the more domains you add to the configuration,
249
		 * the more time is required for fetching the content!
250
		 *
251
		 * @param array List of domain names
252
		 * @since 2021.04
253
		 */
254
		$domains = $context->config()->get( 'client/html/cms/page/domains', ['text'] );
255
256
		$path = '/' . trim( $view->param( 'path' ), '/' );
257
258
		if( $page = $controller->uses( $domains )->compare( '==', 'cms.url', $path )->search()->first() )
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

258
		if( $page = $controller->/** @scrutinizer ignore-call */ uses( $domains )->compare( '==', 'cms.url', $path )->search()->first() )
Loading history...
259
		{
260
			$this->addMetaItems( $page, $expire, $tags );
261
262
			$view->pageCmsItem = $page;
263
			$view->pageContent = $page->getRefItems( 'text', 'content' )->map( function( $item ) {
264
				$data = ( $json = json_decode( $item->getContent(), true ) ) ? $json['html'] : $item->getContent();
265
				return '<div class="cms-content">' . $data . '</div>';
266
			} )->all();
267
		}
268
269
		return parent::data( $view, $tags, $expire );
270
	}
271
272
273
	/**
274
	 * Returns the list of sub-client names configured for the client.
275
	 *
276
	 * @return array List of HTML client names
277
	 */
278
	protected function getSubClientNames() : array
279
	{
280
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
281
	}
282
283
284
	/** client/html/cms/page/template-body
285
	 * Relative path to the HTML body template of the cms page client.
286
	 *
287
	 * The template file contains the HTML code and processing instructions
288
	 * to generate the result shown in the body of the frontend. The
289
	 * configuration string is the path to the template file relative
290
	 * to the templates directory (usually in client/html/templates).
291
	 *
292
	 * You can overwrite the template file configuration in extensions and
293
	 * provide alternative templates. These alternative templates should be
294
	 * named like the default one but with the string "standard" replaced by
295
	 * an unique name. You may use the name of your project for this. If
296
	 * you've implemented an alternative client class as well, "standard"
297
	 * should be replaced by the name of the new class.
298
	 *
299
	 * @param string Relative path to the template creating code for the HTML page body
300
	 * @since 2021.04
301
	 * @category Developer
302
	 * @see client/html/cms/page/template-header
303
	 */
304
305
	/** client/html/cms/page/template-header
306
	 * Relative path to the HTML header template of the cms page client.
307
	 *
308
	 * The template file contains the HTML code and processing instructions
309
	 * to generate the HTML code that is inserted into the HTML page header
310
	 * of the rendered page in the frontend. The configuration string is the
311
	 * path to the template file relative to the templates directory (usually
312
	 * in client/html/templates).
313
	 *
314
	 * You can overwrite the template file configuration in extensions and
315
	 * provide alternative templates. These alternative templates should be
316
	 * named like the default one but with the string "standard" replaced by
317
	 * an unique name. You may use the name of your project for this. If
318
	 * you've implemented an alternative client class as well, "standard"
319
	 * should be replaced by the name of the new class.
320
	 *
321
	 * @param string Relative path to the template creating code for the HTML page head
322
	 * @since 2021.04
323
	 * @category Developer
324
	 * @see client/html/cms/page/template-body
325
	 */
326
}
327