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