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