1
|
|
|
<?php |
2
|
|
|
namespace Redaxscript\Admin\Controller; |
3
|
|
|
|
4
|
|
|
use Redaxscript\Admin; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* children class to handle common tasks |
8
|
|
|
* |
9
|
|
|
* @since 4.0.0 |
10
|
|
|
* |
11
|
|
|
* @package Redaxscript |
12
|
|
|
* @category Controller |
13
|
|
|
* @author Henry Ruhs |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
class Common extends ControllerAbstract |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* process the class |
20
|
|
|
* |
21
|
|
|
* @since 4.0.0 |
22
|
|
|
* |
23
|
|
|
* @param string $action action to process |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
public function process(string $action = null) : string |
29
|
|
|
{ |
30
|
|
|
$table = $this->_registry->get('tableParameter'); |
31
|
|
|
$id = $this->_registry->get('idParameter'); |
32
|
|
|
$alias = $this->_registry->get('aliasParameter'); |
33
|
|
|
|
34
|
|
|
/* handle publish */ |
35
|
|
|
|
36
|
|
|
if ($action === 'publish') |
37
|
|
|
{ |
38
|
|
|
if ($this->_publish($table, $id)) |
39
|
|
|
{ |
40
|
|
|
return $this->_success( |
41
|
|
|
[ |
42
|
|
|
'route' => $this->_getRoute($table, $id), |
43
|
|
|
'timeout' => 0 |
44
|
|
|
]); |
45
|
|
|
}; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/* handle unpublish */ |
49
|
|
|
|
50
|
|
|
if ($action === 'unpublish') |
51
|
|
|
{ |
52
|
|
|
if ($this->_unpublish($table, $id)) |
53
|
|
|
{ |
54
|
|
|
return $this->_success( |
55
|
|
|
[ |
56
|
|
|
'route' => $this->_getRoute($table, $id), |
57
|
|
|
'timeout' => 0 |
58
|
|
|
]); |
59
|
|
|
}; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/* handle enable */ |
63
|
|
|
|
64
|
|
|
if ($action === 'enable') |
65
|
|
|
{ |
66
|
|
|
if ($this->_enable($table, $id)) |
67
|
|
|
{ |
68
|
|
|
return $this->_success( |
69
|
|
|
[ |
70
|
|
|
'route' => $this->_getRoute($table, $id), |
71
|
|
|
'timeout' => 0 |
72
|
|
|
]); |
73
|
|
|
}; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/* handle disable */ |
77
|
|
|
|
78
|
|
|
if ($action === 'disable') |
79
|
|
|
{ |
80
|
|
|
if ($this->_disable($table, $id)) |
81
|
|
|
{ |
82
|
|
|
return $this->_success( |
83
|
|
|
[ |
84
|
|
|
'route' => $this->_getRoute($table, $id), |
85
|
|
|
'timeout' => 0 |
86
|
|
|
]); |
87
|
|
|
}; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/* handle install */ |
91
|
|
|
|
92
|
|
|
if ($action === 'install') |
93
|
|
|
{ |
94
|
|
|
if ($this->_install($table, $alias)) |
95
|
|
|
{ |
96
|
|
|
return $this->_success( |
97
|
|
|
[ |
98
|
|
|
'route' => $this->_getRoute($table), |
99
|
|
|
'timeout' => 0 |
100
|
|
|
]); |
101
|
|
|
}; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/* handle uninstall */ |
105
|
|
|
|
106
|
|
|
if ($action === 'uninstall') |
107
|
|
|
{ |
108
|
|
|
if ($this->_uninstall($table, $alias)) |
109
|
|
|
{ |
110
|
|
|
return $this->_success( |
111
|
|
|
[ |
112
|
|
|
'route' => $this->_getRoute($table), |
113
|
|
|
'timeout' => 0 |
114
|
|
|
]); |
115
|
|
|
}; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/* handle delete */ |
119
|
|
|
|
120
|
|
|
if ($action === 'delete') |
121
|
|
|
{ |
122
|
|
|
if ($this->_delete($table, $id)) |
123
|
|
|
{ |
124
|
|
|
return $this->_success( |
125
|
|
|
[ |
126
|
|
|
'route' => $this->_getRoute($table, $id), |
127
|
|
|
'timeout' => 0 |
128
|
|
|
]); |
129
|
|
|
}; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/* handle error */ |
133
|
|
|
|
134
|
|
|
return $this->_error( |
135
|
|
|
[ |
136
|
|
|
'route' => $this->_getRoute($table, $id) |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* publish the item |
142
|
|
|
* |
143
|
|
|
* @since 4.0.0 |
144
|
|
|
* |
145
|
|
|
* @param string $table name of the table |
146
|
|
|
* @param int $id identifier of the item |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
|
151
|
|
|
public function _publish(string $table = null, int $id = null) : bool |
152
|
|
|
{ |
153
|
|
|
if ($table === 'categories') |
154
|
|
|
{ |
155
|
|
|
$categoryModel = new Admin\Model\Category(); |
156
|
|
|
return $categoryModel->publishById($id); |
157
|
|
|
} |
158
|
|
|
if ($table === 'articles') |
159
|
|
|
{ |
160
|
|
|
$articleModel = new Admin\Model\Article(); |
161
|
|
|
return $articleModel->publishById($id); |
162
|
|
|
} |
163
|
|
|
if ($table === 'extras') |
164
|
|
|
{ |
165
|
|
|
$extraModel = new Admin\Model\Extra(); |
166
|
|
|
return $extraModel->publishById($id); |
167
|
|
|
} |
168
|
|
|
if ($table === 'comments') |
169
|
|
|
{ |
170
|
|
|
$commentModel = new Admin\Model\Comment(); |
171
|
|
|
return $commentModel->publishById($id); |
172
|
|
|
} |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* unpublish the item |
178
|
|
|
* |
179
|
|
|
* @since 4.0.0 |
180
|
|
|
* |
181
|
|
|
* @param string $table name of the table |
182
|
|
|
* @param int $id identifier of the item |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
|
187
|
|
|
public function _unpublish(string $table = null, int $id = null) : bool |
188
|
|
|
{ |
189
|
|
|
if ($table === 'categories') |
190
|
|
|
{ |
191
|
|
|
$categoryModel = new Admin\Model\Category(); |
192
|
|
|
return $categoryModel->unpublishById($id); |
193
|
|
|
} |
194
|
|
|
if ($table === 'articles') |
195
|
|
|
{ |
196
|
|
|
$articleModel = new Admin\Model\Article(); |
197
|
|
|
return $articleModel->unpublishById($id); |
198
|
|
|
} |
199
|
|
|
if ($table === 'extras') |
200
|
|
|
{ |
201
|
|
|
$extraModel = new Admin\Model\Extra(); |
202
|
|
|
return $extraModel->unpublishById($id); |
203
|
|
|
} |
204
|
|
|
if ($table === 'comments') |
205
|
|
|
{ |
206
|
|
|
$commentModel = new Admin\Model\Comment(); |
207
|
|
|
return $commentModel->unpublishById($id); |
208
|
|
|
} |
209
|
|
|
return false; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* enable the item |
214
|
|
|
* |
215
|
|
|
* @since 4.0.0 |
216
|
|
|
* |
217
|
|
|
* @param string $table name of the table |
218
|
|
|
* @param int $id identifier of the item |
219
|
|
|
* |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
|
223
|
|
|
public function _enable(string $table = null, int $id = null) : bool |
224
|
|
|
{ |
225
|
|
|
if ($table === 'groups') |
226
|
|
|
{ |
227
|
|
|
$groupModel = new Admin\Model\Group(); |
228
|
|
|
return $groupModel->enableById($id); |
229
|
|
|
} |
230
|
|
|
if ($table === 'users') |
231
|
|
|
{ |
232
|
|
|
$userModel = new Admin\Model\User(); |
233
|
|
|
return $userModel->enableById($id); |
234
|
|
|
} |
235
|
|
|
if ($table === 'modules') |
236
|
|
|
{ |
237
|
|
|
$moduleModel = new Admin\Model\Module(); |
238
|
|
|
return $moduleModel->enableById($id); |
239
|
|
|
} |
240
|
|
|
return false; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* disable the item |
245
|
|
|
* |
246
|
|
|
* @since 4.0.0 |
247
|
|
|
* |
248
|
|
|
* @param string $table name of the table |
249
|
|
|
* @param int $id identifier of the item |
250
|
|
|
* |
251
|
|
|
* @return bool |
252
|
|
|
*/ |
253
|
|
|
|
254
|
|
|
public function _disable(string $table = null, int $id = null) : bool |
255
|
|
|
{ |
256
|
|
|
if ($table === 'groups') |
257
|
|
|
{ |
258
|
|
|
$groupModel = new Admin\Model\Group(); |
259
|
|
|
return $groupModel->disableById($id); |
260
|
|
|
} |
261
|
|
|
if ($table === 'users') |
262
|
|
|
{ |
263
|
|
|
$userModel = new Admin\Model\User(); |
264
|
|
|
return $userModel->disableById($id); |
265
|
|
|
} |
266
|
|
|
if ($table === 'modules') |
267
|
|
|
{ |
268
|
|
|
$moduleModel = new Admin\Model\Module(); |
269
|
|
|
return $moduleModel->disableById($id); |
270
|
|
|
} |
271
|
|
|
return false; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* install the item |
276
|
|
|
* |
277
|
|
|
* @since 4.0.0 |
278
|
|
|
* |
279
|
|
|
* @param string $table name of the table |
280
|
|
|
* @param string $alias alias of the item |
281
|
|
|
* |
282
|
|
|
* @return bool |
283
|
|
|
*/ |
284
|
|
|
|
285
|
|
|
public function _install(string $table = null, string $alias = null) : bool |
286
|
|
|
{ |
287
|
|
|
if ($table === 'modules') |
288
|
|
|
{ |
289
|
|
|
$moduleClass = 'Redaxscript\Modules\\' . $alias . '\\' . $alias; |
290
|
|
|
$module = new $moduleClass($this->_registry, $this->_request, $this->_language, $this->_config); |
291
|
|
|
return $module->install(); |
292
|
|
|
} |
293
|
|
|
return false; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* uninstall the item |
298
|
|
|
* |
299
|
|
|
* @since 4.0.0 |
300
|
|
|
* |
301
|
|
|
* @param string $table name of the table |
302
|
|
|
* @param string $alias alias of the item |
303
|
|
|
* |
304
|
|
|
* @return bool |
305
|
|
|
*/ |
306
|
|
|
|
307
|
|
|
public function _uninstall(string $table = null, string $alias = null) : bool |
308
|
|
|
{ |
309
|
|
|
if ($table === 'modules') |
310
|
|
|
{ |
311
|
|
|
$moduleClass = 'Redaxscript\Modules\\' . $alias . '\\' . $alias; |
312
|
|
|
$module = new $moduleClass($this->_registry, $this->_request, $this->_language, $this->_config); |
313
|
|
|
return $module->uninstall(); |
314
|
|
|
} |
315
|
|
|
return false; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* delete the item |
320
|
|
|
* |
321
|
|
|
* @since 4.0.0 |
322
|
|
|
* |
323
|
|
|
* @param string $table name of the table |
324
|
|
|
* @param int $id identifier of the item |
325
|
|
|
* |
326
|
|
|
* @return bool |
327
|
|
|
*/ |
328
|
|
|
|
329
|
|
|
public function _delete(string $table = null, int $id = null) : bool |
330
|
|
|
{ |
331
|
|
|
if ($table === 'categories') |
332
|
|
|
{ |
333
|
|
|
$categoryModel = new Admin\Model\Category(); |
334
|
|
|
return $categoryModel->deleteById($id); |
335
|
|
|
} |
336
|
|
|
if ($table === 'articles') |
337
|
|
|
{ |
338
|
|
|
$articleModel = new Admin\Model\Article(); |
339
|
|
|
return $articleModel->deleteById($id); |
340
|
|
|
} |
341
|
|
|
if ($table === 'extras') |
342
|
|
|
{ |
343
|
|
|
$extraModel = new Admin\Model\Extra(); |
344
|
|
|
return $extraModel->deleteById($id); |
345
|
|
|
} |
346
|
|
|
if ($table === 'comments') |
347
|
|
|
{ |
348
|
|
|
$commentModel = new Admin\Model\Comment(); |
349
|
|
|
return $commentModel->deleteById($id); |
350
|
|
|
} |
351
|
|
|
if ($table === 'groups') |
352
|
|
|
{ |
353
|
|
|
$groupModel = new Admin\Model\Group(); |
354
|
|
|
return $groupModel->deleteById($id); |
355
|
|
|
} |
356
|
|
|
if ($table === 'users') |
357
|
|
|
{ |
358
|
|
|
$userModel = new Admin\Model\User(); |
359
|
|
|
return $userModel->deleteById($id); |
360
|
|
|
} |
361
|
|
|
return false; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* get route |
366
|
|
|
* |
367
|
|
|
* @since 4.0.0 |
368
|
|
|
* |
369
|
|
|
* @param string $table name of the table |
370
|
|
|
* @param int $id identifier of the item |
371
|
|
|
* |
372
|
|
|
* @return string |
373
|
|
|
*/ |
374
|
|
|
|
375
|
|
|
public function _getRoute(string $table = null, int $id = null) : string |
376
|
|
|
{ |
377
|
|
|
if ($this->_registry->get($table . 'Edit')) |
378
|
|
|
{ |
379
|
|
|
if ($id) |
|
|
|
|
380
|
|
|
{ |
381
|
|
|
return 'admin/view/' . $table . '#row-' . $id; |
382
|
|
|
} |
383
|
|
|
if ($table) |
|
|
|
|
384
|
|
|
{ |
385
|
|
|
return 'admin/view/' . $table; |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
return 'admin'; |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: