Output was missing utf8 meta tag.
[moodle-linuxchix.git] / admin / search.php
blobc68ef13801e4ef57903b0a52588c975d7b78a0b0
1 <?php // $Id$
3 // searches for admin settings
5 require_once('../config.php');
6 require_once($CFG->libdir.'/adminlib.php');
8 $query = trim(stripslashes_safe(required_param('query', PARAM_NOTAGS))); // Search string
10 $adminroot = admin_get_root();
11 admin_externalpage_setup('search', $adminroot); // now hidden page
13 $CFG->adminsearchquery = $query; // So we can reference it in search boxes later in this invocation
16 // now we'll deal with the case that the admin has submitted the form with changed settings
18 $statusmsg = '';
20 if ($data = data_submitted()) {
21 $unslashed = (array)stripslashes_recursive($data);
22 if (confirm_sesskey()) {
23 $olddbsessions = !empty($CFG->dbsessions);
24 $changedsettings = search_settings(admin_get_root(), $query);
25 $errors = '';
27 foreach($changedsettings as $changedsetting) {
28 if (!isset($unslashed['s_' . $changedsetting->name])) {
29 $unslashed['s_' . $changedsetting->name] = ''; // needed for checkboxes
31 $errors .= $changedsetting->write_setting($unslashed['s_' . $changedsetting->name]);
34 if ($olddbsessions != !empty($CFG->dbsessions)) {
35 require_logout();
38 if (empty($errors)) {
39 $statusmsg = get_string('changessaved');
40 } else {
41 $statusmsg = get_string('errorwithsettings', 'admin') . ' <br />' . $errors;
43 } else {
44 error(get_string('confirmsesskeybad', 'error'));
46 // now update $SITE - it might have been changed
47 $SITE = get_record('course', 'id', $SITE->id);
48 $COURSE = clone($SITE);
51 // and finally, if we get here, then there are matching settings and we have to print a form
52 // to modify them
53 admin_externalpage_print_header($adminroot);
55 if ($statusmsg != '') {
56 notify ($statusmsg);
59 $resultshtml = search_settings_html(admin_get_root(), $query);
61 echo '<form action="search.php" method="post" id="adminsettings">';
62 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
63 echo '<input type="hidden" name="query" value="' . s($query) . '" />';
64 echo '<fieldset>';
65 echo '<div class="clearer"><!-- --></div>';
66 if ($resultshtml != '') {
67 echo $resultshtml;
68 echo '<div class="form-buttons"><input class="form-submit" type="submit" value="' . get_string('savechanges','admin') . '" /></div>';
69 } else {
70 echo get_string('noresults','admin');
72 echo '</fieldset>';
73 echo '</form>';
75 admin_externalpage_print_footer($adminroot);
78 /**
79 * Find settings using a query.
81 * @param string &$node The node at which to start searching. Should be $ADMIN for all external calls to this function.
82 * @param string $query The search string.
83 * @return array An array containing admin_setting objects that match $query.
85 function search_settings(&$node, $query) {
87 if (is_a($node, 'admin_category')) {
88 $return = array();
89 $entries = array_keys($node->children);
90 foreach ($entries as $entry) {
91 $return = array_merge($return, search_settings($node->children[$entry], $query));
93 return $return;
96 if (is_a($node, 'admin_settingpage')) {
97 $return = array();
98 foreach ($node->settings as $setting) {
99 if (stristr($setting->name,$query) || stristr($setting->visiblename,$query) || stristr($setting->description,$query)) {
100 $return[] =& $setting;
102 unset($setting); // needed to prevent odd (imho) reference behaviour
103 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
105 return $return;
108 return array();
112 function search_settings_html(&$node, $query) {
114 global $CFG;
115 if (is_a($node, 'admin_category')) {
116 $entries = array_keys($node->children);
117 $return = '';
118 foreach ($entries as $entry) {
119 $return .= search_settings_html($node->children[$entry], $query);
121 return $return;
124 if (is_a($node, 'admin_settingpage')) {
125 $foundsettings = array();
126 foreach ($node->settings as $setting) {
127 if (stristr($setting->name,$query) || stristr($setting->visiblename,$query) || stristr($setting->description,$query)) {
128 $foundsettings[] =& $setting;
130 unset($setting); // needed to prevent odd (imho) reference behaviour
131 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
133 $return = '';
134 if (count($foundsettings) > 0) {
135 $return .= print_heading(get_string('searchresults','admin').' - '. '<a href="' . $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=' . $node->name . '">' . $node->visiblename . '</a>', '', 2, 'main', true);
136 $return .= '<fieldset class="adminsettings">' . "\n";
137 foreach ($foundsettings as $foundsetting) {
138 $return .= '<div class="clearer"><!-- --></div>' . "\n";
139 $return .= highlight($query,$foundsetting->output_html());
141 $return .= '</fieldset>';
143 return $return;
146 return '';