Passed
Push — master ( c06d6f...29da27 )
by Aimeos
04:29
created

Standard::init()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 4
nop 0
dl 0
loc 14
rs 9.2222
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\Favorite;
13
14
15
/**
16
 * Default implementation of account favorite 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( 'fav_id', [] );
36
37
		if( $context->user() !== null && !empty( $ids ) && $view->request()->getMethod() === 'POST' )
38
		{
39
			switch( $view->param( 'fav_action' ) )
40
			{
41
				case 'add':
42
					$this->addFavorites( $ids ); break;
43
				case 'delete':
44
					$this->deleteFavorites( $ids ); break;
45
			}
46
		}
47
	}
48
49
50
	/**
51
	 * Adds new product favorite references to the given customer
52
	 *
53
	 * @param array $ids List of product IDs
54
	 */
55
	protected function addFavorites( array $ids )
56
	{
57
		$context = $this->context();
58
59
		/** client/html/account/favorite/maxitems
60
		 * Maximum number of products that can be favorites
61
		 *
62
		 * This option limits the number of products users can add to their
63
		 * favorite list. It must be a positive integer value greater than 0.
64
		 *
65
		 * @param integer Number of products
66
		 * @since 2019.04
67
		 * @category User
68
		 * @category Developer
69
		 */
70
		$max = $context->config()->get( 'client/html/account/favorite/maxitems', 100 );
71
72
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'customer' );
73
		$item = $cntl->uses( ['product' => ['favorite']] )->get();
74
75
		if( count( $item->getRefItems( 'product', null, 'favorite' ) ) + count( $ids ) > $max )
76
		{
77
			$msg = sprintf( $context->translate( 'client', 'You can only save up to %1$s products as favorites' ), $max );
78
			throw new \Aimeos\Client\Html\Exception( $msg );
79
		}
80
81
		foreach( $ids as $id )
82
		{
83
			if( ( $listItem = $item->getListItem( 'product', 'favorite', $id ) ) === null ) {
84
				$listItem = $cntl->createListItem();
85
			}
86
			$cntl->addListItem( 'product', $listItem->setType( 'favorite' )->setRefId( $id ) );
87
		}
88
89
		$cntl->store();
90
	}
91
92
93
	/**
94
	 * Removes product favorite references from the customer
95
	 *
96
	 * @param array $ids List of product IDs
97
	 */
98
	protected function deleteFavorites( array $ids )
99
	{
100
		$cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'customer' );
101
		$item = $cntl->uses( ['product' => ['favorite']] )->get();
102
103
		foreach( $ids as $id )
104
		{
105
			if( ( $listItem = $item->getListItem( 'product', 'favorite', $id ) ) !== null ) {
106
				$cntl->deleteListItem( 'product', $listItem );
107
			}
108
		}
109
110
		$cntl->store();
111
	}
112
113
114
	/**
115
	 * Returns the sanitized page from the parameters for the product list.
116
	 *
117
	 * @param \Aimeos\MW\View\Iface $view View instance with helper for retrieving the required parameters
118
	 * @return int Page number starting from 1
119
	 */
120
	protected function getProductListPage( \Aimeos\MW\View\Iface $view ) : int
121
	{
122
		$page = (int) $view->param( 'fav_page', 1 );
123
		return ( $page < 1 ? 1 : $page );
124
	}
125
126
127
	/**
128
	 * Returns the sanitized page size from the parameters for the product list.
129
	 *
130
	 * @param \Aimeos\MW\View\Iface $view View instance with helper for retrieving the required parameters
131
	 * @return int Page size
132
	 */
133
	protected function getProductListSize( \Aimeos\MW\View\Iface $view ) : int
134
	{
135
		/** client/html/account/favorite/size
136
		 * The number of products shown in a list page for favorite products
137
		 *
138
		 * Limits the number of products that is shown in the list pages to the
139
		 * given value. If more products are available, the products are split
140
		 * into bunches which will be shown on their own list page. The user is
141
		 * able to move to the next page (or previous one if it's not the first)
142
		 * to display the next (or previous) products.
143
		 *
144
		 * The value must be an integer number from 1 to 100. Negative values as
145
		 * well as values above 100 are not allowed. The value can be overwritten
146
		 * per request if the "l_size" parameter is part of the URL.
147
		 *
148
		 * @param integer Number of products
149
		 * @since 2014.09
150
		 * @category User
151
		 * @category Developer
152
		 * @see client/html/catalog/lists/size
153
		 */
154
		$defaultSize = $this->context()->config()->get( 'client/html/account/favorite/size', 48 );
155
156
		$size = (int) $view->param( 'fav-size', $defaultSize );
157
		return ( $size < 1 || $size > 100 ? $defaultSize : $size );
158
	}
159
160
161
	/**
162
	 * Sets the necessary parameter values in the view.
163
	 *
164
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
165
	 * @param array &$tags Result array for the list of tags that are associated to the output
166
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
167
	 * @return \Aimeos\MW\View\Iface Modified view object
168
	 */
169
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
170
	{
171
		$context = $this->context();
172
173
		/** client/html/account/favorite/domains
174
		 * A list of domain names whose items should be available in the account favorite view template
175
		 *
176
		 * The templates rendering product details usually add the images,
177
		 * prices and texts associated to the product item. If you want to
178
		 * display additional or less content, you can configure your own
179
		 * list of domains (attribute, media, price, product, text, etc. are
180
		 * domains) whose items are fetched from the storage. Please keep
181
		 * in mind that the more domains you add to the configuration, the
182
		 * more time is required for fetching the content!
183
		 *
184
		 * @param array List of domain names
185
		 * @since 2014.09
186
		 * @category Developer
187
		 * @see client/html/catalog/domains
188
		 */
189
		$domains = $context->config()->get( 'client/html/account/favorite/domains', ['text', 'price', 'media'] );
190
		$domains['product'] = ['favorite'];
191
192
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'customer' );
193
		$listItems = $cntl->uses( $domains )->get()->getListItems( 'product', 'favorite' );
194
		$total = count( $listItems );
195
196
		$size = $this->getProductListSize( $view );
197
		$current = $this->getProductListPage( $view );
198
		$last = ( $total != 0 ? ceil( $total / $size ) : 1 );
199
200
		$view->favoriteItems = $listItems;
201
		$view->favoritePageFirst = 1;
202
		$view->favoritePagePrev = ( $current > 1 ? $current - 1 : 1 );
203
		$view->favoritePageNext = ( $current < $last ? $current + 1 : $last );
204
		$view->favoritePageLast = $last;
205
		$view->favoritePageCurr = $current;
206
207
		return parent::data( $view, $tags, $expire );
208
	}
209
}
210