      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 1647 2013-02-19 16:11:21Z alphadevx $
     19:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     20:  * @copyright Copyright (c) 2013, 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:         // ensure that a OID is provided
    101:         if (isset($params['oid'])) {
    102:             $BOoid = $params['oid'];
    103:         }else{
    104:             throw new IllegalArguementException('Could not load the DEnum object as an oid was not supplied!');
    105:             return;
    106:         }
    107:
    108:         try {
    109:             $this->BO->load($BOoid);
    110:
    111:             AlphaDAO::disconnect();
    112:
    113:             $this->BOName = 'DEnum';
    114:
    115:             $this->BOView = AlphaView::getInstance($this->BO);
    116:
    117:             echo AlphaView::renderDeleteForm();
    118:
    119:             echo $this->BOView->editView();
    120:         }catch(BONotFoundException $e) {
    121:             self::$logger->error('Unable to load the DEnum of id ['.$params['oid'].'], error was ['.$e->getMessage().']');
    122:         }
    123:
    124:         echo AlphaView::displayPageFoot($this);
    125:
    126:         self::$logger->debug('<<doGET');
    127:     }
    128:
    129:     /**
    130:      * Handle POST requests
    131:      *
    132:      * @param array $params
    133:      * @since 1.0
    134:      */
    135:     public function doPOST($params) {
    136:         self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
    137:
    138:         try {
    139:             // check the hidden security fields before accepting the form POST data
    140:             if(!$this->checkSecurityFields()) {
    141:                 throw new SecurityException('This page cannot accept post data from remote servers!');
    142:                 self::$logger->debug('<<doPOST');
    143:             }
    144:
    145:             // ensure that a OID is provided
    146:             if (isset($params['oid'])) {
    147:                 $BOoid = $params['oid'];
    148:             }else{
    149:                 throw new IllegalArguementException('Could not load the DEnum object as an oid was not supplied!');
    150:             }
    151:
    152:             if (isset($params['saveBut'])) {
    153:                 try {
    154:                     $this->BO->load($BOoid);
    155:                     // update the object from post data
    156:                     $this->BO->populateFromPost();
    157:
    158:                     AlphaDAO::begin();
    159:
    160:                     $this->BO->save();
    161:
    162:                     self::$logger->action('DEnum '.$this->BO->getOID().' saved');
    163:
    164:                     // now save the DEnumItems
    165:                     $tmp = new DEnumItem();
    166:                     $denumItems = $tmp->loadItems($this->BO->getID());
    167:
    168:                     foreach ($denumItems as $item) {
    169:                         $item->set('value', $params['value_'.$item->getID()]);
    170:                         $item->save();
    171:
    172:                         self::$logger->action('DEnumItem '.$item->getOID().' saved');
    173:                     }
    174:
    175:                     // handle new DEnumItem if posted
    176:                     if(isset($params['new_value']) && trim($params['new_value']) != '') {
    177:                         $newItem = new DEnumItem();
    178:                         $newItem->set('value', $params['new_value']);
    179:                         $newItem->set('DEnumID', $this->BO->getID());
    180:                         $newItem->save();
    181:
    182:                         self::$logger->action('DEnumItem '.$newItem->getOID().' created');
    183:                     }
    184:
    185:                     AlphaDAO::commit();
    186:
    187:                     $this->setStatusMessage(AlphaView::displayUpdateMessage(get_class($this->BO).' '.$this->BO->getID().' saved successfully.'));
    188:
    189:                     $this->doGET($params);
    190:                 }catch (FailedSaveException $e) {
    191:                     self::$logger->error('Unable to save the DEnum of id ['.$params['oid'].'], error was ['.$e->getMessage().']');
    192:                     AlphaDAO::rollback();
    193:                 }
    194:
    195:                 AlphaDAO::disconnect();
    196:             }
    197:         }catch(SecurityException $e) {
    198:             echo AlphaView::displayErrorMessage($e->getMessage());
    199:             self::$logger->warn($e->getMessage());
    200:         }catch(IllegalArguementException $e) {
    201:             echo AlphaView::displayErrorMessage($e->getMessage());
    202:             self::$logger->error($e->getMessage());
    203:         }catch(BONotFoundException $e) {
    204:             self::$logger->warn($e->getMessage());
    205:             echo AlphaView::displayErrorMessage('Failed to load the requested item from the database!');
    206:         }
    207:
    208:         self::$logger->debug('<<doPOST');
    209:     }
    210:
    211:     /**
    212:      * Using this callback to blank the new_value field when the page loads, regardless of anything being posted
    213:      *
    214:      * @return string
    215:      * @since 1.0
    216:      */
    217:     public function during_displayPageHead_callback() {
    218:         $html = '';
    219:         $html .= '<script language="javascript">';
    220:         $html .= 'function clearNewField() {';
    221:         $html .= '  document.getElementById("new_value").value = "";';
    222:         $html .= '}';
    223:         $html .= 'addOnloadEvent(clearNewField);';
    224:         $html .= '</script>';
    225:         return $html;
    226:     }
    227:
    228:     /**
    229:      * Use this callback to inject in the admin menu template fragment
    230:      *
    231:      * @since 1.2
    232:      */
    233:     public function after_displayPageHead_callback() {
    234:         $menu = AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
    235:
    236:         return $menu;
    237:     }
    238: }
    239:
    240: // now build the new controller if this file is called directly
    241: if ('EditDEnum.php' == basename($_SERVER['PHP_SELF'])) {
    242:     $controller = new EditDEnum();
    243:
    244:     if(!empty($_POST)) {
    245:         $controller->doPOST($_REQUEST);
    246:     }else{
    247:         $controller->doGET($_GET);
    248:     }
    249: }
    250:
    251: ?>