      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 BO, which must be supplied in GET vars
     14:  *
     15:  * @package alpha::controller
     16:  * @since 1.0
     17:  * @author John Collins <dev@alphaframework.org>
     18:  * @version $Id: Edit.php 1693 2013-12-09 23:33:24Z 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 Edit extends AlphaController implements AlphaControllerInterface {
     57:     /**
     58:      * The business object to be edited
     59:      *
     60:      * @var AlphaDAO
     61:      * @since 1.0
     62:      */
     63:     protected $BO;
     64:
     65:     /**
     66:      * The name of the BO
     67:      *
     68:      * @var string
     69:      * @since 1.0
     70:      */
     71:     protected $BOName;
     72:
     73:     /**
     74:      * The OID of the BO to be edited
     75:      *
     76:      * @var integer
     77:      * @since 1.0
     78:      */
     79:     private $BOoid;
     80:
     81:     /**
     82:      * The AlphaView object used for rendering the object to edit
     83:      *
     84:      * @var AlphaView
     85:      * @since 1.0
     86:      */
     87:     private $BOView;
     88:
     89:     /**
     90:      * Trace logger
     91:      *
     92:      * @var Logger
     93:      * @since 1.0
     94:      */
     95:     private static $logger = null;
     96:
     97:     /**
     98:      * constructor to set up the object
     99:      *
    100:      * @param string $visibility The name of the rights group that can access this controller.
    101:      * @since 1.0
    102:      */
    103:     public function __construct($visibility='Admin') {
    104:         self::$logger = new Logger('Edit');
    105:         self::$logger->debug('>>__construct()');
    106:
    107:         global $config;
    108:
    109:         // ensure that the super class constructor is called, indicating the rights group
    110:         parent::__construct($visibility);
    111:
    112:         self::$logger->debug('<<__construct');
    113:     }
    114:
    115:     /**
    116:      * Handle GET requests
    117:      *
    118:      * @param array $params
    119:      * @since 1.0
    120:      */
    121:     public function doGET($params) {
    122:         self::$logger->debug('>>doGET(params=['.var_export($params, true).'])');
    123:
    124:         try{
    125:             // load the business object (BO) definition
    126:             if (isset($params['bo']) && isset($params['oid'])) {
    127:                 $BOName = $params['bo'];
    128:                 AlphaDAO::loadClassDef($BOName);
    129:
    130:                 /*
    131:                  *  check and see if a custom create controller exists for this BO, and if it does use it otherwise continue
    132:                  */
    133:                 if($this->getCustomControllerName($BOName, 'edit') != null)
    134:                     $this->loadCustomController($BOName, 'edit');
    135:
    136:                 $this->BO = new $BOName();
    137:                 $this->BO->load($params['oid']);
    138:
    139:                 AlphaDAO::disconnect();
    140:
    141:                 $this->BOName = $BOName;
    142:
    143:                 $this->BOView = AlphaView::getInstance($this->BO);
    144:
    145:                 // set up the title and meta details
    146:                 if($this->title == '')
    147:                     $this->setTitle('Editing a '.$BOName);
    148:                 if($this->description == '')
    149:                     $this->setDescription('Page to edit a '.$BOName.'.');
    150:                 if($this->keywords == '')
    151:                     $this->setKeywords('edit,'.$BOName);
    152:
    153:                 echo AlphaView::displayPageHead($this);
    154:
    155:                 echo AlphaView::renderDeleteForm();
    156:
    157:                 echo $this->BOView->editView();
    158:             }else{
    159:                 throw new IllegalArguementException('No BO available to edit!');
    160:             }
    161:         }catch(IllegalArguementException $e) {
    162:             self::$logger->error($e->getMessage());
    163:         }catch(BONotFoundException $e) {
    164:             self::$logger->warn($e->getMessage());
    165:             echo '<p class="error"><br>Failed to load the requested item from the database!</p>';
    166:         }
    167:
    168:         echo AlphaView::displayPageFoot($this);
    169:
    170:         self::$logger->debug('<<doGET');
    171:     }
    172:
    173:     /**
    174:      * Handle POST requests
    175:      *
    176:      * @param array $params
    177:      * @param string $saveMessage Optional status message to display on successful save of the BO, otherwise default will be used
    178:      * @since 1.0
    179:      */
    180:     public function doPOST($params, $saveMessage='') {
    181:         self::$logger->debug('>>doPOST(params=['.var_export($params, true).'])');
    182:
    183:         global $config;
    184:
    185:         try {
    186:             // check the hidden security fields before accepting the form POST data
    187:             if(!$this->checkSecurityFields()) {
    188:                 throw new SecurityException('This page cannot accept post data from remote servers!');
    189:                 self::$logger->debug('<<doPOST');
    190:             }
    191:
    192:             // load the business object (BO) definition
    193:             if (isset($params['bo']) && isset($params['oid'])) {
    194:                 $BOName = $params['bo'];
    195:                 AlphaDAO::loadClassDef($BOName);
    196:
    197:                 $this->BO = new $BOName();
    198:                 $this->BO->load($params['oid']);
    199:
    200:                 $this->BOView = AlphaView::getInstance($this->BO);
    201:
    202:                 // set up the title and meta details
    203:                 $this->setTitle('Editing a '.$BOName);
    204:                 $this->setDescription('Page to edit a '.$BOName.'.');
    205:                 $this->setKeywords('edit,'.$BOName);
    206:
    207:                 echo AlphaView::displayPageHead($this);
    208:
    209:                 if (isset($params['saveBut'])) {
    210:
    211:                     // populate the transient object from post data
    212:                     $this->BO->populateFromPost();
    213:
    214:                     try {
    215:                         $this->BO->save();
    216:
    217:                         self::$logger->action('Saved '.$BOName.' instance with OID '.$this->BO->getOID());
    218:
    219:                         if($saveMessage == '')
    220:                             echo AlphaView::displayUpdateMessage(get_class($this->BO).' '.$this->BO->getID().' saved successfully.');
    221:                         else
    222:                             echo AlphaView::displayUpdateMessage($saveMessage);
    223:                     }catch (LockingException $e) {
    224:                         $this->BO->reload();
    225:                         echo AlphaView::displayErrorMessage($e->getMessage());
    226:                     }
    227:
    228:                     AlphaDAO::disconnect();
    229:
    230:                     echo $this->BOView->editView();
    231:                 }
    232:
    233:                 if (!empty($params['deleteOID'])) {
    234:                     $temp = new $BOName();
    235:                     $temp->load($params['deleteOID']);
    236:
    237:                     try {
    238:                         $temp->delete();
    239:
    240:                         self::$logger->action('Deleted '.$BOName.' instance with OID '.$params['deleteOID']);
    241:
    242:                         AlphaDAO::disconnect();
    243:
    244:                         echo AlphaView::displayUpdateMessage($this->BOName.' '.$params['deleteOID'].' deleted successfully.');
    245:
    246:                         echo '<center>';
    247:
    248:                         $temp = new Button("document.location = '".FrontController::generateSecureURL('act=ListAll&bo='.get_class($this->BO))."'",
    249:                             'Back to List','cancelBut');
    250:                         echo $temp->render();
    251:
    252:                         echo '</center>';
    253:                     }catch(AlphaException $e) {
    254:                         self::$logger->error($e->getMessage());
    255:                         echo AlphaView::displayErrorMessage('Error deleting the OID ['.$params['deleteOID'].'], check the log!');
    256:                     }
    257:                 }
    258:             }else{
    259:                 throw new IllegalArguementException('No BO available to edit!');
    260:             }
    261:         }catch(SecurityException $e) {
    262:             echo AlphaView::displayErrorMessage($e->getMessage());
    263:             self::$logger->warn($e->getMessage());
    264:         }catch(IllegalArguementException $e) {
    265:             echo AlphaView::displayErrorMessage($e->getMessage());
    266:             self::$logger->error($e->getMessage());
    267:         }catch(BONotFoundException $e) {
    268:             self::$logger->warn($e->getMessage());
    269:             echo AlphaView::displayErrorMessage('Failed to load the requested item from the database!');
    270:         }
    271:
    272:         echo AlphaView::displayPageFoot($this);
    273:
    274:         self::$logger->debug('<<doPOST');
    275:     }
    276:
    277:     /**
    278:      * Use this callback to inject in the admin menu template fragment for admin users of
    279:      * the backend only.
    280:      *
    281:      * @since 1.2
    282:      */
    283:     public function after_displayPageHead_callback() {
    284:         $menu = '';
    285:
    286:         if (isset($_SESSION['currentUser']) && AlphaDAO::isInstalled() && $_SESSION['currentUser']->inGroup('Admin') && mb_strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
    287:             $menu .= AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
    288:         }
    289:
    290:         return $menu;
    291:     }
    292: }
    293:
    294: // now build the new controller
    295: if(basename($_SERVER['PHP_SELF']) == 'Edit.php') {
    296:     $controller = new Edit();
    297:
    298:     if(!empty($_POST)) {
    299:         $controller->doPOST($_REQUEST);
    300:     }else{
    301:         $controller->doGET($_GET);
    302:     }
    303: }
    304:
    305: ?>