forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
156 lines (125 loc) · 5.09 KB
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php // $Id$
// searches for admin settings
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
$query = trim(stripslashes_safe(required_param('query', PARAM_NOTAGS))); // Search string
admin_externalpage_setup('search'); // now hidden page
$CFG->adminsearchquery = $query; // So we can reference it in search boxes later in this invocation
// now we'll deal with the case that the admin has submitted the form with changed settings
$statusmsg = '';
if ($data = data_submitted()) {
$unslashed = (array)stripslashes_recursive($data);
if (confirm_sesskey()) {
$olddbsessions = !empty($CFG->dbsessions);
$changedsettings = search_settings(admin_get_root(), $query);
$errors = '';
foreach($changedsettings as $changedsetting) {
if (!isset($unslashed['s_' . $changedsetting->name])) {
$unslashed['s_' . $changedsetting->name] = ''; // needed for checkboxes
}
$errors .= $changedsetting->write_setting($unslashed['s_' . $changedsetting->name]);
}
if ($olddbsessions != !empty($CFG->dbsessions)) {
require_logout();
}
if (empty($errors)) {
$statusmsg = get_string('changessaved');
} else {
$statusmsg = get_string('errorwithsettings', 'admin') . ' <br />' . $errors;
}
} else {
error(get_string('confirmsesskeybad', 'error'));
}
// now update $SITE - it might have been changed
$SITE = get_record('course', 'id', $SITE->id);
$COURSE = clone($SITE);
}
// and finally, if we get here, then there are matching settings and we have to print a form
// to modify them
admin_externalpage_print_header();
if ($statusmsg != '') {
notify ($statusmsg);
}
$resultshtml = search_settings_html(admin_get_root(), $query);
echo '<form action="search.php" method="post" id="adminsettings">';
echo '<div>';
echo '<input type="hidden" name="sesskey" value="' . sesskey() . '" />';
echo '<input type="hidden" name="query" value="' . s($query) . '" />';
echo '</div>';
echo '<fieldset>';
echo '<div class="clearer"><!-- --></div>';
if ($resultshtml != '') {
echo $resultshtml;
echo '<div class="form-buttons"><input class="form-submit" type="submit" value="' . get_string('savechanges','admin') . '" /></div>';
} else {
echo get_string('noresults','admin');
}
echo '</fieldset>';
echo '</form>';
admin_externalpage_print_footer();
/**
* Find settings using a query.
*
* @param string &$node The node at which to start searching. Should be $ADMIN for all external calls to this function.
* @param string $query The search string.
* @return array An array containing admin_setting objects that match $query.
*/
function search_settings(&$node, $query) {
if (is_a($node, 'admin_category')) {
$return = array();
$entries = array_keys($node->children);
foreach ($entries as $entry) {
$return = array_merge($return, search_settings($node->children[$entry], $query));
}
return $return;
}
if (is_a($node, 'admin_settingpage')) {
$return = array();
foreach ($node->settings as $setting) {
if (stristr($setting->name,$query) || stristr($setting->visiblename,$query) || stristr($setting->description,$query)) {
$return[] =& $setting;
}
unset($setting); // needed to prevent odd (imho) reference behaviour
// see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
}
return $return;
}
return array();
}
function search_settings_html(&$node, $query) {
global $CFG;
if ($query == ''){
return '';
}
if (is_a($node, 'admin_category')) {
$entries = array_keys($node->children);
$return = '';
foreach ($entries as $entry) {
$return .= search_settings_html($node->children[$entry], $query);
}
return $return;
}
if (is_a($node, 'admin_settingpage')) {
$foundsettings = array();
foreach ($node->settings as $setting) {
if (stristr($setting->name,$query) || stristr($setting->visiblename,$query) || stristr($setting->description,$query)) {
$foundsettings[] =& $setting;
}
unset($setting); // needed to prevent odd (imho) reference behaviour
// see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
}
$return = '';
if (count($foundsettings) > 0) {
$return .= print_heading(get_string('searchresults','admin').' - '. '<a href="' . $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=' . $node->name . '">' . $node->visiblename . '</a>', '', 2, 'main', true);
$return .= '<fieldset class="adminsettings">' . "\n";
foreach ($foundsettings as $foundsetting) {
$return .= '<div class="clearer"><!-- --></div>' . "\n";
$return .= highlight($query,$foundsetting->output_html());
}
$return .= '</fieldset>';
}
return $return;
}
return '';
}
?>