9de3b2f1c2a75b45ade74d2c0b1b1cc358899fd6
[diogenes.git] / Editor.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
25 /** This class provides a configuration tool for Diogenes plugins.
26 */
27 class Diogenes_Plugin_Editor {
28 /** Should we show plugins parameters? */
29 var $show_params = 1;
30
31 /** Should editing functions be disabled? */
32 var $readonly = false;
33
34 /** The alias of the barrel we are working on. */
35 var $plug_barrel;
36
37 /** The PID of the page we are working on. */
38 var $plug_page;
39
40 /** The write permissions of the page we are working on. */
41 var $plug_page_wperms;
42
43 /** Construct a new plugin editor.
44 *
45 * @param $plug_barrel
46 * @param $plug_page
47 * @param $plug_page_wperms
48 */
49 function Diogenes_Plugin_Editor($plug_barrel, $plug_page, $plug_page_wperms = '')
50 {
51 $this->plug_barrel = $plug_barrel;
52 $this->plug_page = $plug_page;
53 $this->plug_page_wperms = $plug_page_wperms;
54 }
55
56
57 /** Run the plugin editor.
58 *
59 * @param $page
60 * @param $outputvar
61 */
62 function run(&$page, $outputvar = '')
63 {
64 global $globals;
65
66 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
67 $target = isset($_REQUEST['plug_target']) ? $_REQUEST['plug_target'] : '';
68
69 // load all available plugins
70 $cachefile = $globals->plugins->cacheFile($this->plug_barrel);
71
72 // if the tree cache does not exits, try to init it
73 if (!file_exists($cachefile)) {
74 $globals->plugins->compileCache($cachefile, $this->plug_barrel);
75 }
76 $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);
77 $available = $globals->plugins->cachedAvailable($cache, $this->plug_barrel, $this->plug_page);
78
79
80 // handle updates
81 switch ($action) {
82 case "move_up": case "move_down":
83 if ($this->readonly) die("Sorry, this plugin view is read-only.");
84
85 $delta = ($action == "move_down") ? 1 : -1;
86 //$page->info("moving plugin '$target'..");
87 $plugcache_a = $globals->plugins->cacheGet($cache, $this->plug_barrel, $this->plug_page, $target);
88 $plug_a =& $globals->plugins->load($plugcache_a);
89 //$plug_a =& $globals->plugins->get($target);
90
91 if (is_object($plug_a) && ($plug_a->active)) {
92 $old_pos = $plug_a->pos;
93 //$plug_b =& $globals->plugins->getAtPos($old_pos + $delta);
94 $plugcache_b = $globals->plugins->cacheGetAtPos($cache, $this->plug_barrel, $this->plug_page, $old_pos + $delta);
95
96 if (is_array($plugcache_b))
97 {
98 $plug_b =& $globals->plugins->load($plugcache_b);
99
100 // swap the current plugin and the next plugin
101 if (is_object($plug_b) && ($plug_b->active))
102 {
103 $plug_a->writeParams($this->plug_barrel, $this->plug_page, $old_pos + $delta);
104 $plug_b->writeParams($this->plug_barrel, $this->plug_page, $old_pos);
105 }
106 }
107 }
108 $globals->plugins->compileCache($cachefile, $this->plug_barrel);
109 $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);
110 $available = $globals->plugins->cachedAvailable($cache, $this->plug_barrel, $this->plug_page);
111 break;
112
113 case "update":
114 if ($this->readonly) die("Sorry, this plugin view is read-only.");
115
116 // list of active plugins
117 $active = array();
118 if (isset($_REQUEST['plugins_active'])) {
119 $active = array_values($_REQUEST['plugins_active']);
120 }
121
122 foreach ($available as $plugin) {
123 $plugentry =& $globals->plugins->cacheGet($cache, $this->plug_barrel, $this->plug_page, $plugin);
124 if (!is_array($plugentry) and $this->plug_page) {
125 $plugentry = $globals->plugins->cacheGet($cache, $this->plug_barrel, 0, $plugin);
126 if (is_array($plugentry))
127 {
128 $plugentry['active'] = 0;
129 }
130 }
131
132 // check we have a valid cache entry
133 if (!is_array($plugentry)) {
134 $page->info("could not find plugin '$plugin' in cache for barrel '{$this->plug_barrel}'");
135 return;
136 }
137
138 $plug_h =& $globals->plugins->load($plugentry);
139
140 if (is_object($plug_h) && is_array($plugentry)) {
141 $pos = array_search($plugin, $active);
142
143 if ($pos !== false) {
144 // check the plugin is allowed in the current context
145 if ($this->plug_barrel and $this->plug_page) {
146 $wperms = $this->plug_page_wperms;
147
148 // $page->info("checking plugin '$plugin' vs. write permissions '$wperms'..");
149 if (!$plug_h->allow_wperms($wperms))
150 {
151 $page->info("plugin '$plugin' is not allowed with write permissions '$wperms'!");
152 break;
153 }
154 }
155
156 // retrieve parameters from REQUEST
157 foreach ($plug_h->params as $key => $val)
158 {
159 if (isset($_REQUEST[$plug_h->name."_".$key])) {
160 $plug_h->params[$key] = $_REQUEST[$plug_h->name."_".$key];
161 }
162 }
163
164 // write parameters to database
165 $plug_h->writeParams($this->plug_barrel, $this->plug_page, $pos);
166 } else {
167 // erase parameters from database
168 $plug_h->eraseParams($this->plug_barrel, $this->plug_page);
169 }
170 }
171 }
172
173 // log this action
174 if ($this->plug_barrel)
175 {
176 if ($this->plug_page)
177 {
178 $page->log('page_plugins', $this->plug_barrel.":".$this->plug_page);
179 } else {
180 $page->log('barrel_plugins', $this->plug_barrel.":*");
181 }
182 }
183
184 $globals->plugins->compileCache($cachefile, $this->plug_barrel);
185 $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);
186 $available = $globals->plugins->cachedAvailable($cache, $this->plug_barrel, $this->plug_page);
187 break;
188 }
189
190 // get dump of plugins to fill out form
191 $page->assign('plug_barrel', $this->plug_barrel);
192 $page->assign('plug_page', $this->plug_page);
193
194 $plugs = array();
195
196 // start by adding the active plugins
197 foreach ($cache as $plugcache)
198 {
199 if (in_array($plugcache['plugin'], $available) and ($plugcache['page'] == $this->plug_page) and ($plugcache['active']))
200 {
201 // check we have a valid plugin handle
202 $plug_h = $globals->plugins->load($plugcache);
203 if (!is_object($plug_h)) {
204
205 $page->info("could not load disabled plugin '{$plugcache['plugin']}' in barrel '{$this->plug_barrel}'");
206
207 } else {
208
209 $plugentry = $plug_h->dump();
210 $plugentry['icon'] = $globals->icons->get_action_icon('plugins');
211 $type = $plugentry['type'];
212 if (!empty($plugs[$type])) {
213 $plugentry['move_up'] = 1;
214 $last = count($plugs[$type]) - 1;
215 $plugs[$type][$last]['move_down'] = 1;
216 } else {
217 $plugs[$type] = array();
218 }
219 array_push($plugs[$type], $plugentry);
220
221 }
222 }
223 }
224
225 // next we add the disabled plugins
226 if (!$this->readonly)
227 {
228 foreach ($available as $plugname)
229 {
230 $plugcache = $globals->plugins->cacheGet($cache, $this->plug_barrel, $this->plug_page, $plugname);
231 if (!is_array($plugcache) or !$plugcache['active'])
232 {
233 $plugcache = $globals->plugins->cacheGet($cache, $this->plug_barrel, 0, $plugname);
234 $plugcache['active'] = 0;
235 $plug_h = $globals->plugins->load($plugcache);
236 if (!is_object($plug_h)) {
237 $page->info("could not load disabled plugin '$plugname' in barrel '{$this->plug_barrel}'");
238 return;
239 }
240
241 $plugentry = $plug_h->dump();
242 $plugentry['icon'] = $globals->icons->get_action_icon('plugins');
243 $type = $plugentry['type'];
244 if (empty($plugs[$type])) {
245 $plugs[$type] = array();
246 }
247 array_push($plugs[$type], $plugentry);
248 }
249 }
250 }
251
252 /*
253 echo "plugins <pre>";
254 print_r($plugs);
255 echo "</pre>";
256 */
257 $page->assign('plugins', $plugs);
258
259 // values
260 $page->assign('show_params', $this->show_params);
261 $page->assign('readonly',$this->readonly);
262
263 // translations
264 $page->assign('msg_submit', __("Submit"));
265 $page->assign('msg_plugedit_plugin', __("plugin"));
266 $page->assign('msg_plugedit_plugins', __("plugins"));
267 $page->assign('msg_plugedit_description', __("description"));
268 $page->assign('msg_plugedit_parameters', __("parameters"));
269 $page->assign('msg_move_up', __("move up"));
270 $page->assign('msg_move_down', __("move down"));
271
272 // if requested, assign the content to be displayed
273 if (!empty($outputvar)) {
274 $page->assign($outputvar, $page->fetch('plugin-editor.tpl'));
275 }
276 }
277
278
279 /** Do not display plugin parameters.
280 *
281 * @param $hide boolean
282 */
283 function hide_params($hide)
284 {
285 $this->show_params = !$hide;
286 }
287
288 }
289
290 ?>