]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarCharts/Jit/JitReports.php
Release 6.2.0beta4
[Github/sugarcrm.git] / include / SugarCharts / Jit / JitReports.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38
39
40 require_once("include/SugarCharts/Jit/Jit.php");
41
42 class JitReports extends Jit {
43         
44         private $processed_report_keys = array();
45         
46         function __construct() {
47                 parent::__construct();
48         }
49         
50                 function calculateReportGroupTotal($dataset){
51                 $total = 0;                             
52                 foreach ($dataset as $value){
53                         $total += $value['numerical_value'];
54                 }
55                 
56                 return $total;
57         }       
58         
59         function processReportData($dataset, $level=1, $first=false){
60                 $data = '';
61                 
62                 // rearrange $dataset to get the correct order for the first row
63                 if ($first){
64                         $temp_dataset = array();
65                         foreach ($this->super_set as $key){
66                                 $temp_dataset[$key] = (isset($dataset[$key])) ? $dataset[$key] : array();
67                         }
68                         $dataset = $temp_dataset;                       
69                 }
70                 
71                 foreach ($dataset as $key=>$value){
72                         if ($first && empty($value)){
73                                 $data .= $this->processDataGroup(4, $key, 'NULL', '', '');
74                         }                       
75                         else if (array_key_exists('numerical_value', $dataset)){
76                                 $link = (isset($dataset['link'])) ? '#'.$dataset['link'] : '';
77                                 $data .= $this->processDataGroup($level, $dataset['group_base_text'], $dataset['numerical_value'], $dataset['numerical_value'], $link);
78                                 array_push($this->processed_report_keys, $dataset['group_base_text']);
79                                 return $data;
80                         }
81                         else{
82                                 $data .= $this->processReportData($value, $level+1);
83                         }
84                 }
85                 
86                 return $data;
87         }
88         
89         function processReportGroup($dataset){
90                 $super_set = array();
91
92         foreach($dataset as $groupBy => $groups){
93             $prev_super_set = $super_set;
94             if (count($groups) > count($super_set)){
95                     $super_set = array_keys($groups);
96                 foreach($prev_super_set as $prev_group){
97                     if (!in_array($prev_group, $groups)){
98                         array_push($super_set, $prev_group);
99                     }       
100                 }       
101             }       
102             else{ 
103                 foreach($groups as $group => $groupData){
104                     if (!in_array($group, $super_set)){ 
105                         array_push($super_set, $group);
106                     }       
107                 }       
108             }       
109         }     
110         $super_set = array_unique($super_set);
111
112                 return $super_set;
113         }
114         
115         function xmlDataReportSingleValue(){
116                 $data = '';             
117                 foreach ($this->data_set as $key => $dataset){
118                         $total = $this->calculateReportGroupTotal($dataset);
119                         $this->checkYAxis($total);                                              
120
121                         $data .= $this->tab('<group>', 2);
122                         $data .= $this->tab('<title>' . $key . '</title>', 3);
123                         $data .= $this->tab('<subgroups>', 3);
124                         $data .= $this->tab('<group>',4);
125                         $data .= $this->tab('<title>' . $total . '</title>',5);
126                         $data .= $this->tab('<value>' . $total . '</value>',5);
127                         $data .= $this->tab('<label>' . $key . '</label>',5);
128                         $data .= $this->tab('<link></link>',5);
129                         $data .= $this->tab('</group>',4);
130                         $data .= $this->tab('</subgroups>', 3);                         
131                         $data .= $this->tab('</group>', 2);                     
132                 }
133                 return $data;
134         }
135         
136         function xmlDataReportChart(){
137                 $data = '';
138                 // correctly process the first row
139                 $first = true;  
140                 foreach ($this->data_set as $key => $dataset){
141                         
142                         $total = $this->calculateReportGroupTotal($dataset);
143                         $this->checkYAxis($total);
144                         
145                         $data .= $this->tab('<group>', 2);
146                         $data .= $this->tab('<title>' . $key . '</title>', 3);
147                         $data .= $this->tab('<value>' . $total . '</value>', 3);
148                         $data .= $this->tab('<label>' . $total . '</label>', 3);                                
149                         $data .= $this->tab('<subgroups>', 3);
150                         
151                         if ((isset($dataset[$total]) && $total != $dataset[$total]['numerical_value']) || !array_key_exists($key, $dataset)){
152                                         $data .= $this->processReportData($dataset, 4, $first);
153                         }
154
155                         if (!$first){                                                                                   
156                                 $not_processed = array_diff($this->super_set, $this->processed_report_keys);
157                                 $processed_diff_count = count($this->super_set) - count($not_processed);
158
159                                 if ($processed_diff_count != 0){
160                                         foreach ($not_processed as $title){
161                                                 $data .= $this->processDataGroup(4, $title, 'NULL', '', '');
162                                         }
163                                 }
164                         }
165                         $data .= $this->tab('</subgroups>', 3);                         
166                         $data .= $this->tab('</group>', 2);                             
167                         $this->processed_report_keys = array();
168                         // we're done with the first row!
169                         //$first = false;
170                 }
171                 return $data;           
172         }
173         
174         public function processXmlData(){
175                 $data = '';
176                 
177                 $this->super_set = $this->processReportGroup($this->data_set);
178                 $single_value = false;
179
180                 foreach ($this->data_set as $key => $dataset){
181                         if ((isset($dataset[$key]) && count($this->data_set[$key]) == 1)){
182                                 $single_value = true;
183                         }
184                         else{
185                                 $single_value = false;
186                         }
187                 }
188                 if ($this->chart_properties['type'] == 'line chart' && $single_value){
189                         $data .= $this->xmlDataReportSingleValue();
190                 }
191                 else{
192                         $data .= $this->xmlDataReportChart();
193                 }
194                 
195                 return $data;           
196         }       
197                 
198         /**
199      * wrapper function to return the html code containing the chart in a div
200          * 
201      * @param   string $name    name of the div
202          *                      string $xmlFile location of the XML file
203          *                      string $style   optional additional styles for the div
204      * @return  string returns the html code through smarty
205      */                                 
206         function display($name, $xmlFile, $width='320', $height='480', $reportChartDivStyle, $resize=false){
207                 
208                 
209                 parent::display($name, $xmlFile, $width, $height, $resize=false);                       
210                 
211                 return $this->ss->fetch('include/SugarCharts/Jit/tpls/chart.tpl');      
212         }
213 }
214
215 ?>