danielebarchiesi@0
|
1 <?php
|
danielebarchiesi@0
|
2
|
danielebarchiesi@0
|
3 /**
|
danielebarchiesi@0
|
4 * @file
|
danielebarchiesi@0
|
5 * Provides a simple "jump menu".
|
danielebarchiesi@0
|
6 *
|
danielebarchiesi@0
|
7 * A jump menu is a select box and an optional 'go' button which can be removed
|
danielebarchiesi@0
|
8 * if javascript is in use. Each item is keyed to the href that the button
|
danielebarchiesi@0
|
9 * should go to. With javascript, the page is immediately redirected. Without
|
danielebarchiesi@0
|
10 * javascript, the form is submitted and a drupal_goto() is given.
|
danielebarchiesi@0
|
11 *
|
danielebarchiesi@0
|
12 */
|
danielebarchiesi@0
|
13
|
danielebarchiesi@0
|
14 /**
|
danielebarchiesi@0
|
15 * Generate a jump menu form.
|
danielebarchiesi@0
|
16 *
|
danielebarchiesi@0
|
17 * This can either be used with drupal_get_form() or directly added to a
|
danielebarchiesi@0
|
18 * form. The button provides its own submit handler so by default, other
|
danielebarchiesi@0
|
19 * submit handlers will not be called.
|
danielebarchiesi@0
|
20 *
|
danielebarchiesi@0
|
21 * One note: Do not use #tree = TRUE or it will be unable to find the
|
danielebarchiesi@0
|
22 * proper value.
|
danielebarchiesi@0
|
23 *
|
danielebarchiesi@0
|
24 * @code
|
danielebarchiesi@0
|
25 * ctools_include('jump-menu');
|
danielebarchiesi@0
|
26 * $output = drupal_get_form('ctools_jump_menu', $targets, $options);
|
danielebarchiesi@0
|
27 * @endcode
|
danielebarchiesi@0
|
28 *
|
danielebarchiesi@0
|
29 * @param $select
|
danielebarchiesi@0
|
30 * An array suitable for use as the #options. The keys will be the direct
|
danielebarchiesi@0
|
31 * URLs that will be jumped to, so you absolutely must encode these using
|
danielebarchiesi@0
|
32 * url() in order for them to work reliably.
|
danielebarchiesi@0
|
33 *
|
danielebarchiesi@0
|
34 * @param $options
|
danielebarchiesi@0
|
35 * $options may be an array with the following options:
|
danielebarchiesi@0
|
36 * - 'title': The text to display for the #title attribute.
|
danielebarchiesi@0
|
37 * - 'description': The text to display for the #description attribute.
|
danielebarchiesi@0
|
38 * - 'default_value': The text to display for the #default_value attribute.
|
danielebarchiesi@0
|
39 * - 'hide': If TRUE the go button will be set to hide via javascript and
|
danielebarchiesi@0
|
40 * will submit on change.
|
danielebarchiesi@0
|
41 * - 'button': The text to display on the button.
|
danielebarchiesi@0
|
42 * - 'image': If set, an image button will be used instead, and the image
|
danielebarchiesi@0
|
43 * set to this.
|
danielebarchiesi@0
|
44 * - 'inline': If set to TRUE (default) the display will be forced inline.
|
danielebarchiesi@0
|
45 */
|
danielebarchiesi@0
|
46 function ctools_jump_menu($form, &$form_state, $select, $options = array()) {
|
danielebarchiesi@0
|
47 $options += array(
|
danielebarchiesi@0
|
48 'button' => t('Go'),
|
danielebarchiesi@0
|
49 'choose' => t('- Choose -'),
|
danielebarchiesi@0
|
50 'inline' => TRUE,
|
danielebarchiesi@0
|
51 'hide' => TRUE,
|
danielebarchiesi@0
|
52 );
|
danielebarchiesi@0
|
53
|
danielebarchiesi@0
|
54 ctools_add_js('jump-menu');
|
danielebarchiesi@0
|
55
|
danielebarchiesi@0
|
56 if (!empty($options['choose'])) {
|
danielebarchiesi@0
|
57 $select = array('' => $options['choose']) + $select;
|
danielebarchiesi@0
|
58 }
|
danielebarchiesi@0
|
59
|
danielebarchiesi@0
|
60 $form['jump'] = array(
|
danielebarchiesi@0
|
61 '#type' => 'select',
|
danielebarchiesi@0
|
62 '#options' => $select,
|
danielebarchiesi@0
|
63 '#attributes' => array(
|
danielebarchiesi@0
|
64 'class' => array('ctools-jump-menu-select'),
|
danielebarchiesi@0
|
65 ),
|
danielebarchiesi@0
|
66 );
|
danielebarchiesi@0
|
67
|
danielebarchiesi@0
|
68 if (!empty($options['title'])) {
|
danielebarchiesi@0
|
69 $form['jump']['#title'] = $options['title'];
|
danielebarchiesi@0
|
70 }
|
danielebarchiesi@0
|
71
|
danielebarchiesi@0
|
72 if (!empty($options['description'])) {
|
danielebarchiesi@0
|
73 $form['jump']['#description'] = $options['description'];
|
danielebarchiesi@0
|
74 }
|
danielebarchiesi@0
|
75
|
danielebarchiesi@0
|
76 if (!empty($options['default_value'])) {
|
danielebarchiesi@0
|
77 $form['jump']['#default_value'] = $options['default_value'];
|
danielebarchiesi@0
|
78 }
|
danielebarchiesi@0
|
79
|
danielebarchiesi@0
|
80 if (isset($options['image'])) {
|
danielebarchiesi@0
|
81 $form['go'] = array(
|
danielebarchiesi@0
|
82 '#type' => 'image_button',
|
danielebarchiesi@0
|
83 '#src' => $options['image'],
|
danielebarchiesi@0
|
84 '#submit' => array('ctools_jump_menu_submit'),
|
danielebarchiesi@0
|
85 '#attributes' => array(
|
danielebarchiesi@0
|
86 'class' => array('ctools-jump-menu-button'),
|
danielebarchiesi@0
|
87 ),
|
danielebarchiesi@0
|
88 );
|
danielebarchiesi@0
|
89 }
|
danielebarchiesi@0
|
90 else {
|
danielebarchiesi@0
|
91 $form['go'] = array(
|
danielebarchiesi@0
|
92 '#type' => 'submit',
|
danielebarchiesi@0
|
93 '#value' => $options['button'],
|
danielebarchiesi@0
|
94 '#submit' => array('ctools_jump_menu_submit'),
|
danielebarchiesi@0
|
95 '#attributes' => array(
|
danielebarchiesi@0
|
96 'class' => array('ctools-jump-menu-button'),
|
danielebarchiesi@0
|
97 ),
|
danielebarchiesi@0
|
98 );
|
danielebarchiesi@0
|
99 }
|
danielebarchiesi@0
|
100
|
danielebarchiesi@0
|
101 if ($options['inline']) {
|
danielebarchiesi@0
|
102 $form['jump']['#prefix'] = '<div class="container-inline">';
|
danielebarchiesi@0
|
103 $form['go']['#suffix'] = '</div>';
|
danielebarchiesi@0
|
104 }
|
danielebarchiesi@0
|
105
|
danielebarchiesi@0
|
106 if ($options['hide']) {
|
danielebarchiesi@0
|
107 $form['jump']['#attributes']['class'][] = 'ctools-jump-menu-change';
|
danielebarchiesi@0
|
108 $form['go']['#attributes']['class'][] = 'ctools-jump-menu-hide';
|
danielebarchiesi@0
|
109 }
|
danielebarchiesi@0
|
110
|
danielebarchiesi@0
|
111 return $form;
|
danielebarchiesi@0
|
112 }
|
danielebarchiesi@0
|
113
|
danielebarchiesi@0
|
114 /**
|
danielebarchiesi@0
|
115 * Submit handler for the jump menu.
|
danielebarchiesi@0
|
116 *
|
danielebarchiesi@0
|
117 * This is normally only invoked upon submit without javascript enabled.
|
danielebarchiesi@0
|
118 */
|
danielebarchiesi@0
|
119 function ctools_jump_menu_submit($form, &$form_state) {
|
danielebarchiesi@0
|
120 if ($form_state['values']['jump'] === '') {
|
danielebarchiesi@0
|
121 // We have nothing to do when the user has not selected any value.
|
danielebarchiesi@0
|
122 return;
|
danielebarchiesi@0
|
123 }
|
danielebarchiesi@0
|
124
|
danielebarchiesi@0
|
125 // If the path we are redirecting to contains the string :: then treat the
|
danielebarchiesi@0
|
126 // the string after the double colon as the path to redirect to.
|
danielebarchiesi@0
|
127 // This allows duplicate paths to be used in jump menus for multiple options.
|
danielebarchiesi@0
|
128 $redirect_array = explode("::", $form_state['values']['jump']);
|
danielebarchiesi@0
|
129
|
danielebarchiesi@0
|
130 if(isset($redirect_array[1]) && !empty($redirect_array[1])){
|
danielebarchiesi@0
|
131 $redirect = $redirect_array[1];
|
danielebarchiesi@0
|
132 }
|
danielebarchiesi@0
|
133 else {
|
danielebarchiesi@0
|
134 $redirect = $form_state['values']['jump'];
|
danielebarchiesi@0
|
135 }
|
danielebarchiesi@0
|
136
|
danielebarchiesi@0
|
137 // If the path we are redirecting to starts with the base path (for example,
|
danielebarchiesi@0
|
138 // "/somepath/node/1"), we need to strip the base path off before passing it
|
danielebarchiesi@0
|
139 // to $form_state['redirect'].
|
danielebarchiesi@0
|
140 $base_path = base_path();
|
danielebarchiesi@0
|
141 if (strpos($redirect, $base_path) === 0) {
|
danielebarchiesi@0
|
142 $redirect = substr($redirect, strlen($base_path));
|
danielebarchiesi@0
|
143 }
|
danielebarchiesi@0
|
144
|
danielebarchiesi@0
|
145 // Parse the URL so that query strings and fragments are preserved in the
|
danielebarchiesi@0
|
146 // redirect.
|
danielebarchiesi@0
|
147 $redirect = drupal_parse_url($redirect);
|
danielebarchiesi@0
|
148 $redirect['path'] = urldecode($redirect['path']);
|
danielebarchiesi@0
|
149 $form_state['redirect'] = array($redirect['path'], $redirect);
|
danielebarchiesi@0
|
150 }
|