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

Source for file ViewExcel.php

Documentation is available at ViewExcel.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/util/Logger.inc';
  11. require_once $config->get('sysRoot').'alpha/util/convertors/AlphaDAO2Excel.inc';
  12. require_once $config->get('sysRoot').'alpha/controller/AlphaControllerInterface.inc';
  13.  
  14. /**
  15.  *
  16.  * Controller for viewing Business Objects as Excel spreadsheets
  17.  * 
  18.  * @package alpha::controller
  19.  * @since 1.0
  20.  * @author John Collins <dev@alphaframework.org>
  21.  * @version $Id: ViewExcel.php 1341 2011-03-17 15:02:02Z 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 ViewExcel extends AlphaController implements AlphaControllerInterface {
  60.     /**
  61.      * Trace logger
  62.      * 
  63.      * @var Logger 
  64.      * @since 1.0
  65.      */
  66.     private static $logger null;
  67.     
  68.     /**
  69.      * Constructor
  70.      * 
  71.      * @since 1.0
  72.      */
  73.     public function __construct({
  74.         self::$logger new Logger('ViewExcel');
  75.         self::$logger->debug('>>__construct()');
  76.         
  77.         // ensure that the super class constructor is called, indicating the rights group
  78.         parent::__construct('Public');
  79.         
  80.         self::$logger->debug('<<__construct');
  81.     }
  82.     
  83.     /**
  84.      * Loads the BO indicated in the GET request and handles the conversion to Excel
  85.      *
  86.      * @param array $params 
  87.      * @throws ResourceNotFoundException
  88.      * @since 1.0
  89.      */
  90.     public function doGet($params{
  91.         self::$logger->debug('>>doGet(params=['.var_export($paramstrue).'])');
  92.         
  93.         try {
  94.             if(isset($params['bo'])) {
  95.                 AlphaDAO::loadClassDef($params['bo']);
  96.                 $BO new $params['bo'];
  97.                 
  98.                 // the name of the file download
  99.                 if(isset($params['oid']))
  100.                     $fileName $BO->getTableName().'-'.$params['oid'];
  101.                 else
  102.                     $fileName $BO->getTableName();
  103.                 
  104.                 //header info for browser
  105.                 header('Content-Type: application/vnd.ms-excel');
  106.                 header('Content-Disposition: attachment; filename='.$fileName.'.xls');
  107.                 header('Pragma: no-cache');
  108.                 header('Expires: 0');
  109.                 
  110.                 // handle a single BO
  111.                 if(isset($params['oid'])) {
  112.                     $BO->load($params['oid']);
  113.                     AlphaDAO::disconnect();
  114.                     
  115.                     $convertor new AlphaDAO2Excel($BO);
  116.                     $convertor->render();
  117.                 }else{
  118.                     // handle all BOs of this type
  119.                     $BOs $BO->loadAll();
  120.                     AlphaDAO::disconnect();
  121.                     
  122.                     $first true;
  123.                     
  124.                     foreach($BOs as $BO{
  125.                         $convertor new AlphaDAO2Excel($BO);
  126.                         if($first{
  127.                             $convertor->render(true);
  128.                             $first false;
  129.                         }else{
  130.                             $convertor->render(false);
  131.                         }
  132.                     }
  133.                 }
  134.             }else{
  135.                 throw new IllegalArguementException('No BO parameter available for ViewExcel controller!');
  136.             }
  137.         }catch (BONotFoundException $e{
  138.             self::$logger->error($e->getMessage());
  139.             throw new ResourceNotFoundException($e->getMessage());
  140.         }catch (IllegalArguementException $e{
  141.             self::$logger->error($e->getMessage());
  142.             throw new ResourceNotFoundException($e->getMessage());
  143.         }
  144.         
  145.         self::$logger->debug('<<__doGet');
  146.     }
  147.     
  148.     /**
  149.      * Handle POST requests
  150.      * 
  151.      * @param array $params 
  152.      * @since 1.0
  153.      */
  154.     public function doPOST($params{
  155.         self::$logger->debug('>>doPOST($params=['.var_export($paramstrue).'])');        
  156.         
  157.         self::$logger->debug('<<doPOST');
  158.     }
  159.     
  160. }
  161.  
  162. // now build the new controller if this file is called directly
  163. if ('ViewExcel.php' == basename($_SERVER['PHP_SELF'])) {
  164.     $controller new ViewExcel();
  165.     
  166.     if(!empty($_POST)) {            
  167.         $controller->doPOST($_POST);
  168.     }else{
  169.         $controller->doGET($_GET);
  170.     }
  171. }
  172.  
  173. ?>

Documentation generated on Thu, 17 Mar 2011 16:45:09 +0000 by phpDocumentor 1.4.3