f0b283ddd85e96b31dbf7bfa8e70fab899df352e
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))
75 $globals->plugins
->compileCache($this->plug_barrel
, $page);
77 $cache = $globals->plugins
->readCache($cachefile, $this->plug_barrel
);
78 $available = $globals->plugins
->cachedAvailable($cache, $this->plug_barrel
, $this->plug_page
);
83 case "move_up": case "move_down":
84 if ($this->readonly
) die("Sorry, this plugin view is read-only.");
86 $delta = ($action == "move_down") ?
1 : -1;
87 $page->info("moving plugin '$target'..");
88 $plugcache_a = $globals->plugins
->cacheGet($cache, $this->plug_barrel
, $this->plug_page
, $target);
89 $plug_a =& $globals->plugins
->load($plugcache_a['name'], $plugcache_a);
91 if (is_object($plug_a) && ($plug_a->active
)) {
92 $old_pos = $plug_a->pos
;
93 $plugcache_b = $globals->plugins
->cacheGetAtPos($cache, $this->plug_barrel
, $this->plug_page
, $old_pos +
$delta);
95 if (is_array($plugcache_b))
97 $plug_b =& $globals->plugins
->load($plugcache_b['name'], $plugcache_b);
99 // swap the current plugin and the next plugin
100 if (is_object($plug_b) && ($plug_b->active
))
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);
111 if ($this->readonly
) die("Sorry, this plugin view is read-only.");
112 foreach ($available as $plugin => $plugentry)
114 // check we have a valid cache entry
115 if (!is_array($plugentry)) {
116 $page->info("could not find plugin '$plugin' in cache for barrel '{$this->plug_barrel}'");
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}'");
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
;
131 // $page->info("checking plugin '$plugin' vs. write permissions '$wperms'..");
132 if (!$plug_h->allow_wperms($wperms))
134 $page->info("plugin '$plugin' is not allowed with write permissions '$wperms'!");
139 // retrieve parameters from REQUEST
140 if (isset($_REQUEST[$plug_h->name
."_status"]))
142 $plug_h->status
= $_REQUEST[$plug_h->name
."_status"];
144 foreach ($plug_h->getParamNames() as $key)
146 if (isset($_REQUEST[$plug_h->name
."_".$key])) {
147 $plug_h->setParamValue($key, $_REQUEST[$plug_h->name
."_".$key]);
151 // write parameters to database
152 $plug_h->toDatabase($this->plug_barrel
, $this->plug_page
, $pos);
159 // if necessary, rebuild the plugin cache
163 if ($this->plug_barrel
)
165 if ($this->plug_page
)
167 $page->log('page_plugins', $this->plug_barrel
.":".$this->plug_page
);
169 $page->log('barrel_plugins', $this->plug_barrel
.":*");
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
);
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
);
183 // start by adding the active plugins
184 foreach ($available as $plugname => $plugcache)
186 if ($plugcache['status'] == PLUG_ACTIVE
)
188 $plugentry = $plugcache;
189 $plugentry['icon'] = $globals->icons
->get_action_icon('plugins');
190 $type = $plugentry['type'];
191 if (!empty($plugs[$type])) {
192 $plugentry['move_up'] = 1;
193 $last = count($plugs[$type]) - 1;
194 $plugs[$type][$last]['move_down'] = 1;
196 $plugs[$type] = array();
198 array_push($plugs[$type], $plugentry);
202 // next we add the inactive plugins
203 if (!$this->readonly
)
205 foreach ($available as $plugname => $plugcache)
207 if ( ($plugcache['status'] == PLUG_AVAILABLE
) ||
208 (($plugcache['status'] == PLUG_DISABLED
) && !$this->plug_barrel
) )
210 $plugentry = $plugcache;
211 $plugentry['icon'] = $globals->icons
->get_action_icon('plugins');
212 $type = $plugentry['type'];
213 if (empty($plugs[$type])) {
214 $plugs[$type] = array();
216 array_push($plugs[$type], $plugentry);
221 $page->assign('plugins', $plugs);
224 $page->assign('show_params', $this->show_params
);
225 $page->assign('readonly',$this->readonly
);
226 $statusvals = array(0 => 'disabled', '1' => 'available', 2 => 'enabled');
227 $page->assign('statusvals', $statusvals);
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"));
238 // if requested, assign the content to be displayed
239 if (!empty($outputvar)) {
240 $page->assign($outputvar, $page->fetch('plugin-editor.tpl'));
245 /** Do not display plugin parameters.
247 * @param $hide boolean
249 function hide_params($hide)
251 $this->show_params
= !$hide;