Bumped to 1.7.6
[moodle.git] / admin / filters.php
blob9d7832e9b674ec2a70fc836ed6003078e72181f7
1 <?php // $Id$
2 // filters.php
3 // Edit list of available text filters
5 require_once('../config.php');
6 require_once($CFG->libdir.'/adminlib.php');
7 require_once($CFG->libdir.'/tablelib.php');
9 // defines
10 define('FILTER_TABLE','filter_administration_table');
12 $adminroot = admin_get_root();
13 admin_externalpage_setup('managefilters', $adminroot);
15 // get values from page
16 $params = new object();
17 $params->action = optional_param('action', '', PARAM_ACTION);
18 $params->filterpath = optional_param('filterpath', '', PARAM_PATH);
19 $params->cachetext = optional_param('cachetext', 0, PARAM_INT);
20 $params->filterall = optional_param('filterall', 0, PARAM_BOOL);
21 $params->filteruploadedfiles = optional_param('filteruploadedfiles', 0, PARAM_INT);
22 $params->filtermatchoneperpage = optional_param('filtermatchoneperpage', 0, PARAM_BOOL);
23 $params->filtermatchonepertext = optional_param('filtermatchonepertext', 0, PARAM_BOOL);
25 // some basic information
26 $url = 'filters.php';
27 $myurl = "$url?sesskey=" . sesskey();
28 $img = "$CFG->pixpath/t";
30 // get translated strings for use on page
31 $txt = new object();
32 $txt->managefilters = get_string('managefilters');
33 $txt->administration = get_string('administration');
34 $txt->configuration = get_string('configuration');
35 $txt->name = get_string('name');
36 $txt->hide = get_string('hide');
37 $txt->show = get_string('show');
38 $txt->hideshow = "$txt->hide/$txt->show";
39 $txt->settings = get_string('settings');
40 $txt->up = get_string('up');
41 $txt->down = get_string('down');
42 $txt->updown = "$txt->up/$txt->down";
43 $txt->cachetext = get_string('cachetext', 'admin');
44 $txt->configcachetext = get_string('configcachetext', 'admin');
45 $txt->filteruploadedfiles = get_string('filteruploadedfiles','admin');
46 $txt->configfilteruploadedfiles = get_string('configfilteruploadedfiles','admin');
47 $txt->filterall = get_string('filterall','admin');
48 $txt->filtermatchoneperpage = get_string('filtermatchoneperpage','admin');
49 $txt->filtermatchonepertext = get_string('filtermatchonepertext','admin');
50 $txt->configfilterall = get_string('configfilterall','admin');
51 $txt->configfiltermatchoneperpage = get_string('configfiltermatchoneperpage','admin');
52 $txt->configfiltermatchonepertext = get_string('configfiltermatchonepertext','admin');
53 $txt->cachecontrols = get_string('cachecontrols');
54 $txt->yes = get_string('yes');
55 $txt->no = get_string('no');
56 $txt->none = get_string('none');
57 $txt->allfiles = get_string('allfiles');
58 $txt->htmlfilesonly = get_string('htmlfilesonly');
60 // get a list of possible filters (and translate name if possible)
61 // note filters can be in the dedicated filters area OR in their
62 // associated modules
63 $installedfilters = array();
64 $filtersettings = array();
65 $filterlocations = array('mod','filter');
66 foreach ($filterlocations as $filterlocation) {
67 $plugins = get_list_of_plugins($filterlocation);
68 foreach ($plugins as $plugin) {
69 $pluginpath = "$CFG->dirroot/$filterlocation/$plugin/filter.php";
70 $settingspath = "$CFG->dirroot/$filterlocation/$plugin/filterconfig.html";
71 if (is_readable($pluginpath)) {
72 $name = trim(get_string("filtername", $plugin));
73 if (empty($name) or ($name == '[[filtername]]')) {
74 $name = ucfirst($plugin);
76 $installedfilters["$filterlocation/$plugin"] = $name;
77 if (is_readable($settingspath)) {
78 $filtersettings[] = "$filterlocation/$plugin";
84 // get all the currently selected filters
85 if (!empty($CFG->textfilters)) {
86 $oldactivefilters = explode(',', $CFG->textfilters);
87 $oldactivefilters = array_unique($oldactivefilters);
88 } else {
89 $oldactivefilters = array();
92 // take this opportunity to clean up filters
93 $activefilters = array();
94 foreach ($oldactivefilters as $oldactivefilter) {
95 if (!empty($oldactivefilter) and array_key_exists($oldactivefilter, $installedfilters)) {
96 $activefilters[] = $oldactivefilter;
100 //======================
101 // Process Actions
102 //======================
104 if ($params->action=="") {
105 // store cleaned active filers in db
106 set_config('textfilters', implode(',', $activefilters));
107 } elseif (($params->action=="hide") and confirm_sesskey()) {
108 $key=array_search($params->filterpath, $activefilters);
109 // check filterpath is valid
110 if ($key===false) {
111 // ignore it - doubleclick??
112 } else {
113 // just delete it
114 unset($activefilters[$key]);
115 set_config('textfilters', implode(',', $activefilters));
117 } elseif (($params->action=="show") and confirm_sesskey()) {
118 // check filterpath is valid
119 if (!array_key_exists($params->filterpath, $installedfilters)) {
120 error("Filter $params->filterpath is not currently installed", $url);
121 } elseif (array_search($params->filterpath,$activefilters)) {
122 // filterpath is already active - doubleclick??
123 } else {
124 // add it to installed filters
125 $activefilters[] = $params->filterpath;
126 $activefilters = array_unique($activefilters);
127 set_config('textfilters', implode(',', $activefilters));
129 } elseif (($params->action=="down") and confirm_sesskey()) {
130 $key=array_search($params->filterpath, $activefilters);
131 // check filterpath is valid
132 if ($key===false) {
133 error("Filter $params->filterpath is not currently active", $url);
134 } elseif ($key>=(count($activefilters)-1)) {
135 // cannot be moved any further down - doubleclick??
136 } else {
137 // swap with $key+1
138 $fsave = $activefilters[$key];
139 $activefilters[$key] = $activefilters[$key+1];
140 $activefilters[$key+1] = $fsave;
141 set_config('textfilters', implode(',', $activefilters));
143 } elseif (($params->action=="up") and confirm_sesskey()) {
144 $key=array_search($params->filterpath, $activefilters);
145 // check filterpath is valid
146 if ($key===false) {
147 error("Filter $params->filterpath is not currently active", $url);
148 } elseif ($key<1) {
149 //cannot be moved any further up - doubleclick??
150 } else {
151 // swap with $key-1
152 $fsave = $activefilters[$key];
153 $activefilters[$key] = $activefilters[$key-1];
154 $activefilters[$key-1] = $fsave;
155 set_config('textfilters', implode(',', $activefilters));
157 } elseif (($params->action=="config") and confirm_sesskey()) {
158 set_config('cachetext', $params->cachetext);
159 set_config('filteruploadedfiles', $params->filteruploadedfiles);
160 set_config('filterall', $params->filterall);
161 set_config('filtermatchoneperpage', $params->filtermatchoneperpage);
162 set_config('filtermatchonepertext', $params->filtermatchonepertext);
165 //======================
166 // Build Display Objects
167 //======================
169 // construct the display array with installed filters
170 // at the top in the right order
171 $displayfilters = array();
172 foreach ($activefilters as $activefilter) {
173 $name = $installedfilters[$activefilter];
174 $displayfilters[$activefilter] = $name;
176 foreach ($installedfilters as $key => $filter) {
177 if (!array_key_exists($key, $displayfilters)) {
178 $displayfilters[$key] = $filter;
182 // construct the flexible table ready to display
183 $table = new flexible_table(FILTER_TABLE);
184 $table->define_columns(array('name', 'hideshow', 'order', 'settings'));
185 $table->column_style('hideshow', 'text-align', 'center');
186 $table->column_style('order', 'text-align', 'center');
187 $table->column_style('settings', 'text-align', 'center');
188 $table->define_headers(array($txt->name, $txt->hideshow, $txt->updown, $txt->settings));
189 $table->define_baseurl("$CFG->wwwroot/$CFG->admin/filters.php");
190 $table->set_attribute('id', 'blocks');
191 $table->set_attribute('class', 'flexible generaltable generalbox');
192 $table->set_attribute('style', 'margin:auto;');
193 $table->set_attribute('cellpadding', '5');
194 $table->setup();
196 // iterate through filters adding to display table
197 $updowncount = 1;
198 $activefilterscount = count($activefilters);
199 foreach ($displayfilters as $path => $name) {
200 $upath = urlencode($path);
201 // get hide/show link
202 if (in_array($path, $activefilters)) {
203 $hideshow = "<a href=\"$myurl&amp;action=hide&amp;filterpath=$upath\">";
204 $hideshow .= "<img src=\"{$CFG->pixpath}/i/hide.gif\" height=\"16\" width=\"16\" alt=\"hide\" /></a>";
205 $hidden = false;
206 $displayname = "<span>$name</span>";
208 else {
209 $hideshow = "<a href=\"$myurl&amp;action=show&amp;filterpath=$upath\">";
210 $hideshow .= "<img src=\"{$CFG->pixpath}/i/show.gif\" height=\"16\" width=\"16\" alt=\"show\" /></a>";
211 $hidden = true;
212 $displayname = "<span class=\"dimmed_text\">$name</span>";
215 // get up/down link (only if not hidden)
216 $updown = '';
217 if (!$hidden) {
218 if ($updowncount>1) {
219 $updown .= "<a href=\"$myurl&amp;action=up&amp;filterpath=$upath\">";
220 $updown .= "<img src=\"$img/up.gif\" alt=\"up\" /></a>&nbsp;";
222 else {
223 $updown .= "<img src=\"$CFG->pixpath/spacer.gif\" height=\"16\" width=\"16\" alt=\"\" />&nbsp;";
225 if ($updowncount<$activefilterscount) {
226 $updown .= "<a href=\"$myurl&amp;action=down&amp;filterpath=$upath\">";
227 $updown .= "<img src=\"$img/down.gif\" alt=\"down\" /></a>";
229 else {
230 $updown .= "<img src=\"$CFG->pixpath/spacer.gif\" height=\"16\" width=\"16\" alt=\"\" />";
232 ++$updowncount;
235 // settings link (if defined)
236 $settings = '';
237 if (in_array($path, $filtersettings)) {
238 $settings = "<a href=\"filter.php?filter=" . urlencode($path) . "\">";
239 $settings .= "{$txt->settings}</a>";
242 // write data into the table object
243 $table->add_data(array($displayname, $hideshow, $updown, $settings));
246 // build options list for cache lifetime
247 $seconds = array(604800,86400,43200,10800,7200,3600,2700,1800,900,600,540,480,420,360,300,240,180,120,60,30,0);
248 unset($lifetimeoptions);
249 foreach ($seconds as $second) {
250 if ($second>=86400) {
251 $options[$second] = get_string('numdays','',$second/86400);
253 elseif ($second>=3600) {
254 $options[$second] = get_string('numhours','',$second/3600);
256 elseif ($second>=60) {
257 $options[$second] = get_string('numminutes','',$second/60);
259 elseif ($second>=1) {
260 $options[$second] = get_string('numseconds','',$second);
262 else {
263 $options[$second] = get_string('no');
267 //==============================
268 // Display logic
269 //==============================
271 admin_externalpage_print_header($adminroot);
273 print_heading_with_help($txt->managefilters, 'filters');
275 // print the table of all the filters
276 $table->print_html();
278 // cache control table has been removed
280 admin_externalpage_print_footer($adminroot);