1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017-2020 |
6
|
|
|
* @package Admin |
7
|
|
|
* @subpackage JQAdm |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Admin\JQAdm\Attribute\Text; |
12
|
|
|
|
13
|
|
|
sprintf( 'text' ); // for translation |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Default implementation of attribute text 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/attribute/text/name |
27
|
|
|
* Name of the text subpart used by the JQAdm attribute implementation |
28
|
|
|
* |
29
|
|
|
* Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Attribute\Text\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.07 |
34
|
|
|
* @category Developer |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Copies a resource |
40
|
|
|
* |
41
|
|
|
* @return string|null HTML output |
42
|
|
|
*/ |
43
|
|
|
public function copy() : ?string |
44
|
|
|
{ |
45
|
|
|
$view = $this->getObject()->addData( $this->getView() ); |
46
|
|
|
$view->textData = $this->toArray( $view->item, true ); |
47
|
|
|
$view->textBody = ''; |
48
|
|
|
|
49
|
|
|
foreach( $this->getSubClients() as $client ) { |
50
|
|
|
$view->textBody .= $client->copy(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->render( $view ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Creates a new resource |
59
|
|
|
* |
60
|
|
|
* @return string|null HTML output |
61
|
|
|
*/ |
62
|
|
|
public function create() : ?string |
63
|
|
|
{ |
64
|
|
|
$view = $this->getObject()->addData( $this->getView() ); |
65
|
|
|
$siteid = $this->getContext()->getLocale()->getSiteId(); |
66
|
|
|
$data = $view->param( 'text', [] ); |
67
|
|
|
|
68
|
|
|
foreach( $data as $idx => $entry ) |
69
|
|
|
{ |
70
|
|
|
$data[$idx]['attribute.lists.siteid'] = $siteid; |
71
|
|
|
$data[$idx]['text.siteid'] = $siteid; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$view->textData = $data; |
75
|
|
|
$view->textBody = ''; |
76
|
|
|
|
77
|
|
|
foreach( $this->getSubClients() as $client ) { |
78
|
|
|
$view->textBody .= $client->create(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->render( $view ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Deletes a resource |
87
|
|
|
* |
88
|
|
|
* @return string|null HTML output |
89
|
|
|
*/ |
90
|
|
|
public function delete() : ?string |
91
|
|
|
{ |
92
|
|
|
parent::delete(); |
93
|
|
|
|
94
|
|
|
$item = $this->getView()->item; |
95
|
|
|
$item->deleteListItems( $item->getListItems( 'text', null, null, false )->toArray(), true ); |
96
|
|
|
|
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns a single resource |
103
|
|
|
* |
104
|
|
|
* @return string|null HTML output |
105
|
|
|
*/ |
106
|
|
|
public function get() : ?string |
107
|
|
|
{ |
108
|
|
|
$view = $this->getObject()->addData( $this->getView() ); |
109
|
|
|
$view->textData = $this->toArray( $view->item ); |
110
|
|
|
$view->textBody = ''; |
111
|
|
|
|
112
|
|
|
foreach( $this->getSubClients() as $client ) { |
113
|
|
|
$view->textBody .= $client->get(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->render( $view ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Saves the data |
122
|
|
|
* |
123
|
|
|
* @return string|null HTML output |
124
|
|
|
*/ |
125
|
|
|
public function save() : ?string |
126
|
|
|
{ |
127
|
|
|
$view = $this->getView(); |
128
|
|
|
|
129
|
|
|
$view->item = $this->fromArray( $view->item, $view->param( 'text', [] ) ); |
130
|
|
|
$view->textBody = ''; |
131
|
|
|
|
132
|
|
|
foreach( $this->getSubClients() as $client ) { |
133
|
|
|
$view->textBody .= $client->save(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns the sub-client given by its name. |
142
|
|
|
* |
143
|
|
|
* @param string $type Name of the client type |
144
|
|
|
* @param string|null $name Name of the sub-client (Default if null) |
145
|
|
|
* @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
146
|
|
|
*/ |
147
|
|
|
public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
148
|
|
|
{ |
149
|
|
|
/** admin/jqadm/attribute/text/decorators/excludes |
150
|
|
|
* Excludes decorators added by the "common" option from the attribute JQAdm client |
151
|
|
|
* |
152
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
153
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
154
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
155
|
|
|
* modify what is returned to the caller. |
156
|
|
|
* |
157
|
|
|
* This option allows you to remove a decorator added via |
158
|
|
|
* "admin/jqadm/common/decorators/default" before they are wrapped |
159
|
|
|
* around the JQAdm client. |
160
|
|
|
* |
161
|
|
|
* admin/jqadm/attribute/text/decorators/excludes = array( 'decorator1' ) |
162
|
|
|
* |
163
|
|
|
* This would remove the decorator named "decorator1" from the list of |
164
|
|
|
* common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
165
|
|
|
* "admin/jqadm/common/decorators/default" to the JQAdm client. |
166
|
|
|
* |
167
|
|
|
* @param array List of decorator names |
168
|
|
|
* @since 2017.07 |
169
|
|
|
* @category Developer |
170
|
|
|
* @see admin/jqadm/common/decorators/default |
171
|
|
|
* @see admin/jqadm/attribute/text/decorators/global |
172
|
|
|
* @see admin/jqadm/attribute/text/decorators/local |
173
|
|
|
*/ |
174
|
|
|
|
175
|
|
|
/** admin/jqadm/attribute/text/decorators/global |
176
|
|
|
* Adds a list of globally available decorators only to the attribute JQAdm client |
177
|
|
|
* |
178
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
179
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
180
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
181
|
|
|
* modify what is returned to the caller. |
182
|
|
|
* |
183
|
|
|
* This option allows you to wrap global decorators |
184
|
|
|
* ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
185
|
|
|
* |
186
|
|
|
* admin/jqadm/attribute/text/decorators/global = array( 'decorator1' ) |
187
|
|
|
* |
188
|
|
|
* This would add the decorator named "decorator1" defined by |
189
|
|
|
* "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
190
|
|
|
* |
191
|
|
|
* @param array List of decorator names |
192
|
|
|
* @since 2017.07 |
193
|
|
|
* @category Developer |
194
|
|
|
* @see admin/jqadm/common/decorators/default |
195
|
|
|
* @see admin/jqadm/attribute/text/decorators/excludes |
196
|
|
|
* @see admin/jqadm/attribute/text/decorators/local |
197
|
|
|
*/ |
198
|
|
|
|
199
|
|
|
/** admin/jqadm/attribute/text/decorators/local |
200
|
|
|
* Adds a list of local decorators only to the attribute JQAdm client |
201
|
|
|
* |
202
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
203
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
204
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
205
|
|
|
* modify what is returned to the caller. |
206
|
|
|
* |
207
|
|
|
* This option allows you to wrap local decorators |
208
|
|
|
* ("\Aimeos\Admin\JQAdm\Attribute\Decorator\*") around the JQAdm client. |
209
|
|
|
* |
210
|
|
|
* admin/jqadm/attribute/text/decorators/local = array( 'decorator2' ) |
211
|
|
|
* |
212
|
|
|
* This would add the decorator named "decorator2" defined by |
213
|
|
|
* "\Aimeos\Admin\JQAdm\Attribute\Decorator\Decorator2" only to the JQAdm client. |
214
|
|
|
* |
215
|
|
|
* @param array List of decorator names |
216
|
|
|
* @since 2017.07 |
217
|
|
|
* @category Developer |
218
|
|
|
* @see admin/jqadm/common/decorators/default |
219
|
|
|
* @see admin/jqadm/attribute/text/decorators/excludes |
220
|
|
|
* @see admin/jqadm/attribute/text/decorators/global |
221
|
|
|
*/ |
222
|
|
|
return $this->createSubClient( 'attribute/text/' . $type, $name ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Returns the list of sub-client names configured for the client. |
228
|
|
|
* |
229
|
|
|
* @return array List of JQAdm client names |
230
|
|
|
*/ |
231
|
|
|
protected function getSubClientNames() : array |
232
|
|
|
{ |
233
|
|
|
/** admin/jqadm/attribute/text/standard/subparts |
234
|
|
|
* List of JQAdm sub-clients rendered within the attribute text section |
235
|
|
|
* |
236
|
|
|
* The output of the frontend is composed of the code generated by the JQAdm |
237
|
|
|
* clients. Each JQAdm client can consist of serveral (or none) sub-clients |
238
|
|
|
* that are responsible for rendering certain sub-parts of the output. The |
239
|
|
|
* sub-clients can contain JQAdm clients themselves and therefore a |
240
|
|
|
* hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
241
|
|
|
* the output that is placed inside the container of its parent. |
242
|
|
|
* |
243
|
|
|
* At first, always the JQAdm code generated by the parent is printed, then |
244
|
|
|
* the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
245
|
|
|
* determines the order of the output of these sub-clients inside the parent |
246
|
|
|
* container. If the configured list of clients is |
247
|
|
|
* |
248
|
|
|
* array( "subclient1", "subclient2" ) |
249
|
|
|
* |
250
|
|
|
* you can easily change the order of the output by reordering the subparts: |
251
|
|
|
* |
252
|
|
|
* admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
253
|
|
|
* |
254
|
|
|
* You can also remove one or more parts if they shouldn't be rendered: |
255
|
|
|
* |
256
|
|
|
* admin/jqadm/<clients>/subparts = array( "subclient1" ) |
257
|
|
|
* |
258
|
|
|
* As the clients only generates structural JQAdm, the layout defined via CSS |
259
|
|
|
* should support adding, removing or reordering content by a fluid like |
260
|
|
|
* design. |
261
|
|
|
* |
262
|
|
|
* @param array List of sub-client names |
263
|
|
|
* @since 2017.07 |
264
|
|
|
* @category Developer |
265
|
|
|
*/ |
266
|
|
|
return $this->getContext()->getConfig()->get( 'admin/jqadm/attribute/text/standard/subparts', [] ); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Adds the required data used in the text template |
272
|
|
|
* |
273
|
|
|
* @param \Aimeos\MW\View\Iface $view View object |
274
|
|
|
* @return \Aimeos\MW\View\Iface View object with assigned parameters |
275
|
|
|
*/ |
276
|
|
|
public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface |
277
|
|
|
{ |
278
|
|
|
$context = $this->getContext(); |
279
|
|
|
|
280
|
|
|
$textTypeManager = \Aimeos\MShop::create( $context, 'text/type' ); |
281
|
|
|
$listTypeManager = \Aimeos\MShop::create( $context, 'attribute/lists/type' ); |
282
|
|
|
|
283
|
|
|
$search = $textTypeManager->createSearch( true )->setSlice( 0, 10000 ); |
284
|
|
|
$search->setConditions( $search->compare( '==', 'text.type.domain', 'attribute' ) ); |
285
|
|
|
$search->setSortations( [$search->sort( '+', 'text.type.position' )] ); |
286
|
|
|
|
287
|
|
|
$listSearch = $listTypeManager->createSearch( true )->setSlice( 0, 10000 ); |
288
|
|
|
$listSearch->setConditions( $listSearch->compare( '==', 'attribute.lists.type.domain', 'text' ) ); |
289
|
|
|
$listSearch->setSortations( [$listSearch->sort( '+', 'attribute.lists.type.position' )] ); |
290
|
|
|
|
291
|
|
|
$view->textTypes = $textTypeManager->searchItems( $search ); |
292
|
|
|
$view->textListTypes = $listTypeManager->searchItems( $listSearch ); |
293
|
|
|
|
294
|
|
|
return $view; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Creates new and updates existing items using the data array |
300
|
|
|
* |
301
|
|
|
* @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object without referenced domain items |
302
|
|
|
* @param array $data Data array |
303
|
|
|
* @return \Aimeos\MShop\Attribute\Item\Iface Modified attribute item |
304
|
|
|
*/ |
305
|
|
|
protected function fromArray( \Aimeos\MShop\Attribute\Item\Iface $item, array $data ) : \Aimeos\MShop\Attribute\Item\Iface |
306
|
|
|
{ |
307
|
|
|
$context = $this->getContext(); |
308
|
|
|
|
309
|
|
|
$textManager = \Aimeos\MShop::create( $context, 'text' ); |
310
|
|
|
$listManager = \Aimeos\MShop::create( $context, 'attribute/lists' ); |
311
|
|
|
|
312
|
|
|
$listItems = $item->getListItems( 'text', null, null, false ); |
313
|
|
|
|
314
|
|
|
|
315
|
|
|
foreach( $data as $idx => $entry ) |
316
|
|
|
{ |
317
|
|
|
if( trim( $this->getValue( $entry, 'text.content', '' ) ) === '' ) { |
318
|
|
|
continue; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
if( ( $listItem = $item->getListItem( 'text', $entry['attribute.lists.type'], $entry['text.id'], false ) ) === null ) { |
322
|
|
|
$listItem = $listManager->createItem(); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
if( ( $refItem = $listItem->getRefItem() ) === null ) { |
326
|
|
|
$refItem = $textManager->createItem(); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$refItem->fromArray( $entry, true ); |
330
|
|
|
$conf = []; |
331
|
|
|
|
332
|
|
|
foreach( (array) $this->getValue( $entry, 'config', [] ) as $cfg ) |
333
|
|
|
{ |
334
|
|
|
if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' ) { |
335
|
|
|
$conf[$key] = trim( $cfg['val'] ?? '' ); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
$listItem->fromArray( $entry, true ); |
340
|
|
|
$listItem->setPosition( $idx ); |
341
|
|
|
$listItem->setConfig( $conf ); |
342
|
|
|
|
343
|
|
|
$item->addListItem( 'text', $listItem, $refItem ); |
344
|
|
|
|
345
|
|
|
unset( $listItems[$listItem->getId()] ); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
return $item->deleteListItems( $listItems->toArray(), true ); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Constructs the data array for the view from the given item |
354
|
|
|
* |
355
|
|
|
* @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object including referenced domain items |
356
|
|
|
* @param bool $copy True if items should be copied, false if not |
357
|
|
|
* @return string[] Multi-dimensional associative list of item data |
358
|
|
|
*/ |
359
|
|
|
protected function toArray( \Aimeos\MShop\Attribute\Item\Iface $item, bool $copy = false ) : array |
360
|
|
|
{ |
361
|
|
|
$data = []; |
362
|
|
|
$siteId = $this->getContext()->getLocale()->getSiteId(); |
363
|
|
|
|
364
|
|
|
foreach( $item->getListItems( 'text', null, null, false ) as $listItem ) |
365
|
|
|
{ |
366
|
|
|
if( ( $refItem = $listItem->getRefItem() ) === null ) { |
367
|
|
|
continue; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$list = $listItem->toArray( true ) + $refItem->toArray( true ); |
371
|
|
|
|
372
|
|
|
if( $copy === true ) |
373
|
|
|
{ |
374
|
|
|
$list['attribute.lists.siteid'] = $siteId; |
375
|
|
|
$list['attribute.lists.id'] = ''; |
376
|
|
|
$list['text.siteid'] = $siteId; |
377
|
|
|
$list['text.id'] = null; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
$list['attribute.lists.datestart'] = str_replace( ' ', 'T', $list['attribute.lists.datestart'] ); |
381
|
|
|
$list['attribute.lists.dateend'] = str_replace( ' ', 'T', $list['attribute.lists.dateend'] ); |
382
|
|
|
$list['config'] = []; |
383
|
|
|
|
384
|
|
|
foreach( $listItem->getConfig() as $key => $value ) { |
385
|
|
|
$list['config'][] = ['key' => $key, 'val' => $value]; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
$data[] = $list; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return $data; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Returns the rendered template including the view data |
397
|
|
|
* |
398
|
|
|
* @param \Aimeos\MW\View\Iface $view View object with data assigned |
399
|
|
|
* @return string|null HTML output |
400
|
|
|
*/ |
401
|
|
|
protected function render( \Aimeos\MW\View\Iface $view ) : string |
402
|
|
|
{ |
403
|
|
|
/** admin/jqadm/attribute/text/template-item |
404
|
|
|
* Relative path to the HTML body template of the text subpart for attributes. |
405
|
|
|
* |
406
|
|
|
* The template file contains the HTML code and processing instructions |
407
|
|
|
* to generate the result shown in the body of the frontend. The |
408
|
|
|
* configuration string is the path to the template file relative |
409
|
|
|
* to the templates directory (usually in admin/jqadm/templates). |
410
|
|
|
* |
411
|
|
|
* You can overwrite the template file configuration in extensions and |
412
|
|
|
* provide alternative templates. These alternative templates should be |
413
|
|
|
* named like the default one but with the string "default" replaced by |
414
|
|
|
* an unique name. You may use the name of your project for this. If |
415
|
|
|
* you've implemented an alternative client class as well, "default" |
416
|
|
|
* should be replaced by the name of the new class. |
417
|
|
|
* |
418
|
|
|
* @param string Relative path to the template creating the HTML code |
419
|
|
|
* @since 2016.04 |
420
|
|
|
* @category Developer |
421
|
|
|
*/ |
422
|
|
|
$tplconf = 'admin/jqadm/attribute/text/template-item'; |
423
|
|
|
$default = 'attribute/item-text-standard'; |
424
|
|
|
|
425
|
|
|
return $view->render( $view->config( $tplconf, $default ) ); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
|