Passed
Branch master (d032f7)
by Aimeos
05:30
created

Standard   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 299
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 299
rs 10
c 0
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 3 1
A getOrderItems() 0 15 1
A getSubClientNames() 0 36 1
A render() 0 25 1
A getSubClient() 0 76 1
A copy() 0 3 1
A create() 0 3 1
A get() 0 34 4
A getOrderBaseItems() 0 14 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2018
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Customer\Order;
12
13
sprintf( 'order' ); // for translation
14
15
16
/**
17
 * Default implementation of customer order JQAdm client.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
class Standard
23
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
24
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
25
{
26
	/** admin/jqadm/customer/order/name
27
	 * Name of the order subpart used by the JQAdm customer implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Customer\Address\Myname".
30
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
31
	 *
32
	 * @param string Last part of the JQAdm class name
33
	 * @since 2017.06
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Copies a resource
40
	 *
41
	 * @return string HTML output
42
	 */
43
	public function copy()
44
	{
45
		return $this->get();
46
	}
47
48
49
	/**
50
	 * Creates a new resource
51
	 *
52
	 * @return string HTML output
53
	 */
54
	public function create()
55
	{
56
		return $this->get();
57
	}
58
59
60
	/**
61
	 * Returns a single resource
62
	 *
63
	 * @return string HTML output
64
	 */
65
	public function get()
66
	{
67
		$view = $this->getView();
68
69
		try
70
		{
71
			$total = 0;
72
			$params = $this->storeSearchParams( $view->param( 'uo', [] ), 'customerorder' );
73
			$orderItems = $this->getOrderItems( $view->item, $params, $total );
74
			$baseItems = $this->getOrderBaseItems( $orderItems );
75
76
			$view->orderItems = $orderItems;
77
			$view->orderBaseItems = $baseItems;
78
			$view->orderTotal = $total;
79
			$view->orderBody = '';
80
81
			foreach( $this->getSubClients() as $client ) {
82
				$view->orderBody .= $client->search();
83
			}
84
		}
85
		catch( \Aimeos\MShop\Exception $e )
86
		{
87
			$error = array( 'customer-order' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) );
88
			$view->errors = $view->get( 'errors', [] ) + $error;
89
			$this->logException( $e );
90
		}
91
		catch( \Exception $e )
92
		{
93
			$error = array( 'customer-order' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
94
			$view->errors = $view->get( 'errors', [] ) + $error;
95
			$this->logException( $e );
96
		}
97
98
		return $this->render( $view );
99
	}
100
101
102
	/**
103
	 * Saves the data
104
	 */
105
	public function save()
106
	{
107
		return $this->get();
108
	}
109
110
111
	/**
112
	 * Returns the sub-client given by its name.
113
	 *
114
	 * @param string $type Name of the client type
115
	 * @param string|null $name Name of the sub-client (Default if null)
116
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
117
	 */
118
	public function getSubClient( $type, $name = null )
119
	{
120
		/** admin/jqadm/customer/order/decorators/excludes
121
		 * Excludes decorators added by the "common" option from the customer JQAdm client
122
		 *
123
		 * Decorators extend the functionality of a class by adding new aspects
124
		 * (e.g. log what is currently done), executing the methods of the underlying
125
		 * class only in certain conditions (e.g. only for logged in users) or
126
		 * modify what is returned to the caller.
127
		 *
128
		 * This option allows you to remove a decorator added via
129
		 * "admin/jqadm/common/decorators/default" before they are wrapped
130
		 * around the JQAdm client.
131
		 *
132
		 *  admin/jqadm/customer/order/decorators/excludes = array( 'decorator1' )
133
		 *
134
		 * This would remove the decorator named "decorator1" from the list of
135
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
136
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
137
		 *
138
		 * @param array List of decorator names
139
		 * @since 2017.07
140
		 * @category Developer
141
		 * @see admin/jqadm/common/decorators/default
142
		 * @see admin/jqadm/customer/order/decorators/global
143
		 * @see admin/jqadm/customer/order/decorators/local
144
		 */
145
146
		/** admin/jqadm/customer/order/decorators/global
147
		 * Adds a list of globally available decorators only to the customer JQAdm client
148
		 *
149
		 * Decorators extend the functionality of a class by adding new aspects
150
		 * (e.g. log what is currently done), executing the methods of the underlying
151
		 * class only in certain conditions (e.g. only for logged in users) or
152
		 * modify what is returned to the caller.
153
		 *
154
		 * This option allows you to wrap global decorators
155
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
156
		 *
157
		 *  admin/jqadm/customer/order/decorators/global = array( 'decorator1' )
158
		 *
159
		 * This would add the decorator named "decorator1" defined by
160
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
161
		 *
162
		 * @param array List of decorator names
163
		 * @since 2017.07
164
		 * @category Developer
165
		 * @see admin/jqadm/common/decorators/default
166
		 * @see admin/jqadm/customer/order/decorators/excludes
167
		 * @see admin/jqadm/customer/order/decorators/local
168
		 */
169
170
		/** admin/jqadm/customer/order/decorators/local
171
		 * Adds a list of local decorators only to the customer JQAdm client
172
		 *
173
		 * Decorators extend the functionality of a class by adding new aspects
174
		 * (e.g. log what is currently done), executing the methods of the underlying
175
		 * class only in certain conditions (e.g. only for logged in users) or
176
		 * modify what is returned to the caller.
177
		 *
178
		 * This option allows you to wrap local decorators
179
		 * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client.
180
		 *
181
		 *  admin/jqadm/customer/order/decorators/local = array( 'decorator2' )
182
		 *
183
		 * This would add the decorator named "decorator2" defined by
184
		 * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client.
185
		 *
186
		 * @param array List of decorator names
187
		 * @since 2017.07
188
		 * @category Developer
189
		 * @see admin/jqadm/common/decorators/default
190
		 * @see admin/jqadm/customer/order/decorators/excludes
191
		 * @see admin/jqadm/customer/order/decorators/global
192
		 */
193
		return $this->createSubClient( 'customer/order/' . $type, $name );
194
	}
