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

Standard   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 245
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getBody() 0 39 3
B getSubClient() 0 78 1
A process() 0 17 4
A getSubClientNames() 0 4 1
A addData() 0 25 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2021
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Account\Subscription\Lists;
12
13
14
/**
15
 * Default implementation of acount subscription list HTML client.
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/account/subscription/lists/subparts
25
	 * List of HTML sub-clients rendered within the account subscription list 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 2018.04
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/account/subscription/lists/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 getBody( string $uid = '' ) : string
68
	{
69
		$view = $this->getView();
70
71
		if( !in_array( $view->param( 'sub_action', 'list' ), ['cancel', 'list'], true ) ) {
72
			return '';
73
		}
74
75
		$html = '';
76
		foreach( $this->getSubClients() as $subclient ) {
77
			$html .= $subclient->setView( $view )->getBody( $uid );
78
		}
79
		$view->listsBody = $html;
80
81
		/** client/html/account/subscription/lists/template-body
82
		 * Relative path to the HTML body template of the account subscription list client.
83
		 *
84
		 * The template file contains the HTML code and processing instructions
85
		 * to generate the result shown in the body of the frontend. The
86
		 * configuration string is the path to the template file relative
87
		 * to the templates directory (usually in client/html/templates).
88
		 *
89
		 * You can overwrite the template file configuration in extensions and
90
		 * provide alternative templates. These alternative templates should be
91
		 * named like the default one but with the string "standard" replaced by
92
		 * an unique name. You may use the name of your project for this. If
93
		 * you've implemented an alternative client class as well, "standard"
94
		 * should be replaced by the name of the new class.
95
		 *
96
		 * @param string Relative path to the template creating code for the HTML page body
97
		 * @since 2018.04
98
		 * @category Developer
99
		 * @see client/html/account/subscription/lists/template-header
100
		 */
101
		$tplconf = 'client/html/account/subscription/lists/template-body';
102
		$default = 'account/subscription/lists-body-standard';
103
104
		return $view->render( $view->config( $tplconf, $default ) );
105
	}
106
107
108
	/**
109
	 * Returns the sub-client given by its name.
110
	 *
111
	 * @param string $type Name of the client type
112
	 * @param string|null $name Name of the sub-client (Default if null)
113
	 * @return \Aimeos\Client\Html\Iface Sub-client object
114
	 */
