]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarCharts/Jit/JitReports.php
Release 6.5.3
[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     /**
60      * Method checks is our dataset from currency field or not
61      *
62      * @param array $dataset of chart
63      * @return bool is currency
64      */
65     public function isCurrencyReportGroupTotal(array $dataset)
66     {
67         $isCurrency = true;
68         foreach ($dataset as $value)
69         {
70             if (empty($value['numerical_is_currency']))
71             {
72                 $isCurrency = false;
73                 break;
74             }
75         }
76         return $isCurrency;
77     }
78
79         function processReportData($dataset, $level=1, $first=false){
80                 $data = '';
81                 
82                 // rearrange $dataset to get the correct order for the first row
83                 if ($first){
84                         $temp_dataset = array();
85                         foreach ($this->super_set as $key){
86                                 $temp_dataset[$key] = (isset($dataset[$key])) ? $dataset[$key] : array();
87                         }
88                         $dataset = $temp_dataset;                       
89                 }
90                 
91                 foreach ($dataset as $key=>$value){
92                         if ($first && empty($value)){
93                                 $data .= $this->processDataGroup(4, $key, 'NULL', '', '');
94                         }                       
95                         else if (array_key_exists('numerical_value', $dataset)){
96                                 $link = (isset($dataset['link'])) ? '#'.$dataset['link'] : '';
97                                 $data .= $this->processDataGroup($level, $dataset['group_base_text'], $dataset['numerical_value'], $dataset['numerical_value'], $link);
98                                 array_push($this->processed_report_keys, $dataset['group_base_text']);
99                                 return $data;
100                         }
101                         else{
102                                 $data .= $this->processReportData($value, $level+1);
103                         }
104                 }
105                 
106                 return $data;
107         }
108         
109         function processReportGroup($dataset){
110                 $super_set = array();
111
112         foreach($dataset as $groupBy => $groups){
113             $prev_super_set = $super_set;
114             if (count($groups) > count($super_set)){
115                     $super_set = array_keys($groups);
116                 foreach($prev_super_set as $prev_group){
117                     if (!in_array($prev_group, $groups)){
118                         array_push($super_set, $prev_group);
119                     }       
120                 }       
121             }       
122             else{ 
123                 foreach($groups as $group => $groupData){
124                     if (!in_array($group, $super_set)){ 
125                         array_push($super_set, $group);
126                     }       
127                 }       
128             }       
129         }     
130         $super_set = array_unique($super_set);
131
132                 return $super_set;
133         }
134         
135         function xmlDataReportSingleValue(){
136                 $data = '';             
137                 foreach ($this->data_set as $key => $dataset){
138                         $total = $this->calculateReportGroupTotal($dataset);
139                         $this->checkYAxis($total);                                              
140
141                         $data .= $this->tab('<group>', 2);
142                         $data .= $this->tabValue('title',$key, 3);
143                         $data .= $this->tab('<subgroups>', 3);
144                         $data .= $this->tab('<group>',4);
145                         $data .= $this->tabValue('title',$total,5);
146                         $data .= $this->tabValue('value',$total,5);
147                         $data .= $this->tabValue('label',$key,5);
148                         $data .= $this->tab('<link></link>',5);
149                         $data .= $this->tab('</group>',4);
150                         $data .= $this->tab('</subgroups>', 3);                         
151                         $data .= $this->tab('</group>', 2);                     
152                 }
153                 return $data;
154         }
155         
156     function xmlDataReportChart()
157     {
158         global $app_strings;
159                 $data = '';
160                 // correctly process the first row
161                 $first = true;  
162                 foreach ($this->data_set as $key => $dataset){
163                         
164                         $total = $this->calculateReportGroupTotal($dataset);
165                         $this->checkYAxis($total);
166
167                         $data .= $this->tab('<group>', 2);
168                         $data .= $this->tabValue('title',$key, 3);
169                         $data .= $this->tabValue('value',$total, 3);
170
171             $label = $total;
172             if ($this->isCurrencyReportGroupTotal($dataset))
173             {;
174                 $label = currency_format_number($total, array(
175                     'currency_symbol' => $this->currency_symbol,
176                     'decimals' => ($this->chart_properties['thousands'] ? 0 : null)
177                 ));
178             }
179             if ($this->chart_properties['thousands'])
180             {
181                 $label .= $app_strings['LBL_THOUSANDS_SYMBOL'];
182             }
183             $data .= $this->tabValue('label', $label, 3);
184
185             $data .= $this->tab('<subgroups>', 3);
186                         
187                         if ((isset($dataset[$total]) && $total != $dataset[$total]['numerical_value']) || !array_key_exists($key, $dataset)){
188                                         $data .= $this->processReportData($dataset, 4, $first);
189                         }
190                         else if(count($this->data_set) == 1 && $first){
191                             foreach ($dataset as $k=>$v){
192                                 if(isset($v['numerical_value'])) {
193                                     $data .= $this->processDataGroup(4, $k, $v['numerical_value'], $v['numerical_value'], '');
194                                 }
195                             }
196                         }                       
197
198                         if (!$first){                                                                                   
199                                 $not_processed = array_diff($this->super_set, $this->processed_report_keys);
200                                 $processed_diff_count = count($this->super_set) - count($not_processed);
201
202                                 if ($processed_diff_count != 0){
203                                         foreach ($not_processed as $title){
204                                                 $data .= $this->processDataGroup(4, $title, 'NULL', '', '');
205                                         }
206                                 }
207                         }
208                         
209                         $data .= $this->tab('</subgroups>', 3);                         
210                         $data .= $this->tab('</group>', 2);                             
211                         $this->processed_report_keys = array();
212                         // we're done with the first row!
213                         //$first = false;
214                 }
215                 return $data;           
216         }
217         
218         public function processXmlData(){
219                 $data = '';
220                 
221                 $this->super_set = $this->processReportGroup($this->data_set);
222                 $single_value = false;
223
224                 foreach ($this->data_set as $key => $dataset){
225                         if ((isset($dataset[$key]) && count($this->data_set[$key]) == 1)){
226                                 $single_value = true;
227                         }
228                         else{
229                                 $single_value = false;
230                         }
231                 }
232                 if ($this->chart_properties['type'] == 'line chart' && $single_value){
233                         $data .= $this->xmlDataReportSingleValue();
234                 }
235                 else{
236                         $data .= $this->xmlDataReportChart();
237                 }
238                 
239                 return $data;           
240         }       
241                 
242         /**
243      * wrapper function to return the html code containing the chart in a div
244          * 
245      * @param   string $name    name of the div
246          *                      string $xmlFile location of the XML file
247          *                      string $style   optional additional styles for the div
248      * @return  string returns the html code through smarty
249      */                                 
250         function display($name, $xmlFile, $width='320', $height='480', $reportChartDivStyle, $resize=false){
251                 if(empty($name)) {
252                         $name = "unsavedReport";        
253                 }
254                 
255                 parent::display($name, $xmlFile, $width, $height, $resize=false);                       
256                 
257                 return $this->ss->fetch('include/SugarCharts/Jit/tpls/chart.tpl');      
258                 
259                 
260         }
261 }