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

Source for file Create.php

Documentation is available at Create.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 create a new BO, whose classname 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: Create.php 1341 2011-03-17 15:02:02Z 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 Create extends AlphaController implements AlphaControllerInterface {
  59.     /**
  60.      * The name of the BO
  61.      * 
  62.      * @var string 
  63.      * @since 1.0
  64.      */
  65.     protected $BOname;
  66.     
  67.     /**
  68.      * The new BO to be created
  69.      * 
  70.      * @var AlphaDAO 
  71.      * @since 1.0
  72.      */
  73.     protected $BO;
  74.     
  75.     /**
  76.      * The AlphaView object used for rendering the objects to create
  77.      * 
  78.      * @var AlphaView 
  79.      * @since 1.0
  80.      */
  81.     private $BOView;
  82.     
  83.     /**
  84.      * Trace logger
  85.      * 
  86.      * @var Logger 
  87.      * @since 1.0
  88.      */
  89.     private static $logger null;
  90.                                 
  91.     /**
  92.      * Constructor to set up the object
  93.      * 
  94.      * @param string $visibility 
  95.      * @since 1.0
  96.      */
  97.     public function __construct($visibility='Admin'{
  98.         self::$logger new Logger('Create');
  99.         self::$logger->debug('>>__construct(visibility=['.$visibility.'])');
  100.         
  101.         global $config;
  102.         
  103.         // ensure that the super class constructor is called, indicating the rights group
  104.         parent::__construct($visibility);
  105.         
  106.         self::$logger->debug('<<__construct');
  107.     }
  108.     
  109.     /**
  110.      * Handle GET requests
  111.      * 
  112.      * @param array $params 
  113.      * @throws IllegalArguementException
  114.      * @throws ResourceNotFoundException
  115.      * @since 1.0
  116.      */
  117.     public function doGET($params{
  118.         self::$logger->debug('>>doGET($params=['.var_export($paramstrue).'])');
  119.         
  120.         try{
  121.             // load the business object (BO) definition
  122.             if (isset($params['bo'])) {
  123.                 $BOname $params['bo'];
  124.                 $this->BOname = $BOname;
  125.             }elseif(isset($this->BOname)) {
  126.                 $BOname $this->BOname;
  127.             }else{
  128.                 throw new IllegalArguementException('No BO available to create!');
  129.             }
  130.             
  131.             AlphaDAO::loadClassDef($BOname);
  132.         
  133.             /*
  134.              *  check and see if a custom create controller exists for this BO, and if it does use it otherwise continue
  135.              */
  136.             if($this->getCustomControllerName($BOname'create'!= null)
  137.                 $this->loadCustomController($BOname'create');
  138.         
  139.             $this->BO = new $BOname();
  140.                 
  141.             $this->BOView AlphaView::getInstance($this->BO);
  142.                 
  143.             // set up the title and meta details
  144.             if(!isset($this->title))
  145.                 $this->setTitle('Create a new '.$BOname);
  146.             if(!isset($this->description))
  147.                 $this->setDescription('Page to create a new '.$BOname.'.');
  148.             if(!isset($this->keywords))
  149.                 $this->setKeywords('create,new,'.$BOname);                
  150.                         
  151.             echo AlphaView::displayPageHead($this);
  152.                 
  153.             echo $this->BOView->createView();
  154.         }catch(IllegalArguementException $e{
  155.             self::$logger->warn($e->getMessage());
  156.             throw new ResourceNotFoundException('The file that you have requested cannot be found!');
  157.         }
  158.         
  159.         echo AlphaView::displayPageFoot($this);
  160.         
  161.         self::$logger->debug('<<doGET');
  162.     }
  163.     
  164.     /**
  165.      * Method to handle POST requests
  166.      * 
  167.      * @param array $params 
  168.      * @throws ResourceNotAllowedException
  169.      * @since 1.0
  170.      */
  171.     public function doPOST($params{
  172.         self::$logger->debug('>>doPOST($params=['.var_export($paramstrue).'])');
  173.         
  174.         global $config;
  175.         
  176.         try {
  177.             // check the hidden security fields before accepting the form POST data
  178.             if(!$this->checkSecurityFields())
  179.                 throw new SecurityException('This page cannot accept post data from remote servers!');
  180.             
  181.             // load the business object (BO) definition
  182.             if (isset($params['bo'])) {
  183.                 $BOname $params['bo'];
  184.                 $this->BOname = $BOname;
  185.             }elseif(isset($this->BOname)) {
  186.                 $BOname $this->BOname;
  187.             }else{
  188.                 throw new IllegalArguementException('No BO available to create!');
  189.             }
  190.             
  191.             AlphaDAO::loadClassDef($BOname);
  192.                 
  193.             $this->BO = new $BOname();
  194.         
  195.             if (isset($params['createBut'])) {            
  196.                 // populate the transient object from post data
  197.                 $this->BO->populateFromPost();
  198.                             
  199.                 $this->BO->save();
  200.     
  201.                 AlphaDAO::disconnect();
  202.                 
  203.                 try {
  204.                     if ($this->getNextJob(!= '')                    
  205.                         header('Location: '.$this->getNextJob());
  206.                     else                    
  207.                         header('Location: '.FrontController::generateSecureURL('act=Detail&bo='.get_class($this->BO).'&oid='.$this->BO->getID()));
  208.                 }catch(AlphaException $e{
  209.                     echo AlphaView::displayPageHead($this);
  210.                     self::$logger->error($e->getTraceAsString());
  211.                     echo AlphaView::displayErrorMessage('Error creating the new ['.$BOname.'], check the log!');
  212.                 }
  213.             }
  214.             
  215.             if (isset($params['cancelBut'])) {
  216.                 header('Location: '.FrontController::generateSecureURL('act=ListBusinessObjects'));
  217.             }
  218.         }catch(SecurityException $e{
  219.             self::$logger->warn($e->getMessage());
  220.             echo AlphaView::displayPageHead($this);
  221.             throw new ResourceNotAllowedException($e->getMessage());
  222.         }catch(IllegalArguementException $e{
  223.             self::$logger->warn($e->getMessage());
  224.             echo AlphaView::displayPageHead($this);
  225.             throw new ResourceNotFoundException('The file that you have requested cannot be found!');
  226.         }catch(ValidationException $e{
  227.             self::$logger->warn($e->getMessage().', query ['.$this->BO->getLastQuery().']');
  228.             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
  229.             $this->doGET($params);
  230.         }
  231.         
  232.         self::$logger->debug('<<doPOST');
  233.     }
  234. }
  235.  
  236. // now build the new controller
  237. if(basename($_SERVER['PHP_SELF']== 'Create.php'{
  238.     $controller new Create();
  239.     
  240.     if(!empty($_POST)) {            
  241.         $controller->doPOST($_REQUEST);
  242.     }else{
  243.         $controller->doGET($_GET);
  244.     }
  245. }
  246.  
  247. ?>

Documentation generated on Thu, 17 Mar 2011 16:43:59 +0000 by phpDocumentor 1.4.3