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

Source for file ListAll.php

Documentation is available at ListAll.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. require_once $config->get('sysRoot').'alpha/util/helpers/AlphaValidator.inc';
  13.  
  14. /**
  15.  * 
  16.  * Controller used to list a BO, which must be supplied in GET vars
  17.  * 
  18.  * @package alpha::controller
  19.  * @since 1.0
  20.  * @author John Collins <dev@alphaframework.org>
  21.  * @version $Id: ListAll.php 1453 2011-12-04 15:12:54Z johnc $
  22.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  23.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  24.  *  All rights reserved.
  25.  * 
  26.  *  <pre>
  27.  *  Redistribution and use in source and binary forms, with or
  28.  *  without modification, are permitted provided that the
  29.  *  following conditions are met:
  30.  * 
  31.  *  * Redistributions of source code must retain the above
  32.  *    copyright notice, this list of conditions and the
  33.  *    following disclaimer.
  34.  *  * Redistributions in binary form must reproduce the above
  35.  *    copyright notice, this list of conditions and the
  36.  *    following disclaimer in the documentation and/or other
  37.  *    materials provided with the distribution.
  38.  *  * Neither the name of the Alpha Framework nor the names
  39.  *    of its contributors may be used to endorse or promote
  40.  *    products derived from this software without specific
  41.  *    prior written permission.
  42.  *   
  43.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  44.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  45.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  46.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  47.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  48.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  49.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  50.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  51.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  52.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  53.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  54.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56.  *  </pre>
  57.  *  
  58.  */
  59. class ListAll extends AlphaController implements AlphaControllerInterface {
  60.     /**
  61.      * The name of the BO
  62.      * 
  63.      * @var string 
  64.      * @since 1.0
  65.      */
  66.     protected $BOname;
  67.     
  68.     /**
  69.      * The new default AlphaView object used for rendering the onjects to list
  70.      * 
  71.      * @var AlphaView 
  72.      * @since 1.0
  73.      */
  74.     protected $BOView;
  75.     
  76.     /**
  77.      * The start number for list pageination
  78.      * 
  79.      * @var integer 
  80.      * @since 1.0
  81.      */
  82.     protected $startPoint;
  83.     
  84.     /**
  85.      * The count of the BOs of this type in the database
  86.      * 
  87.      * @var integer 
  88.      * @since 1.0
  89.      */
  90.     protected $BOCount = 0;
  91.     
  92.     /**
  93.      * The field name to sort the list by (optional, default is OID)
  94.      * 
  95.      * @var string 
  96.      * @since 1.0
  97.      */
  98.     protected $sort;
  99.     
  100.     /**
  101.      * The order to sort the list by (optional, should be ASC or DESC, default is ASC)
  102.      * 
  103.      * @var string 
  104.      * @since 1.0
  105.      */
  106.     protected $order;
  107.     
  108.     /**
  109.      * The name of the BO field to filter the list by (optional)
  110.      * 
  111.      * @var string 
  112.      * @since 1.0
  113.      */
  114.     protected $filterField;
  115.     
  116.     /**
  117.      * The value of the filterField to filter by (optional)
  118.      * 
  119.      * @var string 
  120.      * @since 1.0
  121.      */
  122.     protected $filterValue;
  123.     
  124.     /**
  125.      * Trace logger
  126.      * 
  127.      * @var Logger 
  128.      * @since 1.0
  129.      */
  130.     private static $logger null;
  131.                                 
  132.     /**
  133.      * Constructor to set up the object
  134.      * 
  135.      * @param string $visibility 
  136.      * @since 1.0
  137.      */
  138.     public function __construct($visibility='Admin'{
  139.         self::$logger new Logger('ListAll');
  140.         self::$logger->debug('>>__construct()');
  141.         
  142.         global $config;
  143.                 
  144.         // ensure that the super class constructor is called, indicating the rights group
  145.         parent::__construct($visibility);
  146.         
  147.         self::$logger->debug('<<__construct');
  148.     }
  149.  
  150.     /**
  151.      * Handle GET requests
  152.      * 
  153.      * @param array $params 
  154.      * @since 1.0
  155.      */
  156.     public function doGET($params{
  157.         self::$logger->debug('>>doGET($params=['.var_export($paramstrue).'])');
  158.         
  159.         try{
  160.             // load the business object (BO) definition
  161.             if (isset($params['bo'])) {
  162.                 $BOname $params['bo'];
  163.                 $this->BOname = $BOname;
  164.             }elseif(isset($this->BOname)) {
  165.                 $BOname $this->BOname;
  166.             }else{
  167.                 throw new IllegalArguementException('No BO available to list!');
  168.             }
  169.             
  170.             if (isset($params['order'])) {
  171.                 if($params['order'== 'ASC' || $params['order'== 'DESC')
  172.                     $this->order = $params['order'];
  173.                 else
  174.                     throw new IllegalArguementException('Order value ['.$params['order'].'] provided is invalid!');
  175.             }
  176.             
  177.             if (isset($params['sort']))
  178.                 $this->sort = $params['sort'];
  179.                 
  180.             AlphaDAO::loadClassDef($BOname);
  181.             
  182.             /*
  183.              *  check and see if a custom create controller exists for this BO, and if it does use it otherwise continue
  184.              */
  185.             if($this->getCustomControllerName($BOname'list'!= null)
  186.                 $this->loadCustomController($BOname'list');
  187.                 
  188.             $this->BO = new $BOname();
  189.             $this->BOView = AlphaView::getInstance($this->BO);
  190.                 
  191.             echo AlphaView::displayPageHead($this);
  192.         }catch(IllegalArguementException $e{
  193.             self::$logger->error($e->getMessage());
  194.         }
  195.         
  196.         $this->displayBodyContent();
  197.         
  198.         echo AlphaView::displayPageFoot($this);
  199.         
  200.         self::$logger->debug('<<doGET');
  201.     }
  202.     
  203.     /**
  204.      * Handle POST requests
  205.      * 
  206.      * @param array $params 
  207.      * @since 1.0
  208.      */
  209.     public function doPOST($params{
  210.         self::$logger->debug('>>doPOST($params=['.var_export($paramstrue).'])');
  211.         
  212.         try{
  213.             // check the hidden security fields before accepting the form POST data
  214.             if(!$this->checkSecurityFields()) {
  215.                 throw new SecurityException('This page cannot accept post data from remote servers!');
  216.                 self::$logger->debug('<<doPOST');
  217.             }
  218.             
  219.             // load the business object (BO) definition
  220.             if (isset($params['bo'])) {
  221.                 $BOname $params['bo'];
  222.                 $this->BOname = $BOname;
  223.             }elseif(isset($this->BOname)) {
  224.                 $BOname $this->BOname;
  225.             }else{
  226.                 throw new IllegalArguementException('No BO available to list!');
  227.             }
  228.             
  229.             if (isset($params['order'])) {
  230.                 if($params['order'== 'ASC' || $params['order'== 'DESC')
  231.                     $this->order = $params['order'];
  232.                 else
  233.                     throw new IllegalArguementException('Order value ['.$params['order'].'] provided is invalid!');
  234.             }
  235.             
  236.             if (isset($params['sort']))
  237.                 $this->sort = $params['sort'];
  238.             
  239.             AlphaDAO::loadClassDef($BOname);
  240.                 
  241.             $this->BO = new $BOname();        
  242.             $this->BOname = $BOname;        
  243.             $this->BOView = AlphaView::getInstance($this->BO);
  244.             
  245.             echo AlphaView::displayPageHead($this);
  246.                 
  247.             if (!empty($params['deleteOID'])) {
  248.                 if(!AlphaValidator::isInteger($params['deleteOID']))
  249.                         throw new IllegalArguementException('Invalid deleteOID ['.$params['deleteOID'].'] provided on the request!');
  250.                 
  251.                 try {
  252.                     $temp new $BOname();
  253.                     $temp->load($params['deleteOID']);
  254.                     
  255.                     AlphaDAO::begin();
  256.                     $temp->delete();
  257.                     AlphaDAO::commit();
  258.  
  259.                     echo AlphaView::displayUpdateMessage($BOname.' '.$params['deleteOID'].' deleted successfully.');
  260.                             
  261.                     $this->displayBodyContent();
  262.                 }catch(AlphaException $e{
  263.                     self::$logger->error($e->getMessage());
  264.                     echo AlphaView::displayErrorMessage('Error deleting the BO of OID ['.$params['deleteOID'].'], check the log!');
  265.                     AlphaDAO::rollback();
  266.                 }
  267.                 
  268.                 AlphaDAO::disconnect();
  269.             }
  270.         }catch(SecurityException $e{
  271.             echo AlphaView::displayErrorMessage($e->getMessage());
  272.             self::$logger->warn($e->getMessage());
  273.         }catch(IllegalArguementException $e{
  274.             echo AlphaView::displayErrorMessage($e->getMessage());
  275.             self::$logger->error($e->getMessage());
  276.         }
  277.         
  278.         echo AlphaView::displayPageFoot($this);
  279.         
  280.         self::$logger->debug('<<doPOST');
  281.     }
  282.     
  283.     /**
  284.      * Sets up the title etc. and pagination start point
  285.      * 
  286.      * @since 1.0
  287.      */
  288.     public function before_displayPageHead_callback({
  289.         // set up the title and meta details
  290.         if(!isset($this->title))
  291.             $this->setTitle('Listing all '.$this->BOname);
  292.         if(!isset($this->description))
  293.             $this->setDescription('Page listing all '.$this->BOname.'.');
  294.         if(!isset($this->keywords))
  295.             $this->setKeywords('list,all,'.$this->BOname);
  296.         // set the start point for the list pagination
  297.         if (isset($_GET['start']$this->startPoint = $_GET['start']$this->startPoint = 1);
  298.     }
  299.     
  300.     /**
  301.      * Method to display the page footer with pageination links
  302.      * 
  303.      * @return string 
  304.      * @since 1.0
  305.      */
  306.     public function before_displayPageFoot_callback({
  307.         $html $this->renderPageLinks();
  308.         
  309.         $html .= '<br>';
  310.         
  311.         return $html;
  312.     }
  313.     
  314.     /**
  315.      * Method for rendering the pagination links
  316.      * 
  317.      * @return string 
  318.      * @since 1.0
  319.      */
  320.     protected function renderPageLinks({
  321.         global $config;
  322.         
  323.         $html '';
  324.         
  325.         $end (($this->startPoint-1)+$config->get('sysListPageAmount'));
  326.         
  327.         if($end $this->BOCount)
  328.             $end $this->BOCount;
  329.         
  330.         if($this->BOCount > 0)
  331.             $html .= '<p align="center">Displaying '.($this->startPoint).' to '.$end.' of <strong>'.$this->BOCount.'</strong>.&nbsp;&nbsp;';
  332.         else
  333.             $html .= '<p align="center">The list is empty.&nbsp;&nbsp;';
  334.                 
  335.         if ($this->startPoint > 1{
  336.             // handle secure URLs
  337.             if(isset($_GET['tk']))
  338.                 $html .= '<a href="'.FrontController::generateSecureURL('act=ListAll&bo='.$this->BOname.'&start='.($this->startPoint-$config->get('sysListPageAmount'))).'">&lt;&lt;-Previous</a>&nbsp;&nbsp;';
  339.             else
  340.                 $html .= '<a href="'.$_SERVER["PHP_SELF"].'?bo='.$this->BOname."&start=".($this->startPoint-$config->get('sysListPageAmount')).'">&lt;&lt;-Previous</a>&nbsp;&nbsp;';
  341.         }elseif($this->BOCount > $config->get('sysListPageAmount')){
  342.             $html .= '&lt;&lt;-Previous&nbsp;&nbsp;';
  343.         }
  344.         $page 1;
  345.         for ($i 0$i $this->BOCount$i+=$config->get('sysListPageAmount')) {
  346.             if($i != ($this->startPoint-1)) {
  347.                 // handle secure URLs
  348.                 if(isset($_GET['tk']))
  349.                     $html .= '&nbsp;<a href="'.FrontController::generateSecureURL('act=ListAll&bo='.$this->BOname.'&start='.($i+1)).'">'.$page.'</a>&nbsp;';
  350.                 else
  351.                     $html .= '&nbsp;<a href="'.$_SERVER["PHP_SELF"].'?bo='.$this->BOname."&start=".($i+1).'">'.$page.'</a>&nbsp;';
  352.             }elseif($this->BOCount > $config->get('sysListPageAmount')){
  353.                 $html .= '&nbsp;'.$page.'&nbsp;';
  354.             }
  355.             $page++;
  356.         }
  357.         if ($this->BOCount > $end{
  358.             // handle secure URLs
  359.             if(isset($_GET['tk']))
  360.                 $html .= '&nbsp;&nbsp;<a href="'.FrontController::generateSecureURL('act=ListAll&bo='.$this->BOname.'&start='.($this->startPoint+$config->get('sysListPageAmount'))).'">Next-&gt;&gt;</a>';
  361.             else
  362.                 $html .= '&nbsp;&nbsp;<a href="'.$_SERVER["PHP_SELF"].'?bo='.$this->BOname."&start=".($this->startPoint+$config->get('sysListPageAmount')).
  363.                     '">Next-&gt;&gt;</a>';
  364.         }elseif($this->BOCount > $config->get('sysListPageAmount')){
  365.             $html .= '&nbsp;&nbsp;Next-&gt;&gt;';
  366.         }
  367.         $html .= '</p>';
  368.         
  369.         return $html;
  370.     }
  371.     
  372.     /**
  373.      * Method to display the main body HTML for this page
  374.      * 
  375.      * @since 1.0
  376.      */
  377.     protected function displayBodyContent({
  378.         global $config;
  379.         
  380.         // get all of the BOs and invoke the listView on each one
  381.         $temp new $this->BOname;
  382.         
  383.         if(isset($this->filterField&& isset($this->filterValue)) {
  384.             if(isset($this->sort&& isset($this->order)) {
  385.                 $objects $temp->loadAllByAttribute($this->filterField$this->filterValue$this->startPoint-1$config->get('sysListPageAmount'),
  386.                     $this->sort$this->order);
  387.             }else{
  388.                 $objects $temp->loadAllByAttribute($this->filterField$this->filterValue$this->startPoint-1$config->get('sysListPageAmount'));
  389.             }
  390.                 
  391.             $this->BOCount = $temp->getCount(array($this->filterField)array($this->filterValue));
  392.         }else{
  393.             if(isset($this->sort&& isset($this->order))
  394.                 $objects $temp->loadAll($this->startPoint-1$config->get('sysListPageAmount')$this->sort$this->order);
  395.             else
  396.                 $objects $temp->loadAll($this->startPoint-1$config->get('sysListPageAmount'));
  397.                 
  398.             $this->BOCount = $temp->getCount();
  399.         }
  400.         
  401.         AlphaDAO::disconnect();
  402.         
  403.         echo AlphaView::renderDeleteForm();
  404.         
  405.         foreach($objects as $object{
  406.             $temp AlphaView::getInstance($object);
  407.             $temp->listView();
  408.         }
  409.     }
  410. }
  411.  
  412. // now build the new controller
  413. if(basename($_SERVER['PHP_SELF']== 'ListAll.php'{
  414.     $controller new ListAll();
  415.     
  416.     if(!empty($_POST)) {            
  417.         $controller->doPOST($_REQUEST);
  418.     }else{
  419.         $controller->doGET($_GET);
  420.     }
  421. }
  422.  
  423. ?>

Documentation generated on Tue, 13 Dec 2011 20:27:07 +0000 by phpDocumentor 1.4.3