Passed
Push — master ( 8bf70e...6a7093 )
by Aimeos
03:42
created

Standard::getSubClientNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Catalog\Session\Pinned;
13
14
15
/**
16
 * Default implementation of catalog session pinned section for 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
	/**
26
	 * Returns the HTML code for insertion into the body.
27
	 *
28
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
29
	 * @return string HTML code
30
	 */
31
	public function body( string $uid = '' ) : string
32
	{
33
		$view = $this->view();
34
		$context = $this->context();
35
		$session = $context->session();
36
37
		/** client/html/catalog/session/pinned
38
		 * All parameters defined for the catalog session pinned subpart
39
		 *
40
		 * This returns all settings related to the catalog session pinned subpart.
41
		 * Please refer to the single settings for details.
42
		 *
43
		 * @param array Associative list of name/value settings
44
		 * @see client/html/catalog/session#pinned
45
		 */
46
		$config = $context->config()->get( 'client/html/catalog/session/pinned', [] );
47
		$key = $this->getParamHash( [], $uid . ':catalog:session-pinned-body', $config );
48
49
		if( ( $html = $session->get( $key ) ) === null )
50
		{
51
			/** client/html/catalog/session/pinned/template-body
52
			 * Relative path to the HTML body template of the catalog session pinned client.
53
			 *
54
			 * The template file contains the HTML code and processing instructions
55
			 * to generate the result shown in the body of the frontend. The
56
			 * configuration string is the path to the template file relative
57
			 * to the templates directory (usually in client/html/templates).
58
			 *
59
			 * You can overwrite the template file configuration in extensions and
60
			 * provide alternative templates. These alternative templates should be
61
			 * named like the default one but suffixed by
62
			 * an unique name. You may use the name of your project for this. If
63
			 * you've implemented an alternative client class as well, it
64
			 * should be suffixed by the name of the new class.
65
			 *
66
			 * @param string Relative path to the template creating code for the HTML page body
67
			 * @since 2014.03
68
			 * @see client/html/catalog/session/pinned/template-header
69
			 */
70
			$tplconf = 'client/html/catalog/session/pinned/template-body';
71
			$default = 'catalog/session/pinned-body';
72
73
			$html = $view->render( $view->config( $tplconf, $default ) );
74
75
			$cached = $session->get( 'aimeos/catalog/session/pinned/cache', [] ) + array( $key => true );
76
			$session->set( 'aimeos/catalog/session/pinned/cache', $cached );
77
			$session->set( $key, $html );
78
		}
79
80
		$view->block()->set( 'catalog/session/pinned', $html );
81
82
		return $html;
83
	}
84
85
86
	/**
87
	 * Processes the input, e.g. store given values.
88
	 *
89
	 * A view must be available and this method doesn't generate any output
90
	 * besides setting view variables if necessary.
91
	 */
92
	public function init()
93
	{
94
		$refresh = false;
95
		$view = $this->view();
96
		$context = $this->context();
97
		$session = $context->session();
98
		$pinned = $session->get( 'aimeos/catalog/session/pinned/list', [] );
99
100
		if( $view->request()->getMethod() === 'POST' )
101
		{
102
			switch( $view->param( 'pin_action' ) )
103
			{
104
				case 'add':
105
106
					foreach( (array) $view->param( 'pin_id', [] ) as $id ) {
107
						$pinned[$id] = $id;
108
					}
109
110
					/** client/html/catalog/session/pinned/maxitems
111
					 * Maximum number of products displayed in the "pinned" section
112
					 *
113
					 * This option limits the number of products that are shown in the
114
					 * "pinned" section after the users added the product to their list
115
					 * of pinned products. It must be a positive integer value greater
116
					 * than 0.
117
					 *
118
					 * Note: The higher the value is the more data has to be transfered
119
					 * to the client each time the user loads a page with the list of
120
					 * pinned products.
121
					 *
122
					 * @param integer Number of products
123
					 * @since 2014.09
124
					 */
125
					$max = $context->config()->get( 'client/html/catalog/session/pinned/maxitems', 50 );
126
127
					$pinned = array_slice( $pinned, -$max, $max, true );
128
					$refresh = true;
129
					break;
130
131
				case 'delete':
132
133
					foreach( (array) $view->param( 'pin_id', [] ) as $id ) {
134
						unset( $pinned[$id] );
135
					}
136
137
					$refresh = true;
138
					break;
139
			}
140
		}
141
142
143
		if( $refresh )
144
		{
145
			$session->set( 'aimeos/catalog/session/pinned/list', $pinned );
146
147
			foreach( $session->get( 'aimeos/catalog/session/pinned/cache', [] ) as $key => $value ) {
148
				$session->set( $key, null );
149
			}
150
		}
151
	}
152
153
154
	/**
155
	 * Sets the necessary parameter values in the view.
156
	 *
157
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
158
	 * @param array &$tags Result array for the list of tags that are associated to the output
159
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
160
	 * @return \Aimeos\MW\View\Iface Modified view object
161
	 */
162
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
163
	{
164
		$items = [];
165
		$context = $this->context();
166
		$config = $context->config();
167
		$session = $context->session();
168
169
		$domains = $config->get( 'client/html/catalog/domains', ['media', 'price', 'text'] );
170
171
		/** client/html/catalog/session/pinned/domains
172
		 * A list of domain names whose items should be available in the pinned view template for the product
173
		 *
174
		 * The templates rendering product details usually add the images,
175
		 * prices and texts, etc. associated to the product
176
		 * item. If you want to display additional or less content, you can
177
		 * configure your own list of domains (attribute, media, price, product,
178
		 * text, etc. are domains) whose items are fetched from the storage.
179
		 * Please keep in mind that the more domains you add to the configuration,
180
		 * the more time is required for fetching the content!
181
		 *
182
		 * @param array List of domain names
183
		 * @since 2015.04
184
		 * @see client/html/catalog/domains
185
		 * @see client/html/catalog/lists/domains
186
		 * @see client/html/catalog/detail/domains
187
		 */
188
		$domains = $config->get( 'client/html/catalog/session/pinned/domains', $domains );
189
190
		if( ( $pinned = $session->get( 'aimeos/catalog/session/pinned/list', [] ) ) !== [] )
191
		{
192
			$result = \Aimeos\Controller\Frontend::create( $context, 'product' )
193
				->uses( $domains )->product( $pinned )->slice( 0, count( $pinned ) )->search();
194
195
			foreach( array_reverse( $pinned ) as $id )
196
			{
197
				if( isset( $result[$id] ) ) {
198
					$items[$id] = $result[$id];
199
				}
200
			}
201
		}
202
203
		$view->pinnedProductItems = $items;
204
		$view->pinnedParams = $this->getClientParams( $view->param() );
205
206
		return parent::data( $view, $tags, $expire );
207
	}
208
}
209