1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2021-2025 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Frontend |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Frontend\Cms; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Default implementation of the cms frontend controller |
16
|
|
|
* |
17
|
|
|
* @package Controller |
18
|
|
|
* @subpackage Frontend |
19
|
|
|
*/ |
20
|
|
|
class Standard |
21
|
|
|
extends \Aimeos\Controller\Frontend\Base |
22
|
|
|
implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
23
|
|
|
{ |
24
|
|
|
/** controller/frontend/cms/name |
25
|
|
|
* Class name of the used cms frontend controller implementation |
26
|
|
|
* |
27
|
|
|
* Each default frontend controller can be replace by an alternative imlementation. |
28
|
|
|
* To use this implementation, you have to set the last part of the class |
29
|
|
|
* name as configuration value so the controller factory knows which class it |
30
|
|
|
* has to instantiate. |
31
|
|
|
* |
32
|
|
|
* For example, if the name of the default class is |
33
|
|
|
* |
34
|
|
|
* \Aimeos\Controller\Frontend\Cms\Standard |
35
|
|
|
* |
36
|
|
|
* and you want to replace it with your own version named |
37
|
|
|
* |
38
|
|
|
* \Aimeos\Controller\Frontend\Cms\Mycms |
39
|
|
|
* |
40
|
|
|
* then you have to set the this configuration option: |
41
|
|
|
* |
42
|
|
|
* controller/jobs/frontend/cms/name = Mycms |
43
|
|
|
* |
44
|
|
|
* The value is the last part of your own class name and it's case sensitive, |
45
|
|
|
* so take care that the configuration value is exactly named like the last |
46
|
|
|
* part of the class name. |
47
|
|
|
* |
48
|
|
|
* The allowed characters of the class name are A-Z, a-z and 0-9. No other |
49
|
|
|
* characters are possible! You should always start the last part of the class |
50
|
|
|
* name with an upper case character and continue only with lower case characters |
51
|
|
|
* or numbers. Avoid chamel case names like "MyCms"! |
52
|
|
|
* |
53
|
|
|
* @param string Last part of the class name |
54
|
|
|
* @since 2021.04 |
55
|
|
|
* @category Developer |
56
|
|
|
*/ |
57
|
|
|
|
58
|
|
|
/** controller/frontend/cms/decorators/excludes |
59
|
|
|
* Excludes decorators added by the "common" option from the cms frontend controllers |
60
|
|
|
* |
61
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
62
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
63
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
64
|
|
|
* modify what is returned to the caller. |
65
|
|
|
* |
66
|
|
|
* This option allows you to remove a decorator added via |
67
|
|
|
* "controller/frontend/common/decorators/default" before they are wrapped |
68
|
|
|
* around the frontend controller. |
69
|
|
|
* |
70
|
|
|
* controller/frontend/cms/decorators/excludes = array( 'decorator1' ) |
71
|
|
|
* |
72
|
|
|
* This would remove the decorator named "decorator1" from the list of |
73
|
|
|
* common decorators ("\Aimeos\Controller\Frontend\Common\Decorator\*") added via |
74
|
|
|
* "controller/frontend/common/decorators/default" for the cms frontend controller. |
75
|
|
|
* |
76
|
|
|
* @param array List of decorator names |
77
|
|
|
* @since 2021.04 |
78
|
|
|
* @category Developer |
79
|
|
|
* @see controller/frontend/common/decorators/default |
80
|
|
|
* @see controller/frontend/cms/decorators/global |
81
|
|
|
* @see controller/frontend/cms/decorators/local |
82
|
|
|
*/ |
83
|
|
|
|
84
|
|
|
/** controller/frontend/cms/decorators/global |
85
|
|
|
* Adds a list of globally available decorators only to the cms frontend controllers |
86
|
|
|
* |
87
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
88
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
89
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
90
|
|
|
* modify what is returned to the caller. |
91
|
|
|
* |
92
|
|
|
* This option allows you to wrap global decorators |
93
|
|
|
* ("\Aimeos\Controller\Frontend\Common\Decorator\*") around the frontend controller. |
94
|
|
|
* |
95
|
|
|
* controller/frontend/cms/decorators/global = array( 'decorator1' ) |
96
|
|
|
* |
97
|
|
|
* This would add the decorator named "decorator1" defined by |
98
|
|
|
* "\Aimeos\Controller\Frontend\Common\Decorator\Decorator1" only to the frontend controller. |
99
|
|
|
* |
100
|
|
|
* @param array List of decorator names |
101
|
|
|
* @since 2021.04 |
102
|
|
|
* @category Developer |
103
|
|
|
* @see controller/frontend/common/decorators/default |
104
|
|
|
* @see controller/frontend/cms/decorators/excludes |
105
|
|
|
* @see controller/frontend/cms/decorators/local |
106
|
|
|
*/ |
107
|
|
|
|
108
|
|
|
/** controller/frontend/cms/decorators/local |
109
|
|
|
* Adds a list of local decorators only to the cms frontend controllers |
110
|
|
|
* |
111
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
112
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
113
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
114
|
|
|
* modify what is returned to the caller. |
115
|
|
|
* |
116
|
|
|
* This option allows you to wrap local decorators |
117
|
|
|
* ("\Aimeos\Controller\Frontend\Cms\Decorator\*") around the frontend controller. |
118
|
|
|
* |
119
|
|
|
* controller/frontend/cms/decorators/local = array( 'decorator2' ) |
120
|
|
|
* |
121
|
|
|
* This would add the decorator named "decorator2" defined by |
122
|
|
|
* "\Aimeos\Controller\Frontend\Cms\Decorator\Decorator2" only to the frontend |
123
|
|
|
* controller. |
124
|
|
|
* |
125
|
|
|
* @param array List of decorator names |
126
|
|
|
* @since 2021.04 |
127
|
|
|
* @category Developer |
128
|
|
|
* @see controller/frontend/common/decorators/default |
129
|
|
|
* @see controller/frontend/cms/decorators/excludes |
130
|
|
|
* @see controller/frontend/cms/decorators/global |
131
|
|
|
*/ |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
private array $domains = []; |
135
|
|
|
private array $conditions = []; |
136
|
|
|
private \Aimeos\Base\Criteria\Iface $filter; |
137
|
|
|
private \Aimeos\MShop\Common\Manager\Iface $manager; |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Common initialization for controller classes |
142
|
|
|
* |
143
|
|
|
* @param \Aimeos\MShop\ContextIface $context Common MShop context object |
144
|
|
|
*/ |
145
|
|
|
public function __construct( \Aimeos\MShop\ContextIface $context ) |
146
|
|
|
{ |
147
|
|
|
parent::__construct( $context ); |
148
|
|
|
|
149
|
|
|
$this->manager = \Aimeos\MShop::create( $context, 'cms' ); |
150
|
|
|
$this->filter = $this->manager->filter( true ); |
151
|
|
|
$this->conditions[] = $this->filter->getConditions(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Clones objects in controller and resets values |
157
|
|
|
*/ |
158
|
|
|
public function __clone() |
159
|
|
|
{ |
160
|
|
|
$this->filter = clone $this->filter; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Adds generic condition for filtering |
166
|
|
|
* |
167
|
|
|
* @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~=" |
168
|
|
|
* @param string $key Search key defined by the cms manager, e.g. "cms.status" |
169
|
|
|
* @param array|string $value Value or list of values to compare to |
170
|
|
|
* @return \Aimeos\Controller\Frontend\Cms\Iface Cms controller for fluent interface |
171
|
|
|
* @since 2021.04 |
172
|
|
|
*/ |
173
|
|
|
public function compare( string $operator, string $key, $value ) : Iface |
174
|
|
|
{ |
175
|
|
|
$this->conditions[] = $this->filter->compare( $operator, $key, $value ); |
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns the cms for the given cms code |
182
|
|
|
* |
183
|
|
|
* @param string $code Unique cms code |
184
|
|
|
* @return \Aimeos\MShop\Cms\Item\Iface Cms item including the referenced domains items |
185
|
|
|
* @since 2021.04 |
186
|
|
|
*/ |
187
|
|
|
public function find( string $code ) : \Aimeos\MShop\Cms\Item\Iface |
188
|
|
|
{ |
189
|
|
|
return $this->manager->find( $code, $this->domains, null, null, true ); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Creates a search function string for the given name and parameters |
195
|
|
|
* |
196
|
|
|
* @param string $name Name of the search function without parenthesis, e.g. "cms:has" |
197
|
|
|
* @param array $params List of parameters for the search function with numeric keys starting at 0 |
198
|
|
|
* @return string Search function string that can be used in compare() |
199
|
|
|
*/ |
200
|
|
|
public function function( string $name, array $params ) : string |
201
|
|
|
{ |
202
|
|
|
return $this->filter->make( $name, $params ); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Returns the cms for the given cms ID |
208
|
|
|
* |
209
|
|
|
* @param string $id Unique cms ID |
210
|
|
|
* @return \Aimeos\MShop\Cms\Item\Iface Cms item including the referenced domains items |
211
|
|
|
* @since 2021.04 |
212
|
|
|
*/ |
213
|
|
|
public function get( string $id ) : \Aimeos\MShop\Cms\Item\Iface |
214
|
|
|
{ |
215
|
|
|
return $this->manager->get( $id, $this->domains, true ); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Adds a filter to return only items containing a reference to the given ID |
221
|
|
|
* |
222
|
|
|
* @param string $domain Domain name of the referenced item, e.g. "product" |
223
|
|
|
* @param string|null $type Type code of the reference, e.g. "default" or null for all types |
224
|
|
|
* @param string|null $refId ID of the referenced item of the given domain or null for all references |
225
|
|
|
* @return \Aimeos\Controller\Frontend\Cms\Iface Cms controller for fluent interface |
226
|
|
|
* @since 2019.10 |
227
|
|
|
*/ |
228
|
|
|
public function has( string $domain, ?string $type = null, ?string $refId = null ) : Iface |
229
|
|
|
{ |
230
|
|
|
$params = [$domain]; |
231
|
|
|
!$type ?: $params[] = $type; |
232
|
|
|
!$refId ?: $params[] = $refId; |
233
|
|
|
|
234
|
|
|
$func = $this->filter->make( 'cms:has', $params ); |
235
|
|
|
$this->conditions[] = $this->filter->compare( '!=', $func, null ); |
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Parses the given array and adds the conditions to the list of conditions |
242
|
|
|
* |
243
|
|
|
* @param array $conditions List of conditions, e.g. ['=~' => ['cms.label' => 'test']] |
244
|
|
|
* @return \Aimeos\Controller\Frontend\Cms\Iface Cms controller for fluent interface |
245
|
|
|
* @since 2021.04 |
246
|
|
|
*/ |
247
|
|
|
public function parse( array $conditions ) : Iface |
248
|
|
|
{ |
249
|
|
|
if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) { |
250
|
|
|
$this->conditions[] = $cond; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Returns the category for the given category URL name |
259
|
|
|
* |
260
|
|
|
* @param string $name category URL name |
261
|
|
|
* @return \Aimeos\MShop\Cms\Item\Iface Cms item including the referenced domains items |
262
|
|
|
* @since 2023.10 |
263
|
|
|
*/ |
264
|
|
|
public function resolve( string $name ) : \Aimeos\MShop\Cms\Item\Iface |
265
|
|
|
{ |
266
|
|
|
$search = $this->manager->filter( null )->add( 'cms.url', '==', '/' . trim( $name, '/' ) )->slice( 0, 1 ); |
267
|
|
|
|
268
|
|
|
if( ( $item = $this->manager->search( $search, $this->domains )->first() ) === null ) |
269
|
|
|
{ |
270
|
|
|
$msg = $this->context()->translate( 'controller/frontend', 'Unable to find CMS page "%1$s"' ); |
271
|
|
|
throw new \Aimeos\Controller\Frontend\Cms\Exception( sprintf( $msg, $name ), 404 ); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return $item; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Returns the cmss filtered by the previously assigned conditions |
280
|
|
|
* |
281
|
|
|
* @param int &$total Parameter where the total number of found cmss will be stored in |
282
|
|
|
* @return \Aimeos\Map Ordered list of cms items implementing \Aimeos\MShop\Cms\Item\Iface |
283
|
|
|
* @since 2021.04 |
284
|
|
|
*/ |
285
|
|
|
public function search( ?int &$total = null ) : \Aimeos\Map |
286
|
|
|
{ |
287
|
|
|
$this->filter->setConditions( $this->filter->and( $this->conditions ) ); |
288
|
|
|
return $this->manager->search( $this->filter, $this->domains, $total ); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Sets the start value and the number of returned cms items for slicing the list of found cms items |
294
|
|
|
* |
295
|
|
|
* @param int $start Start value of the first cms item in the list |
296
|
|
|
* @param int $limit Number of returned cms items |
297
|
|
|
* @return \Aimeos\Controller\Frontend\Cms\Iface Cms controller for fluent interface |
298
|
|
|
* @since 2021.04 |
299
|
|
|
*/ |
300
|
|
|
public function slice( int $start, int $limit ) : Iface |
301
|
|
|
{ |
302
|
|
|
$this->filter->slice( $start, $limit ); |
303
|
|
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Sets the sorting of the result list |
309
|
|
|
* |
310
|
|
|
* @param string|null $key Sorting key of the result list like "cms.label", null for no sorting |
311
|
|
|
* @return \Aimeos\Controller\Frontend\Cms\Iface Cms controller for fluent interface |
312
|
|
|
* @since 2021.04 |
313
|
|
|
*/ |
314
|
|
|
public function sort( ?string $key = null ) : Iface |
315
|
|
|
{ |
316
|
|
|
$sort = []; |
317
|
|
|
$list = ( $key ? explode( ',', $key ) : [] ); |
318
|
|
|
|
319
|
|
|
foreach( $list as $sortkey ) |
320
|
|
|
{ |
321
|
|
|
$direction = ( $sortkey[0] === '-' ? '-' : '+' ); |
322
|
|
|
$sort[] = $this->filter->sort( $direction, ltrim( $sortkey, '+-' ) ); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
$this->filter->setSortations( $sort ); |
326
|
|
|
return $this; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Sets the referenced domains that will be fetched too when retrieving items |
332
|
|
|
* |
333
|
|
|
* @param array $domains Domain names of the referenced items that should be fetched too |
334
|
|
|
* @return \Aimeos\Controller\Frontend\Cms\Iface Cms controller for fluent interface |
335
|
|
|
* @since 2021.04 |
336
|
|
|
*/ |
337
|
|
|
public function uses( array $domains ) : Iface |
338
|
|
|
{ |
339
|
|
|
$this->domains = $domains; |
340
|
|
|
return $this; |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|