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

Standard::getSubClient()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 8.48
c 0
b 0
f 0
cc 1
nc 1
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Catalog\Suggest;
13
14
15
/**
16
 * Default implementation of catalog suggest section HTML clients.
17
 *
18
 * @package Client
19
 * @subpackage Html
20
 */
21
class Standard
22
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
23
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
24
{
25
	/** client/html/catalog/suggest/subparts
26
	 * List of HTML sub-clients rendered within the catalog suggest client
27
	 *
28
	 * The output of the frontend is composed of the code generated by the HTML
29
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
30
	 * that are responsible for rendering certain sub-parts of the output. The
31
	 * sub-clients can contain HTML clients themselves and therefore a
32
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
33
	 * the output that is placed inside the container of its parent.
34
	 *
35
	 * At first, always the HTML code generated by the parent is printed, then
36
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
37
	 * determines the order of the output of these sub-clients inside the parent
38
	 * container. If the configured list of clients is
39
	 *
40
	 *  array( "subclient1", "subclient2" )
41
	 *
42
	 * you can easily change the order of the output by reordering the subparts:
43
	 *
44
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
45
	 *
46
	 * You can also remove one or more parts if they shouldn't be rendered:
47
	 *
48
	 *  client/html/<clients>/subparts = array( "subclient1" )
49
	 *
50
	 * As the clients only generates structural HTML, the layout defined via CSS
51
	 * should support adding, removing or reordering content by a fluid like
52
	 * design.
53
	 *
54
	 * Note: Up to 2015-02, this configuration option was available as
55
	 * client/html/catalog/lists/simple/subparts
56
	 *
57
	 * @param array List of sub-client names
58
	 * @since 2015.02
59
	 * @category Developer
60
	 */
61
	private $subPartPath = 'client/html/catalog/suggest/subparts';
62
	private $subPartNames = [];
63
	private $view;
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
76
		try
77
		{
78
			if( !isset( $this->view ) ) {
79
				$view = $this->view = $this->getObject()->addData( $view );
80
			}
81
82
			$html = '';
83
			foreach( $this->getSubClients() as $subclient ) {
84
				$html .= $subclient->setView( $view )->getBody( $uid );
85
			}
86
			$view->suggestBody = $html;
87
		}
88
		catch( \Exception $e )
89
		{
90
			$this->getContext()->getLogger()->log( $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
91
			return '';
92
		}
93
94
		/** client/html/catalog/suggest/template-body
95
		 * Relative path to the HTML body template of the catalog suggest client.
96
		 *
97
		 * The template file contains the HTML code and processing instructions
98
		 * to generate the result shown in the body of the frontend. The
99
		 * configuration string is the path to the template file relative
100
		 * to the templates directory (usually in client/html/templates).
101
		 *
102
		 * You can overwrite the template file configuration in extensions and
103
		 * provide alternative templates. These alternative templates should be
104
		 * named like the default one but with the string "standard" replaced by
105
		 * an unique name. You may use the name of your project for this. If
106
		 * you've implemented an alternative client class as well, "standard"
107
		 * should be replaced by the name of the new class.
108
		 *
109
		 * Note: Up to 2015-02, this configuration option was available as
110
		 * client/html/catalog/lists/simple/template-body
111
		 *
112
		 * @param string Relative path to the template creating code for the HTML page body
113
		 * @since 2015.02
114
		 * @category Developer
115
		 * @see client/html/catalog/suggest/template-header
116
		 * @see client/html/catalog/suggest/domains
117
		 */
118
		$tplconf = 'client/html/catalog/suggest/template-body';
119
		$default = 'catalog/suggest/body-standard';
120
121
		return $view->render( $view->config( $tplconf, $default ) );
122
	}
123
124
125
	/**
126
	 * Returns the HTML string for insertion into the header.
127
	 *
128
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
129
	 * @return string|null String including HTML tags for the header on error
130
	 */
131
	public function getHeader( string $uid = '' ) : ?string
132
	{
133
		$view = $this->getView();
134
135
		try
136
		{
137
			if( !isset( $this->view ) ) {
138
				$view = $this->view = $this->getObject()->addData( $view );
139
			}
140
141
			$html = '';
142
			foreach( $this->getSubClients() as $subclient ) {
143
				$html .= $subclient->setView( $view )->getHeader( $uid );
144
			}
145
			$view->suggestHeader = $html;
146
		}
147
		catch( \Exception $e )
148
		{
149
			$this->getContext()->getLogger()->log( $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
150
			return null;
151
		}
152
153
		/** client/html/catalog/suggest/template-header
154
		 * Relative path to the HTML header template of the catalog suggest client.
155
		 *
156
		 * The template file contains the HTML code and processing instructions
157
		 * to generate the HTML code that is inserted into the HTML page header
158
		 * of the rendered page in the frontend. The configuration string is the
159
		 * path to the template file relative to the templates directory (usually
160
		 * in client/html/templates).
161
		 *
162
		 * You can overwrite the template file configuration in extensions and
163
		 * provide alternative templates. These alternative templates should be
164
		 * named like the default one but with the string "standard" replaced by
165
		 * an unique name. You may use the name of your project for this. If
166
		 * you've implemented an alternative client class as well, "standard"
167
		 * should be replaced by the name of the new class.
168
		 *
169
		 * Note: Up to 2015-02, this configuration option was available as
170
		 * client/html/catalog/lists/simple/template-header
171
		 *
172
		 * @param string Relative path to the template creating code for the HTML page head
173
		 * @since 2015.02
174
		 * @category Developer
175
		 * @see client/html/catalog/suggest/template-body
176
		 * @see client/html/catalog/suggest/domains
177
		 */
178
		$tplconf = 'client/html/catalog/suggest/template-header';
179
		$default = 'catalog/suggest/header-standard';
180
181
		return $view->render( $view->config( $tplconf, $default ) );
182
	}
183
184
185
	/**
186
	 * Returns the sub-client given by its name.
187
	 *
188
	 * @param string $type Name of the client type
189
	 * @param string|null $name Name of the sub-client (Default if null)
190
	 * @return \Aimeos\Client\Html\Iface Sub-client object
191
	 */
192
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
193
	{
194
		/** client/html/catalog/suggest/decorators/excludes
195
		 * Excludes decorators added by the "common" option from the catalog suggest 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 remove a decorator added via
203
		 * "client/html/common/decorators/default" before they are wrapped
204
		 * around the html client.
205
		 *
206
		 *  client/html/catalog/suggest/decorators/excludes = array( 'decorator1' )
207
		 *
208
		 * This would remove the decorator named "decorator1" from the list of
209
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
210
		 * "client/html/common/decorators/default" to the html client.
211
		 *
212
		 * @param array List of decorator names
213
		 * @since 2015.02
214
		 * @category Developer
215
		 * @see client/html/common/decorators/default
216
		 * @see client/html/catalog/suggest/decorators/global
217
		 * @see client/html/catalog/suggest/decorators/local
218
		 */
219
220
		/** client/html/catalog/suggest/decorators/global
221
		 * Adds a list of globally available decorators only to the catalog suggest html client
222
		 *
223
		 * Decorators extend the functionality of a class by adding new aspects
224
		 * (e.g. log what is currently done), executing the methods of the underlying
225
		 * class only in certain conditions (e.g. only for logged in users) or
226
		 * modify what is returned to the caller.
227
		 *
228
		 * This option allows you to wrap global decorators
229
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
230
		 *
231
		 *  client/html/catalog/suggest/decorators/global = array( 'decorator1' )
232
		 *
233
		 * This would add the decorator named "decorator1" defined by
234
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
235
		 *
236
		 * @param array List of decorator names
237
		 * @since 2015.02
238
		 * @category Developer
239
		 * @see client/html/common/decorators/default
240
		 * @see client/html/catalog/suggest/decorators/excludes
241
		 * @see client/html/catalog/suggest/decorators/local
242
		 */
243
244
		/** client/html/catalog/suggest/decorators/local
245
		 * Adds a list of local decorators only to the catalog suggest html client
246
		 *
247
		 * Decorators extend the functionality of a class by adding new aspects
248
		 * (e.g. log what is currently done), executing the methods of the underlying
249
		 * class only in certain conditions (e.g. only for logged in users) or
250
		 * modify what is returned to the caller.
251
		 *
252
		 * This option allows you to wrap local decorators
253
		 * ("\Aimeos\Client\Html\Catalog\Decorator\*") around the html client.
254
		 *
255
		 *  client/html/catalog/suggest/decorators/local = array( 'decorator2' )
256
		 *
257
		 * This would add the decorator named "decorator2" defined by
258
		 * "\Aimeos\Client\Html\Catalog\Decorator\Decorator2" only to the html client.
259
		 *
260
		 * @param array List of decorator names
261
		 * @since 2015.02
262
		 * @category Developer
263
		 * @see client/html/common/decorators/default
264
		 * @see client/html/catalog/suggest/decorators/excludes
265
		 * @see client/html/catalog/suggest/decorators/global
266
		 */
267
268
		return $this->createSubClient( 'catalog/suggest/' . $type, $name );
269
	}
270
271
272
	/**
273
	 * Processes the input, e.g. store given values.
274
	 *
275
	 * A view must be available and this method doesn't generate any output
276
	 * besides setting view variables if necessary.
277
	 */
278
	public function process()
279
	{
280
		try
281
		{
282
			parent::process();
283
		}
284
		catch( \Exception $e )
285
		{
286
			$this->getContext()->getLogger()->log( $e->getMessage() . PHP_EOL . $e->getTraceAsString() );
287
		}
288
	}
289
290
291
	/**
292
	 * Returns the list of sub-client names configured for the client.
293
	 *
294
	 * @return array List of HTML client names
295
	 */
296
	protected function getSubClientNames() : array
297
	{
298
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
299
	}
300
301
302
	/**
303
	 * Sets the necessary parameter values in the view.
304
	 *
305
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
306
	 * @param array &$tags Result array for the list of tags that are associated to the output
307
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
308
	 * @return \Aimeos\MW\View\Iface Modified view object
309
	 */
310
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
311
	{
312
		$context = $this->getContext();
313
		$config = $context->getConfig();
314
315
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'product' )
316
			->text( $view->param( 'f_search' ) ); // sort by relevance first
317
318
319
		/** client/html/catalog/suggest/domains
320
		 * List of domain items that should be fetched along with the products
321
		 *
322
		 * The suggsted entries for the full text search in the catalog filter component
323
		 * usually consist of the names of the matched products. By default, only the
324
		 * product item including the localized name is available. You can add more domains
325
		 * like e.g. "media" to get the images of the product as well.
326
		 *
327
		 * **Note:** The more domains you will add, the slower the autocomplete requests
328
		 * will be! Keep it to an absolute minium for user friendly response times.
329
		 *
330
		 * @param array List of domain names
331
		 * @since 2016.08
332
		 * @category Developer
333
		 * @see client/html/catalog/suggest/template-body
334
		 * @see client/html/catalog/suggest/restrict
335
		 * @see client/html/catalog/suggest/size
336
		 */
337
		$domains = $config->get( 'client/html/catalog/suggest/domains', ['text'] );
338
339
		/** client/html/catalog/suggest/size
340
		 * The number of products shown in the list of suggestions
341
		 *
342
		 * Limits the number of products that are shown in the list of suggested
343
		 * products.
344
		 *
345
		 * @param integer Number of products
346
		 * @since 2018.10
347
		 * @category Developer
348
		 * @category User
349
		 * @see client/html/catalog/suggest/domains
350
		 * @see client/html/catalog/suggest/restrict
351
		 */
352
		$size = $config->get( 'client/html/catalog/suggest/size', 24 );
353
354
		/** client/html/catalog/suggest/restrict
355
		 * Restricts suggestions to category and attribute facets
356
		 *
357
		 * Limits the shown suggestions to the current category and selected
358
		 * attribute facets. If disabled, suggestions are limited by the
359
		 * entered text only.
360
		 *
361
		 * @param boolean True to use category and facets, false for all results
362
		 * @since 2019.07
363
		 * @category Developer
364
		 * @category User
365
		 * @see client/html/catalog/suggest/domains
366
		 * @see client/html/catalog/suggest/size
367
		 */
368
		if( $config->get( 'client/html/catalog/suggest/restrict', true ) == true )
369
		{
370
			$level = $config->get( 'client/html/catalog/lists/levels', \Aimeos\MW\Tree\Manager\Base::LEVEL_ONE );
371
			$catids = $view->param( 'f_catid', $config->get( 'client/html/catalog/lists/catid-default' ) );
372
373
			$cntl->category( $catids, 'default', $level )
374
				->allOf( $view->param( 'f_attrid', [] ) )
375
				->oneOf( $view->param( 'f_optid', [] ) )
376
				->oneOf( $view->param( 'f_oneid', [] ) );
377
		}
378
379
		$view->suggestItems = $cntl->uses( $domains )->slice( 0, $size )->search();
380
381
		return parent::addData( $view, $tags, $expire );
382
	}
383
}
384