Alpha Framework alpha--controller
[ class tree: alpha--controller ] [ index: alpha--controller ] [ all elements ]

Source for file Edit.php

Documentation is available at Edit.php

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

Documentation generated on Tue, 13 Dec 2011 20:26:46 +0000 by phpDocumentor 1.4.3