]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarCharts/JsChart.php
Release 6.2.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-2011 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_config['tmp_dir']. $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                 $content = $this->tab("'label': [\n",1);
270                 $labels = array();
271                 $xml = new SimpleXMLElement($xmlstr);
272                 foreach($xml->data->group as $group) {
273                         $labels[] = $this->tab("'".$this->processSpecialChars($group->title)."'",2);
274                 }
275                 $labelStr = join(",\n",$labels)."\n";
276                 $content .= $labelStr;
277                 $content .= $this->tab("],\n",1);
278                 return $content;
279         }
280
281         function buildDataBarChartStacked($xmlstr) {
282                 $content = $this->tab("'values': [\n",1);
283                 $data = array();
284                 $xml = new SimpleXMLElement($xmlstr);
285                 foreach($xml->data->group as $group) {
286                         $groupcontent = $this->tab("{\n",1);
287                         $groupcontent .= $this->tab("'label': '".$this->processSpecialChars($group->title)."',\n",2);
288                         $groupcontent .= $this->tab("'gvalue': '{$group->value}',\n",2);
289                         $groupcontent .= $this->tab("'gvaluelabel': '{$group->label}',\n",2);
290                         $subgroupValues = array();
291                         $subgroupValueLabels = array();
292                         $subgroupLinks = array();
293                         foreach($group->subgroups->group as $subgroups) {
294                                 $subgroupValues[] = $this->tab(($subgroups->value == "NULL") ? 0 : $subgroups->value,3);
295                                 $subgroupValueLabels[] = $this->tab("'".$this->processSpecialChars($subgroups->label)."'",3);
296                                 $subgroupLinks[] = $this->tab("'".$subgroups->link."'",3);
297                         }
298                         $subgroupValuesStr = join(",\n",$subgroupValues)."\n";
299                         $subgroupValueLabelsStr = join(",\n",$subgroupValueLabels)."\n";
300                         $subgroupLinksStr = join(",\n",$subgroupLinks)."\n";
301
302                         $groupcontent .= $this->tab("'values': [\n".$subgroupValuesStr,2);
303                         $groupcontent .= $this->tab("],\n",2);
304                         $groupcontent .= $this->tab("'valuelabels': [\n".$subgroupValueLabelsStr,2);
305                         $groupcontent .= $this->tab("],\n",2);
306                         $groupcontent .= $this->tab("'links': [\n".$subgroupLinksStr,2);
307                         $groupcontent .= $this->tab("]\n",2);
308                         $groupcontent .= $this->tab("}",1);
309                         $data[] = $groupcontent;
310                 }
311                 $content .= join(",\n",$data)."\n";
312                 $content .= $this->tab("]",1);
313                 return $content;
314         }
315
316         function buildDataBarChartGrouped($xmlstr) {
317                 $content = $this->tab("'values': [\n",1);
318                 $data = array();
319                 $xml = new SimpleXMLElement($xmlstr);
320                 foreach($xml->data->group as $group) {
321                         $groupcontent = $this->tab("{\n",1);
322                         $groupcontent .= $this->tab("'label': '".$this->processSpecialChars($group->title)."',\n",2);
323                         $groupcontent .= $this->tab("'gvalue': '{$group->value}',\n",2);
324                         $groupcontent .= $this->tab("'gvaluelabel': '{$group->label}',\n",2);
325                         $subgroupValues = array();
326                         $subgroupValueLabels = array();
327                         $subgroupLinks = array();
328                         $subgroupTitles = array();
329                         foreach($group->subgroups->group as $subgroups) {
330                                 $subgroupValues[] = $this->tab(($subgroups->value == "NULL") ? 0 : $subgroups->value,3);
331                                 $subgroupValueLabels[] = $this->tab("'".$subgroups->label."'",3);
332                                 $subgroupLinks[] = $this->tab("'".$subgroups->link."'",3);
333                                 $subgroupTitles[] = $this->tab("'".$this->processSpecialChars($subgroups->title)."'",3);
334                         }
335                         $subgroupValuesStr = join(",\n",$subgroupValues)."\n";
336                         $subgroupValueLabelsStr = join(",\n",$subgroupValueLabels)."\n";
337                         $subgroupLinksStr = join(",\n",$subgroupLinks)."\n";
338                         $subgroupTitlesStr = join(",\n",$subgroupTitles)."\n";
339
340                         $groupcontent .= $this->tab("'values': [\n".$subgroupValuesStr,2);
341                         $groupcontent .= $this->tab("],\n",2);
342                         $groupcontent .= $this->tab("'valuelabels': [\n".$subgroupValueLabelsStr,2);
343                         $groupcontent .= $this->tab("],\n",2);
344                         $groupcontent .= $this->tab("'links': [\n".$subgroupLinksStr,2);
345                         $groupcontent .= $this->tab("],\n",2);
346                         $groupcontent .= $this->tab("'titles': [\n".$subgroupTitlesStr,2);
347                         $groupcontent .= $this->tab("]\n",2);
348                         $groupcontent .= $this->tab("}",1);
349                         $data[] = $groupcontent;
350                 }
351                 $content .= join(",\n",$data)."\n";
352                 $content .= $this->tab("]",1);
353                 return $content;
354         }
355
356         function buildDataBarChart($xmlstr) {
357                 $content = $this->tab("'values': [\n",1);
358                 $data = array();
359                 $xml = new SimpleXMLElement($xmlstr);
360                 $groupcontent = "";
361                 $groupcontentArr = array();
362
363                 foreach($xml->data->group as $group) {
364                 $groupcontent = $this->tab("{\n",1);
365                 $groupcontent .= $this->tab("'label': [\n",2);
366                 $groupcontent .= $this->tab("'".$this->processSpecialChars($group->title)."'\n",3);
367                 $groupcontent .= $this->tab("],\n",2);
368                 $groupcontent .= $this->tab("'values': [\n",2);
369                 $groupcontent .= $this->tab(($group->value == "NULL") ? 0 : $group->value."\n",3);
370                 $groupcontent .= $this->tab("],\n",2);
371                 if($group->label) {
372                         $groupcontent .= $this->tab("'valuelabels': [\n",2);
373                         $groupcontent .= $this->tab("'{$group->label}'\n",3);
374                         $groupcontent .= $this->tab("],\n",2);
375                 }
376                 $groupcontent .= $this->tab("'links': [\n",2);
377                 $groupcontent .= $this->tab("'{$group->link}'\n",3);
378                 $groupcontent .= $this->tab("]\n",2);
379                 $groupcontent .= $this->tab("}",1);
380                 $groupcontentArr[] = $groupcontent;
381                 }
382                 $content .= join(",\n",$groupcontentArr)."\n";
383                 $content .= $this->tab("]",1);
384                 return $content;
385         }
386
387           function buildLabelsPieChart($xmlstr) {
388                 $content = $this->tab("'label': [\n",1);
389                 $labels = array();
390                 $xml = new SimpleXMLElement($xmlstr);
391                 foreach($xml->data->group as $group) {
392                         $labels[] = $this->tab("'".$this->processSpecialChars($group->title)."'",2);
393                 }
394                 $labelStr = join(",\n",$labels)."\n";
395                 $content .= $labelStr;
396                 $content .= $this->tab("],\n",1);
397                 return $content;
398         }
399
400
401         function buildDataPieChart($xmlstr) {
402                 $content = $this->tab("'values': [\n",1);
403                 $data = array();
404                 $xml = new SimpleXMLElement($xmlstr);
405                 $groupcontent = "";
406                 $groupcontentArr = array();
407
408                 foreach($xml->data->group as $group) {
409                 $groupcontent = $this->tab("{\n",1);
410                 $groupcontent .= $this->tab("'label': [\n",2);
411                 $groupcontent .= $this->tab("'".$this->processSpecialChars($group->title)."'\n",3);
412                 $groupcontent .= $this->tab("],\n",2);
413                 $groupcontent .= $this->tab("'values': [\n",2);
414                 $groupcontent .= $this->tab("{$group->value}\n",3);
415                 $groupcontent .= $this->tab("],\n",2);
416                 $groupcontent .= $this->tab("'valuelabels': [\n",2);
417                 $groupcontent .= $this->tab("'{$group->label}'\n",3);
418                 $groupcontent .= $this->tab("],\n",2);
419                 $groupcontent .= $this->tab("'links': [\n",2);
420                 $groupcontent .= $this->tab("'{$group->link}'\n",3);
421                 $groupcontent .= $this->tab("]\n",2);
422                 $groupcontent .= $this->tab("}",1);
423                 $groupcontentArr[] = $groupcontent;
424                 }
425
426
427                 $content .= join(",\n",$groupcontentArr)."\n";
428                 $content .= $this->tab("\n]",1);
429                 return $content;
430         }
431
432         function buildLabelsGaugeChart($xmlstr) {
433                 $content = $this->tab("'label': [\n",1);
434                 $labels = array();
435                 $xml = new SimpleXMLElement($xmlstr);
436                 foreach($xml->data->group as $group) {
437                         $labels[] = $this->tab("'".$this->processSpecialChars($group->title)."'",2);
438                 }
439                 $labelStr = join(",\n",$labels)."\n";
440                 $content .= $labelStr;
441                 $content .= $this->tab("],\n",1);
442                 return $content;
443         }
444
445         function buildDataGaugeChart($xmlstr) {
446                 $content = $this->tab("'values': [\n",1);
447                 $data = array();
448                 $xml = new SimpleXMLElement($xmlstr);
449                 foreach($xml->data->group as $group) {
450                         $groupcontent = $this->tab("{\n",1);
451                         $groupcontent .= $this->tab("'label': '".$this->processSpecialChars($group->title)."',\n",2);
452                         $groupcontent .= $this->tab("'gvalue': '{$group->value}',\n",2);
453                         $finalComma = ($group->title != "GaugePosition") ? "," : "";
454                         $groupcontent .= $this->tab("'gvaluelabel': '{$group->label}'{$finalComma}\n",2);
455                         $subgroupTitles = array();
456                         $subgroupValues = array();
457                         $subgroupValueLabels = array();
458                         $subgroupLinks = array();
459
460                         if(is_object($group->subgroups->group)) {
461                                 foreach($group->subgroups->group as $subgroups) {
462                                         $subgroupTitles[] = $this->tab("'".$subgroups->title."'",3);
463                                         //$subgroupValues[] = $this->tab($subgroups->value,3);
464                                         $subgroupValues[] = $subgroups->value;
465                                         $subgroupValueLabels[] = $this->tab("'".$subgroups->label."'",3);
466                                         $subgroupLinks[] = $this->tab("'".$subgroups->link."'",3);
467                                 }
468                                 $subgroupTitlesStr = join(",\n",$subgroupTitles)."\n";
469                                 $subgroupValuesStr = join(",\n",$subgroupValues)."\n";
470                                 $subgroupValueLabelsStr = join(",\n",$subgroupValueLabels)."\n";
471                                 $subgroupLinksStr = join(",\n",$subgroupLinks)."\n";
472
473                                 //$groupcontent .= $this->tab("'labels': [\n".$subgroupTitlesStr,2);
474                                 //$groupcontent .= $this->tab("],\n",2);
475                                 $val = ((int)$subgroupValues[1] == (int)$subgroupValues[0]) ? $this->tab($subgroupValues[1],3)."\n" : $this->tab($subgroupValues[1] - $subgroupValues[0],3)."\n";
476
477                                 $groupcontent .= $this->tab("'values': [\n".$val,2);
478                                 $groupcontent .= $this->tab("],\n",2);
479                                 $groupcontent .= $this->tab("'valuelabels': [\n".$subgroupValueLabelsStr,2);
480                                 $groupcontent .= $this->tab("]\n",2);
481                                 //$groupcontent .= $this->tab("'links': [\n".$subgroupLinksStr,2);
482                                 //$groupcontent .= $this->tab("]\n",2);
483
484                         }
485
486                                 $groupcontent .= $this->tab("}",1);
487                                 $data[] = $groupcontent;
488
489                 }
490
491                 $content .= join(",\n",$data)."\n";
492
493
494                 $content .= $this->tab("]",1);
495                 return $content;
496         }
497
498
499         function getConfigProperties() {
500                 $path = SugarThemeRegistry::current()->getImageURL('sugarColors.xml',false);
501
502                 if(!file_exists($path)) {
503                         $GLOBALS['log']->debug("Cannot open file ($path)");
504                 }
505                 $xmlstr = file_get_contents($path);
506                 $xml = new SimpleXMLElement($xmlstr);
507                 return $xml->charts;
508         }
509
510         function buildChartColors() {
511
512                 $content = $this->tab("'color': [\n",1);
513                 $colorArr = array();
514                 $xml = $this->getConfigProperties();
515                 $colors = ($this->chartType == "gauge chart") ? $xml->gaugeChartElementColors->color : $xml->chartElementColors->color;
516                 foreach($colors as $color) {
517                         $colorArr[] = $this->tab("'".str_replace("0x","#",$color)."'",2);
518                 }
519                 $content .= join(",\n",$colorArr)."\n";
520                 $content .= $this->tab("],\n",1);
521
522                 return $content;
523
524         }
525
526         function buildJson($xmlstr){
527                 if($this->checkData($xmlstr)) {
528                         $content = "{\n";
529                         if ($this->chartType == "pie chart" || $this->chartType == "funnel chart 3D") {
530                                 $content .= $this->buildProperties($xmlstr);
531                                 $content .= $this->buildLabelsPieChart($xmlstr);
532                                 $content .= $this->buildChartColors();
533                                 $content .= $this->buildDataPieChart($xmlstr);
534                         }
535                         elseif ($this->chartType == "gauge chart") {
536                                 $content .= $this->buildProperties($xmlstr);
537                                 $content .= $this->buildLabelsGaugeChart($xmlstr);
538                                 $content .= $this->buildChartColors();
539                                 $content .= $this->buildDataGaugeChart($xmlstr);
540                         }
541                         elseif ($this->chartType == "horizontal bar chart" || $this->chartType == "bar chart") {
542                                 $content .= $this->buildProperties($xmlstr);
543                                 $content .= $this->buildLabelsBarChart($xmlstr);
544                                 $content .= $this->buildChartColors();
545                                 $content .= $this->buildDataBarChart($xmlstr);
546                         }
547                         elseif ($this->chartType == "group by chart") {
548                                 $content .= $this->buildProperties($xmlstr);
549                                 $content .= $this->buildLabelsBarChartStacked($xmlstr);
550                                 $content .= $this->buildChartColors();
551                                 $content .= $this->buildDataBarChartGrouped($xmlstr);
552                         } else {
553                                 $content .= $this->buildProperties($xmlstr);
554                                 $content .= $this->buildLabelsBarChartStacked($xmlstr);
555                                 $content .= $this->buildChartColors();
556                                 $content .= $this->buildDataBarChartStacked($xmlstr);
557                         }
558                         $content .= "\n}";
559                         return $content;
560                 } else {
561                         return "No Data";
562                 }
563         }
564
565         function buildHTMLLegend($xmlFile) {
566                 $xmlstr = $this->processXML($xmlFile);
567                 $xml = new SimpleXMLElement($xmlstr);
568                 $this->chartType = $xml->properties->type;
569                 $html = "<table align=\"left\" cellpadding=\"2\" cellspacing=\"2\">";
570
571                 if ($this->chartType == "group by chart" || $this->chartType == "horizontal group by chart") {
572                         $groups = $xml->data->group[0]->subgroups->group;
573                         $items = (sizeof($xml->data->group[0]->subgroups->group) <= 5) ? 5 : sizeof($xml->data->group[0]->subgroups->group);
574                 } else {
575                         $groups = $xml->data->group;
576                         $items = (sizeof($xml->data->group) <= 5) ? 5 : sizeof($xml->data->group);
577                 }
578
579                 $rows = ceil($items/5);
580                 $fullItems = $rows * 5;
581                 $remainder = ($items < $fullItems) ? $fullItems - $items : 0;
582                 $i = 0;
583                 $x = 0;
584
585
586                 $colorArr = array();
587                 $xmlColors = $this->getConfigProperties();
588                 $colors = ($this->chartType == "gauge chart") ? $xmlColors->gaugeChartElementColors->color : $xmlColors->chartElementColors->color;
589
590                 foreach($colors as $color) {
591                         $colorArr[] = str_replace("0x","#",$color);
592                 }
593
594
595                 foreach($groups as $group) {
596                         if($i == 5) {$i = 0;}
597                         $html .= ($i == 0) ? "<tr>" : "";
598                         $html .= "<td width=\"50\">";
599                         $html .= "<div style=\"background-color:".$colorArr[$x].";\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>";
600                         $html .= "</td>";
601                         $html .= "<td>";
602                         $html .= $group->title;
603                         $html .= "</td>";
604                         $html .= ($x+1 == $items) ? "<td colspan=".($remainder*2)."></td>" : "";
605                         $html .= ($i == 4) ? "</tr>" : "";
606                         $x++;
607                         $i++;
608                 }
609
610
611                 $html .= "</table>";
612                 return $html;
613         }
614
615         function saveJsonFile($jsonContents) {
616
617                 $this->jsonFilename = str_replace(".xml",".js",$this->xmlFile);
618                 //$jsonContents = mb_convert_encoding($jsonContents, 'UTF-16LE', 'UTF-8');
619
620                 // open file
621                 if (!$fh = sugar_fopen($this->jsonFilename, 'w')) {
622                         $GLOBALS['log']->debug("Cannot open file ($this->jsonFilename)");
623                         return;
624                 }
625
626                 // write the contents to the file
627                 if (fwrite($fh,$jsonContents) === FALSE) {
628                         $GLOBALS['log']->debug("Cannot write to file ($this->jsonFilename)");
629                         return false;
630                 }
631
632                 $GLOBALS['log']->debug("Success, wrote ($jsonContents) to file ($this->jsonFilename)");
633
634                 fclose($fh);
635                 return true;
636         }
637
638         function get_image_cache_file_name ($xmlFile,$ext = ".png") {
639                 $filename = str_replace("/xml/","/images/",str_replace(".xml",$ext,$xmlFile));
640
641                 return $filename;
642         }
643
644
645         function getXMLChartProperties($xmlStr) {
646                 $props = array();
647                 $xml = new SimpleXMLElement($xmlstr);
648                 foreach($xml->properties->children() as $properties) {
649                         $props[$properties->getName()] = $properties;
650                 }
651                 return $props;
652         }
653         
654         function processSpecialChars($str) {
655                 return addslashes(html_entity_decode($str,ENT_QUOTES));
656         }
657         
658         function processXML($xmlFile) {
659
660                 if(!file_exists($xmlFile)) {
661                         $GLOBALS['log']->debug("Cannot open file ($xmlFile)");
662                 }
663
664                 $pattern = array();
665                 $replacement = array();
666                 $content = file_get_contents($xmlFile);
667                 $content = mb_convert_encoding($content, 'UTF-8','UTF-16LE' );
668                 $pattern[] = '/\<link\>([a-zA-Z0-9#?&%.;\[\]\/=+_-\s]+)\<\/link\>/e';
669                 $replacement[] = "'<link>'.urlencode(\"$1\").'</link>'";
670 //              $pattern[] = '/NULL/e';
671 //              $replacement[] = "";
672                 return preg_replace($pattern,$replacement, $content);
673         }
674
675
676 }
677
678 ?>