195
196
197
	/**
198
	 * Returns the basket items for the given orders
199
	 *
200
	 * @param \Aimeos\MShop\Order\Item\Iface[] $items Order item objects
201
	 * @return \Aimeos\MShop\Order\Item\Base\Iface[] Basket items
202
	 */
203
	protected function getOrderBaseItems( array $items )
204
	{
205
		$ids = [];
206
207
		foreach( $items as $item ) {
208
			$ids[] = $item->getBaseId();
209
		}
210
211
		$manager = \Aimeos\MShop::create( $this->getContext(), 'order/base' );
212
213
		$search = $manager->createSearch()->setSlice( 0, count( $ids ) );
214
		$search->setConditions( $search->compare( '==', 'order.base.id', $ids ) );
215
216
		return $manager->searchItems( $search );
217
	}
218
219
220
	/**
221
	 * Returns the order items of the customer
222
	 *
223
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
224
	 * @param array $params Associative list of GET/POST parameters
225
	 * @param integer $total Value/result parameter that will contain the item total afterwards
226
	 * @return \Aimeos\MShop\Order\Item\Iface[] Order items of the customer
227
	 */
228
	protected function getOrderItems( \Aimeos\MShop\Customer\Item\Iface $item, array $params = [], &$total )
229
	{
230
		$manager = \Aimeos\MShop::create( $this->getContext(), 'order' );
231
232
		$search = $manager->createSearch();
233
		$search->setSortations( [$search->sort( '-', 'order.ctime' )] );
234
235
		$search = $this->initCriteria( $search, $params );
236
		$expr = [
237
			$search->compare( '==', 'order.base.customerid', $item->getId() ),
238
			$search->getConditions(),
239
		];
240
		$search->setConditions( $search->combine( '&&', $expr ) );
241
242
		return $manager->searchItems( $search, [], $total );
243
	}
244
245
246
	/**
247
	 * Returns the list of sub-client names configured for the client.
248
	 *
249
	 * @return array List of JQAdm client names
250
	 */
251
	protected function getSubClientNames()
252
	{
253
		/** admin/jqadm/customer/order/standard/subparts
254
		 * List of JQAdm sub-clients rendered within the customer order section
255
		 *
256
		 * The output of the frontend is composed of the code generated by the JQAdm
257
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
258
		 * that are responsible for rendering certain sub-parts of the output. The
259
		 * sub-clients can contain JQAdm clients themselves and therefore a
260
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
261
		 * the output that is placed inside the container of its parent.
262
		 *
263
		 * At first, always the JQAdm code generated by the parent is printed, then
264
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
265
		 * determines the order of the output of these sub-clients inside the parent
266
		 * container. If the configured list of clients is
267
		 *
268
		 *  array( "subclient1", "subclient2" )
269
		 *
270
		 * you can easily change the order of the output by reordering the subparts:
271
		 *
272
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
273
		 *
274
		 * You can also remove one or more parts if they shouldn't be rendered:
275
		 *
276
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
277
		 *
278
		 * As the clients only generates structural JQAdm, the layout defined via CSS
279
		 * should support adding, removing or reordering content by a fluid like
280
		 * design.
281
		 *
282
		 * @param array List of sub-client names
283
		 * @since 2017.07
284
		 * @category Developer
285
		 */
286
		return $this->getContext()->getConfig()->get( 'admin/jqadm/customer/order/standard/subparts', [] );
287
	}
288
289
290
	/**
291
	 * Returns the rendered template including the view data
292
	 *
293
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
294
	 * @return string HTML output
295
	 */
296
	protected function render( \Aimeos\MW\View\Iface $view )
297
	{
298
		/** admin/jqadm/customer/order/template-item
299
		 * Relative path to the HTML body template of the order subpart for customers.
300
		 *
301
		 * The template file contains the HTML code and processing instructions
302
		 * to generate the result shown in the body of the frontend. The
303
		 * configuration string is the path to the template file relative
304
		 * to the templates directory (usually in admin/jqadm/templates).
305
		 *
306
		 * You can overwrite the template file configuration in extensions and
307
		 * provide alternative templates. These alternative templates should be
308
		 * named like the default one but with the string "default" replaced by
309
		 * an unique name. You may use the name of your project for this. If
310
		 * you've implemented an alternative client class as well, "default"
311
		 * should be replaced by the name of the new class.
312
		 *
313
		 * @param string Relative path to the template creating the HTML code
314
		 * @since 2016.04
315
		 * @category Developer
316
		 */
317
		$tplconf = 'admin/jqadm/customer/order/template-item';
318
		$default = 'customer/item-order-standard';
319
320
		return $view->render( $view->config( $tplconf, $default ) );
321
	}
322
}
323