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