Passed
Push — master ( 29da27...7ca054 )
by Aimeos
03:58
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\Account\Watch;
13
14
15
/**
16
 * Default implementation of account watch HTML client.
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
	 * Processes the input, e.g. store given values.
27
	 *
28
	 * A view must be available and this method doesn't generate any output
29
	 * besides setting view variables if necessary.
30
	 */
31
	public function init()
32
	{
33
		$view = $this->view();
34
		$context = $this->context();
35
		$ids = (array) $view->param( 'wat_id', [] );
36
37
		if( $context->user() !== null && !empty( $ids ) && $view->request()->getMethod() === 'POST' )
38
		{
39
			switch( $view->param( 'wat_action' ) )
40
			{
41
				case 'add':
42
					$this->addItems( $view, $ids ); break;
43
				case 'edit':
44
					$this->editItems( $view, $ids ); break;
45
				case 'delete':
46
					$this->deleteItems( $view, $ids ); break;
47
			}
48
		}
49
	}
50
51
52
	/**
53
	 * Adds one or more list items to the given customer item
54
	 *
55
	 * @param \Aimeos\MW\View\Iface $view View object
56
	 * @param array $ids List of referenced IDs
57
	 */
58
	protected function addItems( \Aimeos\MW\View\Iface $view, array $ids )
59
	{
60
		$context = $this->context();
61
62
		/** client/html/account/watch/maxitems
63
		 * Maximum number of products that can be watched in parallel
64
		 *
65
		 * This option limits the number of products that can be watched
66
		 * after the users added the products to their watch list.
67
		 * It must be a positive integer value greater than 0.
68
		 *
69
		 * Note: It's recommended to set this value not too high as this
70
		 * leads to a high memory consumption when the e-mails are generated
71
		 * to notify the customers. The memory used will up to 100*maxitems
72
		 * of the footprint of one product item including the associated
73
		 * texts, prices and media.
74
		 *
75
		 * @param integer Number of products
76
		 * @since 2014.09
77
		 * @category User
78
		 * @category Developer
79
		 */
80
		$max = $context->config()->get( 'client/html/account/watch/maxitems', 100 );
81
82
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'customer' );
83
		$item = $cntl->uses( ['product' => ['watch']] )->get();
84
85
		if( count( $item->getRefItems( 'product', null, 'watch' ) ) + count( $ids ) > $max )
86
		{
87
			$msg = sprintf( $context->translate( 'client', 'You can only watch up to %1$s products' ), $max );
88
			throw new \Aimeos\Client\Html\Exception( $msg );
89
		}
90
91
		foreach( $ids as $id )
92
		{
93
			if( ( $listItem = $item->getListItem( 'product', 'watch', $id ) ) === null ) {
94
				$listItem = $cntl->createListItem();
95
			}
96
			$cntl->addListItem( 'product', $listItem->setType( 'watch' )->setRefId( $id ) );
97
		}
98
99
		$cntl->store();
100
	}
101
102
103
	/**
104
	 * Removes the referencing list items from the given item
105
	 *
106
	 * @param \Aimeos\MW\View\Iface $view View object
107
	 * @param array $ids List of referenced IDs
108
	 */
109
	protected function deleteItems( \Aimeos\MW\View\Iface $view, array $ids )
110
	{
111
		$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'customer' );
112
		$item = $cntl->uses( ['product' => ['watch']] )->get();
113
114
		foreach( $ids as $id )
115
		{
116
			if( ( $listItem = $item->getListItem( 'product', 'watch', $id ) ) !== null ) {
117
				$cntl->deleteListItem( 'product', $listItem );
118
			}
119
		}
120
121
		$cntl->store();
122
	}
123
124
125
	/**
126
	 * Updates the item using the given reference IDs
127
	 *
128
	 * @param \Aimeos\MW\View\Iface $view View object
129
	 * @param array $ids List of referenced IDs
130
	 */
131
	protected function editItems( \Aimeos\MW\View\Iface $view, array $ids )
132
	{
133
		$context = $this->context();
134
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'customer' );
135
		$item = $cntl->uses( ['product' => ['watch']] )->get();
136
137
		$config = [
138
			'timeframe' => $view->param( 'wat_timeframe', 7 ),
139
			'pricevalue' => $view->param( 'wat_pricevalue', '0.00' ),
140
			'price' => $view->param( 'wat_price', 0 ),
141
			'stock' => $view->param( 'wat_stock', 0 ),
142
			'currency' => $context->locale()->getCurrencyId(),
143
		];
