1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Administrative page callbacks for the path module. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Return a listing of all defined URL aliases. |
---|
10 | * When filter key passed, perform a standard search on the given key, |
---|
11 | * and return the list of matching URL aliases. |
---|
12 | */ |
---|
13 | function path_admin_overview($keys = NULL) { |
---|
14 | // Add the filter form above the overview table. |
---|
15 | $output = drupal_get_form('path_admin_filter_form', $keys); |
---|
16 | // Enable language column if locale is enabled or if we have any alias with language |
---|
17 | $count = db_result(db_query("SELECT COUNT(*) FROM {url_alias} WHERE language != ''")); |
---|
18 | $multilanguage = (module_exists('locale') || $count); |
---|
19 | |
---|
20 | if ($keys) { |
---|
21 | // Replace wildcards with MySQL/PostgreSQL wildcards. |
---|
22 | $keys = preg_replace('!\*+!', '%', $keys); |
---|
23 | $sql = "SELECT * FROM {url_alias} WHERE dst LIKE '%%%s%%'"; |
---|
24 | } |
---|
25 | else { |
---|
26 | $sql = 'SELECT * FROM {url_alias}'; |
---|
27 | } |
---|
28 | $header = array( |
---|
29 | array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'), |
---|
30 | array('data' => t('System'), 'field' => 'src'), |
---|
31 | array('data' => t('Operations'), 'colspan' => '2') |
---|
32 | ); |
---|
33 | if ($multilanguage) { |
---|
34 | $header[3] = $header[2]; |
---|
35 | $header[2] = array('data' => t('Language'), 'field' => 'language'); |
---|
36 | } |
---|
37 | $sql .= tablesort_sql($header); |
---|
38 | $result = pager_query($sql, 50, 0 , NULL, $keys); |
---|
39 | |
---|
40 | $rows = array(); |
---|
41 | $destination = drupal_get_destination(); |
---|
42 | while ($data = db_fetch_object($result)) { |
---|
43 | $row = array(check_plain($data->dst), check_plain($data->src), l(t('edit'), "admin/build/path/edit/$data->pid", array('query' => $destination)), l(t('delete'), "admin/build/path/delete/$data->pid", array('query' => $destination))); |
---|
44 | if ($multilanguage) { |
---|
45 | $row[4] = $row[3]; |
---|
46 | $row[3] = $row[2]; |
---|
47 | $row[2] = module_invoke('locale', 'language_name', $data->language); |
---|
48 | } |
---|
49 | $rows[] = $row; |
---|
50 | } |
---|
51 | |
---|
52 | if (empty($rows)) { |
---|
53 | $empty_message = $keys ? t('No URL aliases found.') : t('No URL aliases available.') ; |
---|
54 | $rows[] = array(array('data' => $empty_message, 'colspan' => ($multilanguage ? 5 : 4))); |
---|
55 | } |
---|
56 | |
---|
57 | $output .= theme('table', $header, $rows); |
---|
58 | $output .= theme('pager', NULL, 50, 0); |
---|
59 | |
---|
60 | return $output; |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Menu callback; handles pages for creating and editing URL aliases. |
---|
65 | */ |
---|
66 | function path_admin_edit($pid = 0) { |
---|
67 | if ($pid) { |
---|
68 | $alias = path_load($pid); |
---|
69 | drupal_set_title(check_plain($alias['dst'])); |
---|
70 | $output = drupal_get_form('path_admin_form', $alias); |
---|
71 | } |
---|
72 | else { |
---|
73 | $output = drupal_get_form('path_admin_form'); |
---|
74 | } |
---|
75 | |
---|
76 | return $output; |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Return a form for editing or creating an individual URL alias. |
---|
81 | * |
---|
82 | * @ingroup forms |
---|
83 | * @see path_admin_form_validate() |
---|
84 | * @see path_admin_form_submit() |
---|
85 | */ |
---|
86 | function path_admin_form(&$form_state, $edit = array('src' => '', 'dst' => '', 'language' => '', 'pid' => NULL)) { |
---|
87 | |
---|
88 | $form['#alias'] = $edit; |
---|
89 | |
---|
90 | $form['src'] = array( |
---|
91 | '#type' => 'textfield', |
---|
92 | '#title' => t('Existing system path'), |
---|
93 | '#default_value' => $edit['src'], |
---|
94 | '#maxlength' => 128, |
---|
95 | '#size' => 45, |
---|
96 | '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'), |
---|
97 | '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='), |
---|
98 | '#required' => TRUE, |
---|
99 | ); |
---|
100 | $form['dst'] = array( |
---|
101 | '#type' => 'textfield', |
---|
102 | '#title' => t('Path alias'), |
---|
103 | '#default_value' => $edit['dst'], |
---|
104 | '#maxlength' => 128, |
---|
105 | '#size' => 45, |
---|
106 | '#description' => t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'), |
---|
107 | '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='), |
---|
108 | '#required' => TRUE, |
---|
109 | ); |
---|
110 | // This will be a hidden value unless locale module is enabled |
---|
111 | $form['language'] = array( |
---|
112 | '#type' => 'value', |
---|
113 | '#value' => $edit['language'] |
---|
114 | ); |
---|
115 | if ($edit['pid']) { |
---|
116 | $form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']); |
---|
117 | $form['submit'] = array('#type' => 'submit', '#value' => t('Update alias')); |
---|
118 | } |
---|
119 | else { |
---|
120 | $form['submit'] = array('#type' => 'submit', '#value' => t('Create new alias')); |
---|
121 | } |
---|
122 | |
---|
123 | return $form; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | /** |
---|
128 | * Verify that a new URL alias is valid |
---|
129 | */ |
---|
130 | function path_admin_form_validate($form, &$form_state) { |
---|
131 | $src = $form_state['values']['src']; |
---|
132 | $dst = $form_state['values']['dst']; |
---|
133 | $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0; |
---|
134 | // Language is only set if locale module is enabled, otherwise save for all languages. |
---|
135 | $language = isset($form_state['values']['language']) ? $form_state['values']['language'] : ''; |
---|
136 | |
---|
137 | if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != %d AND dst = '%s' AND language = '%s'", $pid, $dst, $language))) { |
---|
138 | form_set_error('dst', t('The alias %alias is already in use in this language.', array('%alias' => $dst))); |
---|
139 | } |
---|
140 | $item = menu_get_item($src); |
---|
141 | if (!$item || !$item['access']) { |
---|
142 | form_set_error('src', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $src))); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Save a new URL alias to the database. |
---|
148 | */ |
---|
149 | function path_admin_form_submit($form, &$form_state) { |
---|
150 | // Language is only set if locale module is enabled |
---|
151 | path_set_alias($form_state['values']['src'], $form_state['values']['dst'], isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0, isset($form_state['values']['language']) ? $form_state['values']['language'] : ''); |
---|
152 | |
---|
153 | drupal_set_message(t('The alias has been saved.')); |
---|
154 | $form_state['redirect'] = 'admin/build/path'; |
---|
155 | return; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Menu callback; confirms deleting an URL alias |
---|
160 | */ |
---|
161 | function path_admin_delete_confirm($form_state, $pid) { |
---|
162 | $path = path_load($pid); |
---|
163 | if (user_access('administer url aliases')) { |
---|
164 | $form['pid'] = array('#type' => 'value', '#value' => $pid); |
---|
165 | $output = confirm_form($form, |
---|
166 | t('Are you sure you want to delete path alias %title?', array('%title' => $path['dst'])), |
---|
167 | isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/path'); |
---|
168 | } |
---|
169 | return $output; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Execute URL alias deletion |
---|
174 | */ |
---|
175 | function path_admin_delete_confirm_submit($form, &$form_state) { |
---|
176 | if ($form_state['values']['confirm']) { |
---|
177 | path_admin_delete($form_state['values']['pid']); |
---|
178 | $form_state['redirect'] = 'admin/build/path'; |
---|
179 | return; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | /** |
---|
185 | * Return a form to filter URL aliases. |
---|
186 | * |
---|
187 | * @ingroup forms |
---|
188 | * @see path_admin_filter_form_submit() |
---|
189 | */ |
---|
190 | function path_admin_filter_form(&$form_state, $keys = '') { |
---|
191 | $form['#attributes'] = array('class' => 'search-form'); |
---|
192 | $form['basic'] = array('#type' => 'fieldset', |
---|
193 | '#title' => t('Filter aliases') |
---|
194 | ); |
---|
195 | $form['basic']['inline'] = array('#prefix' => '<div class="container-inline">', '#suffix' => '</div>'); |
---|
196 | $form['basic']['inline']['filter'] = array( |
---|
197 | '#type' => 'textfield', |
---|
198 | '#title' => '', |
---|
199 | '#default_value' => $keys, |
---|
200 | '#maxlength' => 128, |
---|
201 | '#size' => 25, |
---|
202 | ); |
---|
203 | $form['basic']['inline']['submit'] = array( |
---|
204 | '#type' => 'submit', |
---|
205 | '#value' => t('Filter'), |
---|
206 | '#submit' => array('path_admin_filter_form_submit_filter'), |
---|
207 | ); |
---|
208 | if ($keys) { |
---|
209 | $form['basic']['inline']['reset'] = array( |
---|
210 | '#type' => 'submit', |
---|
211 | '#value' => t('Reset'), |
---|
212 | '#submit' => array('path_admin_filter_form_submit_reset'), |
---|
213 | ); |
---|
214 | } |
---|
215 | return $form; |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * Process filter form submission when the Filter button is pressed. |
---|
220 | */ |
---|
221 | function path_admin_filter_form_submit_filter($form, &$form_state) { |
---|
222 | $form_state['redirect'] = 'admin/build/path/list/'. trim($form_state['values']['filter']); |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * Process filter form submission when the Reset button is pressed. |
---|
227 | */ |
---|
228 | function path_admin_filter_form_submit_reset($form, &$form_state) { |
---|
229 | $form_state['redirect'] = 'admin/build/path/list'; |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | /** |
---|
234 | * Helper function for grabbing filter keys. |
---|
235 | */ |
---|
236 | function path_admin_filter_get_keys() { |
---|
237 | // Extract keys as remainder of path |
---|
238 | $path = explode('/', $_GET['q'], 5); |
---|
239 | return count($path) == 5 ? $path[4] : ''; |
---|
240 | } |
---|