      1: <?php
      2:
      3: // include the config file
      4: if(!isset($config)) {
      5:     require_once '../util/AlphaConfig.inc';
      6:     $config = AlphaConfig::getInstance();
      7:
      8:     require_once $config->get('app.root').'alpha/util/AlphaAutoLoader.inc';
      9: }
     10:
     11: /**
     12:  *
     13:  * Controller used to edit DEnums and associated DEnumItems
     14:  *
     15:  * @package alpha::controller
     16:  * @since 1.0
     17:  * @author John Collins <dev@alphaframework.org>
     18:  * @version $Id: EditDEnum.php 1745 2014-03-29 15:19:05Z alphadevx $
     19:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     20:  * @copyright Copyright (c) 2014, John Collins (founder of Alpha Framework).
     21:  * All rights reserved.
     22:  *
     23:  * <pre>
     24:  * Redistribution and use in source and binary forms, with or
     25:  * without modification, are permitted provided that the
     26:  * following conditions are met:
     27:  *
     28:  * * Redistributions of source code must retain the above
     29:  *   copyright notice, this list of conditions and the
     30:  *   following disclaimer.
     31:  * * Redistributions in binary form must reproduce the above
     32:  *   copyright notice, this list of conditions and the
     33:  *   following disclaimer in the documentation and/or other
     34:  *   materials provided with the distribution.
     35:  * * Neither the name of the Alpha Framework nor the names
     36:  *   of its contributors may be used to endorse or promote
     37:  *   products derived from this software without specific
     38:  *   prior written permission.
     39:  *
     40:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     41:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     42:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     43:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     44:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     45:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     46:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     47:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     48:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     49:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     50:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     51:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     52:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     53:  * </pre>
     54:  *
     55:  */
     56: class EditDEnum extends Edit implements AlphaControllerInterface {
     57:     /**
     58:      * Trace logger
     59:      *
     60:      * @var Logger
     61:      * @since 1.0
     62:      */
     63:     private static $logger = null;
     64:
     65:     /**
     66:      * constructor to set up the object
     67:      *
     68:      * @since 1.0
     69:      */
     70:     public function __construct() {
     71:         self::$logger = new Logger('EditDEnum');
     72:         self::$logger->debug('>>__construct()');
     73:
     74:         // ensure that the super class constructor is called, indicating the rights group
     75:         parent::__construct('Admin');
     76:
     77:         // set up the title and meta details
     78:         $this->setTitle('Editing a DEnum');
     79:         $this->setDescription('Page to edit a DEnum.');
     80:         $this->setKeywords('edit,DEnum');
     81:
     82:         $this->BO = new DEnum();
     83:
     84:         self::$logger->debug('<<__construct');
     85:     }
     86:
     87:     /**
     88:      * Handle GET requests
     89:      *
     90:      * @param array $params
     91:      * @since 1.0
     92:      */
     93:     public function doGET($params) {
     94:         self::$logger->debug('>>doGET($params=['.var_export($params, true).'])');
     95:
     96:         global $config;
     97:
     98:         echo AlphaView::displayPageHead($this);
     99:
    100:         $message = $this->getStatusMessage();
    101:         if(!empty($message))
    102:             echo $message;
    103:
    104:         // ensure that a OID is provided
    105:         if (isset($params['oid'])) {
    106:             $BOoid = $params['oid'];
    107:         }else{
    108:             throw new IllegalArguementException('Could not load the DEnum object as an oid was not supplied!');
    109:             return;
    110:         }
    111:
    112:         try {
    113:             $this->BO->load($BOoid);
    114:
    115:             AlphaDAO::disconnect();
    116:
    117:             $this->BOName = 'DEnum';
    118:
    119:             $this->BOView = AlphaView::getInstance($this->BO);
    120:
    121:             echo AlphaView::renderDeleteForm();
    122:
    123:             echo $this->BOView->editView();
    124:         }catch(BONotFoundException $e) {
    125:             self::$logger->error('Unable to load the DEnum of id ['.$params['oid'].'], error was ['.$e->getMessage().']');
    126:         }
    127:
    128:         echo AlphaView::displayPageFoot($this);
    129:
    130:         self::$logger->debug('<<doGET');
    131:     }
    132:
    133:     /**
    134:      * Handle POST requests
    135:      *
    136:      * @param array $params
    137:      * @since 1.0
    138:      */
    139:     public function doPOST($params) {
    140:         self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
    141:
    142:         try {
    143:             // check the hidden security fields before accepting the form POST data
    144:             if(!$this->checkSecurityFields()) {
    145:                 throw new SecurityException('This page cannot accept post data from remote servers!');
    146:                 self::$logger->debug('<<doPOST');
    147:             }
    148:
    149:             // ensure that a OID is provided
    150:             if (isset($params['oid'])) {
    151:                 $BOoid = $params['oid'];
    152:             }else{
    153:                 throw new IllegalArguementException('Could not load the DEnum object as an oid was not supplied!');
    154:             }
    155:
    156:             if (isset($params['saveBut'])) {
    157:                 try {
    158:                     $this->BO->load($BOoid);
    159:                     // update the object from post data
    160:                     $this->BO->populateFromPost();
    161:
    162:                     AlphaDAO::begin();
    163:
    164:                     $this->BO->save();
    165:
    166:                     self::$logger->action('DEnum '.$this->BO->getOID().' saved');
    167:
    168:                     // now save the DEnumItems
    169:                     $tmp = new DEnumItem();
    170:                     $denumItems = $tmp->loadItems($this->BO->getID());
    171:
    172:                     foreach ($denumItems as $item) {
    173:                         $item->set('value', $params['value_'.$item->getID()]);
    174:                         $item->save();
    175:
    176:                         self::$logger->action('DEnumItem '.$item->getOID().' saved');
    177:                     }
    178:
    179:                     // handle new DEnumItem if posted
    180:                     if(isset($params['new_value']) && trim($params['new_value']) != '') {
    181:                         $newItem = new DEnumItem();
    182:                         $newItem->set('value', $params['new_value']);
    183:                         $newItem->set('DEnumID', $this->BO->getID());
    184:                         $newItem->save();
    185:
    186:                         self::$logger->action('DEnumItem '.$newItem->getOID().' created');
    187:                     }
    188:
    189:                     AlphaDAO::commit();
    190:
    191:                     $this->setStatusMessage(AlphaView::displayUpdateMessage(get_class($this->BO).' '.$this->BO->getID().' saved successfully.'));
    192:
    193:                     return $this->doGET($params);
    194:                 }catch (FailedSaveException $e) {
    195:                     self::$logger->error('Unable to save the DEnum of id ['.$params['oid'].'], error was ['.$e->getMessage().']');
    196:                     AlphaDAO::rollback();
    197:                 }
    198:
    199:                 AlphaDAO::disconnect();
    200:             }
    201:         }catch(SecurityException $e) {
    202:             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
    203:             self::$logger->warn($e->getMessage());
    204:         }catch(IllegalArguementException $e) {
    205:             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
    206:             self::$logger->error($e->getMessage());
    207:         }catch(BONotFoundException $e) {
    208:             self::$logger->warn($e->getMessage());
    209:             $this->setStatusMessage(AlphaView::displayErrorMessage('Failed to load the requested item from the database!'));
    210:         }
    211:
    212:         echo AlphaView::displayPageHead($this);
    213:
    214:         $message = $this->getStatusMessage();
    215:         if(!empty($message))
    216:             echo $message;
    217:
    218:         echo AlphaView::displayPageFoot($this);
    219:         self::$logger->debug('<<doPOST');
    220:     }
    221:
    222:     /**
    223:      * Use this callback to inject in the admin menu template fragment
    224:      *
    225:      * @since 1.2
    226:      */
    227:     public function after_displayPageHead_callback() {
    228:         $menu = AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
    229:
    230:         return $menu;
    231:     }
    232: }
    233:
    234: // now build the new controller if this file is called directly
    235: if ('EditDEnum.php' == basename($_SERVER['PHP_SELF'])) {
    236:     $controller = new EditDEnum();
    237:
    238:     if(!empty($_POST)) {
    239:         $controller->doPOST($_REQUEST);
    240:     }else{
    241:         $controller->doGET($_GET);
    242:     }
    243: }
    244:
    245: ?>