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