]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarCharts/JsChart.php
Release 6.5.0
[Github/sugarcrm.git] / include / SugarCharts / JsChart.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/SugarChart.php");
41
42 class JsChart extends SugarChart {
43         protected $ss;
44         var $xmlFile;
45         var $jsonFilename;
46         var $chartId;
47         var $width;
48         var $height;
49         var $chartType;
50
51         function __construct() {
52                 parent::__construct();
53         }
54
55         function isSupported($chartType) {
56                 $charts = array(
57                         "stacked group by chart",
58                         "group by chart",
59                         "bar chart",
60                         "horizontal group by chart",
61                         "horizontal",
62                         "horizontal bar chart",
63                         "pie chart",
64                         "gauge chart",
65                         "funnel chart 3D",
66                         "line chart",
67                 );
68
69                 if(in_array($chartType,$charts)) {
70                         return true;
71                 } else {
72                         return false;
73                 }
74
75         }
76
77         function tab($str, $depth){
78        // $str = preg_replace('/(<\w+>)(.*)(<\/\w+>)/e', "'\\1'.htmlentities(from_html('\\2')).'\\3'", $str);
79         return str_repeat("\t", $depth) . $str . "\n";
80         }
81
82         function display($name, $xmlFile, $width='320', $height='480', $resize=false) {
83
84
85                 $this->chartId = $name;
86                 $this->height = $height;
87                 $this->width = $width;
88                 $this->xmlFile = $xmlFile;
89                 $this->chartType = $this->chart_properties['type'];
90
91                 $style = array();
92                 $chartConfig = array();
93         try {
94                     $xmlStr = $this->processXML($this->xmlFile);
95                     $json = $this->buildJson($xmlStr);
96         }
97         catch(Exception $e) {
98             $GLOBALS['log']->fatal("Unable to return chart data, invalid xml for file {$this->xmlFile}");
99             return '';
100         }
101                 $this->saveJsonFile($json);
102                 $this->ss->assign("chartId", $this->chartId);
103                 $this->ss->assign("filename", $this->jsonFilename);
104                 global $mod_strings;
105                 if (isset($mod_strings['LBL_REPORT_SHOW_CHART']))
106                     $this->ss->assign("showchart", $mod_strings['LBL_REPORT_SHOW_CHART']);
107
108                 $dimensions = $this->getChartDimensions($xmlStr);
109                 $this->ss->assign("width", $dimensions['width']);
110                 $this->ss->assign("height", $dimensions['height']);
111                 $config = $this->getConfigProperties();
112                 $style['gridLineColor'] = str_replace("0x","#",$config->gridLines);
113                 $style['font-family'] = $config->labelFontFamily;
114                 $style['color'] = str_replace("0x","#",$config->labelFontColor);
115                 $this->ss->assign("css", $style);
116                 foreach($this->getChartConfigParams($xmlStr) as $key => $value) {
117                         $chartConfig[$key] = $value;
118                 }
119                 $chartConfig['imageExportType'] = $this->image_export_type;
120                 $this->ss->assign("config", $chartConfig);
121                 if($json == "No Data") {
122                         $this->ss->assign("error", "No Data");
123                 }
124
125                 if(!$this->isSupported($this->chartType)) {
126                         $this->ss->assign("error", "Unsupported Chart Type");
127                 }
128
129         }
130
131
132         function getDashletScript($id,$xmlFile="") {
133
134                 global $sugar_config, $current_user, $current_language;
135                 $this->id = $id;
136                 $this->chartId = $id;
137                 $this->xmlFile = (!$xmlFile) ? sugar_cached("xml/{$current_user->id}_{$this->id}.xml") : $xmlFile;
138
139
140                 $style = array();
141                 $chartConfig = array();
142                 $this->ss->assign("chartId", $this->chartId);
143                 $this->ss->assign("filename", str_replace(".xml",".js",$this->xmlFile));
144                 $config = $this->getConfigProperties();
145                 $style['gridLineColor'] = str_replace("0x","#",$config->gridLines);
146                 $style['font-family'] = $config->labelFontFamily;
147                 $style['color'] = str_replace("0x","#",$config->labelFontColor);
148                 $this->ss->assign("css", $style);
149                 $xmlStr = $this->processXML($this->xmlFile);
150                 foreach($this->getChartConfigParams($xmlStr) as $key => $value) {
151                         $chartConfig[$key] = $value;
152                 }
153                 
154                 $chartConfig['imageExportType'] = $this->image_export_type;
155                 $this->ss->assign("config", $chartConfig);
156
157         }
158
159         function chartArray($chartsArray) {
160
161                 $customChartsArray = array();
162                 $style = array();
163                 $chartConfig = array();
164                 foreach($chartsArray as $id => $data) {
165                         $customChartsArray[$id] = array();
166                         $customChartsArray[$id]['chartId'] = $id;
167                         $customChartsArray[$id]['filename'] = str_replace(".xml",".js",$data['xmlFile']);
168                         $customChartsArray[$id]['width'] = $data['width'];
169                         $customChartsArray[$id]['height'] = $data['height'];
170
171                         $config = $this->getConfigProperties();
172                         $style['gridLineColor'] = str_replace("0x","#",$config->gridLines);
173                         $style['font-family'] = (string)$config->labelFontFamily;
174                         $style['color'] = str_replace("0x","#",$config->labelFontColor);
175                         $customChartsArray[$id]['css'] = $style;
176                         $xmlStr = $this->processXML($data['xmlFile']);
177                         $xml = new SimpleXMLElement($xmlStr);
178                         $params = $this->getChartConfigParams($xmlStr);
179                         $customChartsArray[$id]['supported'] = ($this->isSupported($xml->properties->type)) ? "true" : "false";
180                         foreach($params as $key => $value) {
181                                 $chartConfig[$key] = $value;
182                         }
183                         $chartConfig['imageExportType'] = $this->image_export_type;
184                         $customChartsArray[$id]['chartConfig'] = $chartConfig;
185                 }
186
187                 return $customChartsArray;
188         }
189
190         function getChartConfigParams($xmlStr) {
191
192                 $xml = new SimpleXMLElement($xmlStr);
193
194                 $chartType = $xml->properties->type;
195                 if($chartType == "pie chart") {
196                         return array ("pieType" => "basic","tip" => "name","chartType" => "pieChart");
197                 } elseif($chartType == "line chart") {
198                         return array ("lineType" => "basic","tip" => "name","chartType" => "lineChart");
199                 } elseif($chartType == "funnel chart 3D") {
200                         return array ("funnelType" => "basic","tip" => "name","chartType" => "funnelChart");
201                 } elseif($chartType == "gauge chart") {
202                         return array ("gaugeType" => "basic","tip" => "name","chartType" => "gaugeChart");
203                 } elseif($chartType == "stacked group by chart") {
204                         return array ("orientation" => "vertical","barType" => "stacked","tip" => "name","chartType" => "barChart");
205                 } elseif($chartType == "group by chart") {
206                         return array("orientation" => "vertical", "barType" => "grouped", "tip" => "title","chartType" => "barChart");
207                 } elseif($chartType == "bar chart") {
208                         return array("orientation" => "vertical", "barType" => "basic", "tip" => "label","chartType" => "barChart");
209                 } elseif ($chartType == "horizontal group by chart") {
210                         return array("orientation" => "horizontal", "barType" => "stacked", "tip" => "name","chartType" => "barChart");
211                 } elseif ($chartType == "horizontal bar chart" || "horizontal") {
212                         return array("orientation" => "horizontal","barType" => "basic","tip" => "label","chartType" => "barChart");
213                 } else {
214                         return array("orientation" => "vertical","barType" => "stacked","tip" => "name","chartType" => "barChart");
215                 }
216         }
217         function getChartDimensions($xmlStr) {
218                 if($this->getNumNodes($xmlStr) > 9 && $this->chartType != "pie chart") {
219                         if($this->chartType == "horizontal group by chart" || $this->chartType == "horizontal bar chart") {
220                                 $height = ($this->getNumNodes($xmlStr) * 60) + 100;
221                                 return array("width"=>$this->width, "height"=>($height));
222                         } else {
223                                 return array("width"=>($this->width * 2), "height"=>$this->height);
224                         }
225                 } else {
226                         return array("width"=>"100%", "height"=>$this->height);
227                 }
228         }
229
230         function checkData($xmlstr) {
231         $xml = new SimpleXMLElement($xmlstr);
232         if(sizeof($xml->data->group) > 0) {
233                         return true;
234                 } else {
235                         return false;
236                 }
237         }
238
239         function getNumNodes($xmlstr) {
240                 $xml = new SimpleXMLElement($xmlstr);
241                 return sizeof($xml->data->group);
242         }
243
244         function buildProperties($xmlstr) {
245                 $content = $this->tab("'properties': [\n",1);
246                 $properties = array();
247                 $xml = new SimpleXMLElement($xmlstr);
248                 foreach($xml->properties->children() as $property) {
249                         $properties[] = $this->tab("'".$property->getName()."':"."'".$this->processSpecialChars($property)."'",2);
250                 }
251                 $content .= $this->tab("{\n",1);
252                 $content .= join(",\n",$properties)."\n";
253                 $content .= $this->tab("}\n",1);
254                 $content .= $this->tab("],\n",1);
255                 return $content;
256         }
257
258         function buildLabelsBarChartStacked($xmlstr) {
259                 $content = $this->tab("'label': [\n",1);
260                 $labels = array();
261                 $xml = new SimpleXMLElement($xmlstr);
262                 foreach($xml->data->group[0]->subgroups->group as $group) {
263                         $labels[] = $this->tab("'".$this->processSpecialChars($group->title)."'",2);
264                 }
265                 $content .= join(",\n",$labels)."\n";
266                 $content .= $this->tab("],\n",1);
267                 return $content;
268         }
269
270         function buildLabelsBarChart($xmlstr) {
271
272         // fix for bug42326: if there is label data that is deeper than that of a normal bar chart, render the labels
273         //    like a stacked bar chart, this applies when a stacked chart has been converted in templates_chart.php to a bar chart due to it
274         //    having nothing but single value columns
275         $xml = new SimpleXMLElement($xmlstr);
276         if (count($xml->data->group[0]->subgroups->group) > 0) {
277             return $this->buildLabelsBarChartStacked($xmlstr);
278         }
279
280         $content = $this->tab("'label': [\n",1);
281                 $labels = array();
282
283                 foreach($xml->data->group as $group) {
284                         $labels[] = $this->tab("'".$this->processSpecialChars($group->title)."'",2);
285                 }
286                 $labelStr = join(",\n",$labels)."\n";
287                 $content .= $labelStr;
288                 $content .= $this->tab("],\n",1);
289                 return $content;
290         }
291
292         function buildDataBarChartStacked($xmlstr) {
293                 $content = $this->tab("'values': [\n",1);
294                 $data = array();
295                 $xml = new SimpleXMLElement($xmlstr);
296                 foreach($xml->data->group as $group) {
297                         $groupcontent = $this->tab("{\n",1);
298                         $groupcontent .= $this->tab("'label': '".$this->processSpecialChars($group->title)."',\n",2);
299                         $groupcontent .= $this->tab("'gvalue': '{$group->value}',\n",2);
300                         $groupcontent .= $this->tab("'gvaluelabel': '{$group->label}',\n",2);
301                         $subgroupValues = array();
302                         $subgroupValueLabels = array();
303                         $subgroupLinks = array();
304                         foreach($group->subgroups->group as $subgroups) {
305                                 $subgroupValues[] = $this->tab(($subgroups->value == "NULL") ? 0 : $subgroups->value,3);
306                                 $subgroupValueLabels[] = $this->tab("'".$this->processSpecialChars($subgroups->label)."'",3);
307                                 $subgroupLinks[] = $this->tab("'".$subgroups->link."'",3);
308                         }
309                         $subgroupValuesStr = join(",\n",$subgroupValues)."\n";
310                         $subgroupValueLabelsStr = join(",\n",$subgroupValueLabels)."\n";
311                         $subgroupLinksStr = join(",\n",$subgroupLinks)."\n";
312
313                         $groupcontent .= $this->tab("'values': [\n".$subgroupValuesStr,2);
314                         $groupcontent .= $this->tab("],\n",2);
315                         $groupcontent .= $this->tab("'valuelabels': [\n".$subgroupValueLabelsStr,2);
316                         $groupcontent .= $this->tab("],\n",2);
317                         $groupcontent .= $this->tab("'links': [\n".$subgroupLinksStr,2);
318                         $groupcontent .= $this->tab("]\n",2);
319                         $groupcontent .= $this->tab("}",1);
320                         $data[] = $groupcontent;
321                 }
322                 $content .= join(",\n",$data)."\n";
323                 $content .= $this->tab("]",1);
324                 return $content;
325         }
326
327         function buildDataBarChartGrouped($xmlstr) {
328                 $content = $this->tab("'values': [\n",1);
329                 $data = array();
330                 $xml = new SimpleXMLElement($xmlstr);
331                 foreach($xml->data->group as $group) {
332                         $groupcontent = $this->tab("{\n",1);
333                         $groupcontent .= $this->tab("'label': '".$this->processSpecialChars($group->title)."',\n",2);
334                         $groupcontent .= $this->tab("'gvalue': '{$group->value}',\n",2);
335                         $groupcontent .= $this->tab("'gvaluelabel': '{$group->label}',\n",2);
336                         $subgroupValues = array();
337                         $subgroupValueLabels = array();
338                         $subgroupLinks = array();
339                         $subgroupTitles = array();
340                         foreach($group->subgroups->group as $subgroups) {
341                                 $subgroupValues[] = $this->tab(($subgroups->value == "NULL") ? 0 : $subgroups->value,3);
342                                 $subgroupValueLabels[] = $this->tab("'".$subgroups->label."'",3);
343                                 $subgroupLinks[] = $this->tab("'".$subgroups->link."'",3);
344                                 $subgroupTitles[] = $this->tab("'".$this->processSpecialChars($subgroups->title)."'",3);
345                         }
346                         $subgroupValuesStr = join(",\n",$subgroupValues)."\n";
347                         $subgroupValueLabelsStr = join(",\n",$subgroupValueLabels)."\n";
348                         $subgroupLinksStr = join(",\n",$subgroupLinks)."\n";
349                         $subgroupTitlesStr = join(",\n",$subgroupTitles)."\n";
350
351                         $groupcontent .= $this->tab("'values': [\n".$subgroupValuesStr,2);
352                         $groupcontent .= $this->tab("],\n",2);
353                         $groupcontent .= $this->tab("'valuelabels': [\n".$subgroupValueLabelsStr,2);
354                         $groupcontent .= $this->tab("],\n",2);
355                         $groupcontent .= $this->tab("'links': [\n".$subgroupLinksStr,2);
356                         $groupcontent .= $this->tab("],\n",2);
357                         $groupcontent .= $this->tab("'titles': [\n".$subgroupTitlesStr,2);
358                         $groupcontent .= $this->tab("]\n",2);
359                         $groupcontent .= $this->tab("}",1);
360                         $data[] = $groupcontent;
361                 }
362                 $content .= join(",\n",$data)."\n";
363                 $content .= $this->tab("]",1);
364                 return $content;
365         }
366
367         function buildDataBarChart($xmlstr) {
368                 $content = $this->tab("'values': [\n",1);
369                 $data = array();
370                 $xml = new SimpleXMLElement($xmlstr);
371                 $groupcontent = "";
372                 $groupcontentArr = array();
373
374                 foreach($xml->data->group as $group) {
375                 $groupcontent = $this->tab("{\n",1);
376                 $groupcontent .= $this->tab("'label': [\n",2);
377                 $groupcontent .= $this->tab("'".$this->processSpecialChars($group->title)."'\n",3);
378                 $groupcontent .= $this->tab("],\n",2);
379                 $groupcontent .= $this->tab("'values': [\n",2);
380                 $groupcontent .= $this->tab(($group->value == "NULL") ? 0 : $group->value."\n",3);
381                 $groupcontent .= $this->tab("],\n",2);
382                 if($group->label) {
383                         $groupcontent .= $this->tab("'valuelabels': [\n",2);
384                         $groupcontent .= $this->tab("'{$group->label}'\n",3);
385                         $groupcontent .= $this->tab("],\n",2);
386                 }
387                 $groupcontent .= $this->tab("'links': [\n",2);
388                 $groupcontent .= $this->tab("'{$group->link}'\n",3);
389                 $groupcontent .= $this->tab("]\n",2);
390                 $groupcontent .= $this->tab("}",1);
391                 $groupcontentArr[] = $groupcontent;
392                 }
393                 $content .= join(",\n",$groupcontentArr)."\n";
394                 $content .= $this->tab("]",1);
395                 return $content;
396         }
397
398           function buildLabelsPieChart($xmlstr) {
399                 $content = $this->tab("'label': [\n",1);
400                 $labels = array();
401                 $xml = new SimpleXMLElement($xmlstr);
402                 foreach($xml->data->group as $group) {
403                         $labels[] = $this->tab("'".$this->processSpecialChars($group->title)."'",2);
404                 }
405                 $labelStr = join(",\n",$labels)."\n";
406                 $content .= $labelStr;
407                 $content .= $this->tab("],\n",1);
408                 return $content;
409         }
410
411
412         function buildDataPieChart($xmlstr) {
413                 $content = $this->tab("'values': [\n",1);
414                 $data = array();
415                 $xml = new SimpleXMLElement($xmlstr);
416                 $groupcontent = "";
417                 $groupcontentArr = array();
418
419                 foreach($xml->data->group as $group) {
420                 $groupcontent = $this->tab("{\n",1);
421                 $groupcontent .= $this->tab("'label': [\n",2);
422                 $groupcontent .= $this->tab("'".$this->processSpecialChars($group->title)."'\n",3);
423                 $groupcontent .= $this->tab("],\n",2);
424                 $groupcontent .= $this->tab("'values': [\n",2);
425                 $groupcontent .= $this->tab("{$group->value}\n",3);
426                 $groupcontent .= $this->tab("],\n",2);
427                 $groupcontent .= $this->tab("'valuelabels': [\n",2);
428                 $groupcontent .= $this->tab("'{$group->label}'\n",3);
429                 $groupcontent .= $this->tab("],\n",2);
430                 $groupcontent .= $this->tab("'links': [\n",2);
431                 $groupcontent .= $this->tab("'{$group->link}'\n",3);
432                 $groupcontent .= $this->tab("]\n",2);
433                 $groupcontent .= $this->tab("}",1);
434                 $groupcontentArr[] = $groupcontent;
435                 }
436
437
438                 $content .= join(",\n",$groupcontentArr)."\n";
439                 $content .= $this->tab("\n]",1);
440                 return $content;
441         }
442
443         function buildLabelsGaugeChart($xmlstr) {
444                 $content = $this->tab("'label': [\n",1);
445                 $labels = array();
446                 $xml = new SimpleXMLElement($xmlstr);
447                 foreach($xml->data->group as $group) {
448                         $labels[] = $this->tab("'".$this->processSpecialChars($group->title)."'",2);
449                 }
450                 $labelStr = join(",\n",$labels)."\n";
451                 $content .= $labelStr;
452                 $content .= $this->tab("],\n",1);
453                 return $content;
454         }
455
456         function buildDataGaugeChart($xmlstr) {
457                 $content = $this->tab("'values': [\n",1);
458                 $data = array();
459                 $xml = new SimpleXMLElement($xmlstr);
460                 foreach($xml->data->group as $group) {
461                         $groupcontent = $this->tab("{\n",1);
462                         $groupcontent .= $this->tab("'label': '".$this->processSpecialChars($group->title)."',\n",2);
463                         $groupcontent .= $this->tab("'gvalue': '{$group->value}',\n",2);
464                         $finalComma = ($group->title != "GaugePosition") ? "," : "";
465                         $groupcontent .= $this->tab("'gvaluelabel': '{$group->label}'{$finalComma}\n",2);
466                         $subgroupTitles = array();
467                         $subgroupValues = array();
468                         $subgroupValueLabels = array();
469                         $subgroupLinks = array();
470
471                         if(is_object($group->subgroups->group)) {
472                                 foreach($group->subgroups->group as $subgroups) {
473                                         $subgroupTitles[] = $this->tab("'".$subgroups->title."'",3);
474                                         //$subgroupValues[] = $this->tab($subgroups->value,3);
475                                         $subgroupValues[] = $subgroups->value;
476                                         $subgroupValueLabels[] = $this->tab("'".$subgroups->label."'",3);
477                                         $subgroupLinks[] = $this->tab("'".$subgroups->link."'",3);
478                                 }
479                                 $subgroupTitlesStr = join(",\n",$subgroupTitles)."\n";
480                                 $subgroupValuesStr = join(",\n",$subgroupValues)."\n";
481                                 $subgroupValueLabelsStr = join(",\n",$subgroupValueLabels)."\n";
482                                 $subgroupLinksStr = join(",\n",$subgroupLinks)."\n";
483
484                                 //$groupcontent .= $this->tab("'labels': [\n".$subgroupTitlesStr,2);
485                                 //$groupcontent .= $this->tab("],\n",2);
486                                 $val = ((int)$subgroupValues[1] == (int)$subgroupValues[0]) ? $this->tab($subgroupValues[1],3)."\n" : $this->tab($subgroupValues[1] - $subgroupValues[0],3)."\n";
487
488                                 $groupcontent .= $this->tab("'values': [\n".$val,2);
489                                 $groupcontent .= $this->tab("],\n",2);
490                                 $groupcontent .= $this->tab("'valuelabels': [\n".$subgroupValueLabelsStr,2);
491                                 $groupcontent .= $this->tab("]\n",2);
492                                 //$groupcontent .= $this->tab("'links': [\n".$subgroupLinksStr,2);
493                                 //$groupcontent .= $this->tab("]\n",2);
494
495                         }
496
497                                 $groupcontent .= $this->tab("}",1);
498                                 $data[] = $groupcontent;
499
500                 }
501
502                 $content .= join(",\n",$data)."\n";
503
504
505                 $content .= $this->tab("]",1);
506                 return $content;
507         }
508
509
510         function getConfigProperties() {
511                 $path = SugarThemeRegistry::current()->getImageURL('sugarColors.xml',false);
512
513                 if(!file_exists($path)) {
514                         $GLOBALS['log']->debug("Cannot open file ($path)");
515                 }
516                 $xmlstr = file_get_contents($path);
517                 $xml = new SimpleXMLElement($xmlstr);
518                 return $xml->charts;
519         }
520
521         function buildChartColors() {
522
523                 $content = $this->tab("'color': [\n",1);
524                 $colorArr = array();
525                 $xml = $this->getConfigProperties();
526                 $colors = ($this->chartType == "gauge chart") ? $xml->gaugeChartElementColors->color : $xml->chartElementColors->color;
527                 foreach($colors as $color) {
528                         $colorArr[] = $this->tab("'".str_replace("0x","#",$color)."'",2);
529                 }
530                 $content .= join(",\n",$colorArr)."\n";
531                 $content .= $this->tab("],\n",1);
532
533                 return $content;
534
535         }
536
537         function buildJson($xmlstr){
538                 if($this->checkData($xmlstr)) {
539                         $content = "{\n";
540                         if ($this->chartType == "pie chart" || $this->chartType == "funnel chart 3D") {
541                                 $content .= $this->buildProperties($xmlstr);
542                                 $content .= $this->buildLabelsPieChart($xmlstr);
543                                 $content .= $this->buildChartColors();
544                                 $content .= $this->buildDataPieChart($xmlstr);
545                         }
546                         elseif ($this->chartType == "gauge chart") {
547                                 $content .= $this->buildProperties($xmlstr);
548                                 $content .= $this->buildLabelsGaugeChart($xmlstr);
549                                 $content .= $this->buildChartColors();
550                                 $content .= $this->buildDataGaugeChart($xmlstr);
551                         }
552                         elseif ($this->chartType == "horizontal bar chart" || $this->chartType == "bar chart") {
553                                 $content .= $this->buildProperties($xmlstr);
554                                 $content .= $this->buildLabelsBarChart($xmlstr);
555                                 $content .= $this->buildChartColors();
556                                 $content .= $this->buildDataBarChart($xmlstr);
557                         }
558                         elseif ($this->chartType == "group by chart") {
559                                 $content .= $this->buildProperties($xmlstr);
560                                 $content .= $this->buildLabelsBarChartStacked($xmlstr);
561                                 $content .= $this->buildChartColors();
562                                 $content .= $this->buildDataBarChartGrouped($xmlstr);
563                         } else {
564                                 $content .= $this->buildProperties($xmlstr);
565                                 $content .= $this->buildLabelsBarChartStacked($xmlstr);
566                                 $content .= $this->buildChartColors();
567                                 $content .= $this->buildDataBarChartStacked($xmlstr);
568                         }
569                         $content .= "\n}";
570                         return $content;
571                 } else {
572                         return "No Data";
573                 }
574         }
575
576         function buildHTMLLegend($xmlFile) {
577                 $xmlstr = $this->processXML($xmlFile);
578                 $xml = new SimpleXMLElement($xmlstr);
579                 $this->chartType = $xml->properties->type;
580                 $html = "<table align=\"left\" cellpadding=\"2\" cellspacing=\"2\">";
581
582         if (
583             $this->chartType == "group by chart"
584             || $this->chartType == "horizontal group by chart"
585             || $this->chartType == 'line chart'
586             || $this->chartType == 'stacked group by chart'
587         )
588         {
589                         $groups = $xml->data->group[0]->subgroups->group;
590                         $items = (sizeof($xml->data->group[0]->subgroups->group) <= 5) ? 5 : sizeof($xml->data->group[0]->subgroups->group);
591                 } else {
592             if ($this->chartType == "funnel chart 3D") {
593                 // reverse legend
594                 $groups = array();
595                 foreach($xml->data->group as $group) {
596                     array_unshift($groups, $group);
597                 }
598             } else {
599                 $groups = $xml->data->group;
600             }
601                         $items = (sizeof($xml->data->group) <= 5) ? 5 : sizeof($xml->data->group);
602                 }
603
604                 $rows = ceil($items/5);
605                 $fullItems = $rows * 5;
606                 $remainder = ($items < $fullItems) ? $fullItems - $items : 0;
607                 $i = 0;
608                 $x = 0;
609
610
611                 $colorArr = array();
612                 $xmlColors = $this->getConfigProperties();
613                 $colors = ($this->chartType == "gauge chart") ? $xmlColors->gaugeChartElementColors->color : $xmlColors->chartElementColors->color;
614
615                 foreach($colors as $color) {
616                         $colorArr[] = str_replace("0x","#",$color);
617                 }
618
619         $isTrClosed = false;
620                 foreach($groups as $group) {
621                         if($i == 5) {$i = 0;}
622                         $html .= ($i == 0) ? "<tr>" : "";
623                         $html .= "<td width=\"50\">";
624                         $html .= "<div style=\"background-color:".$colorArr[$x].";\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>";
625                         $html .= "</td>";
626                         $html .= "<td>";
627                         $html .= $group->title;
628                         $html .= "</td>";
629                         $html .= ($x+1 == $items) ? "<td colspan=".($remainder*2)."></td>" : "";
630             if ($i == 4)
631             {
632                 $html .= "</tr>";
633                 $isTrClosed = true;
634             }
635             else
636             {
637                 $isTrClosed = false;
638             }
639                         $x++;
640                         $i++;
641                 }
642         if ($isTrClosed == false)
643         {
644             $html .= '</tr>';
645         }
646
647                 $html .= "</table>";
648                 return $html;
649         }
650
651         function saveJsonFile($jsonContents) {
652                 $this->jsonFilename = str_replace(".xml",".js",$this->xmlFile);
653                 //$jsonContents = $GLOBALS['locale']->translateCharset($jsonContents, 'UTF-8', 'UTF-16LE');
654
655                 // open file
656                 if (!$fh = sugar_fopen($this->jsonFilename, 'w')) {
657                         $GLOBALS['log']->debug("Cannot open file ($this->jsonFilename)");
658                         return;
659                 }
660
661                 // write the contents to the file
662                 if (fwrite($fh,$jsonContents) === FALSE) {
663                         $GLOBALS['log']->debug("Cannot write to file ($this->jsonFilename)");
664                         return false;
665                 }
666
667                 $GLOBALS['log']->debug("Success, wrote ($jsonContents) to file ($this->jsonFilename)");
668
669                 fclose($fh);
670                 return true;
671         }
672
673         function get_image_cache_file_name ($xmlFile,$ext = ".png") {
674                 $filename = str_replace("/xml/","/images/",str_replace(".xml",$ext,$xmlFile));
675
676                 return $filename;
677         }
678
679
680         function getXMLChartProperties($xmlStr) {
681                 $props = array();
682                 $xml = new SimpleXMLElement($xmlstr);
683                 foreach($xml->properties->children() as $properties) {
684                         $props[$properties->getName()] = $properties;
685                 }
686                 return $props;
687         }
688         
689         function processSpecialChars($str) {
690                 return addslashes(html_entity_decode($str,ENT_QUOTES));
691         }
692         
693         function processXML($xmlFile) {
694
695                 if(!file_exists($xmlFile)) {
696                         $GLOBALS['log']->debug("Cannot open file ($xmlFile)");
697                 }
698
699                 $pattern = array();
700                 $replacement = array();
701                 $content = file_get_contents($xmlFile);
702                 $content = $GLOBALS['locale']->translateCharset($content,'UTF-16LE', 'UTF-8');
703                 $pattern[] = '/\<link\>([a-zA-Z0-9#?&%.;\[\]\/=+_-\s]+)\<\/link\>/e';
704                 $replacement[] = "'<link>'.urlencode(\"$1\").'</link>'";
705 //              $pattern[] = '/NULL/e';
706 //              $replacement[] = "";
707                 return preg_replace($pattern,$replacement, $content);
708         }
709
710
711 }
712
713 ?>