/[drupal]/contributions/modules/title_rewrite/title_rewrite_forms.inc
ViewVC logotype

Contents of /contributions/modules/title_rewrite/title_rewrite_forms.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Thu Aug 7 18:13:13 2008 UTC (15 months, 3 weeks ago) by shannonlucas
Branch: MAIN
CVS Tags: DRUPAL-5--1-2, HEAD
Changes since 1.1: +15 -99 lines
File MIME type: text/x-php
[#290465] addressed default character set and collation for table. [#271653] Delete form now uses confirm_form()
1 <?php
2 // $Id: title_rewrite_forms.inc,v 1.1 2008/04/07 15:37:55 shannonlucas Exp $
3
4 /**
5 * @file
6 * The form logic for the title rewrite module.
7 */
8
9
10 /**
11 * Build the settings form for the module.
12 *
13 * @return
14 * The form array.
15 */
16 function title_rewrite_admin_overview() {
17 $form = array();
18
19 $result = db_query('SELECT * from {title_rewrite} ORDER BY weight ASC');
20 $titles = array();
21 while ($row = db_fetch_array($result)) {
22 $id = $row['id'];
23
24 $form[$id]['id'] = array(
25 '#type' => 'value',
26 '#value' => $id,
27 );
28
29 $form[$id]['name'] = array('#value' => $row['name']);
30 $form[$id]['weight'] = array('#value' => $row['weight']);
31 $form[$id]['edit'] = array('#value' => l(t('Edit'), 'admin/settings/custom_title/edit/'. $id));
32 $form[$id]['delete'] = array('#value' => $default ? '' : l(t('Delete'), 'admin/settings/custom_title/delete/'. $id));
33 }
34
35 return $form;
36 }
37
38
39 /**
40 * Generate the token reference.
41 *
42 * @return
43 * The form data to display.
44 */
45 function title_rewrite_tokenref_form() {
46 $form = array();
47
48 $tokens = token_get_list();
49
50 // -------------------------------------------------------------------------
51 // The global tokens section
52 $form['global_tokens'] = array(
53 '#type' => 'fieldset',
54 '#title' => t('Global Tokens'),
55 '#collapsible' => TRUE,
56 '#weight' => -5,
57 '#description' => t('Global tokens are available site wide. They may be used for title rewrite rules that affect any page.'),
58 );
59
60 foreach($tokens['global'] as $token => $description) {
61 $form['global_tokens'][$token]['left'] = array(
62 '#type' => 'item',
63 '#value' => '[' . $token . ']',
64 );
65
66 $form['global_tokens'][$token]['right'] = array(
67 '#type' => 'item',
68 '#value' => $description,
69 );
70 }
71
72 // -------------------------------------------------------------------------
73 // The node tokens section
74 $form['node_tokens'] = array(
75 '#type' => 'fieldset',
76 '#title' => t('Node Tokens'),
77 '#collapsible' => TRUE,
78 '#weight' => -3,
79 '#description' => t('Node tokens are available for page-level node presentation.'),
80 );
81
82 foreach($tokens['node'] as $token => $description) {
83 $form['node_tokens'][$token]['left'] = array(
84 '#type' => 'item',
85 '#value' => '[' . $token . ']',
86 );
87
88 $form['node_tokens'][$token]['right'] = array(
89 '#type' => 'item',
90 '#value' => $description,
91 );
92 }
93
94 return $form;
95 }
96
97 /**
98 * Generate the form for creating a new custom title rule or editing an
99 * existing one.
100 *
101 * @param $id
102 * The ID of an existing title pattern to edit. If specified, this title pattern
103 * will be used to populate the form fields.
104 *
105 * @return
106 * The form contents.
107 */
108 function title_rewrite_create_form($id = 0) {
109 $form = array();
110
111 $existing = ($id > 0) ? _title_rewrite_load_instance($id) : NULL;
112
113 $form['rewrite_rule'] = array(
114 '#type' => 'fieldset',
115 '#title' => ($existing !== NULL) ? t('Edit Existing Title Rewrite Rule')
116 : t('Create New Title Rewrite Rule'),
117 '#collapsible' => FALSE,
118 '#weight' => -5,
119 );
120
121 $form['rewrite_rule']['pattern_id'] = array(
122 '#type' => 'value',
123 '#value' => ($existing !== NULL) ? $existing['id'] : -1,
124 );
125
126 $form['rewrite_rule']['name'] = array(
127 '#type' => 'textfield',
128 '#title' => t('Name'),
129 '#default_value' => ($existing !== NULL) ? $existing['name'] : '',
130 '#description' => t('A unique name to help you remember what this rewrite rule does when it\'s shown in the list view.'),
131 );
132
133 $weights = 0;
134
135 $form['rewrite_rule']['weight'] = array(
136 '#type' => 'select',
137 '#title' => t('Weight'),
138 '#default_value' => ($existing !== NULL) ? $existing['weight'] : 0,
139 '#options' => drupal_map_assoc(range(-10, 10)),
140 '#description' => t('This determines the order that rules are evaluated. Rules with lower weight values are evaluated before rules with higher weights.'),
141 );
142
143 $description = t('This text will be used to replace the contents of the HTML %title element on pages matching the paths below. You may include any of the tokens specified on the !ref_page in this field.',
144 array(
145 '%title' => '<title>',
146 '!ref_page' => l(t('Token Reference Page'), 'admin/settings/custom_title/tokens')
147 )
148 );
149
150 $form['rewrite_rule']['title'] = array(
151 '#type' => 'textfield',
152 '#title' => t('Rewrite Text'),
153 '#default_value' => ($existing !== NULL) ? $existing['token_title'] : '',
154 '#description' => $description,
155 );
156
157 $options = array(
158 1 => t('Apply rewrite only the listed pages.'),
159 2 => t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).'),
160 );
161
162 $form['rewrite_rule']['type'] = array(
163 '#type' => 'radios',
164 '#title' => t('Rule Application'),
165 '#default_value' => ($existing !== NULL) ? $existing['type'] : 1,
166 '#options' => $options,
167 '#description' => t('Determines the method used apply the re-write rule.'),
168 );
169
170 $description = t('Enter one page per line as Drupal paths. The "*" character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page. If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.',
171 array('%blog' => 'blog',
172 '%blog-wildcard' => 'blog/*',
173 '%front' => '<front>',
174 '%php' => '<?php ?>')
175 );
176
177 $form['rewrite_rule']['paths'] = array(
178 '#type' => 'textarea',
179 '#title' => t('Path(s)'),
180 '#default_value' => ($existing !== NULL) ? $existing['path_expr'] : '',
181 '#description' => $description,
182 );
183
184 $form['submit'] = array(
185 '#type' => 'submit',
186 '#value' => t('Save Rewrite Rule'),
187 );
188
189 $form['cancel'] = array(
190 '#type' => 'submit',
191 '#value' => t('Cancel'),
192 );
193
194 return $form;
195 }
196
197
198 /**
199 * Validate the pattern editing/creation form.
200 *
201 * @param $form_id
202 * The ID of the form.
203 * @param $form_values
204 * The form values.
205 */
206 function title_rewrite_create_form_validate($form_id, $form_values) {
207 // Make sure that the name is unique.
208 $name = trim($form_values['name']);
209 $pid = $form_values['pattern_id'];
210
211 $count = db_num_rows(db_query('SELECT name FROM {title_rewrite} WHERE id != %d AND name = \'%s\'', $pid, $name));
212
213 if ($count > 0) {
214 form_set_error('name', t('Please ensure that each pattern name is unique.'));
215 }
216 }
217
218
219 /**
220 * Save the data from the pattern editing/creation form.
221 *
222 * @param $form_id
223 * The ID of the form.
224 * @param $form_values
225 * The form values.
226 */
227 function title_rewrite_create_form_submit($form_id, $form_values) {
228 $pid = $form_values['pattern_id'];
229 $name = trim($form_values['name']);
230 $token_title = trim($form_values['title']);
231 $path_expr = trim($form_values['paths']);
232 $weight = intval($form_values['weight']);
233 $type = intval($form_values['type']);
234
235 // -------------------------------------------------------------------------
236 // Bail out on cancel.
237 if ($form_values['op'] == t('Cancel')) {
238 return 'admin/settings/custom_title';
239 }
240
241 // -------------------------------------------------------------------------
242 // Existing (if) or new (else)?
243 if ($pid > -1) {
244 db_query('UPDATE {title_rewrite} SET name = \'%s\', path_expr = \'%s\',
245 token_title = \'%s\', weight = %d, type = %d
246 WHERE id = %d',
247 $name, $path_expr, $token_title, $weight, $type, $pid);
248 drupal_set_message(t('Updated title pattern \'@name\'', array('@name' => $name)));
249 }
250 else {
251 db_query('INSERT INTO {title_rewrite} (name, path_expr, token_title, weight, type)
252 VALUES (\'%s\', \'%s\', \'%s\', %d, %d)',
253 $name, $path_expr, $token_title, $weight, $type);
254 drupal_set_message(t('Created custom title pattern \'@name\'', array('@name' => $name)));
255 }
256
257 return 'admin/settings/custom_title';
258 }
259
260
261 /**
262 * Generate the confirmation form for deleting a title.
263 *
264 * @param $id
265 * The ID of the form to delete.
266 *
267 * @return
268 * The form elements.
269 */
270 function title_rewrite_delete_form($id) {
271 $form = array();
272
273 $existing = _title_rewrite_load_instance($id);
274
275 $form['pattern_id'] = array(
276 '#type' => 'value',
277 '#value' => $id,
278 );
279
280 $question = '<p>' .
281 t('You are about to remove the title re-write rule "%name".',
282 array('%name' => $existing['name']));
283 $desc = '<p>' . t('This rule matches paths using the pattern !pat and creates a title using the token pattern !token',
284 array('!pat' => '"<em>' . $existing['path_expr'] . '</em>"',
285 '!token' => '"<em>' . $existing['token_title'] . '</em>"')) .
286 '</p><p>' . t('This action cannot be undone.') . '</p>';
287
288 return confirm_form($form, $question, 'admin/settings/custom_title', $desc, t('Delete'), t('Cancel'));
289 }
290
291
292 /**
293 * Handle the delete form submission.
294 *
295 * @param $form_id
296 * The form's ID
297 * @param $form_values
298 * The form values.
299 *
300 * @return
301 * The path to send the browser to after the form is submitted.
302 */
303 function title_rewrite_delete_form_submit($form_id, $form_values) {
304 $id = $form_values['pattern_id'];
305
306 db_query('DELETE FROM {title_rewrite} WHERE id = %d', $id);
307 drupal_set_message("Custom title rule deleted.");
308
309 return 'admin/settings/custom_title';
310 }
311
312
313 /**
314 * Theme the table in the admin overview form.
315 *
316 * @param $form
317 * The form elements to theme.
318 *
319 * @return
320 * The rendered form elements.
321 */
322 function theme_title_rewrite_admin_overview($form) {
323 $rows = array();
324 foreach ($form as $name => $element) {
325 if (isset($element['name']) && is_array($element['name'])) {
326 $rows[] = array(
327 drupal_render($element['name']),
328 drupal_render($element['weight']),
329 drupal_render($element['edit']),
330 drupal_render($element['delete'])
331 );
332 unset($form[$name]);
333 }
334 }
335 $header = array(t('Name'), t('Weight'), t('Edit'), t('Delete'));
336 $output = theme('table', $header, $rows);
337 $output .= drupal_render($form);
338
339 return $output;
340 }
341
342
343 /**
344 * Theme the token reference form.
345 *
346 * @param $form
347 * The token reference form.
348 *
349 * @return
350 * The themed form.
351 */
352 function theme_title_rewrite_tokenref_form($form) {
353 // -------------------------------------------------------------------------
354 // The global tokens.
355 $rows = array();
356 foreach ($form['global_tokens'] as $name => $element) {
357 if (isset($element['left']) && is_array($element['left'])) {
358 $rows[] = array(
359 array(
360 'data' => drupal_render($element['left']),
361 'class' => 'title-rewrite-ref-left'
362 ),
363 array(
364 'data' => drupal_render($element['right']),
365 'class' => 'title-rewrite-ref-right'
366 ),
367 );
368
369 unset($form['global_tokens'][$name]);
370 }
371 }
372
373 $form['global_tokens']['tokens'] = array(
374 '#value' => theme('table', array(), $rows, array('class' => 'title-rewrite-token-table')),
375 );
376
377 // -------------------------------------------------------------------------
378 // The node tokens.
379 $rows = array();
380 foreach ($form['node_tokens'] as $name => $element) {
381 if (isset($element['left']) && is_array($element['left'])) {
382 $rows[] = array(
383 array(
384 'data' => drupal_render($element['left']),
385 'class' => 'title-rewrite-ref-left'
386 ),
387 array(
388 'data' => drupal_render($element['right']),
389 'class' => 'title-rewrite-ref-right'
390 ),
391 );
392
393 unset($form['node_tokens'][$name]);
394 }
395 }
396
397 $form['node_tokens']['tokens'] = array(
398 '#value' => theme('table', array(), $rows, array('class' => 'title-rewrite-token-table')),
399 );
400
401 return drupal_render($form);
402 }
403

  ViewVC Help
Powered by ViewVC 1.1.2