]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarCharts/Jit/JitReports.php
Release 6.5.0
[Github/sugarcrm.git] / include / SugarCharts / Jit / JitReports.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 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->tabValue('title',$key, 3);
123                         $data .= $this->tab('<subgroups>', 3);
124                         $data .= $this->tab('<group>',4);
125                         $data .= $this->tabValue('title',$total,5);
126                         $data .= $this->tabValue('value',$total,5);
127                         $data .= $this->tabValue('label',$key,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->tabValue('title',$key, 3);
147                         $data .= $this->tabValue('value',$total, 3);
148                         $data .= $this->tabValue('label',$total, 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                         else if(count($this->data_set) == 1 && $first){
155                             foreach ($dataset as $k=>$v){
156                                 if(isset($v['numerical_value'])) {
157                                     $data .= $this->processDataGroup(4, $k, $v['numerical_value'], $v['numerical_value'], '');
158                                 }
159                             }
160                         }                       
161
162                         if (!$first){                                                                                   
163                                 $not_processed = array_diff($this->super_set, $this->processed_report_keys);
164                                 $processed_diff_count = count($this->super_set) - count($not_processed);
165
166                                 if ($processed_diff_count != 0){
167                                         foreach ($not_processed as $title){
168                                                 $data .= $this->processDataGroup(4, $title, 'NULL', '', '');
169                                         }
170                                 }
171                         }
172                         
173                         $data .= $this->tab('</subgroups>', 3);                         
174                         $data .= $this->tab('</group>', 2);                             
175                         $this->processed_report_keys = array();
176                         // we're done with the first row!
177                         //$first = false;
178                 }
179                 return $data;           
180         }
181         
182         public function processXmlData(){
183                 $data = '';
184                 
185                 $this->super_set = $this->processReportGroup($this->data_set);
186                 $single_value = false;
187
188                 foreach ($this->data_set as $key => $dataset){
189                         if ((isset($dataset[$key]) && count($this->data_set[$key]) == 1)){
190                                 $single_value = true;
191                         }
192                         else{
193                                 $single_value = false;
194                         }
195                 }
196                 if ($this->chart_properties['type'] == 'line chart' && $single_value){
197                         $data .= $this->xmlDataReportSingleValue();
198                 }
199                 else{
200                         $data .= $this->xmlDataReportChart();
201                 }
202                 
203                 return $data;           
204         }       
205                 
206         /**
207      * wrapper function to return the html code containing the chart in a div
208          * 
209      * @param   string $name    name of the div
210          *                      string $xmlFile location of the XML file
211          *                      string $style   optional additional styles for the div
212      * @return  string returns the html code through smarty
213      */                                 
214         function display($name, $xmlFile, $width='320', $height='480', $reportChartDivStyle, $resize=false){
215                 if(empty($name)) {
216                         $name = "unsavedReport";        
217                 }
218                 
219                 parent::display($name, $xmlFile, $width, $height, $resize=false);                       
220                 
221                 return $this->ss->fetch('include/SugarCharts/Jit/tpls/chart.tpl');      
222         }
223 }
224
225 ?>