Passed
Push — master ( 45d989...85608f )
by Aimeos
03:45
created

Standard::addData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product\Characteristic;
12
13
sprintf( 'characteristic' ); // for translation
14
15
16
/**
17
 * Default implementation of product qualities 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/product/characteristic/name
27
	 * Name of the characteristic subpart used by the JQAdm product implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Product\Characteristic\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 2016.04
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Adds the required data used in the template
40
	 *
41
	 * @param \Aimeos\MW\View\Iface $view View object
42
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
43
	 */
44
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
45
	{
46
		$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute/type' );
47
48
		$search = $manager->createSearch( true )->setSlice( 0, 1000 );
49
		$search->setSortations( [$search->sort( '+', 'attribute.type.position' )] );
50
51
		$view->attributeTypes = $manager->searchItems( $search );
52
53
		return $view;
54
	}
55
56
57
	/**
58
	 * Copies a resource
59
	 *
60
	 * @return string|null HTML output
61
	 */
62
	public function copy() : ?string
63
	{
64
		$view = $this->getObject()->addData( $this->getView() );
65
		$view->characteristicBody = '';
66
67
		foreach( $this->getSubClients() as $client ) {
68
			$view->characteristicBody .= $client->copy();
69
		}
70
71
		return $this->render( $view );
72
	}
73
74
75
	/**
76
	 * Creates a new resource
77
	 *
78
	 * @return string|null HTML output
79
	 */
80
	public function create() : ?string
81
	{
82
		$view = $this->getObject()->addData( $this->getView() );
83
		$view->characteristicBody = '';
84
85
		foreach( $this->getSubClients() as $client ) {
86
			$view->characteristicBody .= $client->create();
87
		}
88
89
		return $this->render( $view );
90
	}
91
92
93
	/**
94
	 * Returns a single resource
95
	 *
96
	 * @return string|null HTML output
97
	 */
98
	public function get() : ?string
99
	{
100
		$view = $this->getObject()->addData( $this->getView() );
101
		$view->characteristicBody = '';
102
103
		foreach( $this->getSubClients() as $client ) {
104
			$view->characteristicBody .= $client->get();
105
		}
106
107
		return $this->render( $view );
108
	}
109
110
111
	/**
112
	 * Saves the data
113
	 *
114
	 * @return string|null HTML output
115
	 */
116
	public function save() : ?string
117
	{
118
		$view = $this->getView();
119
120
		$view->characteristicBody = '';
121
122
		foreach( $this->getSubClients() as $client ) {
123
			$view->characteristicBody .= $client->save();
124
		}
125
126
		return null;
127
	}
128
129
130
	/**
131
	 * Returns the sub-client given by its name.
132
	 *
133
	 * @param string $type Name of the client type
134
	 * @param string|null $name Name of the sub-client (Default if null)
135
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
136
	 */
137
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
138
	{
139
		/** admin/jqadm/product/characteristic/decorators/excludes
140
		 * Excludes decorators added by the "common" option from the product JQAdm client
141
		 *
142
		 * Decorators extend the functionality of a class by adding new aspects
143
		 * (e.g. log what is currently done), executing the methods of the underlying
144
		 * class only in certain conditions (e.g. only for logged in users) or
145
		 * modify what is returned to the caller.
146
		 *
147
		 * This option allows you to remove a decorator added via
148
		 * "admin/jqadm/common/decorators/default" before they are wrapped
149
		 * around the JQAdm client.
150
		 *
151
		 *  admin/jqadm/product/characteristic/decorators/excludes = array( 'decorator1' )
152
		 *
153
		 * This would remove the decorator named "decorator1" from the list of
154
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
155
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
156
		 *
157
		 * @param array List of decorator names
158
		 * @since 2016.01
159
		 * @category Developer
160
		 * @see admin/jqadm/common/decorators/default
161
		 * @see admin/jqadm/product/characteristic/decorators/global
162
		 * @see admin/jqadm/product/characteristic/decorators/local
163
		 */
164
165
		/** admin/jqadm/product/characteristic/decorators/global
166
		 * Adds a list of globally available decorators only to the product JQAdm client
167
		 *
168
		 * Decorators extend the functionality of a class by adding new aspects
169
		 * (e.g. log what is currently done), executing the methods of the underlying
170
		 * class only in certain conditions (e.g. only for logged in users) or
171
		 * modify what is returned to the caller.
172
		 *
173
		 * This option allows you to wrap global decorators
174
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
175
		 *
176
		 *  admin/jqadm/product/characteristic/decorators/global = array( 'decorator1' )
177
		 *
178
		 * This would add the decorator named "decorator1" defined by
179
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
180
		 *
181
		 * @param array List of decorator names
182
		 * @since 2016.01
183
		 * @category Developer
184
		 * @see admin/jqadm/common/decorators/default
185
		 * @see admin/jqadm/product/characteristic/decorators/excludes
186
		 * @see admin/jqadm/product/characteristic/decorators/local
187
		 */
188
189
		/** admin/jqadm/product/characteristic/decorators/local
190
		 * Adds a list of local decorators only to the product JQAdm client
191
		 *
192
		 * Decorators extend the functionality of a class by adding new aspects
193
		 * (e.g. log what is currently done), executing the methods of the underlying
194
		 * class only in certain conditions (e.g. only for logged in users) or
195
		 * modify what is returned to the caller.
196
		 *
197
		 * This option allows you to wrap local decorators
198
		 * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client.
199
		 *
200
		 *  admin/jqadm/product/characteristic/decorators/local = array( 'decorator2' )
201
		 *
202
		 * This would add the decorator named "decorator2" defined by
203
		 * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client.
204
		 *
205
		 * @param array List of decorator names
206
		 * @since 2016.01
207
		 * @category Developer
208
		 * @see admin/jqadm/common/decorators/default
209
		 * @see admin/jqadm/product/characteristic/decorators/excludes
210
		 * @see admin/jqadm/product/characteristic/decorators/global
211
		 */
212
		return $this->createSubClient( 'product/characteristic/' . $type, $name );
213
	}
