      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 1548 2012-07-29 17:07:07Z alphadevx $
     19:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     20:  * @copyright Copyright (c) 2012, 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:                     // now save the DEnumItems
    163:                     $tmp = new DEnumItem();
    164:                     $denumItems = $tmp->loadItems($this->BO->getID());
    165:
    166:                     foreach ($denumItems as $item) {
    167:                         $item->set('value', $params['value_'.$item->getID()]);
    168:                         $item->save();
    169:                     }
    170:
    171:                     // handle new DEnumItem if posted
    172:                     if(isset($params['new_value']) && trim($params['new_value']) != '') {
    173:                         $newItem = new DEnumItem();
    174:                         $newItem->set('value', $params['new_value']);
    175:                         $newItem->set('DEnumID', $this->BO->getID());
    176:                         $newItem->save();
    177:                     }
    178:
    179:                     AlphaDAO::commit();
    180:
    181:                     $this->setStatusMessage(AlphaView::displayUpdateMessage(get_class($this->BO).' '.$this->BO->getID().' saved successfully.'));
    182:
    183:                     $this->doGET($params);
    184:                 }catch (FailedSaveException $e) {
    185:                     self::$logger->error('Unable to save the DEnum of id ['.$params['oid'].'], error was ['.$e->getMessage().']');
    186:                     AlphaDAO::rollback();
    187:                 }
    188:
    189:                 AlphaDAO::disconnect();
    190:             }
    191:         }catch(SecurityException $e) {
    192:             echo AlphaView::displayErrorMessage($e->getMessage());
    193:             self::$logger->warn($e->getMessage());
    194:         }catch(IllegalArguementException $e) {
    195:             echo AlphaView::displayErrorMessage($e->getMessage());
    196:             self::$logger->error($e->getMessage());
    197:         }catch(BONotFoundException $e) {
    198:             self::$logger->warn($e->getMessage());
    199:             echo AlphaView::displayErrorMessage('Failed to load the requested item from the database!');
    200:         }
    201:
    202:         self::$logger->debug('<<doPOST');
    203:     }
    204:
    205:     /**
    206:      * Using this callback to blank the new_value field when the page loads, regardless of anything being posted
    207:      *
    208:      * @return string
    209:      * @since 1.0
    210:      */
    211:     public function during_displayPageHead_callback() {
    212:         $html = '';
    213:         $html .= '<script language="javascript">';
    214:         $html .= 'function clearNewField() {';
    215:         $html .= '  document.getElementById("new_value").value = "";';
    216:         $html .= '}';
    217:         $html .= 'addOnloadEvent(clearNewField);';
    218:         $html .= '</script>';
    219:         return $html;
    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: ?>