1: <?php
2:
3: namespace Alpha\Controller;
4:
5: use Alpha\Util\Logging\Logger;
6: use Alpha\Util\Config\ConfigProvider;
7: use Alpha\Util\Http\Response;
8: use Alpha\View\View;
9: use Alpha\Exception\IllegalArguementException;
10: use Alpha\Exception\SecurityException;
11: use Alpha\Exception\FailedSaveException;
12: use Alpha\Exception\AlphaException;
13: use Alpha\Model\ActiveRecord;
14: use Alpha\Model\Type\DEnum;
15: use Alpha\Model\Type\DEnumItem;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
59: class DEnumController extends ActiveRecordController implements ControllerInterface
60: {
61: 62: 63: 64: 65: 66: 67:
68: private static $logger = null;
69:
70: 71: 72: 73: 74:
75: public function __construct()
76: {
77: self::$logger = new Logger('DEnumController');
78: self::$logger->debug('>>__construct()');
79:
80:
81: parent::__construct('Admin');
82:
83: $this->BO = new DEnum();
84:
85: self::$logger->debug('<<__construct');
86: }
87:
88: 89: 90: 91: 92: 93: 94: 95: 96:
97: public function doGET($request)
98: {
99: self::$logger->debug('>>doGET($request=['.var_export($request, true).'])');
100:
101: $config = ConfigProvider::getInstance();
102:
103: $params = $request->getParams();
104:
105: $body = '';
106:
107:
108: if (isset($params['denumOID'])) {
109: $BOoid = $params['denumOID'];
110:
111:
112: $this->setTitle('Editing a DEnum');
113: $this->setDescription('Page to edit a DEnum.');
114: $this->setKeywords('edit,DEnum');
115:
116: $body .= View::displayPageHead($this);
117:
118: $message = $this->getStatusMessage();
119: if (!empty($message)) {
120: $body .= $message;
121: }
122:
123: try {
124: $this->BO->load($BOoid);
125:
126: ActiveRecord::disconnect();
127:
128: $this->BOName = 'DEnum';
129:
130: $this->BOView = View::getInstance($this->BO);
131:
132: $body .= View::renderDeleteForm($request->getURI());
133:
134: $body .= $this->BOView->editView(array('URI' => $request->getURI()));
135: } catch (RecordNotFoundException $e) {
136: self::$logger->error('Unable to load the DEnum of id ['.$params['denumOID'].'], error was ['.$e->getMessage().']');
137: }
138: } else {
139:
140: $this->setTitle('Listing all DEnums');
141: $this->setDescription('Page to list all DEnums.');
142: $this->setKeywords('list,all,DEnums');
143:
144: $body .= View::displayPageHead($this);
145:
146:
147: if (!$this->BO->checkTableExists()) {
148: $body .= View::displayErrorMessage('Warning! The DEnum tables do not exist, attempting to create them now...');
149: $body .= $this->createDEnumTables();
150: }
151:
152:
153:
154:
155: if (isset($params['start']) ? $this->startPoint = $params['start'] : $this->startPoint = 1);
156:
157: $objects = $this->BO->loadAll($this->startPoint);
158:
159: ActiveRecord::disconnect();
160:
161: $this->BOCount = $this->BO->getCount();
162:
163: $body .= View::renderDeleteForm($request->getURI());
164:
165: foreach ($objects as $object) {
166: $temp = View::getInstance($object);
167: $body .= $temp->listView(array('URI' => $request->getURI()));
168: }
169: }
170:
171: $body .= View::displayPageFoot($this);
172:
173: self::$logger->debug('<<doGET');
174:
175: return new Response(200, $body, array('Content-Type' => 'text/html'));
176: }
177:
178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188:
189: public function doPOST($request)
190: {
191: self::$logger->debug('>>doPOST($request=['.var_export($request, true).'])');
192:
193: $params = $request->getParams();
194:
195: try {
196:
197: if (!$this->checkSecurityFields()) {
198: throw new SecurityException('This page cannot accept post data from remote servers!');
199: self::$logger->debug('<<doPOST');
200: }
201:
202:
203: if (isset($params['denumOID'])) {
204: $BOoid = $params['denumOID'];
205: } else {
206: throw new IllegalArguementException('Could not load the DEnum object as an denumOID was not supplied!');
207: }
208:
209: if (isset($params['saveBut'])) {
210: try {
211: $this->BO->load($BOoid);
212:
213: $this->BO->populateFromArray($params);
214:
215: ActiveRecord::begin();
216:
217: $this->BO->save();
218:
219: self::$logger->action('DEnum '.$this->BO->getOID().' saved');
220:
221:
222: $tmp = new DEnumItem();
223: $denumItems = $tmp->loadItems($this->BO->getID());
224:
225: foreach ($denumItems as $item) {
226: $item->set('value', $params['value_'.$item->getID()]);
227: $item->save();
228:
229: self::$logger->action('DEnumItem '.$item->getOID().' saved');
230: }
231:
232:
233: if (isset($params['new_value']) && trim($params['new_value']) != '') {
234: $newItem = new DEnumItem();
235: $newItem->set('value', $params['new_value']);
236: $newItem->set('DEnumID', $this->BO->getID());
237: $newItem->save();
238:
239: self::$logger->action('DEnumItem '.$newItem->getOID().' created');
240: }
241:
242: ActiveRecord::commit();
243:
244: $this->setStatusMessage(View::displayUpdateMessage(get_class($this->BO).' '.$this->BO->getID().' saved successfully.'));
245:
246: return $this->doGET($request);
247: } catch (FailedSaveException $e) {
248: self::$logger->error('Unable to save the DEnum of id ['.$params['oid'].'], error was ['.$e->getMessage().']');
249: ActiveRecord::rollback();
250: }
251:
252: ActiveRecord::disconnect();
253: }
254: } catch (SecurityException $e) {
255: $this->setStatusMessage(View::displayErrorMessage($e->getMessage()));
256: self::$logger->warn($e->getMessage());
257: } catch (IllegalArguementException $e) {
258: $this->setStatusMessage(View::displayErrorMessage($e->getMessage()));
259: self::$logger->error($e->getMessage());
260: } catch (RecordNotFoundException $e) {
261: self::$logger->warn($e->getMessage());
262: $this->setStatusMessage(View::displayErrorMessage('Failed to load the requested item from the database!'));
263: }
264:
265: $body = View::displayPageHead($this);
266:
267: $message = $this->getStatusMessage();
268: if (!empty($message)) {
269: $body .= $message;
270: }
271:
272: $body .= View::displayPageFoot($this);
273: self::$logger->debug('<<doPOST');
274:
275: return new Response(200, $body, array('Content-Type' => 'text/html'));
276: }
277:
278: 279: 280: 281: 282: 283: 284:
285: private function createDEnumTables()
286: {
287: $tmpDEnum = new DEnum();
288:
289: $body = '<p>Attempting to build table '.DEnum::TABLE_NAME.' for class DEnum : </p>';
290:
291: try {
292: $tmpDEnum->makeTable();
293: $body .= View::displayUpdateMessage('Successfully re-created the database table '.DEnum::TABLE_NAME);
294: self::$logger->action('Re-created the table '.DEnum::TABLE_NAME);
295: } catch (AlphaException $e) {
296: $body .= View::displayErrorMessage('Failed re-created the database table '.DEnum::TABLE_NAME.', check the log');
297: self::$logger->error($e->getMessage());
298: }
299:
300: $tmpDEnumItem = new DEnumItem();
301:
302: $body .= '<p>Attempting to build table '.DEnumItem::TABLE_NAME.' for class DEnumItem : </p>';
303:
304: try {
305: $tmpDEnumItem->makeTable();
306: $body .= View::displayUpdateMessage('Successfully re-created the database table '.DEnumItem::TABLE_NAME);
307: self::$logger->action('Re-created the table '.DEnumItem::TABLE_NAME);
308: } catch (AlphaException $e) {
309: $body .= View::displayErrorMessage('Failed re-created the database table '.DEnumItem::TABLE_NAME.', check the log');
310: self::$logger->error($e->getMessage());
311: }
312:
313: return $body;
314: }
315: }
316: