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
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);
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
)) {
38 $statusmsg = get_string('changessaved');
40 $statusmsg = get_string('errorwithsettings', 'admin') . ' <br />' . $errors;
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
52 admin_externalpage_print_header();
54 if ($statusmsg != '') {
58 $resultshtml = search_settings_html(admin_get_root(), $query);
60 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) . '" />';
66 echo '<div class="clearer"><!-- --></div>';
67 if ($resultshtml != '') {
69 echo '<div class="form-buttons"><input class="form-submit" type="submit" value="' . get_string('savechanges','admin') . '" /></div>';
71 echo get_string('noresults','admin');
76 admin_externalpage_print_footer();
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')) {
90 $entries = array_keys($node->children
);
91 foreach ($entries as $entry) {
92 $return = array_merge($return, search_settings($node->children
[$entry], $query));
97 if (is_a($node, 'admin_settingpage')) {
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
113 function search_settings_html(&$node, $query) {
121 if (is_a($node, 'admin_category')) {
122 $entries = array_keys($node->children
);
124 foreach ($entries as $entry) {
125 $return .= search_settings_html($node->children
[$entry], $query);
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
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>';