Passed
Push — master ( 14e852...e8b893 )
by Aimeos
02:54
created

Standard::modify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2022
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Cms\Page\Cataloglist;
12
13
14
/**
15
 * Default implementation for CMS cataloglist.
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/cataloglist/subparts
25
	 * List of HTML sub-clients rendered within the cms page cataloglist 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.07
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/cms/page/cataloglist/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 body( string $uid = '' ) : string
68
	{
69
		return '';
70
	}
71
72
73
	/**
74
	 * Returns the sub-client given by its name.
75
	 *
76
	 * @param string $type Name of the client type
77
	 * @param string|null $name Name of the sub-client (Default if null)
78
	 * @return \Aimeos\Client\Html\Iface Sub-client object
79
	 */
80
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
81
	{
82
		/** client/html/cms/page/cataloglist/decorators/excludes
83
		 * Excludes decorators added by the "common" option from the cms page cataloglist html client
84
		 *
85
		 * Decorators extend the functionality of a class by adding new aspects
86
		 * (e.g. log what is currently done), executing the methods of the underlying
87
		 * class only in certain conditions (e.g. only for logged in users) or
88
		 * modify what is returned to the caller.
89
		 *
90
		 * This option allows you to remove a decorator added via
91
		 * "client/html/common/decorators/default" before they are wrapped
92
		 * around the html client.
93
		 *
94
		 *  client/html/cms/page/cataloglist/decorators/excludes = array( 'decorator1' )
95
		 *
96
		 * This would remove the decorator named "decorator1" from the list of
97
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
98
		 * "client/html/common/decorators/default" to the html client.
99
		 *
100
		 * @param array List of decorator names
101
		 * @since 2021.07
102
		 * @category Developer
103
		 * @see client/html/common/decorators/default
104
		 * @see client/html/cms/page/cataloglist/decorators/global
105
		 * @see client/html/cms/page/cataloglist/decorators/local
106
		 */
107
108
		/** client/html/cms/page/cataloglist/decorators/global
109
		 * Adds a list of globally available decorators only to the cms page cataloglist html client
110
		 *
111
		 * Decorators extend the functionality of a class by adding new aspects
112
		 * (e.g. log what is currently done), executing the methods of the underlying
113
		 * class only in certain conditions (e.g. only for logged in users) or
114
		 * modify what is returned to the caller.
115
		 *
116
		 * This option allows you to wrap global decorators
117
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
118
		 *
119
		 *  client/html/cms/page/cataloglist/decorators/global = array( 'decorator1' )
120
		 *
121
		 * This would add the decorator named "decorator1" defined by
122
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
123
		 *
124
		 * @param array List of decorator names
125
		 * @since 2021.07
126
		 * @category Developer
127
		 * @see client/html/common/decorators/default
128
		 * @see client/html/cms/page/cataloglist/decorators/excludes
129
		 * @see client/html/cms/page/cataloglist/decorators/local
130
		 */
131
132
		/** client/html/cms/page/cataloglist/decorators/local
133
		 * Adds a list of local decorators only to the cms page cataloglist html client
134
		 *
135
		 * Decorators extend the functionality of a class by adding new aspects
136
		 * (e.g. log what is currently done), executing the methods of the underlying
137
		 * class only in certain conditions (e.g. only for logged in users) or
138
		 * modify what is returned to the caller.
139
		 *
140
		 * This option allows you to wrap local decorators
141
		 * ("\Aimeos\Client\Html\Cms\Decorator\*") around the html client.
142
		 *
143
		 *  client/html/cms/page/cataloglist/decorators/local = array( 'decorator2' )
144
		 *
145
		 * This would add the decorator named "decorator2" defined by
146
		 * "\Aimeos\Client\Html\Cms\Decorator\Decorator2" only to the html client.
147
		 *
148
		 * @param array List of decorator names
149
		 * @since 2021.07
150
		 * @category Developer
151
		 * @see client/html/common/decorators/default
152
		 * @see client/html/cms/page/cataloglist/decorators/excludes
153
		 * @see client/html/cms/page/cataloglist/decorators/global
154
		 */
155
156
		return $this->createSubClient( 'cms/page/cataloglist/' . $type, $name );
157
	}
158
159
160
	/**
161
	 * Modifies the cached content to replace content based on sessions or cookies.
162
	 *
163
	 * @param string $content Cached content
164
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
165
	 * @return string Modified content
166
	 */
167
	public function modify( string $content, string $uid ) : string
168
	{
169
		$content = parent::modify( $content, $uid );
170
171
		return $this->replaceSection( $content, $this->view()->csrf()->formfield(), 'catalog.lists.items.csrf' );
172
	}
