2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
40 require_once("include/SugarCharts/Jit/Jit.php");
42 class JitReports extends Jit {
44 private $processed_report_keys = array();
46 function __construct() {
47 parent::__construct();
50 function calculateReportGroupTotal($dataset){
52 foreach ($dataset as $value){
53 $total += $value['numerical_value'];
60 * Method checks is our dataset from currency field or not
62 * @param array $dataset of chart
63 * @return bool is currency
65 public function isCurrencyReportGroupTotal(array $dataset)
68 foreach ($dataset as $value)
70 if (empty($value['numerical_is_currency']))
79 function processReportData($dataset, $level=1, $first=false){
82 // rearrange $dataset to get the correct order for the first row
84 $temp_dataset = array();
85 foreach ($this->super_set as $key){
86 $temp_dataset[$key] = (isset($dataset[$key])) ? $dataset[$key] : array();
88 $dataset = $temp_dataset;
91 foreach ($dataset as $key=>$value){
92 if ($first && empty($value)){
93 $data .= $this->processDataGroup(4, $key, 'NULL', '', '');
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']);
102 $data .= $this->processReportData($value, $level+1);
109 function processReportGroup($dataset){
110 $super_set = array();
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);
123 foreach($groups as $group => $groupData){
124 if (!in_array($group, $super_set)){
125 array_push($super_set, $group);
130 $super_set = array_unique($super_set);
135 function xmlDataReportSingleValue(){
137 foreach ($this->data_set as $key => $dataset){
138 $total = $this->calculateReportGroupTotal($dataset);
139 $this->checkYAxis($total);
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);
156 function xmlDataReportChart()
160 // correctly process the first row
162 foreach ($this->data_set as $key => $dataset){
164 $total = $this->calculateReportGroupTotal($dataset);
165 $this->checkYAxis($total);
167 $data .= $this->tab('<group>', 2);
168 $data .= $this->tabValue('title',$key, 3);
169 $data .= $this->tabValue('value',$total, 3);
172 if ($this->isCurrencyReportGroupTotal($dataset))
174 $label = currency_format_number($total, array(
175 'currency_symbol' => $this->currency_symbol,
176 'decimals' => ($this->chart_properties['thousands'] ? 0 : null)
179 if ($this->chart_properties['thousands'])
181 $label .= $app_strings['LBL_THOUSANDS_SYMBOL'];
183 $data .= $this->tabValue('label', $label, 3);
185 $data .= $this->tab('<subgroups>', 3);
187 if ((isset($dataset[$total]) && $total != $dataset[$total]['numerical_value']) || !array_key_exists($key, $dataset)){
188 $data .= $this->processReportData($dataset, 4, $first);
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'], '');
199 $not_processed = array_diff($this->super_set, $this->processed_report_keys);
200 $processed_diff_count = count($this->super_set) - count($not_processed);
202 if ($processed_diff_count != 0){
203 foreach ($not_processed as $title){
204 $data .= $this->processDataGroup(4, $title, 'NULL', '', '');
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!
218 public function processXmlData(){
221 $this->super_set = $this->processReportGroup($this->data_set);
222 $single_value = false;
224 foreach ($this->data_set as $key => $dataset){
225 if ((isset($dataset[$key]) && count($this->data_set[$key]) == 1)){
226 $single_value = true;
229 $single_value = false;
232 if ($this->chart_properties['type'] == 'line chart' && $single_value){
233 $data .= $this->xmlDataReportSingleValue();
236 $data .= $this->xmlDataReportChart();
243 * wrapper function to return the html code containing the chart in a div
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
250 function display($name, $xmlFile, $width='320', $height='480', $reportChartDivStyle, $resize=false){
252 $name = "unsavedReport";
255 parent::display($name, $xmlFile, $width, $height, $resize=false);
257 return $this->ss->fetch('include/SugarCharts/Jit/tpls/chart.tpl');