0011282: Translate salutation in xml export
[tine20] / tine20 / Tinebase / Export / Spreadsheet / Abstract.php
1 <?php
2 /**
3  * Tinebase Abstract spreadsheet export class
4  *
5  * @package     Tinebase
6  * @subpackage    Export
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  * @copyright   Copyright (c) 2010-2011 Metaways Infosystems GmbH (http://www.metaways.de)
10  * 
11  */
12
13 /**
14  * Tinebase Abstract spreadsheet export class
15  * 
16  * @package     Tinebase
17  * @subpackage    Export
18  */
19 abstract class Tinebase_Export_Spreadsheet_Abstract extends Tinebase_Export_Abstract
20 {
21     /**
22      * group by this field
23      *
24      * @var string
25      */
26     protected $_groupBy = NULL;
27     
28     /**
29      * config for the groupBy field
30      * 
31      * @var Zend_Config
32      */
33     protected $_groupByFieldConfig = NULL;
34     
35     /**
36      * type of the field
37      * 
38      * @var string
39      */
40     protected $_groupByFieldType = NULL;
41     
42     /**
43      * count of columns
44      * 
45      * @var integer
46      */
47     protected $_columnCount = NULL;
48     
49     /**
50      * the constructor
51      *
52      * @param Tinebase_Model_Filter_FilterGroup $_filter
53      * @param Tinebase_Controller_Record_Interface $_controller (optional)
54      * @param array $_additionalOptions (optional) additional options
55      */
56     public function __construct(Tinebase_Model_Filter_FilterGroup $_filter, Tinebase_Controller_Record_Interface $_controller = NULL, $_additionalOptions = array())
57     {
58         parent::__construct($_filter, $_controller, $_additionalOptions);
59     
60         if ($this->_config->grouping) {
61             $this->_groupBy = (string) $this->_config->grouping->by;
62             $this->_sortInfo = array('sort' => $this->_groupBy);
63         }
64     }
65     
66     /**
67      * holds all records for the matrix
68      * 
69      * @var array
70      */
71     protected $_matrixCache = array();
72     
73     /**
74      * get export document object
75      * 
76      * @return Object the generated document
77      */
78     abstract public function getDocument();
79     
80     /**
81      * get cell value
82      * 
83      * @param Zend_Config $_field
84      * @param Tinebase_Record_Interface $_record
85      * @param string $_cellType
86      * @return string
87      * 
88      * @todo check string type for translated fields?
89      * @todo add 'config' type again?
90      * @todo move generic parts to Tinebase_Export_Abstract
91      */
92     protected function _getCellValue(Zend_Config $_field, Tinebase_Record_Interface $_record, &$_cellType)
93     {
94         $result = NULL;
95         
96         if (! (isset($_field->type) && $_field->separateColumns)) {
97             if (in_array($_field->type, $this->_specialFields)) {
98                 // special field handling
99                 $result = $this->_getSpecialFieldValue($_record, $_field->toArray(), $_field->identifier, $_cellType);
100                 $result = $this->_replaceAndMatchvalue($result, $_field);
101                 return $result;
102                 
103             } else if (isset($field->formula) 
104                 || (! isset($_record->{$_field->identifier}) 
105                     && ! in_array($_field->type, $this->_resolvedFields) 
106                     && ! in_array($_field->identifier, $this->_getCustomFieldNames())
107                 )
108             ) {
109                 // check if empty -> use alternative field
110                 if (isset($_field->empty)) {
111                     $fieldConfig = $_field->toArray();
112                     unset($fieldConfig['empty']);
113                     $fieldConfig['identifier'] = $_field->empty;
114                     $result = $this->_getCellValue(new Zend_Config($fieldConfig), $_record, $_cellType);
115                 }
116                 // don't add value for formula or undefined fields
117                 return $result;
118             }
119         }
120         
121         if ($_field->isMatrixField) {
122             return $this->_getMatrixCellValue($_field, $_record);
123         }
124         
125         switch($_field->type) {
126             case 'datetime':
127                 $result = Tinebase_Translation::dateToStringInTzAndLocaleFormat($_record->{$_field->identifier});
128                 // empty date cells, get displayed as 30.12.1899
129                 if(empty($result)) {
130                     $result = NULL;
131                 }
132                 break;
133             case 'date':
134                 $result = ($_record->{$_field->identifier} instanceof DateTime) ? $_record->{$_field->identifier}->toString('Y-m-d') : $_record->{$_field->identifier};
135                 // empty date cells, get displayed as 30.12.1899
136                 if (empty($result)) {
137                     $result = NULL;
138                 }
139                 break;
140             case 'tags':
141                 $result = $this->_getTags($_record);
142                 break;
143             case 'keyfield':
144                 $result = $this->_getResolvedKeyfield($_record->{$_field->identifier}, $_field->keyfield, $_field->application);
145                 break;
146             case 'currency':
147                 $currency = ($_field->currency) ? $_field->currency : 'EUR';
148                 $result =  ($_record->{$_field->identifier}) ? $_record->{$_field->identifier} : '0';
149                 $result .= ' ' . $currency;
150                 break;
151             case 'percentage':
152                 $result    = $_record->{$_field->identifier} / 100;
153                 break;
154             case 'container_id':
155                 $result = $this->_getContainer($_record, $_field->field, $_field->type);
156                 break;
157                 /*
158             case 'config':
159                 $result = Tinebase_Config::getOptionString($_record, $_field->identifier);
160                 break;
161                 */
162             case 'relation':
163                 $result = $this->_addRelations($_record, $_field->identifier, $_field->field);
164                 break;
165             case 'notes':
166                 $result = $this->_addNotes($_record);
167                 break;
168             default:
169                 if (in_array($_field->identifier, $this->_getCustomFieldNames())) {
170                     // add custom fields
171                     if (isset($_record->customfields[$_field->identifier])) {
172                         $result = $_record->customfields[$_field->identifier];
173                     }
174                 } elseif (isset($_field->divisor)) {
175                     // divisor
176                     $result = $_record->{$_field->identifier} / $_field->divisor;
177                 } elseif (in_array($_field->type, $this->_userFields) || in_array($_field->identifier, $this->_userFields)) {
178                     // resolved user
179                     $result = $this->_getUserValue($_record, $_field);
180                 } else if (is_object($_record->{$_field->identifier}) && method_exists($_record->{$_field->identifier}, '__toString')) {
181                     // call __toString
182                     $result = $_record->{$_field->identifier}->__toString();
183                 } else {
184                     // all remaining
185                     $result = $_record->{$_field->identifier};
186                 }
187                 
188                 if (isset($_field->trim) && $_field->trim == 1) {
189                     $result = trim($result);
190                 }
191                 
192                 // set special value from params
193                 if (isset($_field->values)) {
194                     $values = $_field->values->value->toArray();
195                     if (isset($values[$result])) {
196                         $result = $values[$result];
197                     }
198                 }
199                 
200                 if (isset($_field->translate) && $_field->translate/* && $_cellType === OpenDocument_SpreadSheet_Cell::TYPE_STRING*/) {
201                     $result = $this->_translate->_($result);
202                 }
203                 
204                 $result = $this->_replaceAndMatchvalue($result, $_field);
205         }
206         
207         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' field def: ' . print_r($_field->toArray(), TRUE));
208         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' result: ' . $result);
209         
210         return $result;
211     }
212     
213     /**
214      * returns the value of a matrix field
215      * 
216      * @param Zend_Config $field
217      * @param Tinebase_Record_Interface $record
218      * @throws Tinebase_Exception_Data
219      * @return number
220      */
221     protected function _getMatrixCellValue($field, $record)
222     {
223         $result = 0;
224         
225         switch ($field->type) {
226             case 'tags':
227                 if (! isset($this->_matrixCache[$field->identifier])) {
228                     $this->_matrixCache[$field->identifier] = array();
229                 }
230             
231                 if (! isset($this->_matrixCache[$field->identifier][$record->getId()])) {
232                     // clear cache, its not needed anymore (could have been filled by the previous record)
233                     $this->_matrixCache[$field->identifier] = array();
234                     $this->_matrixCache[$field->identifier][$record->getId()] = Tinebase_Tags::getInstance()->getTagsOfRecord($record);
235                 }
236                 $result = $this->_matrixCache[$field->identifier][$record->getId()]->filter('name', $field->identifier)->count();
237                 break;
238             default:
239                 throw new Tinebase_Exception_Data('Other types than tags are not supported at the moment.');
240         }
241         
242         return $result;
243     }
244
245     /**
246      * get value from resolved user record
247      * 
248      * @param Tinebase_Record_Interface $_record
249      * @param Zend_Config $_fieldConfig
250      */
251     protected function _getUserValue($_record, $_fieldConfig)
252     {
253         $result = '';
254         if (in_array($_fieldConfig->type, $this->_userFields)) {
255             $user = $_record->{$_fieldConfig->type};
256         } else {
257             $user = $_record->{$_fieldConfig->identifier};
258         }
259         
260         if (! empty($user) && is_object($user)) {
261             if ($_fieldConfig->field) {
262                 $result = $user->{$_fieldConfig->field};
263             } else {
264                 $result = $user->accountDisplayName;
265             }
266         }
267         
268         return $result;
269     }
270 }