      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 create a new BO, whose classname 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: Create.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) 2012, 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 Create extends AlphaController implements AlphaControllerInterface {
     57:     /**
     58:      * The name of the BO
     59:      *
     60:      * @var string
     61:      * @since 1.0
     62:      */
     63:     protected $BOname;
     64:
     65:     /**
     66:      * The new BO to be created
     67:      *
     68:      * @var AlphaDAO
     69:      * @since 1.0
     70:      */
     71:     protected $BO;
     72:
     73:     /**
     74:      * The AlphaView object used for rendering the objects to create
     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
     93:      * @since 1.0
     94:      */
     95:     public function __construct($visibility='Admin') {
     96:         self::$logger = new Logger('Create');
     97:         self::$logger->debug('>>__construct(visibility=['.$visibility.'])');
     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 IllegalArguementException
    112:      * @throws ResourceNotFoundException
    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'])) {
    121:                 $BOname = $params['bo'];
    122:                 $this->BOname = $BOname;
    123:             }elseif(isset($this->BOname)) {
    124:                 $BOname = $this->BOname;
    125:             }else{
    126:                 throw new IllegalArguementException('No BO available to create!');
    127:             }
    128:
    129:             AlphaDAO::loadClassDef($BOname);
    130:
    131:             /*
    132:              *  check and see if a custom create controller exists for this BO, and if it does use it otherwise continue
    133:              */
    134:             if($this->getCustomControllerName($BOname, 'create') != null)
    135:                 $this->loadCustomController($BOname, 'create');
    136:
    137:             $this->BO = new $BOname();
    138:
    139:             $this->BOView = AlphaView::getInstance($this->BO);
    140:
    141:             // set up the title and meta details
    142:             if(!isset($this->title))
    143:                 $this->setTitle('Create a new '.$BOname);
    144:             if(!isset($this->description))
    145:                 $this->setDescription('Page to create a new '.$BOname.'.');
    146:             if(!isset($this->keywords))
    147:                 $this->setKeywords('create,new,'.$BOname);
    148:
    149:             echo AlphaView::displayPageHead($this);
    150:
    151:             echo $this->BOView->createView();
    152:         }catch(IllegalArguementException $e) {
    153:             self::$logger->warn($e->getMessage());
    154:             throw new ResourceNotFoundException('The file that you have requested cannot be found!');
    155:         }
    156:
    157:         echo AlphaView::displayPageFoot($this);
    158:
    159:         self::$logger->debug('<<doGET');
    160:     }
    161:
    162:     /**
    163:      * Method to handle POST requests
    164:      *
    165:      * @param array $params
    166:      * @throws ResourceNotAllowedException
    167:      * @since 1.0
    168:      */
    169:     public function doPOST($params) {
    170:         self::$logger->debug('>>doPOST($params=['.var_export($params, true).'])');
    171:
    172:         global $config;
    173:
    174:         try {
    175:             // check the hidden security fields before accepting the form POST data
    176:             if(!$this->checkSecurityFields())
    177:                 throw new SecurityException('This page cannot accept post data from remote servers!');
    178:
    179:             // load the business object (BO) definition
    180:             if (isset($params['bo'])) {
    181:                 $BOname = $params['bo'];
    182:                 $this->BOname = $BOname;
    183:             }elseif(isset($this->BOname)) {
    184:                 $BOname = $this->BOname;
    185:             }else{
    186:                 throw new IllegalArguementException('No BO available to create!');
    187:             }
    188:
    189:             AlphaDAO::loadClassDef($BOname);
    190:
    191:             $this->BO = new $BOname();
    192:
    193:             if (isset($params['createBut'])) {
    194:                 // populate the transient object from post data
    195:                 $this->BO->populateFromPost();
    196:
    197:                 $this->BO->save();
    198:
    199:                 AlphaDAO::disconnect();
    200:
    201:                 try {
    202:                     if ($this->getNextJob() != '')
    203:                         header('Location: '.$this->getNextJob());
    204:                     else
    205:                         header('Location: '.FrontController::generateSecureURL('act=Detail&bo='.get_class($this->BO).'&oid='.$this->BO->getID()));
    206:                 }catch(AlphaException $e) {
    207:                     echo AlphaView::displayPageHead($this);
    208:                     self::$logger->error($e->getTraceAsString());
    209:                     echo AlphaView::displayErrorMessage('Error creating the new ['.$BOname.'], check the log!');
    210:                 }
    211:             }
    212:
    213:             if (isset($params['cancelBut'])) {
    214:                 header('Location: '.FrontController::generateSecureURL('act=ListBusinessObjects'));
    215:             }
    216:         }catch(SecurityException $e) {
    217:             self::$logger->warn($e->getMessage());
    218:             echo AlphaView::displayPageHead($this);
    219:             throw new ResourceNotAllowedException($e->getMessage());
    220:         }catch(IllegalArguementException $e) {
    221:             self::$logger->warn($e->getMessage());
    222:             echo AlphaView::displayPageHead($this);
    223:             throw new ResourceNotFoundException('The file that you have requested cannot be found!');
    224:         }catch(ValidationException $e) {
    225:             self::$logger->warn($e->getMessage().', query ['.$this->BO->getLastQuery().']');
    226:             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
    227:             $this->doGET($params);
    228:         }
    229:
    230:         self::$logger->debug('<<doPOST');
    231:     }
    232:
    233:     /**
    234:      * Use this callback to inject in the admin menu template fragment for admin users of
    235:      * the backend only.
    236:      *
    237:      * @since 1.2
    238:      */
    239:     public function after_displayPageHead_callback() {
    240:         $menu = '';
    241:
    242:         if (isset($_SESSION['currentUser']) && AlphaDAO::isInstalled() && $_SESSION['currentUser']->inGroup('Admin') && strpos($_SERVER['REQUEST_URI'], '/tk/') !== false) {
    243:             $menu .= AlphaView::loadTemplateFragment('html', 'adminmenu.phtml', array());
    244:         }
    245:
    246:         return $menu;
    247:     }
    248: }
    249:
    250: // now build the new controller
    251: if(basename($_SERVER['PHP_SELF']) == 'Create.php') {
    252:     $controller = new Create();
    253:
    254:     if(!empty($_POST)) {
    255:         $controller->doPOST($_REQUEST);
    256:     }else{
    257:         $controller->doGET($_GET);
    258:     }
    259: }
    260:
    261: ?>