OKAY! THIS IS IT! MOODLE 1.6!
[moodle.git] / admin / filters.php
blobd21ab6f032dd27a09cd506b4856d1ade46842b8c
1 <?php // $Id$
2 // filters.php
3 // Edit list of available text filters
5 require_once('../config.php');
6 require_once($CFG->libdir.'/tablelib.php');
8 // defines
9 define('FILTER_TABLE','filter_administration_table');
11 // check for allowed access
12 require_login();
14 if (!isadmin()) {
15 error('Only administrators can use the filters administration page');
17 if (!$site = get_site()) {
18 error('Site is not defined in filters administration page');
21 // get values from page
22 $params = new object();
23 $params->action = optional_param('action', '', PARAM_ACTION);
24 $params->filterpath = optional_param('filterpath', '', PARAM_PATH);
25 $params->cachetext = optional_param('cachetext', 0, PARAM_INT);
26 $params->filterall = optional_param('filterall', 0, PARAM_BOOL);
27 $params->filteruploadedfiles = optional_param('filteruploadedfiles', 0, PARAM_INT);
28 $params->filtermatchoneperpage = optional_param('filtermatchoneperpage', 0, PARAM_BOOL);
29 $params->filtermatchonepertext = optional_param('filtermatchonepertext', 0, PARAM_BOOL);
31 // some basic information
32 $url = 'filters.php';
33 $myurl = "$url?sesskey=" . sesskey();
34 $img = "$CFG->pixpath/t";
36 // get translated strings for use on page
37 $txt = new object();
38 $txt->managefilters = get_string('managefilters');
39 $txt->administration = get_string('administration');
40 $txt->configuration = get_string('configuration');
41 $txt->name = get_string('name');
42 $txt->hide = get_string('hide');
43 $txt->show = get_string('show');
44 $txt->hideshow = "$txt->hide/$txt->show";
45 $txt->settings = get_string('settings');
46 $txt->up = get_string('up');
47 $txt->down = get_string('down');
48 $txt->updown = "$txt->up/$txt->down";
49 $txt->cachetext = get_string('cachetext', 'admin');
50 $txt->configcachetext = get_string('configcachetext', 'admin');
51 $txt->filteruploadedfiles = get_string('filteruploadedfiles','admin');
52 $txt->configfilteruploadedfiles = get_string('configfilteruploadedfiles','admin');
53 $txt->filterall = get_string('filterall','admin');
54 $txt->filtermatchoneperpage = get_string('filtermatchoneperpage','admin');
55 $txt->filtermatchonepertext = get_string('filtermatchonepertext','admin');
56 $txt->configfilterall = get_string('configfilterall','admin');
57 $txt->configfiltermatchoneperpage = get_string('configfiltermatchoneperpage','admin');
58 $txt->configfiltermatchonepertext = get_string('configfiltermatchonepertext','admin');
59 $txt->cachecontrols = get_string('cachecontrols');
60 $txt->yes = get_string('yes');
61 $txt->no = get_string('no');
62 $txt->none = get_string('none');
63 $txt->allfiles = get_string('allfiles');
64 $txt->htmlfilesonly = get_string('htmlfilesonly');
66 // get a list of possible filters (and translate name if possible)
67 // note filters can be in the dedicated filters area OR in their
68 // associated modules
69 $installedfilters = array();
70 $filtersettings = array();
71 $filterlocations = array('mod','filter');
72 foreach ($filterlocations as $filterlocation) {
73 $plugins = get_list_of_plugins($filterlocation);
74 foreach ($plugins as $plugin) {
75 $pluginpath = "$CFG->dirroot/$filterlocation/$plugin/filter.php";
76 $settingspath = "$CFG->dirroot/$filterlocation/$plugin/filterconfig.html";
77 if (is_readable($pluginpath)) {
78 $name = trim(get_string("filtername", $plugin));
79 if (empty($name) or ($name == '[[filtername]]')) {
80 $name = ucfirst($plugin);
82 $installedfilters["$filterlocation/$plugin"] = $name;
83 if (is_readable($settingspath)) {
84 $filtersettings[] = "$filterlocation/$plugin";
90 // get all the currently selected filters
91 if (!empty($CFG->textfilters)) {
92 $oldactivefilters = explode(',', $CFG->textfilters);
93 $oldactivefilters = array_unique($oldactivefilters);
94 } else {
95 $oldactivefilters = array();
98 // take this opportunity to clean up filters
99 $activefilters = array();
100 foreach ($oldactivefilters as $oldactivefilter) {
101 if (!empty($oldactivefilter) and array_key_exists($oldactivefilter, $installedfilters)) {
102 $activefilters[] = $oldactivefilter;
106 //======================
107 // Process Actions
108 //======================
110 if ($params->action=="") {
111 // store cleaned active filers in db
112 set_config('textfilters', implode(',', $activefilters));
113 } elseif (($params->action=="hide") and confirm_sesskey()) {
114 $key=array_search($params->filterpath, $activefilters);
115 // check filterpath is valid
116 if ($key===false) {
117 // ignore it - doubleclick??
118 } else {
119 // just delete it
120 unset($activefilters[$key]);
121 set_config('textfilters', implode(',', $activefilters));
123 } elseif (($params->action=="show") and confirm_sesskey()) {
124 // check filterpath is valid
125 if (!array_key_exists($params->filterpath, $installedfilters)) {
126 error("Filter $params->filterpath is not currently installed", $url);
127 } elseif (array_search($params->filterpath,$activefilters)) {
128 // filterpath is already active - doubleclick??
129 } else {
130 // add it to installed filters
131 $activefilters[] = $params->filterpath;
132 $activefilters = array_unique($activefilters);
133 set_config('textfilters', implode(',', $activefilters));
135 } elseif (($params->action=="down") and confirm_sesskey()) {
136 $key=array_search($params->filterpath, $activefilters);
137 // check filterpath is valid
138 if ($key===false) {
139 error("Filter $params->filterpath is not currently active", $url);
140 } elseif ($key>=(count($activefilters)-1)) {
141 // cannot be moved any further down - doubleclick??
142 } else {
143 // swap with $key+1
144 $fsave = $activefilters[$key];
145 $activefilters[$key] = $activefilters[$key+1];
146 $activefilters[$key+1] = $fsave;
147 set_config('textfilters', implode(',', $activefilters));
149 } elseif (($params->action=="up") and confirm_sesskey()) {
150 $key=array_search($params->filterpath, $activefilters);
151 // check filterpath is valid
152 if ($key===false) {
153 error("Filter $params->filterpath is not currently active", $url);
154 } elseif ($key<1) {
155 //cannot be moved any further up - doubleclick??
156 } else {
157 // swap with $key-1
158 $fsave = $activefilters[$key];
159 $activefilters[$key] = $activefilters[$key-1];
160 $activefilters[$key-1] = $fsave;
161 set_config('textfilters', implode(',', $activefilters));
163 } elseif (($params->action=="config") and confirm_sesskey()) {
164 set_config('cachetext', $params->cachetext);
165 set_config('filteruploadedfiles', $params->filteruploadedfiles);
166 set_config('filterall', $params->filterall);
167 set_config('filtermatchoneperpage', $params->filtermatchoneperpage);
168 set_config('filtermatchonepertext', $params->filtermatchonepertext);
171 //======================
172 // Build Display Objects
173 //======================
175 // construct the display array with installed filters
176 // at the top in the right order
177 $displayfilters = array();
178 foreach ($activefilters as $activefilter) {
179 $name = $installedfilters[$activefilter];
180 $displayfilters[$activefilter] = $name;
182 foreach ($installedfilters as $key => $filter) {
183 if (!array_key_exists($key, $displayfilters)) {
184 $displayfilters[$key] = $filter;
188 // construct the flexible table ready to display
189 $table = new flexible_table(FILTER_TABLE);
190 $table->define_columns(array('name', 'hideshow', 'order', 'settings'));
191 $table->column_style('hideshow', 'text-align', 'center');
192 $table->column_style('order', 'text-align', 'center');
193 $table->column_style('settings', 'text-align', 'center');
194 $table->define_headers(array($txt->name, $txt->hideshow, $txt->updown, $txt->settings));
195 $table->define_baseurl("$CFG->wwwroot/admin/filters.php");
196 $table->set_attribute('id', 'blocks');
197 $table->set_attribute('class', 'flexible generaltable generalbox');
198 $table->set_attribute('style', 'margin:auto;');
199 $table->set_attribute('cellpadding', '5');
200 $table->setup();
202 // iterate through filters adding to display table
203 $updowncount = 1;
204 $activefilterscount = count($activefilters);
205 foreach ($displayfilters as $path => $name) {
206 $upath = urlencode($path);
207 // get hide/show link
208 if (in_array($path, $activefilters)) {
209 $hideshow = "<a href=\"$myurl&amp;action=hide&amp;filterpath=$upath\">";
210 $hideshow .= "<img src=\"{$CFG->pixpath}/i/hide.gif\" height=\"16\" width=\"16\" alt=\"hide\" /></a>";
211 $hidden = false;
212 $displayname = "<span>$name</span>";
214 else {
215 $hideshow = "<a href=\"$myurl&amp;action=show&amp;filterpath=$upath\">";
216 $hideshow .= "<img src=\"{$CFG->pixpath}/i/show.gif\" height=\"16\" width=\"16\" alt=\"show\" /></a>";
217 $hidden = true;
218 $displayname = "<span class=\"dimmed_text\">$name</span>";
221 // get up/down link (only if not hidden)
222 $updown = '';
223 if (!$hidden) {
224 if ($updowncount>1) {
225 $updown .= "<a href=\"$myurl&amp;action=up&amp;filterpath=$upath\">";
226 $updown .= "<img src=\"$img/up.gif\" alt=\"up\" /></a>&nbsp;";
228 else {
229 $updown .= "<img src=\"$CFG->pixpath/spacer.gif\" height=\"16\" width=\"16\" alt=\"\" />&nbsp;";
231 if ($updowncount<$activefilterscount) {
232 $updown .= "<a href=\"$myurl&amp;action=down&amp;filterpath=$upath\">";
233 $updown .= "<img src=\"$img/down.gif\" alt=\"down\" /></a>";
235 else {
236 $updown .= "<img src=\"$CFG->pixpath/spacer.gif\" height=\"16\" width=\"16\" alt=\"\" />";
238 ++$updowncount;
241 // settings link (if defined)
242 $settings = '';
243 if (in_array($path, $filtersettings)) {
244 $settings = "<a href=\"filter.php?filter=" . urlencode($path) . "\">";
245 $settings .= "{$txt->settings}</a>";
248 // write data into the table object
249 $table->add_data(array($displayname, $hideshow, $updown, $settings));
252 // build options list for cache lifetime
253 $seconds = array(604800,86400,43200,10800,7200,3600,2700,1800,900,600,540,480,420,360,300,240,180,120,60,30,0);
254 unset($lifetimeoptions);
255 foreach ($seconds as $second) {
256 if ($second>=86400) {
257 $options[$second] = get_string('numdays','',$second/86400);
259 elseif ($second>=3600) {
260 $options[$second] = get_string('numhours','',$second/3600);
262 elseif ($second>=60) {
263 $options[$second] = get_string('numminutes','',$second/60);
265 elseif ($second>=1) {
266 $options[$second] = get_string('numseconds','',$second);
268 else {
269 $options[$second] = get_string('no');
273 //==============================
274 // Display logic
275 //==============================
277 print_header("$site->shortname: $txt->managefilters", "$site->fullname",
278 "<a href=\"index.php\">$txt->administration</a> -> <a href=\"configure.php\">$txt->configuration</a> " .
279 "-> $txt->managefilters");
281 print_heading_with_help($txt->managefilters, 'filters');
283 // print the table of all the filters
284 $table->print_html();
286 // print the table for the cache controls
287 print_heading($txt->cachecontrols);
288 print_simple_box_start('center');
291 <form name="options" id="options" method="post" action="<?php echo $url; ?>" >
292 <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
293 <input type="hidden" name="action" value="config" />
294 <table cellpadding="20">
295 <tr valign="top">
296 <td nowrap="nowrap" align="right"><?php echo $txt->cachetext; ?></td>
297 <td><?php choose_from_menu($options, "cachetext", $CFG->cachetext, "", "", ""); ?></td>
298 <td><?php echo $txt->configcachetext; ?></td>
299 </tr>
300 <tr valign="top">
301 <td nowrap="nowrap" align="right"><?php echo $txt->filteruploadedfiles; ?></td>
302 <td><?php choose_from_menu(array($txt->none,$txt->allfiles,$txt->htmlfilesonly),
303 "filteruploadedfiles", $CFG->filteruploadedfiles,"","",""); ?></td>
304 <td><?php echo $txt->configfilteruploadedfiles; ?></td>
305 </tr>
306 <tr valign="top">
307 <td nowrap="nowrap" align="right"><?php echo $txt->filtermatchoneperpage; ?></td>
308 <td><?php choose_from_menu(array($txt->no,$txt->yes), "filtermatchoneperpage", $CFG->filtermatchoneperpage,"","",""); ?></td>
309 <td><?php echo $txt->configfiltermatchoneperpage; ?></td>
310 <tr valign="top">
311 <td nowrap="nowrap" align="right"><?php echo $txt->filtermatchonepertext; ?></td>
312 <td><?php choose_from_menu(array($txt->no,$txt->yes), "filtermatchonepertext", $CFG->filtermatchonepertext,"","",""); ?></td>
313 <td><?php echo $txt->configfiltermatchonepertext; ?></td>
314 </tr>
315 </tr>
316 <tr valign="top">
317 <td nowrap="nowrap" align="right"><?php echo $txt->filterall; ?></td>
318 <td><?php choose_from_menu(array($txt->no,$txt->yes), "filterall", $CFG->filterall,"","",""); ?></td>
319 <td><?php echo $txt->configfilterall; ?></td>
320 </tr>
321 <tr valign="top">
322 <td>&nbsp;</td>
323 <td><input type="submit" value="<?php print_string('savechanges'); ?>" /></td>
324 <td>&nbsp;</td>
325 </tr>
326 </table>
327 </form>
329 <?php
330 print_simple_box_end();
332 print_footer();