      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:  * Controller used to display the details of a BO, which must be supplied in GET vars
     13:  *
     14:  * @package alpha::controller
     15:  * @since 1.0
     16:  * @author John Collins <dev@alphaframework.org>
     17:  * @version $Id: Detail.php 1646 2013-02-15 14:43:16Z alphadevx $
     18:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     19:  * @copyright Copyright (c) 2013, John Collins (founder of Alpha Framework).
     20:  * All rights reserved.
     21:  *
     22:  * <pre>
     23:  * Redistribution and use in source and binary forms, with or
     24:  * without modification, are permitted provided that the
     25:  * following conditions are met:
     26:  *
     27:  * * Redistributions of source code must retain the above
     28:  *   copyright notice, this list of conditions and the
     29:  *   following disclaimer.
     30:  * * Redistributions in binary form must reproduce the above
     31:  *   copyright notice, this list of conditions and the
     32:  *   following disclaimer in the documentation and/or other
     33:  *   materials provided with the distribution.
     34:  * * Neither the name of the Alpha Framework nor the names
     35:  *   of its contributors may be used to endorse or promote
     36:  *   products derived from this software without specific
     37:  *   prior written permission.
     38:  *
     39:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     40:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     41:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     42:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     43:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     44:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     45:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     46:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     47:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     48:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     49:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     50:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     51:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     52:  * </pre>
     53:  *
     54:  */
     55: class Detail extends AlphaController implements AlphaControllerInterface {
     56:     /**
     57:      * The BO to be displayed
     58:      *
     59:      * @var AlphaDAO
     60:      * @since 1.0
     61:      */
     62:     protected $BO;
     63:
     64:     /**
     65:      * The name of the BO
     66:      *
     67:      * @var string
     68:      * @since 1.0
     69:      */
     70:     private $BOName;
     71:
     72:     /**
     73:      * The default AlphaView object used for rendering the business object
     74:      *
     75:      * @var AlphaView
     76:      * @since 1.0
     77:      */
     78:     private $BOView;
     79:
     80:     /**
     81:      * Trace logger
     82:      *
     83:      * @var Logger
     84:      * @since 1.0
     85:      */
     86:     private static $logger = null;
     87:
     88:     /**
     89:      * constructor to set up the object
     90:      *
     91:      * @param string $visibility The name of the rights group that can access this controller.
     92:      * @since 1.0
     93:      */
     94:     public function __construct($visibility='Standard') {
     95:         self::$logger = new Logger('Detail');
     96:         self::$logger->debug('>>__construct()');
     97:
     98:         global $config;
     99:
    100:         // ensure that the super class constructor is called, indicating the rights group
    101:         parent::__construct($visibility);
    102:
    103:         self::$logger->debug('<<__construct');
    104:     }
    105:
    106:     /**
    107:      * Handle GET requests
    108:      *
    109:      * @param array $params
    110:      * @throws ResourceNotFoundException
    111:      * @throws IllegalArguementException
    112:      * @since 1.0
    113:      */
    114:     public function doGET($params) {
    115:         self::$logger->debug('>>doGET(params=['.var_export($params, true).'])');
    116:
    117:         try{
    118:             // load the business object (BO) definition
    119:             if (isset($params['bo']) && isset($params['oid'])) {
    120:                 if(!AlphaValidator::isInteger($params['oid']))
    121:                     throw new IllegalArguementException('Invalid oid ['.$params['oid'].'] provided on the request!');
    122:
    123:                 $BOName = $params['bo'];
    124:                 AlphaDAO::loadClassDef($BOName);
    125:
    126:                 /*
    127:                 *  check and see if a custom create controller exists for this BO, and if it does use it otherwise continue
    128:                 */
    129:                 if($this->getCustomControllerName($BOName, 'view') != null)
    130:                     $this->loadCustomController($BOName, 'view');
    131:
    132:                 $this->BO = new $BOName();
    133:                 $this->BO->load($params['oid']);
    134:                 AlphaDAO::disconnect();
    135:
    136:                 $this->BOName = $BOName;
    137:                 $this->BOView = AlphaView::getInstance($this->BO);
    138:
    139:                 echo AlphaView::displayPageHead($this);
    140:                 echo AlphaView::renderDeleteForm();
    141:                 echo $this->BOView->detailedView();
    142:             }else{
    143:                 throw new IllegalArguementException('No BO available to display!');
    144:             }
    145:         }catch(IllegalArguementException $e) {
    146:             self::$logger->warn($e->getMessage());
    147:             throw new ResourceNotFoundException('The file that you have requested cannot be found!');
    148:         }catch(BONotFoundException $e) {
    149:             self::$logger->warn($e->getMessage());
    150:             throw new ResourceNotFoundException('The item that you have requested cannot be found!');
    151:         }
    152:
    153:         echo AlphaView::displayPageFoot($this);
    154:         self::$logger->debug('<<doGET');
    155:     }
    156:
    157:     /**
    158:      * Method to handle POST requests
    159:      *
    160:      * @param array $params
    161:      * @throws IllegalArguementException
    162:      * @throws SecurityException
    163:      * @since 1.0
    164:      */
    165:     public function doPOST($params) {
    166:         self::$logger->debug('>>doPOST(params=['.var_export($params, true).'])');
    167:
    168:         global $config;
    169:
    170:         echo AlphaView::displayPageHead($this);
    171:
    172:         try {
    173:             // check the hidden security fields before accepting the form POST data
    174:             if(!$this->checkSecurityFields())
    175:                 throw new SecurityException('This page cannot accept post data from remote servers!');
    176:
    177:             // load the business object (BO) definition
    178:             if (isset($params['bo'])) {
    179:                 $BOName = $params['bo'];
    180:                 AlphaDAO::loadClassDef($BOName);
    181:
    182:                 $this->BO = new $BOName();
    183:                 $this->BOname = $BOName;
    184:                 $this->BOView = AlphaView::getInstance($this->BO);
    185:
    186:                 if (!empty($params['deleteOID'])) {
    187:                     if(!AlphaValidator::isInteger($params['deleteOID']))
    188:                         throw new IllegalArguementException('Invalid deleteOID ['.$params['deleteOID'].'] provided on the request!');
    189:
    190:                     $temp = new $BOName();
    191:                     $temp->load($params['deleteOID']);
    192:
    193:                     try {
    194:                         AlphaDAO::begin();
    195:                         $temp->delete();
    196:                         self::$logger->action('Deleted '.$BOName.' instance with OID '.$params['deleteOID']);
    197:                         AlphaDAO::commit();
    198:
    199:                         echo AlphaView::displayUpdateMessage($BOName.' '.$params['deleteOID'].' deleted successfully.');
    200:
    201:                         echo '<center>';
    202:
    203:                         $temp = new Button("document.location = '".FrontController::generateSecureURL('act=ListAll&bo='.get_class($this->BO))."'",
    204:                             'Back to List','cancelBut');
    205:                         echo $temp->render();
    206:
    207:                         echo '</center>';
    208:                     }catch(AlphaException $e) {
    209:                         self::$logger->error($e->getMessage());
    210:                         echo AlphaView::displayErrorMessage('Error deleting the BO of OID ['.$params['deleteOID'].'], check the log!');
    211:                         AlphaDAO::rollback();
    212:                     }
    213:
    214:                     AlphaDAO::disconnect();
    215:                 }
    216:             }else{
    217:                 throw new IllegalArguementException('No BO available to display!');
    218:             }
    219:         }catch(SecurityException $e) {
    220:             self::$logger->warn($e->getMessage());
    221:             throw new ResourceNotAllowedException($e->getMessage());
    222:         }catch(IllegalArguementException $e) {
    223:             self::$logger->warn($e->getMessage());
    224:             throw new ResourceNotFoundException('The file that you have requested cannot be found!');
    225:         }catch(BONotFoundException $e) {
    226:             self::$logger->warn($e->getMessage());
    227:             throw new ResourceNotFoundException('The item that you have requested cannot be found!');
    228:         }
    229:
    230:         echo AlphaView::displayPageFoot($this);
    231:         self::$logger->debug('<<doPOST');
    232:     }
    233:
    234:     /**
    235:      * Sets up the title etc.
    236:      *
    237:      * @since 1.0
    238:      */
    239:     public function before_displayPageHead_callback() {
    240:         if($this->title == '' && isset($this->BO))
    241:             $this->setTitle('Displaying '.$this->BOName.' number '.$this->BO->getID());
    242:         if($this->description == '' && isset($this->BO))
    243:             $this->setDescription('Page to display '.$this->BOName.' number '.$this->BO->getID());
    244:         if($this->keywords == '')
    245:             $this->setKeywords('display,details,'.$this->BOName);
    246:     }
    247:
    248:     /**
    249:      * Use this callback to inject in the admin menu template fragment for admin users of
    250:      * the backend only.
    251:      *
    252:      * @since 1.2
    253:      */
    254:     public function after_displayPageHead_callback() {
    255:         $menu = '';
    256:
    257:         if (isset($_SESSION['currentUser']) && AlphaDAO::isInstalled() && $_SESSION['currentUser']->inGroup('Admin') && strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
    258:             $menu .= AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
    259:         }
    260:
    261:         return $menu;
    262:     }
    263: }
    264:
    265: // now build the new controller
    266: if(basename($_SERVER['PHP_SELF']) == 'Detail.php') {
    267:     $controller = new Detail();
    268:
    269:     if(!empty($_POST)) {
    270:         $controller->doPOST($_REQUEST);
    271:     }else{
    272:         $controller->doGET($_GET);
    273:     }
    274: }
    275:
    276: ?>