9de3b2f1c2a75b45ade74d2c0b1b1cc358899fd6
3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
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.
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.
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
22 require_once 'System.php';
25 /** This class provides a configuration tool for Diogenes plugins.
27 class Diogenes_Plugin_Editor
{
28 /** Should we show plugins parameters? */
31 /** Should editing functions be disabled? */
32 var $readonly = false
;
34 /** The alias of the barrel we are working on. */
37 /** The PID of the page we are working on. */
40 /** The write permissions of the page we are working on. */
41 var $plug_page_wperms;
43 /** Construct a new plugin editor.
47 * @param $plug_page_wperms
49 function Diogenes_Plugin_Editor($plug_barrel, $plug_page, $plug_page_wperms = '')
51 $this->plug_barrel
= $plug_barrel;
52 $this->plug_page
= $plug_page;
53 $this->plug_page_wperms
= $plug_page_wperms;
57 /** Run the plugin editor.
62 function run(&$page, $outputvar = '')
66 $action = isset($_REQUEST['action']) ?
$_REQUEST['action'] : '';
67 $target = isset($_REQUEST['plug_target']) ?
$_REQUEST['plug_target'] : '';
69 // load all available plugins
70 $cachefile = $globals->plugins
->cacheFile($this->plug_barrel
);
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
);
76 $cache = $globals->plugins
->readCache($cachefile, $this->plug_barrel
);
77 $available = $globals->plugins
->cachedAvailable($cache, $this->plug_barrel
, $this->plug_page
);
82 case "move_up": case "move_down":
83 if ($this->readonly
) die("Sorry, this plugin view is read-only.");
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);
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);
96 if (is_array($plugcache_b))
98 $plug_b =& $globals->plugins
->load($plugcache_b);
100 // swap the current plugin and the next plugin
101 if (is_object($plug_b) && ($plug_b->active
))
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);
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
);
114 if ($this->readonly
) die("Sorry, this plugin view is read-only.");
116 // list of active plugins
118 if (isset($_REQUEST['plugins_active'])) {
119 $active = array_values($_REQUEST['plugins_active']);
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))
128 $plugentry['active'] = 0;
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}'");
138 $plug_h =& $globals->plugins
->load($plugentry);
140 if (is_object($plug_h) && is_array($plugentry)) {
141 $pos = array_search($plugin, $active);
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
;
148 // $page->info("checking plugin '$plugin' vs. write permissions '$wperms'..");
149 if (!$plug_h->allow_wperms($wperms))
151 $page->info("plugin '$plugin' is not allowed with write permissions '$wperms'!");
156 // retrieve parameters from REQUEST
157 foreach ($plug_h->params
as $key => $val)
159 if (isset($_REQUEST[$plug_h->name
."_".$key])) {
160 $plug_h->params
[$key] = $_REQUEST[$plug_h->name
."_".$key];
164 // write parameters to database
165 $plug_h->writeParams($this->plug_barrel
, $this->plug_page
, $pos);
167 // erase parameters from database
168 $plug_h->eraseParams($this->plug_barrel
, $this->plug_page
);
174 if ($this->plug_barrel
)
176 if ($this->plug_page
)
178 $page->log('page_plugins', $this->plug_barrel
.":".$this->plug_page
);
180 $page->log('barrel_plugins', $this->plug_barrel
.":*");
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
);
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
);
196 // start by adding the active plugins
197 foreach ($cache as $plugcache)
199 if (in_array($plugcache['plugin'], $available) and ($plugcache['page'] == $this->plug_page
) and ($plugcache['active']))
201 // check we have a valid plugin handle
202 $plug_h = $globals->plugins
->load($plugcache);
203 if (!is_object($plug_h)) {
205 $page->info("could not load disabled plugin '{$plugcache['plugin']}' in barrel '{$this->plug_barrel}'");
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;
217 $plugs[$type] = array();
219 array_push($plugs[$type], $plugentry);
225 // next we add the disabled plugins
226 if (!$this->readonly
)
228 foreach ($available as $plugname)
230 $plugcache = $globals->plugins
->cacheGet($cache, $this->plug_barrel
, $this->plug_page
, $plugname);
231 if (!is_array($plugcache) or !$plugcache['active'])
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}'");
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();
247 array_push($plugs[$type], $plugentry);
253 echo "plugins <pre>";
257 $page->assign('plugins', $plugs);
260 $page->assign('show_params', $this->show_params
);
261 $page->assign('readonly',$this->readonly
);
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"));
272 // if requested, assign the content to be displayed
273 if (!empty($outputvar)) {
274 $page->assign($outputvar, $page->fetch('plugin-editor.tpl'));
279 /** Do not display plugin parameters.
281 * @param $hide boolean
283 function hide_params($hide)
285 $this->show_params
= !$hide;