Overview

Namespaces

  • Alpha
    • Controller
      • Front
    • Exception
    • Model
      • Type
    • Task
    • Util
      • Backup
      • Cache
      • Code
        • Highlight
        • Metric
      • Config
      • Convertor
      • Email
      • Extension
      • Feed
      • File
      • Graph
      • Helper
      • Http
        • Filter
        • Session
      • Image
      • Logging
      • Search
      • Security
    • View
      • Renderer
        • Html
        • Json
      • Widget

Classes

  • ActiveRecordController
  • ArticleController
  • AttachmentController
  • CacheController
  • Controller
  • DEnumController
  • ExcelController
  • FeedController
  • GenSecureQueryStringController
  • ImageController
  • IndexController
  • InstallController
  • ListActiveRecordsController
  • LogController
  • LoginController
  • LogoutController
  • MetricController
  • PhpinfoController
  • RecordSelectorController
  • SearchController
  • SequenceController
  • TagController

Interfaces

  • ControllerInterface
  • Overview
  • Namespace
  • Class
  • Tree
  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:  * Controller used to edit DEnums and associated DEnumItems.
 19:  *
 20:  * @since 1.0
 21:  *
 22:  * @author John Collins <dev@alphaframework.org>
 23:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 24:  * @copyright Copyright (c) 2015, 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:      * constructor to set up the object.
 72:      *
 73:      * @since 1.0
 74:      */
 75:     public function __construct()
 76:     {
 77:         self::$logger = new Logger('DEnumController');
 78:         self::$logger->debug('>>__construct()');
 79: 
 80:         // ensure that the super class constructor is called, indicating the rights group
 81:         parent::__construct('Admin');
 82: 
 83:         $this->BO = new DEnum();
 84: 
 85:         self::$logger->debug('<<__construct');
 86:     }
 87: 
 88:     /**
 89:      * Handle GET requests.
 90:      *
 91:      * @param Alpha\Util\Http\Request $request
 92:      *
 93:      * @return Alpha\Util\Http\Response
 94:      *
 95:      * @since 1.0
 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:         // load one DEnum
108:         if (isset($params['denumOID'])) {
109:             $BOoid = $params['denumOID'];
110: 
111:             // set up the title and meta details
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 { // load all DEnums
139:             // set up the title and meta details
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:             // make sure that the DEnum tables exist
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:             // get all of the BOs and invoke the list view on each one
153: 
154:             // set the start point for the list pagination
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:      * Handle POST requests.
180:      *
181:      * @param Alpha\Util\Http\Request $request
182:      *
183:      * @return Alpha\Util\Http\Response
184:      *
185:      * @throws Alpha\Exception\SecurityException
186:      *
187:      * @since 1.0
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:             // check the hidden security fields before accepting the form POST data
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:             // ensure that a OID is provided
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:                     // update the object from post data
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:                     // now save the DEnumItems
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:                     // handle new DEnumItem if posted
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:      * Method to create the DEnum tables if they don't exist.
280:      *
281:      * @since 1.0
282:      *
283:      * @return string
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: 
Alpha Framework 2.0.4 API Documentation API documentation generated by ApiGen 2.8.0