115
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
116
	{
117
		/** client/html/account/subscription/lists/decorators/excludes
118
		 * Excludes decorators added by the "common" option from the account subscription list html client
119
		 *
120
		 * Decorators extend the functionality of a class by adding new aspects
121
		 * (e.g. log what is currently done), executing the methods of the underlying
122
		 * class only in certain conditions (e.g. only for logged in users) or
123
		 * modify what is returned to the caller.
124
		 *
125
		 * This option allows you to remove a decorator added via
126
		 * "client/html/common/decorators/default" before they are wrapped
127
		 * around the html client.
128
		 *
129
		 *  client/html/account/subscription/lists/decorators/excludes = array( 'decorator1' )
130
		 *
131
		 * This would remove the decorator named "decorator1" from the list of
132
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
133
		 * "client/html/common/decorators/default" to the html client.
134
		 *
135
		 * @param array List of decorator names
136
		 * @since 2018.04
137
		 * @category Developer
138
		 * @see client/html/common/decorators/default
139
		 * @see client/html/account/subscription/lists/decorators/global
140
		 * @see client/html/account/subscription/lists/decorators/local
141
		 */
142
143
		/** client/html/account/subscription/lists/decorators/global
144
		 * Adds a list of globally available decorators only to the account subscription list html client
145
		 *
146
		 * Decorators extend the functionality of a class by adding new aspects
147
		 * (e.g. log what is currently done), executing the methods of the underlying
148
		 * class only in certain conditions (e.g. only for logged in users) or
149
		 * modify what is returned to the caller.
150
		 *
151
		 * This option allows you to wrap global decorators
152
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
153
		 *
154
		 *  client/html/account/subscription/lists/decorators/global = array( 'decorator1' )
155
		 *
156
		 * This would add the decorator named "decorator1" defined by
157
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
158
		 *
159
		 * @param array List of decorator names
160
		 * @since 2018.04
161
		 * @category Developer
162
		 * @see client/html/common/decorators/default
163
		 * @see client/html/account/subscription/lists/decorators/excludes
164
		 * @see client/html/account/subscription/lists/decorators/local
165
		 */
166
167
		/** client/html/account/subscription/lists/decorators/local
168
		 * Adds a list of local decorators only to the account subscription list html client
169
		 *
170
		 * Decorators extend the functionality of a class by adding new aspects
171
		 * (e.g. log what is currently done), executing the methods of the underlying
172
		 * class only in certain conditions (e.g. only for logged in users) or
173
		 * modify what is returned to the caller.
174
		 *
175
		 * This option allows you to wrap local decorators
176
		 * ("\Aimeos\Client\Html\Account\Decorator\*") around the html client.
177
		 *
178
		 *  client/html/account/subscription/lists/decorators/local = array( 'decorator2' )
179
		 *
180
		 * This would add the decorator named "decorator2" defined by
181
		 * "\Aimeos\Client\Html\Account\Decorator\Decorator2" only to the html client.
182
		 *
183
		 * @param array List of decorator names
184
		 * @since 2018.04
185
		 * @category Developer
186
		 * @see client/html/common/decorators/default
187
		 * @see client/html/account/subscription/lists/decorators/excludes
188
		 * @see client/html/account/subscription/lists/decorators/global
189
		 */
190
191
		return $this->createSubClient( 'account/subscription/lists/' . $type, $name );
192
	}
193
194
195
	/**
196
	 * Processes the input, e.g. store given values.
197
	 *
198
	 * A view must be available and this method doesn't generate any output
199
	 * besides setting view variables if necessary.
200
	 */
201
	public function process()
202
	{
203
		try
204
		{
205
			$view = $this->getView();
206
207
			if( ( $id = $view->param( 'sub_id' ) ) != null && $view->param( 'sub_action' ) === 'cancel' ) {
208
				\Aimeos\Controller\Frontend::create( $this->getContext(), 'subscription' )->cancel( $id );
209
			}
210
211
			parent::process();
212
		}
213
		catch( \Exception $e )
214
		{
215
			$this->logException( $e );
216
		}
217
	}
218
219
220
	/**
221
	 * Returns the list of sub-client names configured for the client.
222
	 *
223
	 * @return array List of HTML client names
224
	 */
225
	protected function getSubClientNames() : array
226
	{
227
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
228
	}
229
230
231
	/**
232
	 * Sets the necessary parameter values in the view.
233
	 *
234
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
235
	 * @param array &$tags Result array for the list of tags that are associated to the output
236
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
237
	 * @return \Aimeos\MW\View\Iface Modified view object
238
	 */
239
	public function addData( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
240
	{
241
		$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'subscription' );
242
243
		$list = [];
244
		$items = $cntl->search();
245
		$map = $items->col( null, 'subscription.ordbaseid' );
246
247
		$orderCntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'order' );
248
		$orderCntl->compare( '==', 'order.baseid', $map->keys()->toArray() );
249
250
		foreach( $orderCntl->search() as $item )
251
		{
252
			$subscription = $map[$item->getBaseId()];
253
254
			if( $subscription && $item->getPaymentStatus() >= \Aimeos\MShop\Order\Item\Base::PAY_AUTHORIZED ) {
255
				$list[$subscription->getId()] = $subscription;
256
			}
257
		}
258
259
		$view->listsItems = map( $list );
260
		$view->listsIntervalItems = $cntl->getIntervals();
261
262
		return parent::addData( $view, $tags, $expire );
263
	}
264
}
265