date_admin.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. /**
  3. * @file
  4. * Date administration code.
  5. */
  6. /**
  7. * Settings for the default formatter.
  8. */
  9. function date_default_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  10. $display = $instance['display'][$view_mode];
  11. $settings = $display['settings'];
  12. $formatter = $display['type'];
  13. $form = array();
  14. $form['format_type'] = array(
  15. '#title' => t('Choose how users view dates and times:'),
  16. '#type' => 'select',
  17. '#options' => date_format_type_options(),
  18. '#default_value' => $settings['format_type'],
  19. '#description' => t('To add or edit options, visit <a href="@date-time-page">Date and time settings</a>.', array('@date-time-page' => url('admin/config/regional/date-time'))),
  20. '#weight' => 0,
  21. );
  22. $form['fromto'] = array(
  23. '#title' => t('Display:'),
  24. '#type' => 'select',
  25. '#options' => array(
  26. 'both' => t('Both Start and End dates'),
  27. 'value' => t('Start date only'),
  28. 'value2' => t('End date only'),
  29. ),
  30. '#access' => $field['settings']['todate'],
  31. '#default_value' => $settings['fromto'],
  32. '#weight' => 1,
  33. );
  34. // Make the string translatable by keeping it as a whole rather than
  35. // translating prefix and suffix separately.
  36. list($prefix, $suffix) = explode('@count', t('Show @count value(s)'));
  37. $form['multiple_number'] = array(
  38. '#type' => 'textfield',
  39. '#title' => t('Multiple values:'),
  40. '#size' => 5,
  41. '#field_prefix' => $prefix,
  42. '#field_suffix' => $suffix,
  43. '#default_value' => $settings['multiple_number'],
  44. '#weight' => 2,
  45. '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
  46. '#description' => t('Identify a specific number of values to display, or leave blank to show all values.'),
  47. );
  48. list($prefix, $suffix) = explode('@isodate', t('starting from @isodate'));
  49. $form['multiple_from'] = array(
  50. '#type' => 'textfield',
  51. '#size' => 15,
  52. '#field_prefix' => $prefix,
  53. '#field_suffix' => $suffix,
  54. '#default_value' => $settings['multiple_from'],
  55. '#weight' => 3,
  56. '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
  57. );
  58. list($prefix, $suffix) = explode('@isodate', t('ending with @isodate'));
  59. $form['multiple_to'] = array(
  60. '#type' => 'textfield',
  61. '#size' => 15,
  62. '#field_prefix' => $prefix,
  63. '#field_suffix' => $suffix,
  64. '#default_value' => $settings['multiple_to'],
  65. '#weight' => 4,
  66. '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
  67. '#description' => t('Identify specific start and/or end dates in the format YYYY-MM-DDTHH:MM:SS, or leave blank for all available dates.'),
  68. );
  69. $form['show_remaining_days'] = array(
  70. '#title' => t('Show remaining days'),
  71. '#type' => 'checkbox',
  72. '#default_value' => $settings['show_remaining_days'],
  73. '#weight' => 0,
  74. );
  75. return $form;
  76. }
  77. /**
  78. * Settings for the interval formatter.
  79. */
  80. function date_interval_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  81. $display = $instance['display'][$view_mode];
  82. $settings = $display['settings'];
  83. $form = array();
  84. $form['interval'] = array(
  85. '#title' => t('Interval'),
  86. '#description' => t("How many time units should be shown in the 'time ago' string."),
  87. '#type' => 'select',
  88. '#options' => drupal_map_assoc(range(1, 6)),
  89. '#default_value' => $settings['interval'],
  90. '#weight' => 0,
  91. );
  92. // Uses the same options used by Views format_interval.
  93. $options = array(
  94. 'raw time ago' => t('Time ago'),
  95. 'time ago' => t('Time ago (with "ago" appended)'),
  96. 'raw time hence' => t('Time hence'),
  97. 'time hence' => t('Time hence (with "hence" appended)'),
  98. 'raw time span' => t('Time span (future dates have "-" prepended)'),
  99. 'inverse time span' => t('Time span (past dates have "-" prepended)'),
  100. 'time span' => t('Time span (with "ago/hence" appended)'),
  101. );
  102. $form['interval_display'] = array(
  103. '#title' => t('Display'),
  104. '#description' => t("How to display the time ago or time hence for this field."),
  105. '#type' => 'select',
  106. '#options' => $options,
  107. '#default_value' => $settings['interval_display'],
  108. '#weight' => 0,
  109. );
  110. return $form;
  111. }
  112. /**
  113. * Settings summary for the default formatter.
  114. */
  115. function date_default_formatter_settings_summary($field, $instance, $view_mode) {
  116. $display = $instance['display'][$view_mode];
  117. $settings = $display['settings'];
  118. $formatter = $display['type'];
  119. $format_types = date_format_type_options();
  120. $summary = array();
  121. $format = FALSE;
  122. switch ($formatter) {
  123. case 'date_plain':
  124. $format = t('Plain');
  125. break;
  126. case 'format_interval':
  127. $format = t('Interval');
  128. break;
  129. default:
  130. if (!empty($format_types[$settings['format_type']])) {
  131. $format = $format_types[$settings['format_type']];
  132. }
  133. }
  134. if ($format) {
  135. $summary[] = t('Display dates using the @format format', array('@format' => $format));
  136. }
  137. else {
  138. $summary[] = t('Display dates using the default format because the specified format (@format) is not defined', array('@format' => $settings['format_type']));
  139. }
  140. if (array_key_exists('fromto', $settings) && $field['settings']['todate']) {
  141. $options = array(
  142. 'both' => t('Display both Start and End dates'),
  143. 'value' => t('Display Start date only'),
  144. 'value2' => t('Display End date only'),
  145. );
  146. if (isset($options[$settings['fromto']])) {
  147. $summary[] = $options[$settings['fromto']];
  148. }
  149. }
  150. if (array_key_exists('multiple_number', $settings) && !empty($field['cardinality'])) {
  151. $summary[] = t('Show @count value(s) starting with @date1, ending with @date2', array(
  152. '@count' => !empty($settings['multiple_number']) ? $settings['multiple_number'] : t('all'),
  153. '@date1' => !empty($settings['multiple_from']) ? $settings['multiple_from'] : t('earliest'),
  154. '@date2' => !empty($settings['multiple_to']) ? $settings['multiple_to'] : t('latest'),
  155. ));
  156. }
  157. if (array_key_exists('show_remaining_days', $settings)) {
  158. $summary[] = t('Show remaining days: @value', array('@value' => ($settings['show_remaining_days'] ? 'yes' : 'no')));
  159. }
  160. return $summary;
  161. }
  162. /**
  163. * Settings summary for the interval formatter.
  164. *
  165. * @TODO Add settings later.
  166. */
  167. function date_interval_formatter_settings_summary($field, $instance, $view_mode) {
  168. $summary = array();
  169. $display = $instance['display'][$view_mode];
  170. $settings = $display['settings'];
  171. $formatter = $display['type'];
  172. $summary[] = t('Display time ago, showing @interval units.', array('@interval' => $settings['interval']));
  173. return $summary;
  174. }
  175. /**
  176. * Helper function for date_field_instance_settings_form().
  177. *
  178. * @see date_field_instance_settings_form_validate()
  179. */
  180. function _date_field_instance_settings_form($field, $instance) {
  181. $widget = $instance['widget'];
  182. $settings = $instance['settings'];
  183. $widget_settings = $instance['widget']['settings'];
  184. $form['default_value'] = array(
  185. '#type' => 'select',
  186. '#title' => t('Default date'),
  187. '#default_value' => $settings['default_value'],
  188. '#options' => array(
  189. 'blank' => t('No default value'),
  190. 'now' => t('Now'),
  191. 'strtotime' => t('Relative'),
  192. ),
  193. '#weight' => 1,
  194. '#fieldset' => 'default_values',
  195. );
  196. $description = t("Describe a time by reference to the current day, like '+90 days' (90 days from the day the field is created) or '+1 Saturday' (the next Saturday). See !strtotime for more details.", array('!strtotime' => l(t('strtotime'), 'http://www.php.net/manual/en/function.strtotime.php')));
  197. $form['default_value_code'] = array(
  198. '#type' => 'textfield',
  199. '#title' => t('Relative default value'),
  200. '#description' => $description,
  201. '#default_value' => $settings['default_value_code'],
  202. '#states' => array(
  203. 'visible' => array(
  204. ':input[name="instance[settings][default_value]"]' => array(
  205. 'value' => 'strtotime',
  206. ),
  207. ),
  208. ),
  209. '#weight' => 1.1,
  210. '#fieldset' => 'default_values',
  211. );
  212. $form['default_value2'] = array(
  213. '#type' => !empty($field['settings']['todate']) ? 'select' : 'hidden',
  214. '#title' => t('Default end date'),
  215. '#default_value' => $settings['default_value2'],
  216. '#options' => array(
  217. 'same' => t('Same as Default date'),
  218. 'blank' => t('No default value'),
  219. 'now' => t('Now'),
  220. 'strtotime' => t('Relative'),
  221. ),
  222. '#weight' => 2,
  223. '#fieldset' => 'default_values',
  224. );
  225. $form['default_value_code2'] = array(
  226. '#type' => !empty($field['settings']['todate']) ? 'textfield' : 'hidden',
  227. '#title' => t('Relative default value for end date'),
  228. '#description' => $description,
  229. '#default_value' => $settings['default_value_code2'],
  230. '#states' => array(
  231. 'visible' => array(
  232. ':input[name="instance[settings][default_value2]"]' => array(
  233. 'value' => 'strtotime',
  234. ),
  235. ),
  236. ),
  237. '#weight' => 2.1,
  238. '#fieldset' => 'default_values',
  239. );
  240. $form['#element_validate'] = array('date_field_instance_settings_form_validate');
  241. $context = array(
  242. 'field' => $field,
  243. 'instance' => $instance,
  244. );
  245. drupal_alter('date_field_instance_settings_form', $form, $context);
  246. return $form;
  247. }
  248. /**
  249. * Form validation handler for _date_field_instance_settings_form().
  250. */
  251. function date_field_instance_settings_form_validate(&$form, &$form_state) {
  252. $settings = $form_state['values']['instance']['settings'];
  253. if ($settings['default_value'] == 'strtotime') {
  254. $is_strtotime = @strtotime($settings['default_value_code']);
  255. if (!$is_strtotime) {
  256. form_set_error('instance][settings][default_value_code', t('The Strtotime default value is invalid.'));
  257. }
  258. }
  259. if (isset($settings['default_value2']) && $settings['default_value2'] == 'strtotime') {
  260. $is_strtotime = @strtotime($settings['default_value_code2']);
  261. if (!$is_strtotime) {
  262. form_set_error('instance][settings][default_value_code2', t('The Strtotime default value for the End Date is invalid.'));
  263. }
  264. }
  265. }
  266. /**
  267. * Helper function for date_field_widget_settings_form().
  268. *
  269. * @see date_field_widget_settings_form_validate()
  270. */
  271. function _date_field_widget_settings_form($field, $instance) {
  272. $widget = $instance['widget'];
  273. $settings = $widget['settings'];
  274. $form = array(
  275. '#element_validate' => array('date_field_widget_settings_form_validate'),
  276. );
  277. $options = array();
  278. if ($widget['type'] == 'date_popup' && module_exists('date_popup')) {
  279. $formats = date_popup_formats();
  280. }
  281. else {
  282. // Example input formats must show all possible date parts, so add seconds.
  283. $formats = str_replace('i', 'i:s', array_keys(system_get_date_formats('short')));
  284. $formats = drupal_map_assoc($formats);
  285. }
  286. $now = date_example_date();
  287. $options['site-wide'] = t('Short date format: @date', array('@date' => date_format_date($now, 'short')));
  288. foreach ($formats as $f) {
  289. $options[$f] = date_format_date($now, 'custom', $f);
  290. }
  291. $form['input_format'] = array(
  292. '#type' => 'select',
  293. '#title' => t('Date entry options'),
  294. '#default_value' => $settings['input_format'],
  295. '#options' => $options,
  296. '#description' => t('Control the order and format of the options users see.'),
  297. '#weight' => 3,
  298. '#fieldset' => 'date_format',
  299. );
  300. // Only a limited set of formats is available for the Date Popup module.
  301. if ($widget['type'] != 'date_popup') {
  302. $form['input_format']['#options']['custom'] = t('Custom format');
  303. $form['input_format_custom'] = array(
  304. '#type' => 'textfield',
  305. '#title' => t('Custom input format'),
  306. '#default_value' => $settings['input_format_custom'],
  307. '#description' => t("Override the input format selected above. Define a php date format string like 'm-d-Y H:i' (see <a href=\"@link\">http://php.net/date</a> for more details).", array('@link' => 'http://php.net/date')),
  308. '#weight' => 5,
  309. '#fieldset' => 'date_format',
  310. '#attributes' => array('class' => array('indent')),
  311. '#states' => array(
  312. 'visible' => array(
  313. ':input[name="instance[widget][settings][input_format]"]' => array('value' => 'custom'),
  314. ),
  315. ),
  316. );
  317. }
  318. else {
  319. $form['input_format_custom'] = array(
  320. '#type' => 'hidden',
  321. '#value' => '',
  322. );
  323. }
  324. if (in_array($widget['type'], array('date_select', 'date_popup'))) {
  325. $form['year_range'] = array(
  326. '#type' => 'date_year_range',
  327. '#default_value' => $settings['year_range'],
  328. '#fieldset' => 'date_format',
  329. '#weight' => 6,
  330. );
  331. $form['increment'] = array(
  332. '#type' => 'select', '#title' => t('Time increments'),
  333. '#default_value' => $settings['increment'],
  334. '#options' => array(
  335. 1 => t('1 minute'),
  336. 5 => t('5 minute'),
  337. 10 => t('10 minute'),
  338. 15 => t('15 minute'),
  339. 30 => t('30 minute')),
  340. '#weight' => 7,
  341. '#fieldset' => 'date_format',
  342. );
  343. }
  344. else {
  345. $form['year_range'] = array(
  346. '#type' => 'hidden',
  347. '#value' => $settings['year_range'],
  348. );
  349. $form['increment'] = array(
  350. '#type' => 'hidden',
  351. '#value' => $settings['increment'],
  352. );
  353. }
  354. $form['label_position'] = array(
  355. '#type' => 'value',
  356. '#value' => $settings['label_position'],
  357. );
  358. $form['text_parts'] = array(
  359. '#type' => 'value',
  360. '#value' => $settings['text_parts'],
  361. );
  362. $form['advanced'] = array(
  363. '#type' => 'fieldset',
  364. '#title' => t('Advanced settings'),
  365. '#collapsible' => TRUE,
  366. '#collapsed' => TRUE,
  367. '#fieldset' => 'date_format',
  368. '#weight' => 9,
  369. );
  370. if (in_array($widget['type'], array('date_select'))) {
  371. $options = array(
  372. 'above' => t('Above'),
  373. 'within' => t('Within'),
  374. 'none' => t('None'),
  375. );
  376. $description = t("The location of date part labels, like 'Year', 'Month', or 'Day' . 'Above' displays the label as titles above each date part. 'Within' inserts the label as the first option in the select list and in blank textfields. 'None' doesn't visually label any of the date parts. Theme functions like 'date_part_label_year' and 'date_part_label_month' control label text.");
  377. }
  378. else {
  379. $options = array(
  380. 'above' => t('Above'),
  381. 'none' => t('None'),
  382. );
  383. $description = t("The location of date part labels, like 'Year', 'Month', or 'Day' . 'Above' displays the label as titles above each date part. 'None' doesn't visually label any of the date parts. Theme functions like 'date_part_label_year' and 'date_part_label_month' control label text.");
  384. }
  385. $form['advanced']['label_position'] = array(
  386. '#type' => 'radios',
  387. '#options' => $options,
  388. '#default_value' => $settings['label_position'],
  389. '#title' => t('Position of date part labels'),
  390. '#description' => $description,
  391. );
  392. $form['advanced']['text_parts'] = array(
  393. '#theme' => $widget['type'] == 'date_select' ? 'date_text_parts' : '',
  394. );
  395. $text_parts = (array) $settings['text_parts'];
  396. foreach (date_granularity_names() as $key => $value) {
  397. if ($widget['type'] == 'date_select') {
  398. $form['advanced']['text_parts'][$key] = array(
  399. '#type' => 'radios',
  400. '#default_value' => in_array($key, $text_parts) ? 1 : 0,
  401. '#options' => array(0 => '', 1 => ''),
  402. );
  403. }
  404. else {
  405. $form['advanced']['text_parts'][$key] = array(
  406. '#type' => 'value',
  407. '#value' => (int) in_array($key, (array) $settings['text_parts']),
  408. );
  409. }
  410. }
  411. $form['advanced']['no_fieldset'] = array(
  412. '#type' => 'checkbox',
  413. '#title' => t('Render as a regular field'),
  414. '#default_value' => !empty($settings['no_fieldset']),
  415. '#description' => t('Whether to render this field as a regular field instead of a fieldset. The date field elements are wrapped in a fieldset by default, and may not display well without it.'),
  416. );
  417. $context = array(
  418. 'field' => $field,
  419. 'instance' => $instance,
  420. );
  421. drupal_alter('date_field_widget_settings_form', $form, $context);
  422. return $form;
  423. }
  424. /**
  425. * Form validation handler for _date_field_widget_settings_form().
  426. */
  427. function date_field_widget_settings_form_validate(&$form, &$form_state) {
  428. // The widget settings are in the wrong place in the form because of #tree on
  429. // the top level.
  430. $settings = $form_state['values']['instance']['widget']['settings'];
  431. $settings = array_merge($settings, $settings['advanced']);
  432. unset($settings['advanced']);
  433. form_set_value(array('#parents' => array('instance', 'widget', 'settings')), $settings, $form_state);
  434. $widget = &$form_state['values']['instance']['widget'];
  435. // Munge the table display for text parts back into an array of text parts.
  436. if (is_array($widget['settings']['text_parts'])) {
  437. form_set_value($form['text_parts'], array_keys(array_filter($widget['settings']['text_parts'])), $form_state);
  438. }
  439. if ($widget['settings']['input_format'] === 'custom' && empty($widget['settings']['input_format_custom'])) {
  440. form_set_error('instance][widget][settings][input_format_custom', t('Please enter a custom date format, or choose one of the preset formats.'));
  441. }
  442. }
  443. /**
  444. * Helper function for date_field_settings_form().
  445. *
  446. * @see date_field_settings_validate()
  447. */
  448. function _date_field_settings_form($field, $instance, $has_data) {
  449. $settings = $field['settings'];
  450. $form = array(
  451. '#element_validate' => array('date_field_settings_validate'),
  452. );
  453. // Make sure granularity is in the right format and has no empty values.
  454. if (!empty($settings['granularity']) && is_array($settings['granularity'])) {
  455. $granularity = array_filter($settings['granularity']);
  456. }
  457. $tz_handling = $settings['tz_handling'];
  458. $description = t('Select the date attributes to collect and store.');
  459. if ($has_data) {
  460. $description .= ' ' . t('Changes to date attributes only effects new or updated content.');
  461. }
  462. $options = date_granularity_names();
  463. $checkbox_year = array(
  464. '#type' => 'checkbox',
  465. '#title' => check_plain($options['year']),
  466. '#value' => 'year',
  467. '#return_value' => 'year',
  468. '#disabled' => TRUE,
  469. );
  470. unset($options['year']);
  471. $form['granularity'] = array(
  472. '#type' => 'checkboxes',
  473. '#title' => t('Date attributes to collect'),
  474. '#default_value' => $granularity,
  475. '#options' => $options,
  476. '#attributes' => array(
  477. 'class' => array('container-inline'),
  478. ),
  479. '#description' => $description,
  480. 'year' => $checkbox_year,
  481. );
  482. $description = t('End dates are used to collect duration. E.g., allow an event to start on September 15, and end on September 16.');
  483. $form['enddate_get'] = array(
  484. '#type' => 'checkbox',
  485. '#title' => t('Collect an end date'),
  486. '#description' => $description,
  487. '#default_value' => (empty($settings['todate']) ? FALSE : TRUE),
  488. '#disabled' => $has_data,
  489. );
  490. $form['enddate_required'] = array(
  491. '#type' => 'checkbox',
  492. '#title' => t('Required'),
  493. '#default_value' => ((isset($settings['todate']) && $settings['todate'] === 'required') ? TRUE : FALSE),
  494. '#disabled' => $has_data,
  495. '#states' => array(
  496. 'invisible' => array(
  497. 'input[name="field[settings][enddate_get]"]' => array('checked' => FALSE),
  498. ),
  499. ),
  500. );
  501. $description = t('Select the timezone handling method for this date field.');
  502. $form['tz_handling'] = array(
  503. '#type' => 'select',
  504. '#title' => t('Time zone handling'),
  505. '#default_value' => $tz_handling,
  506. '#options' => date_timezone_handling_options(),
  507. '#description' => $description,
  508. '#attached' => array(
  509. 'js' => array(drupal_get_path('module', 'date') . '/date_admin.js'),
  510. ),
  511. );
  512. // Force this value to hidden because we don't want to allow it to be changed
  513. // right now, but allow it to be a variable if needed.
  514. $form['timezone_db'] = array(
  515. '#type' => 'hidden',
  516. '#value' => date_get_timezone_db($tz_handling),
  517. );
  518. $form['cache_enabled'] = array(
  519. '#type' => 'checkbox',
  520. '#title' => t('Cache dates'),
  521. '#description' => t('Date objects can be created and cached as date fields are loaded rather than when they are displayed to improve performance.'),
  522. '#default_value' => !empty($settings['cache_enabled']),
  523. '#weight' => 10,
  524. );
  525. $form['cache_count'] = array(
  526. '#type' => 'textfield',
  527. '#title' => t('Maximum dates per field'),
  528. '#default_value' => (isset($settings['cache_count'])) ? $settings['cache_count'] : 4,
  529. '#description' => t("If set to '0', all date values on every entity will be cached. Note that caching every date on fields that may have a large number of multiple or repeating values may create a significant performance penalty when the cache is cleared. The suggested setting for multiple value and repeating fields is no more than 4 values per field."),
  530. '#size' => 3,
  531. '#weight' => 11,
  532. '#states' => array(
  533. 'visible' => array(
  534. 'input[name="field[settings][cache_enabled]"]' => array(
  535. 'checked' => TRUE,
  536. ),
  537. ),
  538. ),
  539. );
  540. $context = array(
  541. 'field' => $field,
  542. 'instance' => $instance,
  543. 'has_data' => $has_data,
  544. );
  545. drupal_alter('date_field_settings_form', $form, $context);
  546. return $form;
  547. }
  548. /**
  549. * Form validation handler for _date_field_settings_form().
  550. */
  551. function date_field_settings_validate(&$form, &$form_state) {
  552. $field = &$form_state['values']['field'];
  553. if ($field['settings']['tz_handling'] == 'none') {
  554. form_set_value($form['timezone_db'], '', $form_state);
  555. }
  556. else {
  557. form_set_value($form['timezone_db'], date_get_timezone_db($field['settings']['tz_handling']), $form_state);
  558. }
  559. if ($field['settings']['tz_handling'] != 'none' && !in_array('hour', array_filter($field['settings']['granularity']))) {
  560. form_set_error('field[settings][tz_handling]', t('Dates without hours granularity must not use any timezone handling.'));
  561. }
  562. // Extract the correct 'todate' value out of the two end date checkboxes.
  563. if ($field['settings']['enddate_get']) {
  564. if ($field['settings']['enddate_required']) {
  565. $field['settings']['todate'] = 'required';
  566. }
  567. else {
  568. $field['settings']['todate'] = 'optional';
  569. }
  570. }
  571. else {
  572. $field['settings']['todate'] = '';
  573. }
  574. // Don't save the pseudo values created in the UI.
  575. unset($field['settings']['enddate_get'], $field['settings']['enddate_required']);
  576. if (!empty($field['settings']['cache_enabled'])) {
  577. if (!is_numeric($field['settings']['cache_count'])) {
  578. form_set_error('field[settings][cache_count]', t('The number of cache values must be a number.'));
  579. }
  580. elseif ($field['settings']['cache_count'] < 0) {
  581. form_set_error('field[settings][cache_count]', t('The number of cache values must be a number 0 or greater.'));
  582. }
  583. }
  584. }
  585. /**
  586. * Timezone handling options.
  587. *
  588. * The 'none' option will do no timezone conversions and will store and display
  589. * dates exactly as entered useful in locales or situations where timezone
  590. * conversions are not working reliably, for dates with no times, for historical
  591. * dates where timezones are irrelevant, or anytime conversion is unnecessary or
  592. * undesirable.
  593. */
  594. function date_timezone_handling_options() {
  595. return array(
  596. 'site' => t("Site's time zone"),
  597. 'date' => t("Date's time zone"),
  598. 'user' => t("User's time zone"),
  599. 'utc' => 'UTC',
  600. 'none' => t('No time zone conversion'),
  601. );
  602. }