summaryrefslogtreecommitdiff
path: root/plugins/data.graphviz.php
blob: e9db497b468ccf3f86098732c82ec35c8bb6dbaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

namespace Bitweaver\Liberty;

use Bitweaver\KernelTools;

/**
 * @version  $Revision$
 * @package  liberty
 * @subpackage plugins_data
 */
// +----------------------------------------------------------------------+
// | Copyright (c) 2004, bitweaver.org
// +----------------------------------------------------------------------+
// | All Rights Reserved. See below for details and a complete list of authors.
// | Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
// |
// | For comments, please use phpdocu.sourceforge.net documentation standards!!!
// | -> see http://phpdocu.sourceforge.net/
// +----------------------------------------------------------------------+
// | Author: StarRider starrrider@sourceforge.net
// +----------------------------------------------------------------------+
// $id: data.example.php,v 1.4.2.9 2005/07/14 09:03:36 starrider Exp $

/******************
 * Initialization *
 ******************/

define( 'PLUGIN_GUID_DATAGRAPHVIZ', 'graphviz' );
global $gLibertySystem;
$pluginParams = [
	'tag' => 'GRAPHVIZ',
	'auto_activate' => false,
	'requires_pair' => true,
	'load_function' => '\data_graphviz',
	'title' => 'GraphViz',
	'help_page' => 'DataPluginExample',
	'description' => KernelTools::tra("This plugin renders it's content as a graphviz image (dot or neato). It requies the Image_GraphViz pear plugin and graphviz to be installed: pear install Image_GraphViz"),
	'help_function' => '\data_graphviz_help',
	'syntax' => "{GRAPHVIZ}digraph  ... {/GRAPHVIZ}",
	'plugin_type' => DATA_PLUGIN,
];
$gLibertySystem->registerPlugin( PLUGIN_GUID_DATAGRAPHVIZ, $pluginParams );
$gLibertySystem->registerDataTag( $pluginParams['tag'], PLUGIN_GUID_DATAGRAPHVIZ );
/*****************
 * Help Function *
 *****************/
function data_graphviz_help() {
	$help =
		'<table class="data help">'
			.'<tr>'
				.'<th>' . KernelTools::tra( "Key" ) . '</th>'
				.'<th>' . KernelTools::tra( "Type" ) . '</th>'
				.'<th>' . KernelTools::tra( "Comments" ) . '</th>'
			.'</tr>'
			.'<tr class="odd">'
				.'<td>x1</td>'
				.'<td>' . KernelTools::tra( "string") . '<br />' . KernelTools::tra("(optional)") . '</td>'
				.'<td>' . KernelTools::tra( "Specifies something / probably to be displayed.")
					.'<br />' . KernelTools::tra( "The Default = <strong>Sorry About That</strong>")
				.'</td>'
			.'</tr>'
			.'<tr class="even">'
				.'<td>XXX</td>'
				.'<td>' . KernelTools::tra( "number") . '<br />' . KernelTools::tra("(optional)") . '</td>'
				.'<td>' . KernelTools::tra( "Specifies something / probably to be displayed.")
					.'<br />' . KernelTools::tra( "The Default =") . ' <strong>3</strong> ' . KernelTools::tra( "Which means - What")
				.'</td>'
			.'</tr>'
		.'</table>'
		. KernelTools::tra("Example: ") . "{EXAM x1=' ' x2=5 }<br />"
		. KernelTools::tra("This will display");
	return $help;
}
/****************
* Load Function *
 ****************/
function data_graphviz($data, $params) {
	$data = trim($data);
	$data = html_entity_decode( $data );
	$storageurl = STORAGE_PKG_URL.'GraphViz/';
	$storagepath = STORAGE_PKG_PATH.'GraphViz/';
	$temppath = TEMP_PKG_PATH.'GraphViz/';

	if( !is_dir( $temppath ) ) {
		KernelTools::mkdir_p( $temppath );
	}
	if( !is_dir( $storagepath ) ) {
		KernelTools::mkdir_p( $storagepath );
	}

	$file = md5( $data );
	$dotFile = $temppath . $file . '.dot';
	$pngFile = $storagepath . $file . '.png';
	$pngURL = $storageurl . $file . '.png';

	if( !file_exists( $pngFile ) ) {
		if( @include_once 'PEAR.php' ) {
			if(@include_once UTIL_PKG_INCLUDE_PATH.'pear/Image/GraphViz.php' ) {
				$graph = new \Image_GraphViz;
				$error = '<div class=error>'.KernelTools::tra("Unable to write temporary file. Please check your server configuration.").'</div>';
				if (!$fp = fopen($dotFile, 'w')) {
					return $error;
				}
				if (fwrite($fp, $data)===false) {
					return $error;
				}
				fclose($fp);
				$graph->renderDotFile( $dotFile, $pngFile, 'png' );
				// No need to keep this lying around
				unlink($dotFile);

				// If it still isn't there....
				if (!file_exists($pngFile)) {
					return '<div class=error>'.KernelTools::tra("Unable to generate graphviz image file. Please check your data.").'</div>';
				}
			}
			else {
				return "<div class=error>".KernelTools::tra("The Image_Graphviz pear plugin is not installed. Install with `pear install Image_Graphviz`.")."</div>";
			}
		}
		else {
			return "<div class=error>".KernelTools::tra("PEAR and the Image_Graphviz pear plugin are not installed.")."</div>";
		}
	}
	return "<img src=\"$pngURL\"/> ";

}