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