rework plugin handling to access parameters via declareParam, getParamValues, setPara...
[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
24 /** Recursive stripslashes.
25 *
26 * @param $value
27 */
28 function stripslashes_recurse($value)
29 {
30 $value = is_array($value) ?
31 array_map('stripslashes_recurse', $value) :
32 stripslashes($value);
33 return $value;
34 }
35
36
37 /** This class describes a Diogenes plugin.
38 */
39 class Diogenes_Plugin_Skel {
40 /** Plugin type (object, filter) */
41 var $type;
42
43 /** Array of plugin parameters */
44 var $params = array();
45
46 /** Plugin name */
47 var $name = "Plugin_Skel";
48
49 /** Plugin description */
50 var $description = "Plugin skeleton";
51
52 /** Plugin version */
53 var $version = "0.1";
54
55 /** Position of the plugin */
56 var $pos;
57
58 /** Is the plugin active ? */
59 var $active = 0;
60
61
62 /** Is the plugin allowed with respect to a given write permission on a page ?
63 *
64 * @param $wperms
65 */
66 function allow_wperms($wperms)
67 {
68 return ($wperms != 'public');
69 }
70
71
72 /** Declare a plugin parameter.
73 */
74 function declareParam($key, $val)
75 {
76 $this->params[$key] = $val;
77 }
78
79
80 /** Return an array of parameter names.
81 */
82 function getParamNames()
83 {
84 return array_keys($this->params);
85 }
86
87
88 /** Return the value of a parameter of the plugin.
89 */
90 function getParamValue($key)
91 {
92 return isset($this->params[$key]) ? $this->params[$key] : '';
93 }
94
95
96 /** Set the value of a parameter of the plugin.
97 */
98 function setParamValue($key, $val)
99 {
100 if (isset($this->params[$key])) {
101 //echo "$this->name : Calling setParamValue($key, $val)<br/>\n";
102 $this->params[$key] = $val;
103 } else {
104 //echo "$this->name : skipping setParamValue($key, $val)<br/>\n";
105 }
106 }
107
108
109 /** Set plugin parameters.
110 *
111 * @param $params
112 */
113 function setParams($params)
114 {
115 $bits = explode("\0", $params);
116 foreach ($bits as $bit)
117 {
118 $frags = explode("=", $bit, 2);
119 $key = $frags[0];
120 if (!empty($key))
121 {
122 $val = isset($frags[1]) ? $frags[1] : '';
123 $this->setParamValue($key, $val);
124 }
125 }
126 }
127
128
129 /** Erase parameters from database.
130 *
131 * @param $barrel
132 * @param $page
133 */
134 function eraseParams($barrel = '', $page = 0)
135 {
136 global $globals;
137
138 //echo $this->name . " : eraseParams($barrel, $page)<br/>\n";
139 $globals->db->query("delete from diogenes_plugin where plugin='{$this->name}' and barrel='$barrel' and page='$page'");
140
141 $this->active = 0;
142 unset($this->pos);
143 foreach ($this->getParamNames() as $key)
144 {
145 //echo "$this->name : erasing param<br/>\n";
146 $this->setParamValue($key, '');
147 }
148 }
149
150
151 /** Store parameters to database.
152 *
153 * @param $barrel
154 * @param $page
155 * @param $pos
156 */
157 function writeParams($barrel = '', $page = 0, $pos = 0)
158 {
159 global $globals;
160
161 $this->pos = $pos;
162 $this->active = 1;
163
164 $params = '';
165 foreach ($this->getParamNames() as $key)
166 {
167 $val = $this->getParamValue($key);
168 //echo "$this->name : $key = $val<br/>\n";
169 $params .= "$key=$val\0";
170 }
171 $globals->db->query("replace into diogenes_plugin set plugin='{$this->name}', barrel='$barrel', page='$page', pos='$pos', params='$params'");
172 }
173
174
175 /** Dump parameters to a table.
176 */
177 function dump()
178 {
179 $plugentr = array();
180
181 // copy over properties
182 $props = array('active', 'name', 'params', 'description', 'version', 'type', 'pos');
183 foreach ($props as $prop)
184 {
185 if (isset($this->$prop))
186 {
187 $plugentr[$prop] = stripslashes_recurse($this->$prop);
188 }
189 }
190 return $plugentr;
191 }
192
193 }
194
195 ?>