      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 display the details of a 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: Detail.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) 2011, 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 Detail extends AlphaController implements AlphaControllerInterface {
     57:     /**
     58:      * The BO to be displayed
     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:     private $BOName;
     72:
     73:     /**
     74:      * The default AlphaView object used for rendering the business object
     75:      *
     76:      * @var AlphaView
     77:      * @since 1.0
     78:      */
     79:     private $BOView;
     80:
     81:     /**
     82:      * Trace logger
     83:      *
     84:      * @var Logger
     85:      * @since 1.0
     86:      */
     87:     private static $logger = null;
     88:
     89:     /**
     90:      * constructor to set up the object
     91:      *
     92:      * @param string $visibility The name of the rights group that can access this controller.
     93:      * @since 1.0
     94:      */
     95:     public function __construct($visibility='Standard') {
     96:         self::$logger = new Logger('Detail');
     97:         self::$logger->debug('>>__construct()');
     98:
     99:         global $config;
    100:
    101:         // ensure that the super class constructor is called, indicating the rights group
    102:         parent::__construct($visibility);
    103:
    104:         self::$logger->debug('<<__construct');
    105:     }
    106:
    107:     /**
    108:      * Handle GET requests
    109:      *
    110:      * @param array $params
    111:      * @throws ResourceNotFoundException
    112:      * @throws IllegalArguementException
    113:      * @since 1.0
    114:      */
    115:     public function doGET($params) {
    116:         self::$logger->debug('>>doGET(params=['.var_export($params, true).'])');
    117:
    118:         try{
    119:             // load the business object (BO) definition
    120:             if (isset($params['bo']) && isset($params['oid'])) {
    121:                 if(!AlphaValidator::isInteger($params['oid']))
    122:                     throw new IllegalArguementException('Invalid oid ['.$params['oid'].'] provided on the request!');
    123:
    124:                 $BOName = $params['bo'];
    125:                 AlphaDAO::loadClassDef($BOName);
    126:
    127:                 /*
    128:                 *  check and see if a custom create controller exists for this BO, and if it does use it otherwise continue
    129:                 */
    130:                 if($this->getCustomControllerName($BOName, 'view') != null)
    131:                     $this->loadCustomController($BOName, 'view');
    132:
    133:                 $this->BO = new $BOName();
    134:                 $this->BO->load($params['oid']);
    135:                 AlphaDAO::disconnect();
    136:
    137:                 $this->BOName = $BOName;
    138:                 $this->BOView = AlphaView::getInstance($this->BO);
    139:
    140:                 echo AlphaView::displayPageHead($this);
    141:                 echo AlphaView::renderDeleteForm();
    142:                 echo $this->BOView->detailedView();
    143:             }else{
    144:                 throw new IllegalArguementException('No BO available to display!');
    145:             }
    146:         }catch(IllegalArguementException $e) {
    147:             self::$logger->warn($e->getMessage());
    148:             throw new ResourceNotFoundException('The file that you have requested cannot be found!');
    149:         }catch(BONotFoundException $e) {
    150:             self::$logger->warn($e->getMessage());
    151:             throw new ResourceNotFoundException('The item that you have requested cannot be found!');
    152:         }
    153:
    154:         echo AlphaView::displayPageFoot($this);
    155:         self::$logger->debug('<<doGET');
    156:     }
    157:
    158:     /**
    159:      * Method to handle POST requests
    160:      *
    161:      * @param array $params
    162:      * @throws IllegalArguementException
    163:      * @throws SecurityException
    164:      * @since 1.0
    165:      */
    166:     public function doPOST($params) {
    167:         self::$logger->debug('>>doPOST(params=['.var_export($params, true).'])');
    168:
    169:         global $config;
    170:
    171:         echo AlphaView::displayPageHead($this);
    172:
    173:         try {
    174:             // check the hidden security fields before accepting the form POST data
    175:             if(!$this->checkSecurityFields())
    176:                 throw new SecurityException('This page cannot accept post data from remote servers!');
    177:
    178:             // load the business object (BO) definition
    179:             if (isset($params['bo'])) {
    180:                 $BOName = $params['bo'];
    181:                 AlphaDAO::loadClassDef($BOName);
    182:
    183:                 $this->BO = new $BOName();
    184:                 $this->BOname = $BOName;
    185:                 $this->BOView = AlphaView::getInstance($this->BO);
    186:
    187:                 if (!empty($params['deleteOID'])) {
    188:                     if(!AlphaValidator::isInteger($params['deleteOID']))
    189:                         throw new IllegalArguementException('Invalid deleteOID ['.$params['deleteOID'].'] provided on the request!');
    190:
    191:                     $temp = new $BOName();
    192:                     $temp->load($params['deleteOID']);
    193:
    194:                     try {
    195:                         AlphaDAO::begin();
    196:                         $temp->delete();
    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: ?>