173
174
175
	/**
176
	 * Sets the necessary parameter values in the view.
177
	 *
178
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
179
	 * @param array &$tags Result array for the list of tags that are associated to the output
180
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
181
	 * @return \Aimeos\MW\View\Iface Modified view object
182
	 */
183
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
184
	{
185
		if( !isset( $view->pageContent ) ) {
186
			return parent::data( $view, $tags, $expire );
187
		}
188
189
		$texts = [];
190
		$context = $this->context();
191
		$config = $context->config();
192
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'product' );
193
194
		/** client/html/cms/page/template-cataloglist
195
		 * Relative path to the HTML template of the page catalog list client.
196
		 *
197
		 * The template file contains the HTML code and processing instructions
198
		 * to generate the HTML code that is inserted into the HTML page
199
		 * of the rendered page in the frontend. The configuration string is the
200
		 * path to the template file relative to the templates directory (usually
201
		 * in client/html/templates).
202
		 *
203
		 * You can overwrite the template file configuration in extensions and
204
		 * provide alternative templates. These alternative templates should be
205
		 * named like the default one but with the string "standard" replaced by
206
		 * an unique name. You may use the name of your project for this. If
207
		 * you've implemented an alternative client class as well, "standard"
208
		 * should be replaced by the name of the new class.
209
		 *
210
		 * @param string Relative path to the template creating code for the catalog list
211
		 * @since 2021.07
212
		 * @category Developer
213
		 * @see client/html/cms/page/template-body
214
		 * @see client/html/cms/page/template-header
215
		 */
216
		$template = $config->get( 'client/html/cms/page/template-cataloglist', 'cms/page/cataloglist/list-standard' );
217
		$domains = $config->get( 'client/html/catalog/lists/domains', ['media', 'media/property', 'price', 'text'] );
218
219
		if( $view->config( 'client/html/cms/page/basket-add', false ) ) {
220
			$domains = array_merge_recursive( $domains, ['product' => ['default'], 'attribute' => ['variant', 'custom', 'config']] );
221
		}
222
223
		libxml_use_internal_errors( true );
224
225
		foreach( $view->pageContent as $content )
226
		{
227
			$dom = new \DOMDocument( '1.0', 'UTF-8' );
228
			$dom->loadHTML( '<?xml encoding="utf-8" ?>' . $content, LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD );
229
			$nodes = $dom->getElementsByTagName( 'cataloglist' );
230
231
			while( $nodes->length > 0 )
232
			{
233
				$node = $nodes->item( 0 );
234
				$catid = $node->hasAttribute( 'catid' ) ? $node->getAttribute( 'catid' ) : null;
0 ignored issues
show
Bug introduced by
The method hasAttribute() does not exist on DOMNode. Did you maybe mean hasAttributes()? ( Ignorable by Annotation )

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

234
				$catid = $node->/** @scrutinizer ignore-call */ hasAttribute( 'catid' ) ? $node->getAttribute( 'catid' ) : null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
235
				$type = $node->hasAttribute( 'type' ) ? $node->getAttribute( 'type' ) : 'default';
236
				$limit = $node->hasAttribute( 'limit' ) ? $node->getAttribute( 'limit' ) : 3;
237
238
				$products = ( clone $cntl )->uses( $domains )
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

238
				$products = ( clone $cntl )->/** @scrutinizer ignore-call */ uses( $domains )
Loading history...
239
					->category( $catid, $type )
240
					->slice( 0, $limit )
241
					->search();
242
243
				$this->addMetaItems( $products, $expire, $tags );
244
245
				$view = $context->view()->set( 'products', $products );
246
247
				$pdom = new \DOMDocument( '1.0', 'UTF-8' );
248
				$pdom->loadHTML( '<?xml encoding="utf-8" ?>' . $view->render( $template ), LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD );
249
250
				$pnode = $dom->importNode( $pdom->documentElement, true );
251
				$node->parentNode->replaceChild( $pnode, $node );
0 ignored issues
show
Bug introduced by
The method replaceChild() does not exist on null. ( Ignorable by Annotation )

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

251
				$node->parentNode->/** @scrutinizer ignore-call */ 
252
                       replaceChild( $pnode, $node );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
252
			}
253
254
			$texts[] = substr( $dom->saveHTML(), 25 );
255
		}
256
257
		libxml_clear_errors();
258
259
		$view->pageContent = $texts;
260
261
		return parent::data( $view, $tags, $expire );
262
	}
263
264
265
	/**
266
	 * Returns the list of sub-client names configured for the client.
267
	 *
268
	 * @return array List of HTML client names
269
	 */
270
	protected function getSubClientNames() : array
271
	{
272
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
273
	}
274
}
275