1<?php
2/**
3 * Code reused from graphviz-Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Carl-Christian Salvesen <calle@ioslo.net>
7 * @author     Andreas Gohr <andi@splitbrain.org>
8 *
9 * reused by Nuno Flores (nuno.flores@gmail.com) for the directions plug-in
10 */
11
12/*
13* generates graphviz image.
14*
15* Is uses google remote api (graphviz) to do so.
16*
17* IT REQUIRES GRAPHVIZ PLUG-IN.
18*
19* Note: graphviz plug-in has a option for using a local graphviz instalation.
20* Please refer to it for more details.
21*
22*/
23
24function dir_generateGraph($info, $data) {
25	// generate .dot text
26
27	$data = dir_parseDataIntoDOT($data);
28
29	// generate image file
30	    // remotely via google
31
32	    $input = dir_prepareGVInput($info, $data, $metadata);
33
34	    // FIXME : add using local graphview instalation (look at graphview plugin)
35
36		// returning xhtml code to image.
37	    $img = DOKU_BASE.'lib/plugins/graphviz/img.php?'.buildURLparams($input);
38	  	$ret .= '<img src="'.$img.'" class="media'.$input['align'].'" alt=""';
39        if($input['width'])  $ret .= ' width="'.$input['width'].'"';
40        if($input['height']) $ret .= ' height="'.$input['height'].'"';
41        if($input['align'] == 'right') $ret .= ' align="right"';
42        if($input['align'] == 'left')  $ret .= ' align="left"';
43        $ret .= '/>';
44
45		return $ret;
46}
47
48/*
49* gets cache filename
50*/
51function dir_cachename($data,$ext){
52    unset($data['width']);
53    unset($data['height']);
54    unset($data['align']);
55    return getcachename(join('x',array_values($data)),'.graphviz.'.$ext);
56}
57
58/*
59* stores dotcode into a cache file and returns
60* meta-data to generate graphviz image later
61*/
62function dir_prepareGVInput($info, $dotcode) {
63
64	$version = date('Y-m-d');
65	if(isset($info['date']))
66		$version = $info['date'];
67
68    // prepare default data
69    $return = array (
70                    'width'     => 600,
71                    'height'    => 300,
72                    'layout'    => 'dot',
73                    'align'     => '',
74                    'version'   => $version,  //force rebuild of images on update
75                   );
76
77    $return['md5'] = md5($dotcode); // we only pass a hash around
78
79    // store input for later use
80	$file = dir_cachename($return,'txt');
81    io_saveFile($file,$dotcode);
82
83    return $return;
84}
85
86/*
87* parses list of jumps into DOT syntax to feed graphviz
88*/
89function dir_parseDataIntoDOT($data) {
90
91	global $conf;
92	$trimLimit = $conf['plugin']['directions']['trim_limit'];
93
94	$out = 'digraph finite_state_machine {';
95	$out .= 'rankdir=LR;'.'size="8,5";';
96
97	foreach ($data as $key=>$value) {
98		$pages = explode("->", $key);
99		$page1 = str_replace('/',':',$pages[0]);
100		$page2 = str_replace('/',':',$pages[1]);
101
102		$penwidth =  $value / 10;
103
104	    $out .= '"'.trim(dir_trimPageTitle(dir_get_first_heading($page1),$trimLimit)).'" -> "';
105	    $out .= trim(dir_trimPageTitle(dir_get_first_heading($page2),$trimLimit)).'" [ label = "'.$value.'" penwidth = '.$penwidth.', weight = 2];';
106	}
107
108	$out .= '}';
109
110	return $out;
111}
112
113?>