      1: <?php
      2:
      3: /**
      4:  * Class for converting a Business Object to an Excel spreadsheet
      5:  *
      6:  * @package alpha::util::convertors
      7:  * @since 1.0
      8:  * @author John Collins <dev@alphaframework.org>
      9:  * @version $Id: AlphaDAO2Excel.inc 1496 2012-02-12 20:32:21Z alphadev $
     10:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     11:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     12:  * All rights reserved.
     13:  *
     14:  * <pre>
     15:  * Redistribution and use in source and binary forms, with or
     16:  * without modification, are permitted provided that the
     17:  * following conditions are met:
     18:  *
     19:  * * Redistributions of source code must retain the above
     20:  *   copyright notice, this list of conditions and the
     21:  *   following disclaimer.
     22:  * * Redistributions in binary form must reproduce the above
     23:  *   copyright notice, this list of conditions and the
     24:  *   following disclaimer in the documentation and/or other
     25:  *   materials provided with the distribution.
     26:  * * Neither the name of the Alpha Framework nor the names
     27:  *   of its contributors may be used to endorse or promote
     28:  *   products derived from this software without specific
     29:  *   prior written permission.
     30:  *
     31:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     32:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     33:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     34:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     35:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     36:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     37:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     38:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     39:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     41:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     42:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     43:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     44:  * </pre>
     45:  *
     46:  */
     47: class AlphaDAO2Excel {
     48:     /**
     49:      * The Business Object we will convert to an Excel sheet
     50:      *
     51:      * @var AlphaDAO
     52:      * @since 1.0
     53:      */
     54:     private $BO;
     55:
     56:     /**
     57:      * Trace logger
     58:      *
     59:      * @var Logger
     60:      * @since 1.0
     61:      */
     62:     private static $logger = null;
     63:
     64:     /**
     65:      * Constructor
     66:      *
     67:      * @param AlphaDAO $BO
     68:      * @since 1.0
     69:      */
     70:     public function __construct($BO) {
     71:         self::$logger = new Logger('AlphaDAO2Excel');
     72:         self::$logger->debug('>>__construct(BO=['.var_export($BO, true).'])');
     73:
     74:         $this->BO = $BO;
     75:
     76:         self::$logger->debug('<<__construct');
     77:     }
     78:
     79:     /**
     80:      * Sends the output as an Excel spreadsheet to standard output
     81:      *
     82:      * @param bool $renderHeaders Set to false to supress headers in the spreadsheet (defaults to true).
     83:      * @since 1.0
     84:      */
     85:     public function render($renderHeaders=true) {
     86:         self::$logger->debug('>>render()');
     87:
     88:         //define separator (tabbed character)
     89:         $sep = "\t";
     90:
     91:         // get the class attributes
     92:         $reflection = new ReflectionClass(get_class($this->BO));
     93:         $properties = $reflection->getProperties();
     94:
     95:         // print headers
     96:         if($renderHeaders) {
     97:             echo $this->BO->getDataLabel('OID').$sep;
     98:             foreach($properties as $propObj) {
     99:                 $propName = $propObj->name;
    100:                 if (!in_array($propName, $this->BO->getTransientAttributes()) && !in_array($propName, $this->BO->getDefaultAttributes())) {
    101:                     echo $this->BO->getDataLabel($propName).$sep;
    102:                 }
    103:             }
    104:
    105:             echo "\n";
    106:         }
    107:
    108:         // print values
    109:         echo $this->BO->getOID().$sep;
    110:         foreach($properties as $propObj) {
    111:             $propName = $propObj->name;
    112:             $prop = $this->BO->getPropObject($propName);
    113:             if (!in_array($propName, $this->BO->getTransientAttributes()) && !in_array($propName, $this->BO->getDefaultAttributes())) {
    114:                 if(get_class($prop) == 'DEnum')
    115:                     echo $prop->getDisplayValue().$sep;
    116:                 elseif(get_class($prop) == 'Relation')
    117:                     echo $prop->getRelatedClassDisplayFieldValue().$sep;
    118:                 else
    119:                     echo preg_replace("/[\n\r]/", "", $prop->getValue()).$sep;
    120:             }
    121:         }
    122:
    123:
    124:         echo "\n";
    125:
    126:         self::$logger->debug('<<render');
    127:     }
    128: }
    129:
    130: ?>