214
215
216
	/**
217
	 * Returns the list of sub-client names configured for the client.
218
	 *
219
	 * @return array List of JQAdm client names
220
	 */
221
	protected function getSubClientNames() : array
222
	{
223
		/** admin/jqadm/product/characteristic/standard/subparts
224
		 * List of JQAdm sub-clients rendered within the product characteristic section
225
		 *
226
		 * The output of the frontend is composed of the code generated by the JQAdm
227
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
228
		 * that are responsible for rendering certain sub-parts of the output. The
229
		 * sub-clients can contain JQAdm clients themselves and therefore a
230
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
231
		 * the output that is placed inside the container of its parent.
232
		 *
233
		 * At first, always the JQAdm code generated by the parent is printed, then
234
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
235
		 * determines the order of the output of these sub-clients inside the parent
236
		 * container. If the configured list of clients is
237
		 *
238
		 *  array( "subclient1", "subclient2" )
239
		 *
240
		 * you can easily change the order of the output by reordering the subparts:
241
		 *
242
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
243
		 *
244
		 * You can also remove one or more parts if they shouldn't be rendered:
245
		 *
246
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
247
		 *
248
		 * As the clients only generates structural JQAdm, the layout defined via CSS
249
		 * should support adding, removing or reordering content by a fluid like
250
		 * design.
251
		 *
252
		 * @param array List of sub-client names
253
		 * @since 2016.01
254
		 * @category Developer
255
		 */
256
		return $this->getContext()->getConfig()->get( 'admin/jqadm/product/characteristic/standard/subparts', [] );
257
	}
258
259
260
	/**
261
	 * Returns the rendered template including the view data
262
	 *
263
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
264
	 * @return string HTML output
265
	 */
266
	protected function render( \Aimeos\MW\View\Iface $view ) : string
267
	{
268
		/** admin/jqadm/product/characteristic/template-item
269
		 * Relative path to the HTML body template of the characteristic subpart for products.
270
		 *
271
		 * The template file contains the HTML code and processing instructions
272
		 * to generate the result shown in the body of the frontend. The
273
		 * configuration string is the path to the template file relative
274
		 * to the templates directory (usually in admin/jqadm/templates).
275
		 *
276
		 * You can overwrite the template file configuration in extensions and
277
		 * provide alternative templates. These alternative templates should be
278
		 * named like the default one but with the string "default" replaced by
279
		 * an unique name. You may use the name of your project for this. If
280
		 * you've implemented an alternative client class as well, "default"
281
		 * should be replaced by the name of the new class.
282
		 *
283
		 * @param string Relative path to the template creating the HTML code
284
		 * @since 2016.04
285
		 * @category Developer
286
		 */
287
		$tplconf = 'admin/jqadm/product/characteristic/template-item';
288
		$default = 'product/item-characteristic-standard';
289
290
		return $view->render( $view->config( $tplconf, $default ) );
291
	}
292
}
293