144
145
		foreach( $ids as $id )
146
		{
147
			if( ( $listItem = $item->getListItem( 'product', 'watch', $id ) ) !== null )
148
			{
149
				$time = time() + ( $config['timeframe'] + 1 ) * 86400;
150
				$listItem = $listItem->setDateEnd( date( 'Y-m-d 00:00:00', $time ) )->setConfig( $config );
151
152
				$cntl->addListItem( 'product', $listItem );
153
			}
154
		}
155
156
		$cntl->store();
157
	}
158
159
160
	/**
161
	 * Returns the sanitized page from the parameters for the product list.
162
	 *
163
	 * @param \Aimeos\MW\View\Iface $view View instance with helper for retrieving the required parameters
164
	 * @return integer Page number starting from 1
165
	 */
166
	protected function getProductListPage( \Aimeos\MW\View\Iface $view ) : int
167
	{
168
		$page = (int) $view->param( 'wat_page', 1 );
169
		return ( $page < 1 ? 1 : $page );
170
	}
171
172
173
	/**
174
	 * Returns the sanitized page size from the parameters for the product list.
175
	 *
176
	 * @param \Aimeos\MW\View\Iface $view View instance with helper for retrieving the required parameters
177
	 * @return integer Page size
178
	 */
179
	protected function getProductListSize( \Aimeos\MW\View\Iface $view ) : int
180
	{
181
		/** client/html/account/watch/size
182
		 * The number of products shown in a list page for watch products
183
		 *
184
		 * Limits the number of products that is shown in the list pages to the
185
		 * given value. If more products are available, the products are split
186
		 * into bunches which will be shown on their own list page. The user is
187
		 * able to move to the next page (or previous one if it's not the first)
188
		 * to display the next (or previous) products.
189
		 *
190
		 * The value must be an integer number from 1 to 100. Negative values as
191
		 * well as values above 100 are not allowed. The value can be overwritten
192
		 * per request if the "l_size" parameter is part of the URL.
193
		 *
194
		 * @param integer Number of products
195
		 * @since 2014.09
196
		 * @category User
197
		 * @category Developer
198
		 * @see client/html/catalog/lists/size
199
		 */
200
		$defaultSize = $this->context()->config()->get( 'client/html/account/watch/size', 48 );
201
202
		$size = (int) $view->param( 'watch-size', $defaultSize );
203
		return ( $size < 1 || $size > 100 ? $defaultSize : $size );
204
	}
205
206
207
	/**
208
	 * Sets the necessary parameter values in the view.
209
	 *
210
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
211
	 * @param array &$tags Result array for the list of tags that are associated to the output
212
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
213
	 * @return \Aimeos\MW\View\Iface Modified view object
214
	 */
215
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
216
	{
217
		$context = $this->context();
218
219
		/** client/html/account/watch/domains
220
		 * A list of domain names whose items should be available in the account watch view template
221
		 *
222
		 * The templates rendering product details usually add the images,
223
		 * prices and texts associated to the product item. If you want to
224
		 * display additional or less content, you can configure your own
225
		 * list of domains (attribute, media, price, product, text, etc. are
226
		 * domains) whose items are fetched from the storage. Please keep
227
		 * in mind that the more domains you add to the configuration, the
228
		 * more time is required for fetching the content!
229
		 *
230
		 * @param array List of domain names
231
		 * @since 2014.09
232
		 * @category Developer
233
		 * @see client/html/catalog/domains
234
		 */
235
		$domains = $context->config()->get( 'client/html/account/watch/domains', ['text', 'price', 'media'] );
236
		$domains['product'] = ['watch'];
237
238
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'customer' );
239
		$listItems = $cntl->uses( $domains )->get()->getListItems( 'product', 'watch' );
240
		$total = count( $listItems );
241
242
		$size = $this->getProductListSize( $view );
243
		$current = $this->getProductListPage( $view );
244
		$last = ( $total != 0 ? ceil( $total / $size ) : 1 );
245
246
		$view->watchItems = $listItems;
247
		$view->watchPageFirst = 1;
248
		$view->watchPagePrev = ( $current > 1 ? $current - 1 : 1 );
249
		$view->watchPageNext = ( $current < $last ? $current + 1 : $last );
250
		$view->watchPageLast = $last;
251
		$view->watchPageCurr = $current;
252
253
		return parent::data( $view, $tags, $expire );
254
	}
255
}
256