adding some strings
[moodle-linuxchix.git] / admin / search.php
blobe94f8ef694805994b08e64b0a18d55cc1b1fea2d
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 admin_externalpage_setup('search'); // now hidden page
12 $CFG->adminsearchquery = $query; // So we can reference it in search boxes later in this invocation
15 // now we'll deal with the case that the admin has submitted the form with changed settings
17 $statusmsg = '';
19 if ($data = data_submitted()) {
20 $unslashed = (array)stripslashes_recursive($data);
21 if (confirm_sesskey()) {
22 $olddbsessions = !empty($CFG->dbsessions);
23 $changedsettings = search_settings(admin_get_root(), $query);
24 $errors = '';
26 foreach($changedsettings as $changedsetting) {
27 if (!isset($unslashed['s_' . $changedsetting->name])) {
28 $unslashed['s_' . $changedsetting->name] = ''; // needed for checkboxes
30 $errors .= $changedsetting->write_setting($unslashed['s_' . $changedsetting->name]);
33 if ($olddbsessions != !empty($CFG->dbsessions)) {
34 require_logout();
37 if (empty($errors)) {
38 $statusmsg = get_string('changessaved');
39 } else {
40 $statusmsg = get_string('errorwithsettings', 'admin') . ' <br />' . $errors;
42 } else {
43 error(get_string('confirmsesskeybad', 'error'));
45 // now update $SITE - it might have been changed
46 $SITE = get_record('course', 'id', $SITE->id);
47 $COURSE = clone($SITE);
50 // and finally, if we get here, then there are matching settings and we have to print a form
51 // to modify them
52 admin_externalpage_print_header();
54 if ($statusmsg != '') {
55 notify ($statusmsg);
58 $resultshtml = search_settings_html(admin_get_root(), $query);
60 echo '<form action="search.php" method="post" id="adminsettings">';
61 echo '<div>';
62 echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
63 echo '<input type="hidden" name="query" value="' . s($query) . '" />';
64 echo '</div>';
65 echo '<fieldset>';
66 echo '<div class="clearer"><!-- --></div>';
67 if ($resultshtml != '') {
68 echo $resultshtml;
69 echo '<div class="form-buttons"><input class="form-submit" type="submit" value="' . get_string('savechanges','admin') . '" /></div>';
70 } else {
71 echo get_string('noresults','admin');
73 echo '</fieldset>';
74 echo '</form>';
76 admin_externalpage_print_footer();
79 /**
80 * Find settings using a query.
82 * @param string &$node The node at which to start searching. Should be $ADMIN for all external calls to this function.
83 * @param string $query The search string.
84 * @return array An array containing admin_setting objects that match $query.
86 function search_settings(&$node, $query) {
88 if (is_a($node, 'admin_category')) {
89 $return = array();
90 $entries = array_keys($node->children);
91 foreach ($entries as $entry) {
92 $return = array_merge($return, search_settings($node->children[$entry], $query));
94 return $return;
97 if (is_a($node, 'admin_settingpage')) {
98 $return = array();
99 foreach ($node->settings as $setting) {
100 if (stristr($setting->name,$query) || stristr($setting->visiblename,$query) || stristr($setting->description,$query)) {
101 $return[] =& $setting;
103 unset($setting); // needed to prevent odd (imho) reference behaviour
104 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
106 return $return;
109 return array();
113 function search_settings_html(&$node, $query) {
115 global $CFG;
117 if ($query == ''){
118 return '';
121 if (is_a($node, 'admin_category')) {
122 $entries = array_keys($node->children);
123 $return = '';
124 foreach ($entries as $entry) {
125 $return .= search_settings_html($node->children[$entry], $query);
127 return $return;
130 if (is_a($node, 'admin_settingpage')) {
131 $foundsettings = array();
132 foreach ($node->settings as $setting) {
133 if (stristr($setting->name,$query) || stristr($setting->visiblename,$query) || stristr($setting->description,$query)) {
134 $foundsettings[] =& $setting;
136 unset($setting); // needed to prevent odd (imho) reference behaviour
137 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
139 $return = '';
140 if (count($foundsettings) > 0) {
141 $return .= print_heading(get_string('searchresults','admin').' - '. '<a href="' . $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=' . $node->name . '">' . $node->visiblename . '</a>', '', 2, 'main', true);
142 $return .= '<fieldset class="adminsettings">' . "\n";
143 foreach ($foundsettings as $foundsetting) {
144 $return .= '<div class="clearer"><!-- --></div>' . "\n";
145 $return .= highlight($query,$foundsetting->output_html());
147 $return .= '</fieldset>';
149 return $return;
152 return '';