f457c5d6ba308d6fd76d6e4d3bada5b177b16e36
[diogenes.git] / include / Plugin / Skel.php
1 <?php
2 /*
3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 // dependency on PEAR
22 require_once 'System.php';
23 require_once 'Tree/Node.php';
24
25 define('PLUG_DISABLED', 0);
26 define('PLUG_ACTIVE', 1);
27 define('PLUG_LOCK', 2);
28
29 /** Recursive stripslashes.
30 *
31 * @param $value
32 */
33 function stripslashes_recurse($value)
34 {
35 $value = is_array($value) ?
36 array_map('stripslashes_recurse', $value) :
37 stripslashes($value);
38 return $value;
39 }
40
41
42 /** This class describes a Diogenes plugin.
43 */
44 class Diogenes_Plugin_Skel {
45 /** Plugin type (object, filter) */
46 var $type = '';
47
48 /** Array of plugin parameters */
49 var $params = array();
50
51 /** Plugin name */
52 var $name = "Plugin_Skel";
53
54 /** Plugin description */
55 var $description = "Plugin skeleton";
56
57 /** Plugin version */
58 var $version = "0.1";
59
60 /** Position of the plugin */
61 var $pos = 0;
62
63 /** The plugin status (disabled, available, active) */
64 var $status = PLUG_DISABLED;
65
66 /** Is the plugin allowed with respect to a given write permission on a page ?
67 *
68 * @param $wperms
69 */
70 function allow_wperms($wperms)
71 {
72 return ($wperms != 'public');
73 }
74
75
76 /** Declare a plugin parameter.
77 */
78 function declareParam($key, $val)
79 {
80 $this->params[$key] = $val;
81 }
82
83
84 /** Return an array of parameter names.
85 */
86 function getParamNames()
87 {
88 return array_keys($this->params);
89 }
90
91
92 /** Return the value of a parameter of the plugin.
93 */
94 function getParamValue($key)
95 {
96 return isset($this->params[$key]) ? $this->params[$key] : '';
97 }
98
99
100 /** Set the value of a parameter of the plugin.
101 */
102 function setParamValue($key, $val)
103 {
104 if (isset($this->params[$key])) {
105 $this->params[$key] = $val;
106 } else {
107 }
108 }
109
110
111 /** Erase parameters from database.
112 *
113 * @param $barrel
114 * @param $page
115 */
116 function eraseParameters($barrel = '', $page = 0)
117 {
118 global $globals;
119 //echo $this->name . " : eraseParams($barrel, $page)<br/>\n";
120 $globals->db->query("delete from diogenes_plugin where plugin='{$this->name}' and barrel='$barrel' and page='$page'");
121
122 unset($this->pos);
123 foreach ($this->getParamNames() as $key)
124 {
125 //echo "$this->name : erasing param<br/>\n";
126 $this->setParamValue($key, '');
127 }
128 }
129
130
131 /** Read parameters from an array.
132 */
133 function fromArray($plugentry)
134 {
135 $this->pos = $plugentry['pos'];
136 $this->status = $plugentry['status'];
137 foreach ($plugentry['params'] as $key => $val)
138 {
139 $this->setParamValue($key, $val);
140 }
141 }
142
143
144 /** Store parameters to database.
145 *
146 * @param $barrel
147 * @param $page
148 * @param $pos
149 */
150 function toDatabase($barrel = '', $page = 0, $pos = 0)
151 {
152 global $globals;
153
154 $this->pos = $pos;
155 $params = var_encode_bin($this->params);
156 //echo "toDatabase called for '{$this->name}' in barrel '$barrel' (status : {$this->status}, params : '$params')<br/>";
157 $globals->db->query("replace into diogenes_plugin set plugin='{$this->name}', status='{$this->status}', barrel='$barrel', page='$page', pos='$pos', params='$params'");
158 }
159
160
161 /** Dump parameters to a table.
162 */
163 function toArray()
164 {
165 $plugentr = array();
166
167 // copy over properties
168 $props = array('status', 'name', 'params', 'description', 'version', 'type', 'pos');
169 foreach ($props as $prop)
170 {
171 $plugentr[$prop] = stripslashes_recurse($this->$prop);
172 }
173 return $plugentr;
174 }
175
176 }
177
178 ?>