|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpha\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Alpha\Util\Logging\Logger; |
|
6
|
|
|
use Alpha\Util\Http\Response; |
|
7
|
|
|
use Alpha\View\View; |
|
8
|
|
|
use Alpha\Exception\IllegalArguementException; |
|
9
|
|
|
use Alpha\Exception\SecurityException; |
|
10
|
|
|
use Alpha\Exception\FailedSaveException; |
|
11
|
|
|
use Alpha\Exception\AlphaException; |
|
12
|
|
|
use Alpha\Exception\RecordNotFoundException; |
|
13
|
|
|
use Alpha\Model\ActiveRecord; |
|
14
|
|
|
use Alpha\Model\Type\DEnum; |
|
15
|
|
|
use Alpha\Model\Type\DEnumItem; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Controller used to edit DEnums and associated DEnumItems. |
|
19
|
|
|
* |
|
20
|
|
|
* @since 1.0 |
|
21
|
|
|
* |
|
22
|
|
|
* @author John Collins <[email protected]> |
|
23
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
|
24
|
|
|
* @copyright Copyright (c) 2018, John Collins (founder of Alpha Framework). |
|
25
|
|
|
* All rights reserved. |
|
26
|
|
|
* |
|
27
|
|
|
* <pre> |
|
28
|
|
|
* Redistribution and use in source and binary forms, with or |
|
29
|
|
|
* without modification, are permitted provided that the |
|
30
|
|
|
* following conditions are met: |
|
31
|
|
|
* |
|
32
|
|
|
* * Redistributions of source code must retain the above |
|
33
|
|
|
* copyright notice, this list of conditions and the |
|
34
|
|
|
* following disclaimer. |
|
35
|
|
|
* * Redistributions in binary form must reproduce the above |
|
36
|
|
|
* copyright notice, this list of conditions and the |
|
37
|
|
|
* following disclaimer in the documentation and/or other |
|
38
|
|
|
* materials provided with the distribution. |
|
39
|
|
|
* * Neither the name of the Alpha Framework nor the names |
|
40
|
|
|
* of its contributors may be used to endorse or promote |
|
41
|
|
|
* products derived from this software without specific |
|
42
|
|
|
* prior written permission. |
|
43
|
|
|
* |
|
44
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
|
45
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
|
46
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
47
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
48
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
|
49
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
50
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
51
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
52
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
53
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
54
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
|
55
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
56
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
57
|
|
|
* </pre> |
|
58
|
|
|
*/ |
|
59
|
|
|
class DEnumController extends ActiveRecordController implements ControllerInterface |
|
60
|
|
|
{ |
|
61
|
|
|
/** |
|
62
|
|
|
* Trace logger. |
|
63
|
|
|
* |
|
64
|
|
|
* @var \Alpha\Util\Logging\Logger |
|
65
|
|
|
* |
|
66
|
|
|
* @since 1.0 |
|
67
|
|
|
*/ |
|
68
|
|
|
private static $logger = null; |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* DEnum to work on |
|
72
|
|
|
* |
|
73
|
|
|
* @var \Alpha\Model\Type\DEnum |
|
74
|
|
|
* |
|
75
|
|
|
* @since 3.0.0 |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $record; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* constructor to set up the object. |
|
81
|
|
|
* |
|
82
|
|
|
* @since 1.0 |
|
83
|
|
|
*/ |
|
84
|
|
|
public function __construct() |
|
85
|
|
|
{ |
|
86
|
|
|
self::$logger = new Logger('DEnumController'); |
|
87
|
|
|
self::$logger->debug('>>__construct()'); |
|
88
|
|
|
|
|
89
|
|
|
// ensure that the super class constructor is called, indicating the rights group |
|
90
|
|
|
parent::__construct('Admin'); |
|
91
|
|
|
|
|
92
|
|
|
$this->record = new DEnum(); |
|
93
|
|
|
|
|
94
|
|
|
self::$logger->debug('<<__construct'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Handle GET requests. |
|
99
|
|
|
* |
|
100
|
|
|
* @param \Alpha\Util\Http\Request $request |
|
101
|
|
|
* |
|
102
|
|
|
* @return \Alpha\Util\Http\Response |
|
103
|
|
|
* |
|
104
|
|
|
* @since 1.0 |
|
105
|
|
|
*/ |
|
106
|
|
|
public function doGET($request) |
|
107
|
|
|
{ |
|
108
|
|
|
self::$logger->debug('>>doGET($request=['.var_export($request, true).'])'); |
|
109
|
|
|
|
|
110
|
|
|
$params = $request->getParams(); |
|
111
|
|
|
|
|
112
|
|
|
$body = ''; |
|
113
|
|
|
|
|
114
|
|
|
// load one DEnum |
|
115
|
|
|
if (isset($params['denumID'])) { |
|
116
|
|
|
$RecordOid = $params['denumID']; |
|
117
|
|
|
|
|
118
|
|
|
// set up the title and meta details |
|
119
|
|
|
$this->setTitle('Editing a DEnum'); |
|
120
|
|
|
$this->setDescription('Page to edit a DEnum.'); |
|
121
|
|
|
$this->setKeywords('edit,DEnum'); |
|
122
|
|
|
|
|
123
|
|
|
$body .= View::displayPageHead($this); |
|
124
|
|
|
|
|
125
|
|
|
$message = $this->getStatusMessage(); |
|
126
|
|
|
if (!empty($message)) { |
|
127
|
|
|
$body .= $message; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
try { |
|
131
|
|
|
$this->record->load($RecordOid); |
|
132
|
|
|
|
|
133
|
|
|
ActiveRecord::disconnect(); |
|
134
|
|
|
|
|
135
|
|
|
$view = View::getInstance($this->record); |
|
136
|
|
|
|
|
137
|
|
|
$body .= View::renderDeleteForm($request->getURI()); |
|
138
|
|
|
|
|
139
|
|
|
$body .= $view->editView(array('URI' => $request->getURI())); |
|
140
|
|
|
} catch (RecordNotFoundException $e) { |
|
141
|
|
|
self::$logger->error('Unable to load the DEnum of id ['.$params['denumID'].'], error was ['.$e->getMessage().']'); |
|
142
|
|
|
} |
|
143
|
|
|
} else { // load all DEnums |
|
144
|
|
|
// set up the title and meta details |
|
145
|
|
|
$this->setTitle('Listing all DEnums'); |
|
146
|
|
|
$this->setDescription('Page to list all DEnums.'); |
|
147
|
|
|
$this->setKeywords('list,all,DEnums'); |
|
148
|
|
|
|
|
149
|
|
|
$body .= View::displayPageHead($this); |
|
150
|
|
|
|
|
151
|
|
|
// make sure that the DEnum tables exist |
|
152
|
|
|
if (!$this->record->checkTableExists()) { |
|
153
|
|
|
$body .= View::displayErrorMessage('Warning! The DEnum tables do not exist, attempting to create them now...'); |
|
154
|
|
|
$body .= $this->createDEnumTables(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// get all of the records and invoke the list view on each one |
|
158
|
|
|
|
|
159
|
|
|
// set the start point for the list pagination |
|
160
|
|
|
if (isset($params['start']) ? $this->start = $params['start'] : $this->start = 1); |
|
161
|
|
|
|
|
162
|
|
|
$objects = $this->record->loadAll($this->start); |
|
163
|
|
|
|
|
164
|
|
|
ActiveRecord::disconnect(); |
|
165
|
|
|
|
|
166
|
|
|
$this->recordCount = $this->record->getCount(); |
|
167
|
|
|
|
|
168
|
|
|
$body .= View::renderDeleteForm($request->getURI()); |
|
169
|
|
|
|
|
170
|
|
|
foreach ($objects as $object) { |
|
171
|
|
|
$temp = View::getInstance($object); |
|
172
|
|
|
$body .= $temp->listView(array('URI' => $request->getURI())); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$body .= View::displayPageFoot($this); |
|
177
|
|
|
|
|
178
|
|
|
self::$logger->debug('<<doGET'); |
|
179
|
|
|
|
|
180
|
|
|
return new Response(200, $body, array('Content-Type' => 'text/html')); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Handle POST requests. |
|
185
|
|
|
* |
|
186
|
|
|
* @param \Alpha\Util\Http\Request $request |
|
187
|
|
|
* |
|
188
|
|
|
* @return \Alpha\Util\Http\Response |
|
189
|
|
|
* |
|
190
|
|
|
* @throws \Alpha\Exception\SecurityException |
|
191
|
|
|
* |
|
192
|
|
|
* @since 1.0 |
|
193
|
|
|
*/ |
|
194
|
|
|
public function doPOST($request) |
|
195
|
|
|
{ |
|
196
|
|
|
self::$logger->debug('>>doPOST($request=['.var_export($request, true).'])'); |
|
197
|
|
|
|
|
198
|
|
|
$params = $request->getParams(); |
|
199
|
|
|
|
|
200
|
|
|
try { |
|
201
|
|
|
// check the hidden security fields before accepting the form POST data |
|
202
|
|
|
if (!$this->checkSecurityFields()) { |
|
203
|
|
|
self::$logger->debug('<<doPOST'); |
|
204
|
|
|
throw new SecurityException('This page cannot accept post data from remote servers!'); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
// ensure that a ID is provided |
|
208
|
|
|
if (isset($params['denumID'])) { |
|
209
|
|
|
$RecordOid = $params['denumID']; |
|
210
|
|
|
} else { |
|
211
|
|
|
throw new IllegalArguementException('Could not load the DEnum object as an denumID was not supplied!'); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
if (isset($params['saveBut'])) { |
|
215
|
|
|
try { |
|
216
|
|
|
$this->record->load($RecordOid); |
|
217
|
|
|
// update the object from post data |
|
218
|
|
|
$this->record->populateFromArray($params); |
|
219
|
|
|
|
|
220
|
|
|
ActiveRecord::begin(); |
|
221
|
|
|
|
|
222
|
|
|
$this->record->save(); |
|
223
|
|
|
|
|
224
|
|
|
self::$logger->action('DEnum '.$this->record->getID().' saved'); |
|
225
|
|
|
|
|
226
|
|
|
// now save the DEnumItems |
|
227
|
|
|
$tmp = new DEnumItem(); |
|
228
|
|
|
$denumItems = $tmp->loadItems($this->record->getID()); |
|
229
|
|
|
|
|
230
|
|
|
foreach ($denumItems as $item) { |
|
231
|
|
|
$item->set('value', $params['value_'.$item->getID()]); |
|
232
|
|
|
$item->save(); |
|
233
|
|
|
|
|
234
|
|
|
self::$logger->action('DEnumItem '.$item->getID().' saved'); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
// handle new DEnumItem if posted |
|
238
|
|
|
if (isset($params['new_value']) && trim($params['new_value']) != '') { |
|
239
|
|
|
$newItem = new DEnumItem(); |
|
240
|
|
|
$newItem->set('value', $params['new_value']); |
|
241
|
|
|
$newItem->set('DEnumID', $this->record->getID()); |
|
242
|
|
|
$newItem->save(); |
|
243
|
|
|
|
|
244
|
|
|
self::$logger->action('DEnumItem '.$newItem->getID().' created'); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
ActiveRecord::commit(); |
|
248
|
|
|
|
|
249
|
|
|
$this->setStatusMessage(View::displayUpdateMessage(get_class($this->record).' '.$this->record->getID().' saved successfully.')); |
|
250
|
|
|
|
|
251
|
|
|
return $this->doGET($request); |
|
252
|
|
|
} catch (FailedSaveException $e) { |
|
253
|
|
|
self::$logger->error('Unable to save the DEnum of id ['.$params['oid'].'], error was ['.$e->getMessage().']'); |
|
254
|
|
|
ActiveRecord::rollback(); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
ActiveRecord::disconnect(); |
|
258
|
|
|
} |
|
259
|
|
|
} catch (SecurityException $e) { |
|
260
|
|
|
$this->setStatusMessage(View::displayErrorMessage($e->getMessage())); |
|
261
|
|
|
self::$logger->warn($e->getMessage()); |
|
262
|
|
|
} catch (IllegalArguementException $e) { |
|
263
|
|
|
$this->setStatusMessage(View::displayErrorMessage($e->getMessage())); |
|
264
|
|
|
self::$logger->error($e->getMessage()); |
|
265
|
|
|
} catch (RecordNotFoundException $e) { |
|
266
|
|
|
self::$logger->warn($e->getMessage()); |
|
267
|
|
|
$this->setStatusMessage(View::displayErrorMessage('Failed to load the requested item from the database!')); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
$body = View::displayPageHead($this); |
|
271
|
|
|
|
|
272
|
|
|
$message = $this->getStatusMessage(); |
|
273
|
|
|
if (!empty($message)) { |
|
274
|
|
|
$body .= $message; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
$body .= View::displayPageFoot($this); |
|
278
|
|
|
self::$logger->debug('<<doPOST'); |
|
279
|
|
|
|
|
280
|
|
|
return new Response(200, $body, array('Content-Type' => 'text/html')); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Method to create the DEnum tables if they don't exist. |
|
285
|
|
|
* |
|
286
|
|
|
* @since 1.0 |
|
287
|
|
|
* |
|
288
|
|
|
* @return string |
|
289
|
|
|
*/ |
|
290
|
|
|
private function createDEnumTables() |
|
291
|
|
|
{ |
|
292
|
|
|
$tmpDEnum = new DEnum(); |
|
293
|
|
|
|
|
294
|
|
|
$body = '<p>Attempting to build table '.DEnum::TABLE_NAME.' for class DEnum : </p>'; |
|
295
|
|
|
|
|
296
|
|
|
try { |
|
297
|
|
|
$tmpDEnum->makeTable(); |
|
298
|
|
|
$body .= View::displayUpdateMessage('Successfully re-created the database table '.DEnum::TABLE_NAME); |
|
299
|
|
|
self::$logger->action('Re-created the table '.DEnum::TABLE_NAME); |
|
300
|
|
|
} catch (AlphaException $e) { |
|
301
|
|
|
$body .= View::displayErrorMessage('Failed re-created the database table '.DEnum::TABLE_NAME.', check the log'); |
|
302
|
|
|
self::$logger->error($e->getMessage()); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
$tmpDEnumItem = new DEnumItem(); |
|
306
|
|
|
|
|
307
|
|
|
$body .= '<p>Attempting to build table '.DEnumItem::TABLE_NAME.' for class DEnumItem : </p>'; |
|
308
|
|
|
|
|
309
|
|
|
try { |
|
310
|
|
|
$tmpDEnumItem->makeTable(); |
|
311
|
|
|
$body .= View::displayUpdateMessage('Successfully re-created the database table '.DEnumItem::TABLE_NAME); |
|
312
|
|
|
self::$logger->action('Re-created the table '.DEnumItem::TABLE_NAME); |
|
313
|
|
|
} catch (AlphaException $e) { |
|
314
|
|
|
$body .= View::displayErrorMessage('Failed re-created the database table '.DEnumItem::TABLE_NAME.', check the log'); |
|
315
|
|
|
self::$logger->error($e->getMessage()); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
return $body; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|