6abc2e2b9fdb9efaab9506716491d940c9d8286e
[tine20] / tine20 / Crm / Export / Helper.php
1 <?php
2 /**
3  * Crm 
4  *
5  * @package     Crm
6  * @subpackage    Export
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Philipp Schuele <p.schuele@metaways.de>
9  * @copyright   Copyright (c) 2010 Metaways Infosystems GmbH (http://www.metaways.de)
10  * 
11  */
12
13 /**
14  * Crm Ods generation class
15  * 
16  * @package     Crm
17  * @subpackage    Export
18  * 
19  */
20 class Crm_Export_Helper
21 {
22     /**
23      * get special fields for export
24      * 
25      * @return array
26      */
27     public static function getSpecialFields()
28     {
29         return array('status', 'source', 'type', 'open_tasks');
30     }
31     
32     /**
33      * get resolved records (task status, ...)
34      * 
35      * @return array
36      */
37     public static function getResolvedRecords()
38     {
39         $result = array();
40         $result['tasksStatus'] = Tasks_Config::getInstance()->get(Tasks_Config::TASK_STATUS)->records;
41         
42         return $result;
43     }
44     
45     /**
46      * get special field value
47      *
48      * @param Tinebase_Record_Interface $_record
49      * @param array $_param
50      * @param string $_key
51      * @param string $_cellType
52      * @param array $_resolvedRecords
53      * @return string
54      */
55     public static function getSpecialFieldValue(Tinebase_Record_Interface $_record, $_param, $_key = NULL, &$_cellType = NULL, $_resolvedRecords = NULL)
56     {
57         if (is_null($_key)) {
58             throw new Tinebase_Exception_InvalidArgument('Missing required parameter $key');
59         }
60         
61         switch($_param['type']) {
62             case 'status':
63                 $value = $_record->getLeadStatus();
64                 break;
65             case 'source':
66                 $settings = Crm_Controller::getInstance()->getConfigSettings();
67                 $source = $settings->getOptionById($_record->leadsource_id, 'leadsources');
68                 if (isset($source['leadsource'])) {
69                     $value = $source['leadsource'];
70                 } else {
71                     Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Leadsource id not found:' . $_record->leadsource_id);
72                     $value = '';
73                 }
74                 break;
75             case 'type':
76                 $settings = Crm_Controller::getInstance()->getConfigSettings();
77                 $type = $settings->getOptionById($_record->leadtype_id, 'leadtypes');
78                 if (isset($type['leadtype'])) {
79                     $value = $type['leadtype'];
80                 } else {
81                     Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Leadtype id not found:' . $_record->leadtype_id);
82                     $value = '';
83                 }
84                 break;
85             case 'open_tasks':
86                 $value = 0;
87                 foreach ($_record->relations as $relation) {
88                     // check if is task and open
89                     if ($relation->type == 'TASK') {
90                         $idx = $_resolvedRecords['tasksStatus']->getIndexById($relation->related_record->status);
91                         if ($idx) {
92                             $status = $_resolvedRecords['tasksStatus'][$idx];
93                             //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($status->toArray(), TRUE));
94                             if ($status->is_open) {
95                                 $value++;
96                             }
97                         }
98                     }
99                 }
100                 break;
101             default:
102                 $value = '';
103         }
104
105         return $value;
106     }
107 }