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
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);
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
)) {
39 $statusmsg = get_string('changessaved');
41 $statusmsg = get_string('errorwithsettings', 'admin') . ' <br />' . $errors;
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
53 admin_externalpage_print_header($adminroot);
55 if ($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) . '" />';
65 echo '<div class="clearer"><!-- --></div>';
66 if ($resultshtml != '') {
68 echo '<div class="form-buttons"><input class="form-submit" type="submit" value="' . get_string('savechanges','admin') . '" /></div>';
70 echo get_string('noresults','admin');
75 admin_externalpage_print_footer($adminroot);
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')) {
89 $entries = array_keys($node->children
);
90 foreach ($entries as $entry) {
91 $return = array_merge($return, search_settings($node->children
[$entry], $query));
96 if (is_a($node, 'admin_settingpage')) {
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
112 function search_settings_html(&$node, $query) {
115 if (is_a($node, 'admin_category')) {
116 $entries = array_keys($node->children
);
118 foreach ($entries as $entry) {
119 $return .= search_settings_html($node->children
[$entry], $query);
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
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>';