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