1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Administrative page callbacks for the date_api module. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Allow users to configure additional date format types. |
---|
10 | */ |
---|
11 | function date_api_date_formats_form($form_state) { |
---|
12 | // Add date_api.js and js settings. |
---|
13 | date_api_add_system_javascript(); |
---|
14 | |
---|
15 | $form = array(); |
---|
16 | |
---|
17 | // Display existing format types and their formats. |
---|
18 | date_api_date_format_form_elements($form); |
---|
19 | |
---|
20 | $form = system_settings_form($form); |
---|
21 | return $form; |
---|
22 | } |
---|
23 | |
---|
24 | /** |
---|
25 | * Allow users to configure custom date formats. |
---|
26 | */ |
---|
27 | function date_api_configure_custom_date_formats() { |
---|
28 | // Add date_api.js and js settings. |
---|
29 | date_api_add_system_javascript(); |
---|
30 | $output = ''; |
---|
31 | |
---|
32 | // Get list of custom date formats. |
---|
33 | $formats = date_get_formats('custom', TRUE); |
---|
34 | |
---|
35 | if (!empty($formats)) { |
---|
36 | $rows = array(); |
---|
37 | foreach ($formats as $format => $format_info) { |
---|
38 | $display_text = date_format_date(date_now(), 'custom', $format); |
---|
39 | $delete_link = l(t('remove'), 'admin/settings/date-time/formats/delete/' . $format_info['dfid']); |
---|
40 | $row = array($display_text, $delete_link); |
---|
41 | $rows[] = $row; |
---|
42 | } |
---|
43 | $output = theme('table', array(), $rows); |
---|
44 | |
---|
45 | } |
---|
46 | else { |
---|
47 | $output = t('No custom formats configured. Please <a href="@link">add</a> some.', array('@link' => url('admin/settings/date-time/formats/add'))); |
---|
48 | } |
---|
49 | |
---|
50 | return $output; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Allow users to add additional date formats. |
---|
55 | */ |
---|
56 | function date_api_add_date_formats_form($form_state) { |
---|
57 | // Add date_api.js and js settings. |
---|
58 | date_api_add_system_javascript(); |
---|
59 | |
---|
60 | $form['add_date_format'] = array( |
---|
61 | '#type' => 'textfield', |
---|
62 | '#title' => t('Format string'), |
---|
63 | '#attributes' => array('class' => 'custom-format'), |
---|
64 | '#description' => t('A user-defined date format. See the <a href="@url">PHP manual</a> for available options. This format is currently set to display as <span>%date</span>.', array('@url' => 'http://php.net/manual/function.date.php', '%date' => date_format_date(date_now(), 'custom', '-'))), |
---|
65 | ); |
---|
66 | |
---|
67 | $form['update'] = array( |
---|
68 | '#type' => 'submit', |
---|
69 | '#value' => t('Save configuration'), |
---|
70 | '#weight' => 3, |
---|
71 | '#submit' => array('date_api_date_time_settings_submit'), |
---|
72 | ); |
---|
73 | |
---|
74 | $form['#validate'][] = 'date_api_date_time_settings_validate'; |
---|
75 | |
---|
76 | return $form; |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Add drop down selects for date format types. |
---|
81 | * |
---|
82 | * @param &$form |
---|
83 | * The form being altered. |
---|
84 | */ |
---|
85 | function date_api_date_format_form_elements(&$form) { |
---|
86 | $form['date_formats'] = array( |
---|
87 | '#type' => 'fieldset', |
---|
88 | '#title' => t('Date formats'), |
---|
89 | ); |
---|
90 | |
---|
91 | // Get list of all available date format types. |
---|
92 | $format_types = date_get_format_types('', TRUE); |
---|
93 | |
---|
94 | // Get list of all available date formats. |
---|
95 | $all_formats = array(); |
---|
96 | $date_formats = date_get_formats('', TRUE); // Call this to rebuild the list, and to have default list. |
---|
97 | foreach ($date_formats as $type => $format_info) { |
---|
98 | $all_formats = array_merge($all_formats, $format_info); |
---|
99 | } |
---|
100 | $custom_formats = date_get_formats('custom'); |
---|
101 | foreach ($format_types as $type => $type_info) { |
---|
102 | // If a system type, only show the available formats for that type and |
---|
103 | // custom ones. |
---|
104 | if ($type_info['locked'] == 1) { |
---|
105 | $formats = date_get_formats($type); |
---|
106 | if (empty($formats)) { |
---|
107 | $formats = $all_formats; |
---|
108 | } |
---|
109 | elseif (!empty($custom_formats)) { |
---|
110 | $formats = array_merge($formats, $custom_formats); |
---|
111 | } |
---|
112 | } |
---|
113 | // If a user configured type, show all available date formats. |
---|
114 | else { |
---|
115 | $formats = $all_formats; |
---|
116 | } |
---|
117 | |
---|
118 | $choices = array(); |
---|
119 | foreach ($formats as $f => $format) { |
---|
120 | $choices[$f] = date_format_date(date_now(), 'custom', $f); |
---|
121 | } |
---|
122 | $keys = array_keys($formats); |
---|
123 | $default = variable_get('date_format_' . $type, array_shift($keys)); |
---|
124 | |
---|
125 | // Get format type info for this format type. |
---|
126 | $type_info = date_get_format_types($type); |
---|
127 | |
---|
128 | date_api_date_format_select_field($form, $type, $type_info, $default, $choices, 1); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | function date_api_date_time_settings_validate($form, &$form_state) { |
---|
133 | $formats = date_get_formats('custom'); |
---|
134 | if (!empty($formats) && in_array($form_state['values']['add_date_format'], array_keys($formats))) { |
---|
135 | form_set_error('add_date_format', t('This format already exists. Please enter a unique format string.')); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | function date_api_date_time_settings_submit($form, &$form_state) { |
---|
140 | if (!empty($form_state['values']['add_date_format'])) { |
---|
141 | $format = array(); |
---|
142 | $format['format'] = $form_state['values']['add_date_format']; |
---|
143 | $format['type'] = 'custom'; |
---|
144 | $format['locked'] = 0; |
---|
145 | $format['is_new'] = 1; |
---|
146 | date_format_save($format); |
---|
147 | } |
---|
148 | |
---|
149 | // Unset, to prevent this getting saved as a variables. |
---|
150 | unset($form_state['values']['add_date_format']); |
---|
151 | |
---|
152 | drupal_set_message(t('Configuration saved.')); |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * Menu callback; present a form for deleting a date format. |
---|
157 | */ |
---|
158 | function date_api_delete_format_form(&$form_state, $dfid) { |
---|
159 | $form = array(); |
---|
160 | $form['dfid'] = array( |
---|
161 | '#type' => 'value', |
---|
162 | '#value' => $dfid, |
---|
163 | ); |
---|
164 | $format = date_get_format($dfid); |
---|
165 | |
---|
166 | $output = confirm_form($form, |
---|
167 | t('Are you sure you want to remove the format %format?', array('%format' => date_format_date(date_now(), 'custom', $format['format']))), |
---|
168 | 'admin/settings/date-time/formats/custom', |
---|
169 | t('This action cannot be undone.'), |
---|
170 | t('Remove'), t('Cancel'), |
---|
171 | 'confirm' |
---|
172 | ); |
---|
173 | |
---|
174 | return $output; |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Delete a configured date format. |
---|
179 | */ |
---|
180 | function date_api_delete_format_form_submit($form, &$form_state) { |
---|
181 | if ($form_state['values']['confirm']) { |
---|
182 | $format = date_get_format($form_state['values']['dfid']); |
---|
183 | date_format_delete($form_state['values']['dfid']); |
---|
184 | drupal_set_message(t('Removed date format %format.', array('%format' => date_format_date(date_now(), 'custom', $format['format'])))); |
---|
185 | $form_state['redirect'] = 'admin/settings/date-time/formats/custom'; |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * Menu callback; present a form for deleting a date format type. |
---|
191 | */ |
---|
192 | function date_api_delete_format_type_form(&$form_state, $format_type) { |
---|
193 | $form = array(); |
---|
194 | $form['format_type'] = array( |
---|
195 | '#type' => 'value', |
---|
196 | '#value' => $format_type, |
---|
197 | ); |
---|
198 | $type_info = date_get_format_types($format_type); |
---|
199 | |
---|
200 | $output = confirm_form($form, |
---|
201 | t('Are you sure you want to remove the format type %format?', array('%format' => $type_info['title'])), |
---|
202 | 'admin/settings/date-time/formats', |
---|
203 | t('This action cannot be undone.'), |
---|
204 | t('Remove'), t('Cancel'), |
---|
205 | 'confirm' |
---|
206 | ); |
---|
207 | |
---|
208 | return $output; |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * Delete a configured date format type. |
---|
213 | */ |
---|
214 | function date_api_delete_format_type_form_submit($form, &$form_state) { |
---|
215 | if ($form_state['values']['confirm']) { |
---|
216 | $type_info = date_get_format_types($form_state['values']['format_type']); |
---|
217 | date_format_type_delete($form_state['values']['format_type']); |
---|
218 | drupal_set_message(t('Removed date format type %format.', array('%format' => $type_info['title']))); |
---|
219 | $form_state['redirect'] = 'admin/settings/date-time/formats'; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * Helper function; return form fields for date format selects. |
---|
225 | */ |
---|
226 | function date_api_date_format_select_field(&$form, $type, $type_info, $default, $choices, $show_remove = 0) { |
---|
227 | // Show date format select list. |
---|
228 | $form['date_formats']['date_format_' . $type] = array( |
---|
229 | '#prefix' => '<div class="date-container"><div class="select-container">', |
---|
230 | // Leave the date-container div open if we are going to be adding to and |
---|
231 | // then closing it below. |
---|
232 | '#suffix' => ($show_remove == 1 && $type_info['locked'] == 0) ? '</div>' : '</div></div>', |
---|
233 | '#type' => 'select', |
---|
234 | '#title' => t('!type date format', array('!type' => t($type_info['title']))), |
---|
235 | '#attributes' => array('class' => 'date-format'), |
---|
236 | '#default_value' => (isset($choices[$default]) ? $default : 'custom'), |
---|
237 | '#options' => $choices, |
---|
238 | ); |
---|
239 | |
---|
240 | // If this isn't a system provided type, allow the user to remove it from |
---|
241 | // the system. |
---|
242 | if ($show_remove == 1 && $type_info['locked'] == 0) { |
---|
243 | $form['date_formats']['date_format_' . $type . '_delete'] = array( |
---|
244 | '#prefix' => '<div class="date-format-delete">', |
---|
245 | '#suffix' => '</div></div>', |
---|
246 | '#value' => l(t('remove'), 'admin/settings/date-time/delete/' . $type), |
---|
247 | ); |
---|
248 | } |
---|
249 | } |
---|