first shot at reworking plugin system
[diogenes.git] / include / Plugin / Editor.php
CommitLineData
6855525e
JL
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
22require_once 'System.php';
23
24
25/** This class provides a configuration tool for Diogenes plugins.
26 */
27class 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 }
56aefc1e
JL
55
56
6855525e
JL
57 /** Run the plugin editor.
58 *
59 * @param $page
60 * @param $outputvar
61 */
62 function run(&$page, $outputvar = '')
63 {
64 global $globals;
56aefc1e 65
6855525e
JL
66 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
67 $target = isset($_REQUEST['plug_target']) ? $_REQUEST['plug_target'] : '';
56aefc1e 68
6855525e
JL
69 // load all available plugins
70 $cachefile = $globals->plugins->cacheFile($this->plug_barrel);
56aefc1e
JL
71
72 // if the tree cache does not exits try to init it
73 if(!file_exists($cachefile))
74 {
75 $globals->plugins->compileCache($this->plug_barrel, $page);
76 }
6855525e
JL
77 $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);
78 $available = $globals->plugins->cachedAvailable($cache, $this->plug_barrel, $this->plug_page);
56aefc1e 79
6855525e 80 // handle updates
56aefc1e 81 $rebuild_cache = 0;
6855525e
JL
82 switch ($action) {
83 case "move_up": case "move_down":
84 if ($this->readonly) die("Sorry, this plugin view is read-only.");
56aefc1e 85
6855525e 86 $delta = ($action == "move_down") ? 1 : -1;
56aefc1e 87 $page->info("moving plugin '$target'..");
6855525e 88 $plugcache_a = $globals->plugins->cacheGet($cache, $this->plug_barrel, $this->plug_page, $target);
56aefc1e
JL
89 $plug_a =& $globals->plugins->load($plugcache_a['name'], $plugcache_a);
90
6855525e
JL
91 if (is_object($plug_a) && ($plug_a->active)) {
92 $old_pos = $plug_a->pos;
6855525e 93 $plugcache_b = $globals->plugins->cacheGetAtPos($cache, $this->plug_barrel, $this->plug_page, $old_pos + $delta);
56aefc1e 94
6855525e
JL
95 if (is_array($plugcache_b))
96 {
56aefc1e
JL
97 $plug_b =& $globals->plugins->load($plugcache_b['name'], $plugcache_b);
98
6855525e
JL
99 // swap the current plugin and the next plugin
100 if (is_object($plug_b) && ($plug_b->active))
101 {
56aefc1e
JL
102 $plug_a->toDatabase($this->plug_barrel, $this->plug_page, $old_pos + $delta);
103 $plug_b->toDatabase($this->plug_barrel, $this->plug_page, $old_pos);
104 }
6855525e 105 }
56aefc1e
JL
106 }
107 $rebuild_cache = 1;
6855525e 108 break;
56aefc1e 109
6855525e
JL
110 case "update":
111 if ($this->readonly) die("Sorry, this plugin view is read-only.");
56aefc1e
JL
112 foreach ($available as $plugin => $plugentry)
113 {
6855525e
JL
114 // check we have a valid cache entry
115 if (!is_array($plugentry)) {
56aefc1e 116 $page->info("could not find plugin '$plugin' in cache for barrel '{$this->plug_barrel}'");
6855525e
JL
117 return;
118 }
56aefc1e
JL
119
120 $plug_h =& $globals->plugins->load($plugin, $plugentry);
121 if (!is_object($plug_h)) {
122 $page->info("could not load plugin '$plugin' in cache for barrel '{$this->plug_barrel}'");
123 return;
124 }
125
126 if ($pos !== false) {
127 // check the plugin is allowed in the current context
128 if ($this->plug_barrel and $this->plug_page) {
129 $wperms = $this->plug_page_wperms;
130
131 // $page->info("checking plugin '$plugin' vs. write permissions '$wperms'..");
132 if (!$plug_h->allow_wperms($wperms))
6855525e 133 {
56aefc1e
JL
134 $page->info("plugin '$plugin' is not allowed with write permissions '$wperms'!");
135 break;
6855525e 136 }
56aefc1e
JL
137 }
138
139 // retrieve parameters from REQUEST
140 if (isset($_REQUEST[$plug_h->name."_status"]))
141 {
142 $plug_h->status = $_REQUEST[$plug_h->name."_status"];
143 }
144 foreach ($plug_h->getParamNames() as $key)
145 {
146 if (isset($_REQUEST[$plug_h->name."_".$key])) {
147 $plug_h->setParamValue($key, $_REQUEST[$plug_h->name."_".$key]);
148 }
149 }
150
151 // write parameters to database
152 $plug_h->toDatabase($this->plug_barrel, $this->plug_page, $pos);
153 $rebuild_cache = 1;
6855525e
JL
154 }
155 }
56aefc1e
JL
156 break;
157 }
158
159 // if necessary, rebuild the plugin cache
160 if ($rebuild_cache)
161 {
6855525e
JL
162 // log this action
163 if ($this->plug_barrel)
164 {
165 if ($this->plug_page)
166 {
56aefc1e 167 $page->log('page_plugins', $this->plug_barrel.":".$this->plug_page);
6855525e 168 } else {
56aefc1e 169 $page->log('barrel_plugins', $this->plug_barrel.":*");
6855525e
JL
170 }
171 }
56aefc1e
JL
172 // rebuild plugin cache
173 $globals->plugins->compileCache($this->plug_barrel, $page);
174 $cache = $globals->plugins->readCache($cachefile, $this->plug_barrel);
175 $available = $globals->plugins->cachedAvailable($cache, $this->plug_barrel, $this->plug_page);
6855525e
JL
176 }
177
178 // get dump of plugins to fill out form
179 $page->assign('plug_barrel', $this->plug_barrel);
180 $page->assign('plug_page', $this->plug_page);
6855525e
JL
181 $plugs = array();
182
183 // start by adding the active plugins
56aefc1e 184 foreach ($available as $plugname => $plugcache)
6855525e 185 {
56aefc1e 186 if ($plugcache['status'] == PLUG_ACTIVE)
6855525e 187 {
56aefc1e
JL
188 $plugentry = $plugcache;
189 $plugentry['icon'] = $globals->icons->get_action_icon('plugins');
190 $type = $plugentry['type'];
191 if (!empty($plugs[$type])) {
6855525e
JL
192 $plugentry['move_up'] = 1;
193 $last = count($plugs[$type]) - 1;
56aefc1e 194 $plugs[$type][$last]['move_down'] = 1;
6855525e 195 } else {
56aefc1e
JL
196 $plugs[$type] = array();
197 }
6855525e 198 array_push($plugs[$type], $plugentry);
56aefc1e 199 }
6855525e 200 }
56aefc1e
JL
201
202 // next we add the inactive plugins
6855525e
JL
203 if (!$this->readonly)
204 {
56aefc1e 205 foreach ($available as $plugname => $plugcache)
6855525e 206 {
56aefc1e
JL
207 if ( ($plugcache['status'] == PLUG_AVAILABLE) ||
208 (($plugcache['status'] == PLUG_DISABLED) && !$this->plug_barrel) )
6855525e 209 {
56aefc1e
JL
210 $plugentry = $plugcache;
211 $plugentry['icon'] = $globals->icons->get_action_icon('plugins');
212 $type = $plugentry['type'];
6855525e
JL
213 if (empty($plugs[$type])) {
214 $plugs[$type] = array();
215 }
216 array_push($plugs[$type], $plugentry);
217 }
218 }
219 }
56aefc1e 220
6855525e 221 $page->assign('plugins', $plugs);
56aefc1e 222
6855525e
JL
223 // values
224 $page->assign('show_params', $this->show_params);
225 $page->assign('readonly',$this->readonly);
56aefc1e
JL
226 $statusvals = array(0 => 'disabled', '1' => 'available', 2 => 'enabled');
227 $page->assign('statusvals', $statusvals);
6855525e
JL
228
229 // translations
230 $page->assign('msg_submit', __("Submit"));
231 $page->assign('msg_plugedit_plugin', __("plugin"));
232 $page->assign('msg_plugedit_plugins', __("plugins"));
233 $page->assign('msg_plugedit_description', __("description"));
234 $page->assign('msg_plugedit_parameters', __("parameters"));
235 $page->assign('msg_move_up', __("move up"));
236 $page->assign('msg_move_down', __("move down"));
56aefc1e 237
6855525e
JL
238 // if requested, assign the content to be displayed
239 if (!empty($outputvar)) {
240 $page->assign($outputvar, $page->fetch('plugin-editor.tpl'));
241 }
242 }
243
244
245 /** Do not display plugin parameters.
246 *
247 * @param $hide boolean
248 */
249 function hide_params($hide)
250 {
251 $this->show_params = !$hide;
252 }
253
254}
255
256?>