/[drupal]/contributions/modules/pageroute/pageroute_ui.forms.inc
ViewVC logotype

Contents of /contributions/modules/pageroute/pageroute_ui.forms.inc

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


Revision 1.1 - (show annotations) (download) (as text)
Sat Sep 13 13:45:09 2008 UTC (14 months, 2 weeks ago) by tauran
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
File MIME type: text/x-php
Initial commit of pageroute d6 dev version.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Adminstration forms, included from pageroute_ui.module
7 */
8
9 function pageroute_ui_route_edit($form_state, $op = 'add', $prid = NULL) {
10 if ($op != 'add' && isset($prid) && is_numeric($prid)) {
11 $route = db_fetch_object(db_query("SELECT * FROM {pageroute_routes} WHERE prid = %d", $prid));
12 $route->options = unserialize($route->options);
13 drupal_set_title(t('Edit route'));
14 }
15
16 $form['path'] = array('#type' => 'textfield',
17 '#title' => t('Path'),
18 '#maxlength' => 127,
19 '#default_value' => isset($route) ? $route->path : '',
20 '#required' => TRUE,
21 '#weight' => -5,
22 );
23 $form['options']['#tree'] = TRUE;
24 $form['options']['tabs'] = array(
25 '#type' => 'radios',
26 '#title' => t('Tabs'),
27 '#options' => array(
28 0 => t('Don\'t show any tabs'),
29 PAGEROUTE_MENU_TABS => t('Use the common drupal menu tabs'),
30 PAGEROUTE_BUTTON_TABS => t('Show submit-like tab buttons above the page content'),
31 // PAGEROUTE_HINT_TABS => t('Show tabs that cannot be clicked')
32 ),
33 '#default_value' => isset($route->options['tabs']) ? $route->options['tabs'] : 0,
34 '#description' => t('Note that the commom drupal menu tabs won\'t save the actual form, if they are used. Also any arguments appended to the URL will be lost. They are in particular useful for pageroutes, which focus on displaying content.'),
35 '#weight' => -1,
36 );
37 $form['options']['access'] = array(
38 '#type' => 'fieldset',
39 '#title' => t('Access control'),
40 '#collapsible' => TRUE,
41 );
42 $form['options']['access']['allowed_roles'] = array(
43 '#type' => 'checkboxes',
44 '#title' => t('Permit access to the pageroute for only this roles'),
45 '#options' => user_roles(),
46 '#default_value' => isset($route->options['access']['allowed_roles']) ? $route->options['access']['allowed_roles'] : array(2),
47 );
48 $form['options']['access']['#weight'] = 5;
49
50 $form['advanced'] = array(
51 '#type' => 'fieldset',
52 '#title' => t('Advanced settings'),
53 '#collapsible' => TRUE,
54 '#collapsed' => TRUE,
55 '#tree' => FALSE,
56 '#weight' => 10,
57 );
58 $form['advanced']['options']['#tree'] = TRUE;
59 $form['advanced']['options']['redirect_path'] = array(
60 '#type' => 'textfield',
61 '#title' => t('Customized redirect path'),
62 '#default_value' => isset($route) ? $route->options['redirect_path'] : '',
63 '#maxlength' => 64,
64 '#size' => 45,
65 '#description' => t('If entered, a user will be redirected to this path after he has completed the pageroute. Specify an existing path. For example: node/28, user, taxonomy/term/1+2.') .' '.
66 t('You may also use the following replacement variables: !uid (User ID), !nid (Node ID). For example use: node/!nid to redirect to the node with the id taken from the pageroute arguments.'),
67 '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q=')
68 );
69 $form['advanced']['options']['no_messages'] = array(
70 '#type' => 'checkbox',
71 '#title' => t('Don\'t show drupal messages during this route.'),
72 '#default_value' => isset($route->options['no_messages']) ? $route->options['no_messages'] : 1,
73 );
74
75 /*
76 if (module_exists('states')) {
77 $form['advanced']['options']['track_user'] = array(
78 '#type' => 'checkbox',
79 '#title' => t('Verify that a user goes through each page of the route.'),
80 '#default_value' => $route->options['track_user'],
81 '#description' => t('If checked, pageroute verifies that a user can only reach the next page. '.
82 'To achieve this, it will track how far a user has ever gone through the route by using the states module.'),
83 );
84 }*/
85
86 $form['submit'] = array('#type' => 'submit',
87 '#value' => t('Save'),
88 '#weight' => 15,
89 );
90 if (isset($route)) {
91 $form['delete'] = array('#type' => 'submit',
92 '#value' => t('Delete'),
93 '#submit' => array('pageroute_ui_route_delete_submit'),
94 '#weight' => 16,
95 );
96 }
97 else {
98 $route = (object) array('new' => TRUE);
99 }
100
101 $form['route'] = array('#type' => 'value', '#value' => &$route);
102
103 $form['#validate'][] = 'pageroute_ui_route_edit_validate';
104 $form['#submit'][] = 'pageroute_ui_route_edit_submit';
105 $form['#theme'] = 'pageroute_ui_route';
106
107 return $form;
108 }
109
110 function pageroute_ui_route_edit_validate($form, &$form_state) {
111 $path = rtrim(ltrim($form_state['values']['path'], '/'), '/');
112 $form_state['values']['path'] = $path;
113
114 if (!valid_url($path)) {
115 form_set_error('path', t('The path has to be a valid URL.'));
116 }
117
118 if (isset($form_state['values']['route']->path) &&
119 ($form_state['values']['route']->path != $path) &&
120 gettype(menu_get_item($path)) == 'array') {
121 form_set_error('path', t('This drupal path is already in use. Choose another path.'));
122 }
123 }
124
125 function pageroute_ui_route_delete_submit($form, &$form_state) {
126 $form_state['redirect'] = 'admin/build/pageroute/'. $form_state['values']['route']->prid .'/delete';
127 }
128
129 function pageroute_ui_route_edit_submit($form, &$form_state) {
130 dprint_r($form_state['values']['path']);
131 $record->path = $form_state['values']['path'];
132 $record->options = serialize($form_state['values']['options']);
133
134 if (!isset($form_state['values']['route']->new)) {
135 $record->prid = $form_state['values']['route']->prid;
136 drupal_write_record('pageroute_routes', $record, array('prid'));
137 }
138 else {
139 drupal_write_record('pageroute_routes', $record);
140 $form_state['values']['route']->prid = db_last_insert_id('pageroute_routes', 'prid');
141 }
142 /*
143 if (module_exists('states')) {
144 states_clear_machine_cache();
145 }*/
146 menu_rebuild();
147 _pageroute_clear_cache();
148
149 dprint_r(drupal_get_destination());
150
151 $form_state['redirect'] = array('admin/build/pageroute', drupal_get_destination());
152 }
153
154 function pageroute_ui_route_delete_confirm($form_state, $prid) {
155
156 if (is_numeric($prid)) {
157 $route = db_fetch_object(db_query("SELECT * FROM {pageroute_routes} WHERE prid = %d", $prid));
158 $route->options = unserialize($route->options);
159 }
160 if (!isset($route)) {
161 drupal_not_found();
162 exit;
163 }
164
165 $form['route'] = array('#type' => 'value', '#value' => $route);
166
167 return confirm_form($form,
168 t('Are you sure you want to delete the route %path?', array('%path' => $route->path)),
169 'admin/build/pageroute',
170 t('Deleting a route will delete all the pages you created in it. This action cannot be undone.'),
171 t('Delete'), t('Cancel')
172 );
173 }
174
175 function pageroute_ui_route_delete_confirm_submit($form, &$form_state) {
176 db_query("DELETE FROM {pageroute_pages} WHERE prid = %d", $form_state['values']['route']->prid);
177 db_query("DELETE FROM {pageroute_routes} WHERE prid = %d", $form_state['values']['route']->prid);
178 cache_clear_all('*', 'cache_menu', TRUE);
179 _pageroute_clear_cache();
180 drupal_set_message('Your route has been deleted.');
181 $form_state['redirect'] = 'admin/build/pageroute';
182 }
183
184 function pageroute_ui_page_edit($op, $prid, $page_name = NULL) {
185 $page = NULL;
186
187 if (is_numeric($prid)) {
188 $route = pageroute_route::load($prid);
189 }
190
191 if (!isset($route)) {
192 drupal_not_found();
193 exit;
194 }
195
196 if ($op == 'add') {
197 drupal_set_title(check_plain($route->path));
198
199 if (in_array($page_name, array_keys(pageroute_get_types()))) {
200 $type = $page_name;
201 }
202 else {
203 return drupal_get_form('pageroute_ui_page_add_type', $prid);
204 }
205 }
206 else if ($op != 'edit') {
207 return pageroute_ui_route_overview($route);
208 }
209 else if ($page_name) {
210 $page = $route->get_suggested_page($page_name);
211
212 if (!isset($page)) {
213 drupal_not_found();
214 exit;
215 }
216 drupal_set_title(check_plain($page->name));
217 $type = $page->type;
218 }
219
220 return drupal_get_form('pageroute_ui_page_edit_page', $route, $type, $page);
221 }
222
223 function pageroute_ui_page_edit_page($form_state, $route, $type, $page = NULL) {
224
225 if (!$page) {
226 $page->name = '';
227 $page->title = '';
228 $page->options['activated'] = 1;
229 $page->options['no_tab'] = 0;
230 $page->options['forward'] = t('Forward');
231 $page->options['back'] = t('Back');
232 $page->options['content-type'] = '';
233 $page->weight = '';
234 $new = TRUE;
235 }
236
237 $page->route = $route;
238
239 $bases = pageroute_get_types('base');
240 $page_class = $bases[$type] .'_page_'. $type;
241 $help = call_user_func(array($page_class, 'help'), &$form_state);
242
243 if ($help) {
244 $form['help'] = array(
245 '#type' => 'fieldset',
246 '#title' => t('Help'),
247 '#collapsible' => TRUE,
248 '#description' => $help,
249 );
250 }
251
252 $form['name'] = array(
253 '#type' => 'textfield',
254 '#title' => t('Name'),
255 '#maxlength' => 63,
256 '#default_value' => $page->name,
257 '#required' => TRUE,
258 '#description' => t('Last part of the page\'s URL. Used for identifing the page.'),
259 );
260 $form['title'] = array(
261 '#type' => 'textfield',
262 '#title' => t('Title'),
263 '#maxlength' => 255,
264 '#default_value' => $page->title,
265 '#description' => t('An optional title which will be set when the page is viewed.'),
266 '#weight' => 1,
267 );
268 $form['options']['activated'] = array(
269 '#type' => 'checkbox',
270 '#title' => t('Activated'),
271 '#default_value' => $page->options['activated'],
272 '#description' => t('When the pageroute is generated, deactivated pages will be ignored.'),
273 '#weight' => 0,
274 );
275 if ($page->route->options['tabs']) {
276 $form['options']['no_tab'] = array(
277 '#type' => 'checkbox',
278 '#title' => t('Don\'t show a tab for this page'),
279 '#default_value' => $page->options['no_tab'],
280 '#weight' => 1,
281 );
282 }
283 $form['options']['forward'] = array(
284 '#type' => 'textfield',
285 '#title' => t('Forward button label'),
286 '#maxlength' => 32,
287 '#default_value' => $page->options['forward'],
288 '#description' => t('The label of the forward button. Leave it empty to hide the button.'),
289 '#weight' => 3,
290 );
291 $form['options']['back'] = array(
292 '#type' => 'textfield',
293 '#title' => t('Back button label'),
294 '#maxlength' => 32,
295 '#default_value' => $page->options['back'],
296 '#description' => t('The label of the back button. Leave it empty to hide the button.'),
297 '#weight' => 4,
298 );
299 $form['options']['cancel'] = array(
300 '#type' => 'textfield',
301 '#title' => t('Cancel link label'),
302 '#maxlength' => 32,
303 '#default_value' => $page->options['cancel'],
304 '#description' => t('The label of the cancel link. Leave it empty to hide the link, but note that the link is the only possibility for the user to not save the form while staying in the route.'),
305 '#weight' => 3,
306 );
307 $form['options']['#tree'] = TRUE;
308 $form['options']['#weight'] = 3;
309
310 $form['weight'] = array(
311 '#type' => 'weight',
312 '#title' => t('Weight'),
313 '#default_value' => $page->weight,
314 '#description' => t('Used for ordering the pages. Pages with lower weights are used first.'),
315 '#weight' => 7,
316 );
317
318 $form['submit'] = array(
319 '#type' => 'submit',
320 '#value' => t('Save'),
321 '#weight' => 8,
322 );
323
324 $form['cancel'] = array(
325 '#type' => 'submit',
326 '#value' => t('Cancel'),
327 '#submit' => array('pageroute_ui_page_cancel_submit'),
328 '#weight' => 9,
329 );
330
331
332 if (!isset($new)) {
333 $form['delete'] = array(
334 '#type' => 'submit',
335 '#value' => t('Delete'),
336 '#submit' => array('pageroute_ui_page_delete_submit'),
337 '#weight' => 10,
338 );
339 $form['page'] = array('#type' => 'value', '#value' => $page);
340 }
341
342 $form['page_type'] = array('#type' => 'value', '#value' => $type);
343 $form['route'] = array('#type' => 'value', '#value' => $route);
344
345 $form['#validate'][] = 'pageroute_ui_page_edit_validate';
346 $form['#submit'][] = 'pageroute_ui_page_edit_submit';
347
348 //let the page type add further form items
349
350 call_user_func(array($page_class, 'ui'), $page, &$form);
351
352 return $form;
353 }
354
355 function pageroute_ui_page_delete_submit($form, &$form_state) {
356 drupal_redirect_form($form, 'admin/build/pageroute/'. $form_state['values']['route']->prid .'/page-delete/'. $form_state['values']['page']->name);
357 }
358
359 function pageroute_ui_page_cancel_submit($form, &$form_state) {
360 drupal_redirect_form($form, 'admin/build/pageroute/'. $form_state['values']['route']->prid);
361 }
362
363 function pageroute_ui_page_edit_validate($form, &$form_state) {
364
365 $name = rtrim(ltrim($form_state['values']['name'], '/'), '/');
366
367 $form_state['values']['name'] = $name;
368
369 if (strpos($name, '/') !== FALSE) {
370 form_set_error('name', t('The page name must not contain a slash "/".'));
371 }
372 if (!valid_url($name)) {
373 form_set_error('name', t('The page name has to be a valid URL.'));
374 }
375
376 if ((!isset($form_state['values']['page']) || $form_state['values']['page']->name != $name) &&
377 db_result(db_query("SELECT * FROM {pageroute_pages} WHERE prid = %d AND name = '%s'",
378 $form_state['values']['route']->prid, $name))) {
379
380 form_set_error('name', t('A page with this name already exists. Choose another name.'));
381 }
382 }
383
384 function pageroute_ui_page_edit_submit($form, &$form_state) {
385
386 if (isset($form_state['values']['page'])) {
387 $edit_page = (object)$form_state['values'];
388 pageroute_ui_update_page($form_state['values']['route'], $edit_page, $form_state['values']['page']->name);
389 pageroute_ui_update_neighbours($form_state['values']['route']);
390 }
391 else {
392 //add a new page
393 $edit_page = (object)$form_state['values'];
394 db_query("INSERT INTO {pageroute_pages} (prid, name, title, weight, type, options) VALUES(%d, '%s', '%s', %d, '%s', '%s')",
395 $form_state['values']['route']->prid, $form_state['values']['name'], $form_state['values']['title'], $form_state['values']['weight'], $form_state['values']['page_type'], serialize($edit_page->options));
396 pageroute_ui_update_neighbours($form_state['values']['route']);
397 }
398 cache_clear_all('*', 'cache_menu', TRUE);
399 _pageroute_clear_cache();
400
401 $form_state['redirect'] = array('admin/build/pageroute/'. $form_state['values']['route']->prid, drupal_get_destination());
402 }
403
404 /*
405 * Shows a form for choosing the inital type of the page
406 */
407 function pageroute_ui_page_add_type($form_state, $prid) {
408
409 $form['type'] = array(
410 '#type' => 'radios',
411 '#title' => t('Choose a page type'),
412 '#options' => pageroute_get_types(),
413 '#required' => TRUE
414 );
415
416 $form['prid'] = array(
417 '#type' => 'hidden',
418 '#value' => $prid
419 );
420
421 $form['submit'] = array(
422 '#type' => 'submit',
423 '#value' => t('Forward')
424 );
425
426 $form['#submit'][] = 'pageroute_ui_page_add_type_submit';
427
428 return $form;
429 }
430
431 function pageroute_ui_page_add_type_submit($form, &$form_state) {
432
433 $form_state['redirect'] = "admin/build/pageroute/". $form_state['values']['prid'] ."/add/". $form_state['values']['type'];
434 }
435
436
437 function pageroute_ui_page_delete_confirm(&$form_state, $prid, $page_name) {
438 $route = pageroute_route::load($prid);
439
440 $page = $route->pages[$route->page_index[$page_name]];
441
442 if (!$page) {
443 drupal_not_found();
444 exit;
445 }
446
447 $form['page'] = array('#type' => 'value', '#value' => $page);
448 $form['route'] = array('#type' => 'value', '#value' => $route);
449
450 return confirm_form($form,
451 t('Are you sure you want to delete the page %name?', array('%name' => $page->name)),
452 'admin/build/pageroute/'. $page->route->prid,
453 t('This action cannot be undone.'), t('Delete'), t('Cancel')
454 );
455 }
456
457 function pageroute_ui_page_delete_confirm_submit($form, &$form_state) {
458 db_query("DELETE FROM {pageroute_pages} WHERE prid = %d AND name ='%s'",
459 $form_state['values']['page']->prid, $form_state['values']['page']->name);
460 pageroute_ui_update_neighbours($route = (object)array('prid' => $form_state['values']['page']->prid));
461 cache_clear_all('*', 'cache_menu', TRUE);
462 _pageroute_clear_cache();
463 drupal_set_message('Your page has been deleted.');
464 $form_state['redirect'] = array('admin/build/pageroute/'. $form_state['values']['page']->prid, drupal_get_destination());
465 }

  ViewVC Help
Powered by ViewVC 1.1.2