/[drupal]/contributions/modules/swfcharts/swfcharts.module
ViewVC logotype

Contents of /contributions/modules/swfcharts/swfcharts.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.8 - (show annotations) (download) (as text)
Sat Oct 13 18:57:13 2007 UTC (2 years, 1 month ago) by hlslaughter
Branch: MAIN
CVS Tags: DRUPAL-5--1-2, HEAD
Changes since 1.7: +5 -5 lines
File MIME type: text/x-php
fix yet another problem with actual base_url, hopefully 'global $base_url' will always contain the correct version, regardless of alt ports, subdirs, etc.
1 <?php
2 // $Id: swfcharts.module,v 1.7 2007/10/13 18:15:45 hlslaughter Exp $
3
4 /**
5 * @file
6 * Creates a wrapper around the XML/SWF Charts program. Allows an API for
7 * generation of graphs and charts as well as a filter for use in a node
8 * context.
9 *
10 * For general XML/SWF Charts info, see:
11 * http://www.maani.us/xml_charts/
12 *
13 * For details on the XML/SWF Charts API, see:
14 * http://www.maani.us/xml_charts/index.php?menu=Reference
15 *
16 */
17
18 /**
19 * Implementation of hook_help().
20 */
21 function swfcharts_help($section) {
22 switch ($section) {
23 case 'admin/help#swfcharts':
24 return t('');
25 break;
26 case 'admin/modules#description':
27 return t('API for generating attractive flash charts and graphs.');
28 break;
29 case 'admin/settings/swfcharts':
30 return t('These are default settings for the "frame" which will be placed around the chart.');
31 break;
32 }
33 }
34
35 /**
36 * Implementation of hook_perm().
37 */
38 function swfcharts_perm() {
39 return array('administer swfcharts');
40 }
41
42 /**
43 * Implementation of hook_menu().
44 */
45 function swfcharts_menu($may_cache) {
46 $items = array();
47
48 if ($may_cache) {
49 $items[] = array(
50 'path' => 'admin/settings/swfcharts',
51 'title' => t('SWF Charts'),
52 'description' => t('Configure default settings for SWF Charts.'),
53 'callback' => 'drupal_get_form',
54 'callback arguments' => array('swfcharts_admin_settings'),
55 'access' => user_access('administer swfcharts'),
56 'type' => MENU_NORMAL_ITEM
57 );
58 $items[] = array(
59 'path' => 'swfcharts/generate_chart',
60 'description' => t('Configure SWF Charts.'),
61 'callback' => 'swfcharts_chart',
62 'callback arguments' => array(),
63 'access' => TRUE,
64 'type' => MENU_CALLBACK
65 );
66 }
67 return $items;
68 }
69
70 /**
71 * Implementation of hook_cron().
72 */
73 function swfcharts_cron() {
74 // Note: if files are created in this dir by a non-apache user, they cannot
75 // be removed by cron and will result in warning messages being generated
76 // when cron is run. Do not put files in this dir.
77 $files_dir = variable_get('swfcharts_xmldir', '');
78 $files_purge = variable_get('swfcharts_files_purge', 120);
79
80 if (!empty($files_dir) && $files_purge) {
81 $d = dir($files_dir);
82 while (FALSE !== ($filename = $d->read())) {
83 $file = $files_dir . '/' . $filename;
84 if (is_file($file)) {
85 $atime = fileatime($file);
86 if ( $atime < time() - (60 * $files_purge)) {
87 unlink($file);
88 }
89 }
90 }
91 $d->close();
92 }
93 }
94
95 /**
96 * Description
97 *
98 * @param (array)$data
99 * Contains the attributes and data of the actual chart. This will be converted
100 * to XML. For a complete reference of possible values, see:
101 * http://www.maani.us/charts/index.php?menu=Reference
102 *
103 * @param optional (array)$config
104 * Named parameters for the chart wrapper. Possible values are as follows:
105 *
106 * $config[height] - Height of background wrapper in pixels
107 * $config[width] - Width of background wrapper in pixels
108 * $config[bgcolor] - Background color (eg #FFFFFF)
109 * $config[quality] - Flash quality. One of: low, medium, high
110 * $config[license] - XML/SWF Charts license
111 *
112 * @return (string)
113 * A themed chart
114 */
115 function swfcharts_chart($data, $config = array()) {
116 global $base_url;
117
118 $charts_rootdir = _swfcharts_charts_rootdir();
119
120 if (!$charts_rootdir) {
121 drupal_set_message("Unable to generate Chart", 'error');
122 watchdog('swfcharts', t('The SWF Charts API does not appear to be installed properly. Please check the ') . l(t('SWF Charts configuration'), 'admin/settings/swfcharts'), WATCHDOG_ERROR);
123 return FALSE;
124 }
125
126 $xml_filename = swfcharts_generate_xml_file($data);
127 if (!$xml_filename) {
128 watchdog('swfcharts', 'Unable to create XML output file, cannot generate SWF Chart.', WATCHDOG_ERROR);
129 return FALSE;
130 }
131
132 $base_url = $base_url . '/';
133 $charts_swf_file = $base_url . $charts_rootdir . '/charts.swf';
134 $charts_library_path = $base_url . $charts_rootdir . '/charts_library';
135 $height = $config['height'] ? $config['height'] :
136 variable_get('swfcharts_default_height', 300);
137 $width = $config['width'] ? $config['width'] :
138 variable_get('swfcharts_default_width', 400);
139 $bgcolor = $config['bgcolor'] ? $config['bgcolor'] :
140 variable_get('swfcharts_default_background_color', '#FFFFFF');
141 $quality = $config['quality'] ? $config['quality'] :
142 variable_get('swfcharts_default_quality','high');
143 $license = $config['license'] ? $config['license'] :
144 variable_get('swfcharts_api_key', NULL);
145
146 $chart_source_string = $charts_swf_file . '?library_path=' . $charts_library_path
147 . '&xml_source=' . $xml_filename;
148 if ($license != NULL) {
149 $chart_source_string .= "&license=" . $license;
150 }
151
152 $output .= '
153 <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
154 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
155 width="'. $width . '"
156 height="'. $height . '"
157 id="charts"
158 >
159 <param name=movie value="' . $chart_source_string . '">
160 <param name=quality value='. $width . '>
161 <param name=bgcolor value='. $bgcolor . '>
162 <embed src="' . $chart_source_string . '"
163 quality=' . $quality . '
164 bgcolor='. $bgcolor . '
165 width="'. $width . '"
166 height="'. $height . '"
167 name="charts"
168 align=""
169 swliveconnect="false"
170 type="application/x-shockwave-flash"
171 pluginspage="http://www.macromedia.com/go/getflashplayer">
172 </embed>
173 </object>
174 ';
175 return theme('swf_chart', $output);
176 }
177
178 /**
179 * Description
180 *
181 * @param (string)$chart
182 * Contains all the HTML required for swfcharts to generate an image.
183 *
184 * @return (string)
185 * themed chart
186 */
187 function theme_swf_chart($chart) {
188 //$output = '<h2>Source</h2> <pre>' . htmlentities($chart) . '<pre>';
189 $output .= '<center>' . "\n" . '<div class="swf_chart">' . $chart . '</div><!-- end swf_chart--></center>' . "\n";
190 return $output;
191 }
192
193 /**
194 * Virtual hook 'admin_settings'
195 */
196 function swfcharts_admin_settings() {
197 // General
198 $form['config'] = array(
199 '#type' => 'fieldset',
200 '#title' => t('SWF Charts configuration'),
201 '#collapsible' => TRUE,
202 '#description' => t('Configuration of XML/SWF Charts library')
203 );
204 $form['config']['swfcharts_rootdir'] = array(
205 '#type' => 'textfield',
206 '#size' => 64,
207 '#title' => t('Path to installation directory'),
208 '#description' => t('The location of the directory where !download_swfcharts
209 is installed. (i.e <b>!swfcharts_rootdir</b>).',
210 array (
211 '!swfcharts_rootdir' => drupal_get_path('module', 'swfcharts') . '/charts',
212 '!download_swfcharts' => l(t('XML/SWF Charts'),
213 'http://www.maani.us/xml_charts/index.php?menu=Download'),
214 )
215 ),
216 '#default_value' => variable_get('swfcharts_rootdir', ''),
217
218
219 );
220 $form['config']['swfcharts_api_key'] = array(
221 '#type' => 'textfield',
222 '#size' => 32,
223 '#title' => t('XML/SWF API Key'),
224 '#description' => t('Optional API Key obtained from ') .
225 l('Mannie.us', 'http://www.maani.us/') . '.',
226 '#default_value' => variable_get('swfcharts_api_key', ''),
227 );
228 $form['general'] = array(
229 '#type' => 'fieldset',
230 '#title' => t('General settings'),
231 '#collapsible' => TRUE,
232 );
233 $form['general']['swfcharts_files_purge'] = array(
234 '#type' => 'textfield',
235 '#size' => 8,
236 '#title' => t('XML cache expire'),
237 '#description' => t('The XML data used to generate charts is cached on the
238 filesystem (files/swfcharts). Files which have not been accessed in this number of
239 minutes will be removed. You may set this to a very large number if your
240 charts are mostly based on static data or if you do not mind a lot of files
241 being stored. Set to 0 to turn off, but be aware of your file system\'s
242 limitations.'),
243 '#default_value' => variable_get('swfcharts_files_purge', '120'),
244 );
245 $form['general']['swfcharts_default_height'] = array(
246 '#type' => 'textfield',
247 '#size' => 5,
248 '#title' => t('Default height'),
249 '#description' => t('Unless specified at runtime, this will be the height
250 of all charts generated.'),
251 '#default_value' => variable_get('swfcharts_default_height', 300),
252 );
253 $form['general']['swfcharts_default_width'] = array(
254 '#type' => 'textfield',
255 '#size' => 5,
256 '#title' => t('Default width'),
257 '#description' => t('Unless specified at runtime, this will be the width
258 of all charts generated.'),
259 '#default_value' => variable_get('swfcharts_default_width', 400),
260 );
261 $form['general']['swfcharts_default_background_color'] = array(
262 '#type' => 'textfield',
263 '#size' => 8,
264 '#title' => t('Background color'),
265 '#description' => t('Color used in the background that will frame the chart. For example, "#FFFFFF"'),
266 '#default_value' => variable_get('swfcharts_default_background_color', '#FFFFFF'),
267 );
268 $form['general']['swfcharts_default_quality'] = array(
269 '#type' => 'select',
270 '#title' => t('SWF Quality'),
271 '#description' => t('SWF default quality.'),
272 '#default_value' => variable_get('swfcharts_default_quality','high'),
273 '#options' => array(
274 'low' => t('Low'),
275 'medium' => t('Medium'),
276 'high' => t('High'),
277 ),
278 );
279 return system_settings_form($form);
280 }
281
282 /**
283 * Description
284 *
285 * @param (array)$data
286 * Contains one or more rows of data that will be converted to XML
287 *
288 * @return (string)
289 * The full (site relative) URL to the temporary file used to store the XML
290 */
291 function swfcharts_generate_xml_file($data) {
292 global $base_url;
293
294 $xmldir = _swfcharts_xmldir();
295 if ($xmldir) {
296 // create a distinct filename corresponding to the data structure so we
297 // can reuse xml files
298 $xml_filename = md5(print_r($data, TRUE)) . '.xml';
299 $xml_filepath = file_create_path($xmldir . '/' . $xml_filename);
300 $xml_fileurl = $base_url . '/' . $xmldir . '/' . $xml_filename;
301
302 if (file_exists($xml_filepath)) {
303 return $xml_fileurl;
304 }
305 else {
306 $xml = _array2chartsXml($data);
307
308 if (file_save_data($xml, $xml_filepath, FILE_EXISTS_ERROR)) {
309 return $xml_fileurl;
310 }
311 else {
312 return FALSE;
313 }
314 }
315 }
316 else {
317 return FALSE;
318 }
319 }
320
321 /**
322 * Determine whether swf charts API is installed properly, if so, return
323 * path to API file
324 */
325 function _swfcharts_charts_rootdir() {
326 $installed = variable_get('swfcharts_api_installed', FALSE);
327 $swfcharts_rootdir = variable_get('swfcharts_rootdir', '');
328
329 // for now, our only criteria is that a file named charts.swf exists
330 if (!$installed) {
331 if (file_exists($swfcharts_rootdir . '/charts.swf') &&
332 file_exists($swfcharts_rootdir . '/charts_library')) {
333 variable_set('swfcharts_api_installed', TRUE);
334 $installed = TRUE;
335 }
336 else {
337 return FALSE;
338 }
339 }
340 return $swfcharts_rootdir;
341 }
342
343 /**
344 * Determine the location where tmp XML files will be stored, create dir
345 * if not exists, report problems.
346 */
347 function _swfcharts_xmldir() {
348 $swfcharts_xmldir = variable_get('swfcharts_xmldir', FALSE);
349 if (empty($swfcharts_xmldir)) {
350 $swfcharts_xmldir = file_directory_path() . '/swfcharts';
351 $dir_created = mkdir($swfcharts_xmldir, 0777);
352 if ($dir_created) {
353 variable_set('swfcharts_xmldir', $swfcharts_xmldir);
354 drupal_set_message(t('Created ' . $swfcharts_xmldir));
355 }
356 else {
357 watchdog('swfcharts', 'Unable to create directory for XML files. SWF Charts will not function properly', WATCHDOG_ERROR);
358 return FALSE;
359 }
360 }
361 return $swfcharts_xmldir;
362 }
363
364 /**
365 * Test the whole process
366 */
367 function _swfcharts_test_chart() {
368
369 $chart['chart_type'] = "bar";
370 $chart [ 'chart_border' ] = array (
371 'top_thickness' => 0,
372 'bottom_thickness' => 4,
373 'left_thickness' => 4,
374 'right_thickness' => 4
375 );
376 $chart['chart_data'] = array (
377 array ( "", "2001", "2002", "2003", "2004" ),
378 array ( "Region A", 5, 10, 30, 63 ),
379 array ( "Region B", 100, 20, 65, 55 ),
380 array ( "Region C", 56, 21, 5, 90 )
381 );
382
383 return swfcharts_chart($chart);
384 }
385
386 /**
387 * This code is copied directly from charts.php with one change. Instead of printing
388 * the XML output, we're returning it so it can be saved to a file
389 */
390 function _array2chartsXml($chart=array()) {
391
392 $xml="<chart>\r\n";
393 $Keys1= array_keys((array) $chart);
394 for ($i1=0;$i1<count($Keys1);$i1++){
395 if(is_array($chart[$Keys1[$i1]])){
396 $Keys2=array_keys($chart[$Keys1[$i1]]);
397 if(is_array($chart[$Keys1[$i1]][$Keys2[0]])){
398 $xml.="\t<".$Keys1[$i1].">\r\n";
399 for($i2=0;$i2<count($Keys2);$i2++){
400 $Keys3=array_keys((array) $chart[$Keys1[$i1]][$Keys2[$i2]]);
401 switch($Keys1[$i1]){
402 case "chart_data":
403 $xml.="\t\t<row>\r\n";
404 for($i3=0;$i3<count($Keys3);$i3++){
405 switch(true){
406 case ($chart[$Keys1[$i1]][$Keys2[$i2]][$Keys3[$i3]]===null):
407 $xml.="\t\t\t<null/>\r\n";
408 break;
409
410 case ($Keys2[$i2]>0 and $Keys3[$i3]>0):
411 $xml.="\t\t\t<number>".$chart[$Keys1[$i1]][$Keys2[$i2]][$Keys3[$i3]]."</number>\r\n";
412 break;
413
414 default:
415 $xml.="\t\t\t<string>".$chart[$Keys1[$i1]][$Keys2[$i2]][$Keys3[$i3]]."</string>\r\n";
416 break;
417 }
418 }
419 $xml.="\t\t</row>\r\n";
420 break;
421
422 case "chart_value_text":
423 $xml.="\t\t<row>\r\n";
424 $count=0;
425 for($i3=0;$i3<count($Keys3);$i3++){
426 if($chart[$Keys1[$i1]][$Keys2[$i2]][$Keys3[$i3]]===null){$xml.="\t\t\t<null/>\r\n";}
427 else{$xml.="\t\t\t<string>".$chart[$Keys1[$i1]][$Keys2[$i2]][$Keys3[$i3]]."</string>\r\n";}
428 }
429 $xml.="\t\t</row>\r\n";
430 break;
431
432 /*case "link_data_text":
433 $xml.="\t\t<row>\r\n";
434 $count=0;
435 for($i3=0;$i3<count($Keys3);$i3++){
436 if($chart[$Keys1[$i1]][$Keys2[$i2]][$Keys3[$i3]]===null){$xml.="\t\t\t<null/>\r\n";}
437 else{$xml.="\t\t\t<string>".$chart[$Keys1[$i1]][$Keys2[$i2]][$Keys3[$i3]]."</string>\r\n";}
438 }
439 $xml.="\t\t</row>\r\n";
440 break;*/
441
442 case "draw":
443 $text="";
444 $xml.="\t\t<".$chart[$Keys1[$i1]][$Keys2[$i2]]['type'];
445 for($i3=0;$i3<count($Keys3);$i3++){
446 if($Keys3[$i3]!="type"){
447 if($Keys3[$i3]=="text"){$text=$chart[$Keys1[$i1]][$Keys2[$i2]][$Keys3[$i3]];}
448 else{$xml.=" ".$Keys3[$i3]."=\"".$chart[$Keys1[$i1]][$Keys2[$i2]][$Keys3[$i3]]."\"";}
449 }
450 }
451 if($text!=""){$xml.=">".$text."</text>\r\n";}
452 else{$xml.=" />\r\n";}
453 break;
454
455
456 default://link, etc.
457 $xml.="\t\t<value";
458 for($i3=0;$i3<count($Keys3);$i3++){
459 $xml.=" ".$Keys3[$i3]."=\"".$chart[$Keys1[$i1]][$Keys2[$i2]][$Keys3[$i3]]."\"";
460 }
461 $xml.=" />\r\n";
462 break;
463 }
464 }
465 $xml.="\t</".$Keys1[$i1].">\r\n";
466 }else{
467 if($Keys1[$i1]=="chart_type" or $Keys1[$i1]=="series_color" or $Keys1[$i1]=="series_image" or $Keys1[$i1]=="series_explode" or $Keys1[$i1]=="axis_value_text"){
468 $xml.="\t<".$Keys1[$i1].">\r\n";
469 for($i2=0;$i2<count($Keys2);$i2++){
470 if($chart[$Keys1[$i1]][$Keys2[$i2]]===null){$xml.="\t\t<null/>\r\n";}
471 else{$xml.="\t\t<value>".$chart[$Keys1[$i1]][$Keys2[$i2]]."</value>\r\n";}
472 }
473 $xml.="\t</".$Keys1[$i1].">\r\n";
474 }else{//axis_category, etc.
475 $xml.="\t<".$Keys1[$i1];
476 for($i2=0;$i2<count($Keys2);$i2++){
477 $xml.=" ".$Keys2[$i2]."=\"".$chart[$Keys1[$i1]][$Keys2[$i2]]."\"";
478 }
479 $xml.=" />\r\n";
480 }
481 }
482 }else{//chart type, etc.
483 $xml.="\t<".$Keys1[$i1].">".$chart[$Keys1[$i1]]."</".$Keys1[$i1].">\r\n";
484 }
485 }
486 $xml.="</chart>\r\n";
487 return $xml;
488 }

  ViewVC Help
Powered by ViewVC 1.1.2