date_api_elements.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. <?php
  2. /**
  3. * @file
  4. * Date API elements themes and validation.
  5. * This file is only included during the edit process to reduce memory usage.
  6. */
  7. /**
  8. * Implements hook_element_info().
  9. *
  10. * Parameters for date form elements, designed to have sane defaults so any
  11. * or all can be omitted.
  12. *
  13. * Fill the element #default_value with a date in datetime format,
  14. * (YYYY-MM-DD HH:MM:SS), adjusted to the proper local timezone.
  15. *
  16. * NOTE - Converting a date stored in the database from UTC to the local zone
  17. * and converting it back to UTC before storing it is not handled by this
  18. * element and must be done in pre-form and post-form processing!!
  19. *
  20. * The date_select element will create a collection of form elements, with a
  21. * separate select or textfield for each date part. The whole collection will
  22. * get reformatted back to a date value of the requested type during validation.
  23. *
  24. * The date_text element will create a textfield that can contain a whole
  25. * date or any part of a date as text. The user input value will be re-formatted
  26. * back into a date value of the requested type during validation.
  27. *
  28. * The date_timezone element will create a drop-down selector to pick a
  29. * timezone name.
  30. *
  31. * The date_year_range element will create two textfields (for users with
  32. * JavaScript enabled they will appear as drop-down selectors with an option
  33. * for custom text entry) to pick a range of years that will be passed to form
  34. * submit handlers as a single string (e.g., -3:+3).
  35. *
  36. * #date_timezone
  37. * The local timezone to be used to create this date.
  38. *
  39. * #date_format
  40. * A format string that describes the format and order of date parts to
  41. * display in the edit form for this element. This makes it possible
  42. * to show date parts in a custom order, or to leave some of them out.
  43. * Be sure to add 'A' or 'a' to get an am/pm selector. Defaults to the
  44. * short site default format.
  45. *
  46. * #date_label_position
  47. * Handling option for date part labels, like 'Year', 'Month', and 'Day',
  48. * can be 'above' the date part, 'within' it, or 'none', default is 'above' .
  49. * The 'within' option shows the label as the first option in a select list
  50. * or the default value for an empty textfield, taking up less screen space.
  51. *
  52. * #date_increment
  53. * Increment minutes and seconds by this amount, default is 1.
  54. *
  55. * #date_year_range
  56. * The number of years to go back and forward in a year selector,
  57. * default is -3:+3 (3 back and 3 forward).
  58. *
  59. * #date_text_parts
  60. * Array of date parts that should use textfields instead of selects
  61. * i.e. array('year') will format the year as a textfield and other
  62. * date parts as drop-down selects.
  63. */
  64. function _date_api_element_info() {
  65. $date_base = array(
  66. '#input' => TRUE, '#tree' => TRUE,
  67. '#date_timezone' => date_default_timezone(),
  68. '#date_flexible' => 0,
  69. '#date_format' => variable_get('date_format_short', 'm/d/Y - H:i'),
  70. '#date_text_parts' => array(),
  71. '#date_increment' => 1,
  72. '#date_year_range' => '-3:+3',
  73. '#date_label_position' => 'above',
  74. );
  75. if (module_exists('ctools')) {
  76. $date_base['#pre_render'] = array('ctools_dependent_pre_render');
  77. }
  78. $type['date_select'] = array_merge($date_base, array(
  79. '#process' => array('date_select_element_process'),
  80. '#theme_wrappers' => array('date_select'),
  81. '#value_callback' => 'date_select_element_value_callback',
  82. ));
  83. $type['date_text'] = array_merge($date_base, array(
  84. '#process' => array('date_text_element_process'),
  85. '#theme_wrappers' => array('date_text'),
  86. '#value_callback' => 'date_text_element_value_callback',
  87. ));
  88. $type['date_timezone'] = array(
  89. '#input' => TRUE, '#tree' => TRUE,
  90. '#process' => array('date_timezone_element_process'),
  91. '#theme_wrappers' => array('date_text'),
  92. '#value_callback' => 'date_timezone_element_value_callback',
  93. );
  94. $type['date_year_range'] = array(
  95. '#input' => TRUE,
  96. '#process' => array('date_year_range_element_process'),
  97. '#value_callback' => 'date_year_range_element_value_callback',
  98. '#element_validate' => array('date_year_range_validate'),
  99. );
  100. return $type;
  101. }
  102. /**
  103. * Create a date object from a datetime string value.
  104. */
  105. function date_default_date($element) {
  106. $granularity = date_format_order($element['#date_format']);
  107. $default_value = $element['#default_value'];
  108. $format = DATE_FORMAT_DATETIME;
  109. // The text and popup widgets might return less than a full datetime string.
  110. if (is_string($element['#default_value']) && strlen($element['#default_value']) < 19) {
  111. switch (strlen($element['#default_value'])) {
  112. case 16:
  113. $format = 'Y-m-d H:i';
  114. break;
  115. case 13:
  116. $format = 'Y-m-d H';
  117. break;
  118. case 10:
  119. $format = 'Y-m-d';
  120. break;
  121. case 7:
  122. $format = 'Y-m';
  123. break;
  124. case 4:
  125. $format = 'Y';
  126. break;
  127. }
  128. }
  129. $date = new DateObject($default_value, $element['#date_timezone'], $format);
  130. if (is_object($date)) {
  131. $date->limitGranularity($granularity);
  132. if ($date->validGranularity($granularity, $element['#date_flexible'])) {
  133. date_increment_round($date, $element['#date_increment']);
  134. }
  135. return $date;
  136. }
  137. return NULL;
  138. }
  139. /**
  140. * Process callback which creates a date_year_range form element.
  141. */
  142. function date_year_range_element_process($element, &$form_state, $form) {
  143. // Year range is stored in the -3:+3 format, but collected as two separate
  144. // textfields.
  145. $element['years_back'] = array(
  146. '#type' => 'textfield',
  147. '#title' => t('Starting year'),
  148. '#default_value' => $element['#value']['years_back'],
  149. '#size' => 10,
  150. '#maxsize' => 10,
  151. '#attributes' => array('class' => array('select-list-with-custom-option', 'back')),
  152. '#description' => t('Enter a relative value (-9, +9) or an absolute year such as 2015.'),
  153. );
  154. $element['years_forward'] = array(
  155. '#type' => 'textfield',
  156. '#title' => t('Ending year'),
  157. '#default_value' => $element['#value']['years_forward'],
  158. '#size' => 10,
  159. '#maxsize' => 10,
  160. '#attributes' => array('class' => array('select-list-with-custom-option', 'forward')),
  161. '#description' => t('Enter a relative value (-9, +9) or an absolute year such as 2015.'),
  162. );
  163. $element['#tree'] = TRUE;
  164. $element['#attached']['js'][] = drupal_get_path('module', 'date_api') . '/date_year_range.js';
  165. $context = array(
  166. 'form' => $form,
  167. );
  168. drupal_alter('date_year_range_process', $element, $form_state, $context);
  169. return $element;
  170. }
  171. /**
  172. * Element value callback for the date_year_range form element.
  173. */
  174. function date_year_range_element_value_callback($element, $input = FALSE, &$form_state = array()) {
  175. // Convert the element's default value from a string to an array (to match
  176. // what we will get from the two textfields when the form is submitted).
  177. if ($input === FALSE) {
  178. list($years_back, $years_forward) = explode(':', $element['#default_value']);
  179. return array(
  180. 'years_back' => $years_back,
  181. 'years_forward' => $years_forward,
  182. );
  183. }
  184. }
  185. /**
  186. * Element validation function for the date_year_range form element.
  187. */
  188. function date_year_range_validate(&$element, &$form_state) {
  189. // Recombine the two submitted form values into the -3:+3 format we will
  190. // validate and save.
  191. $year_range_submitted = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  192. $year_range = $year_range_submitted['years_back'] . ':' . $year_range_submitted['years_forward'];
  193. drupal_array_set_nested_value($form_state['values'], $element['#parents'], $year_range);
  194. if (!date_range_valid($year_range)) {
  195. form_error($element['years_back'], t('Starting year must be in the format -9, or an absolute year such as 1980.'));
  196. form_error($element['years_forward'], t('Ending year must be in the format +9, or an absolute year such as 2030.'));
  197. }
  198. }
  199. /**
  200. * Element value callback for date_timezone element.
  201. */
  202. function date_timezone_element_value_callback($element, $input = FALSE, &$form_state = array()) {
  203. $return = '';
  204. if ($input !== FALSE) {
  205. $return = $input;
  206. }
  207. elseif (!empty($element['#default_value'])) {
  208. $return = array('timezone' => $element['#default_value']);
  209. }
  210. return $return;
  211. }
  212. /**
  213. * Creates a timezone form element.
  214. *
  215. * @param array $element
  216. * The timezone form element.
  217. *
  218. * @return array
  219. * the timezone form element
  220. */
  221. function date_timezone_element_process($element, &$form_state, $form) {
  222. if (date_hidden_element($element)) {
  223. return $element;
  224. }
  225. $element['#tree'] = TRUE;
  226. $label = theme('date_part_label_timezone', array('part_type' => 'select', 'element' => $element));
  227. $element['timezone'] = array(
  228. '#type' => 'select',
  229. '#title' => $label,
  230. '#title_display' => $element['#date_label_position'] == 'above' ? 'before' : 'invisible',
  231. '#options' => date_timezone_names($element['#required']),
  232. '#value' => $element['#value'],
  233. '#weight' => $element['#weight'],
  234. '#required' => $element['#required'],
  235. '#theme' => 'date_select_element',
  236. '#theme_wrappers' => array('form_element'),
  237. );
  238. if (isset($element['#element_validate'])) {
  239. array_push($element['#element_validate'], 'date_timezone_validate');
  240. }
  241. else {
  242. $element['#element_validate'] = array('date_timezone_validate');
  243. }
  244. $context = array(
  245. 'form' => $form,
  246. );
  247. drupal_alter('date_timezone_process', $element, $form_state, $context);
  248. return $element;
  249. }
  250. /**
  251. * Validation for timezone input.
  252. *
  253. * Move the timezone value from the nested field back to the original field.
  254. */
  255. function date_timezone_validate($element, &$form_state) {
  256. if (date_hidden_element($element)) {
  257. return;
  258. }
  259. form_set_value($element, $element['#value']['timezone'], $form_state);
  260. }
  261. /**
  262. * Element value callback for date_text element.
  263. */
  264. function date_text_element_value_callback($element, $input = FALSE, &$form_state = array()) {
  265. $return = array('date' => '');
  266. $date = NULL;
  267. // Normal input from submitting the form element.
  268. // Check is_array() to skip the string input values created by Views pagers.
  269. // Those string values, if present, should be interpreted as empty input.
  270. if ($input != FALSE && is_array($input)) {
  271. $return = $input;
  272. $date = date_text_input_date($element, $input);
  273. }
  274. // No input? Try the default value.
  275. elseif (!empty($element['#default_value'])) {
  276. $date = date_default_date($element);
  277. }
  278. if (date_is_date($date)) {
  279. $return['date'] = date_format_date($date, 'custom', $element['#date_format']);
  280. }
  281. return $return;
  282. }
  283. /**
  284. * Text date input form.
  285. *
  286. * Display all or part of a date in a single textfield.
  287. *
  288. * The exact parts displayed in the field are those in #date_granularity.
  289. * The display of each part comes from #date_format.
  290. */
  291. function date_text_element_process($element, &$form_state, $form) {
  292. if (date_hidden_element($element)) {
  293. return $element;
  294. }
  295. $element['#tree'] = TRUE;
  296. $element['#theme_wrappers'] = array('date_text');
  297. $element['date']['#value'] = isset($element['#value']['date']) ? $element['#value']['date'] : '';
  298. $element['date']['#type'] = 'textfield';
  299. $element['date']['#weight'] = !empty($element['date']['#weight']) ? $element['date']['#weight'] : $element['#weight'];
  300. $element['date']['#attributes'] = array('class' => isset($element['#attributes']['class']) ? $element['#attributes']['class'] += array('date-date') : array('date-date'));
  301. $now = date_example_date();
  302. $element['date']['#title'] = t('Date');
  303. $element['date']['#title_display'] = 'invisible';
  304. $element['date']['#description'] = ' ' . t('Format: @date', array(
  305. '@date' => date_format_date(date_example_date(), 'custom', $element['#date_format']
  306. )));
  307. $element['date']['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
  308. // Make changes if instance is set to be rendered as a regular field.
  309. if (!empty($element['#instance']['widget']['settings']['no_fieldset']) && $element['#field']['cardinality'] == 1) {
  310. $element['date']['#title'] = check_plain($element['#instance']['label']);
  311. $element['date']['#title_display'] = $element['#title_display'];
  312. $element['date']['#required'] = $element['#required'];
  313. }
  314. // Keep the system from creating an error message for the sub-element.
  315. // We'll set our own message on the parent element.
  316. // $element['date']['#required'] = $element['#required'];
  317. $element['date']['#theme'] = 'date_textfield_element';
  318. if (isset($element['#element_validate'])) {
  319. array_push($element['#element_validate'], 'date_text_validate');
  320. }
  321. else {
  322. $element['#element_validate'] = array('date_text_validate');
  323. }
  324. if (!empty($element['#force_value'])) {
  325. $element['date']['#value'] = $element['date']['#default_value'];
  326. }
  327. $context = array(
  328. 'form' => $form,
  329. );
  330. drupal_alter('date_text_process', $element, $form_state, $context);
  331. return $element;
  332. }
  333. /**
  334. * Validation for text input.
  335. *
  336. * When used as a Views widget, the validation step always gets triggered,
  337. * even with no form submission. Before form submission $element['#value']
  338. * contains a string, after submission it contains an array.
  339. */
  340. function date_text_validate($element, &$form_state) {
  341. if (date_hidden_element($element)) {
  342. return;
  343. }
  344. if (is_string($element['#value'])) {
  345. return;
  346. }
  347. $input_exists = NULL;
  348. $input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
  349. // Trim extra spacing off user input of text fields.
  350. if (isset($input['date'])) {
  351. $input['date'] = trim($input['date']);
  352. }
  353. drupal_alter('date_text_pre_validate', $element, $form_state, $input);
  354. $label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
  355. $date = date_text_input_date($element, $input);
  356. // If the field has errors, display them.
  357. // If something was input but there is no date, the date is invalid.
  358. // If the field is empty and required, set error message and return.
  359. $error_field = implode('][', $element['#parents']);
  360. if (empty($date) || !empty($date->errors)) {
  361. if (is_object($date) && !empty($date->errors)) {
  362. $message = t('The value input for field %field is invalid:', array('%field' => $label));
  363. $message .= '<br />' . implode('<br />', $date->errors);
  364. form_set_error($error_field, $message);
  365. return;
  366. }
  367. if (!empty($element['#required'])) {
  368. $message = t('A valid date is required for %title.', array('%title' => $label));
  369. form_set_error($error_field, $message);
  370. return;
  371. }
  372. // Fall through, some other error.
  373. if (!empty($input['date'])) {
  374. form_error($element, t('%title is invalid.', array('%title' => $label)));
  375. return;
  376. }
  377. }
  378. $value = !empty($date) ? $date->format(DATE_FORMAT_DATETIME) : NULL;
  379. form_set_value($element, $value, $form_state);
  380. }
  381. /**
  382. * Helper function for creating a date object out of user input.
  383. */
  384. function date_text_input_date($element, $input) {
  385. if (empty($input) || !is_array($input) || !array_key_exists('date', $input) || empty($input['date'])) {
  386. return NULL;
  387. }
  388. $granularity = date_format_order($element['#date_format']);
  389. $date = new DateObject($input['date'], $element['#date_timezone'], $element['#date_format']);
  390. if (is_object($date)) {
  391. $date->limitGranularity($granularity);
  392. if ($date->validGranularity($granularity, $element['#date_flexible'])) {
  393. date_increment_round($date, $element['#date_increment']);
  394. }
  395. return $date;
  396. }
  397. return NULL;
  398. }
  399. /**
  400. * Element value callback for date_select element.
  401. */
  402. function date_select_element_value_callback($element, $input = FALSE, &$form_state = array()) {
  403. $return = array(
  404. 'year' => '',
  405. 'month' => '',
  406. 'day' => '',
  407. 'hour' => '',
  408. 'minute' => '',
  409. 'second' => '',
  410. );
  411. $date = NULL;
  412. if ($input !== FALSE) {
  413. $return = $input;
  414. $date = date_select_input_date($element, $input);
  415. }
  416. elseif (!empty($element['#default_value'])) {
  417. $date = date_default_date($element);
  418. }
  419. $granularity = date_format_order($element['#date_format']);
  420. $formats = array(
  421. 'year' => 'Y',
  422. 'month' => 'n',
  423. 'day' => 'j',
  424. 'hour' => 'H',
  425. 'minute' => 'i',
  426. 'second' => 's',
  427. );
  428. foreach ($granularity as $field) {
  429. if ($field != 'timezone') {
  430. $return[$field] = date_is_date($date) ? $date->format($formats[$field]) : '';
  431. }
  432. }
  433. return $return;
  434. }
  435. /**
  436. * Flexible date/time drop-down selector.
  437. *
  438. * Splits date into a collection of date and time sub-elements, one
  439. * for each date part. Each sub-element can be either a textfield or a
  440. * select, based on the value of ['#date_settings']['text_fields'].
  441. *
  442. * The exact parts displayed in the field are those in #date_granularity.
  443. * The display of each part comes from ['#date_settings']['format'].
  444. */
  445. function date_select_element_process($element, &$form_state, $form) {
  446. if (date_hidden_element($element)) {
  447. return $element;
  448. }
  449. $date = NULL;
  450. $granularity = date_format_order($element['#date_format']);
  451. if (is_array($element['#default_value'])) {
  452. $date = date_select_input_date($element, $element['#default_value']);
  453. }
  454. elseif (!empty($element['#default_value'])) {
  455. $date = date_default_date($element);
  456. }
  457. $element['#tree'] = TRUE;
  458. $element['#theme_wrappers'] = array('date_select');
  459. $element += (array) date_parts_element($element, $date, $element['#date_format']);
  460. // Store a hidden value for all date parts not in the current display.
  461. $granularity = date_format_order($element['#date_format']);
  462. $formats = array(
  463. 'year' => 'Y',
  464. 'month' => 'n',
  465. 'day' => 'j',
  466. 'hour' => 'H',
  467. 'minute' => 'i',
  468. 'second' => 's',
  469. );
  470. foreach (date_nongranularity($granularity) as $field) {
  471. if ($field != 'timezone') {
  472. $element[$field] = array(
  473. '#type' => 'value',
  474. '#value' => 0,
  475. );
  476. }
  477. }
  478. if (isset($element['#element_validate'])) {
  479. array_push($element['#element_validate'], 'date_select_validate');
  480. }
  481. else {
  482. $element['#element_validate'] = array('date_select_validate');
  483. }
  484. $context = array(
  485. 'form' => $form,
  486. );
  487. drupal_alter('date_select_process', $element, $form_state, $context);
  488. return $element;
  489. }
  490. /**
  491. * Creates form elements for one or more date parts.
  492. *
  493. * Get the order of date elements from the provided format.
  494. * If the format order omits any date parts in the granularity, alter the
  495. * granularity array to match the format, then flip the $order array
  496. * to get the position for each element. Then iterate through the
  497. * elements and create a sub-form for each part.
  498. *
  499. * @param array $element
  500. * The date element.
  501. * @param object $date
  502. * The date object.
  503. * @param string $format
  504. * A date format string.
  505. *
  506. * @return array
  507. * The form array for the submitted date parts.
  508. */
  509. function date_parts_element($element, $date, $format) {
  510. $granularity = date_format_order($format);
  511. $sub_element = array('#granularity' => $granularity);
  512. $order = array_flip($granularity);
  513. $hours_format = strpos(strtolower($element['#date_format']), 'a') ? 'g' : 'G';
  514. $month_function = strpos($element['#date_format'], 'F') !== FALSE ? 'date_month_names' : 'date_month_names_abbr';
  515. $count = 0;
  516. $increment = min(intval($element['#date_increment']), 1);
  517. // Allow empty value as option if date is not required or there is no date.
  518. $part_required = (bool) $element['#required'] && is_object($date);
  519. foreach ($granularity as $field) {
  520. $part_type = in_array($field, $element['#date_text_parts']) ? 'textfield' : 'select';
  521. $sub_element[$field] = array(
  522. '#weight' => $order[$field],
  523. '#required' => $part_required,
  524. '#attributes' => array('class' => isset($element['#attributes']['class']) ? $element['#attributes']['class'] += array('date-' . $field) : array('date-' . $field)),
  525. '#ajax' => !empty($element['#ajax']) ? $element['#ajax'] : FALSE,
  526. );
  527. switch ($field) {
  528. case 'year':
  529. $range = date_range_years($element['#date_year_range'], $date);
  530. $start_year = $range[0];
  531. $end_year = $range[1];
  532. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format('Y') : '';
  533. if ($part_type == 'select') {
  534. $sub_element[$field]['#options'] = drupal_map_assoc(date_years($start_year, $end_year, $part_required));
  535. }
  536. break;
  537. case 'month':
  538. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format('n') : '';
  539. if ($part_type == 'select') {
  540. $sub_element[$field]['#options'] = $month_function($part_required);
  541. }
  542. break;
  543. case 'day':
  544. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format('j') : '';
  545. if ($part_type == 'select') {
  546. $sub_element[$field]['#options'] = drupal_map_assoc(date_days($part_required));
  547. }
  548. break;
  549. case 'hour':
  550. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format($hours_format) : '';
  551. if ($part_type == 'select') {
  552. $sub_element[$field]['#options'] = drupal_map_assoc(date_hours($hours_format, $part_required));
  553. }
  554. $sub_element[$field]['#prefix'] = theme('date_part_hour_prefix', $element);
  555. break;
  556. case 'minute':
  557. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format('i') : '';
  558. if ($part_type == 'select') {
  559. $sub_element[$field]['#options'] = drupal_map_assoc(date_minutes('i', $part_required, $element['#date_increment']));
  560. }
  561. $sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element);
  562. break;
  563. case 'second':
  564. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format('s') : '';
  565. if ($part_type == 'select') {
  566. $sub_element[$field]['#options'] = drupal_map_assoc(date_seconds('s', $part_required, $element['#date_increment']));
  567. }
  568. $sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element);
  569. break;
  570. }
  571. // Add handling for the date part label.
  572. $label = theme('date_part_label_' . $field, array('part_type' => $part_type, 'element' => $element));
  573. if (in_array($field, $element['#date_text_parts'])) {
  574. $sub_element[$field]['#type'] = 'textfield';
  575. $sub_element[$field]['#theme'] = 'date_textfield_element';
  576. $sub_element[$field]['#size'] = 7;
  577. $sub_element[$field]['#title'] = $label;
  578. $sub_element[$field]['#title_display'] = in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before';
  579. if ($element['#date_label_position'] == 'within') {
  580. if (!empty($sub_element[$field]['#options']) && is_array($sub_element[$field]['#options'])) {
  581. $sub_element[$field]['#options'] = array(
  582. '-' . $label => '-' . $label) + $sub_element[$field]['#options'];
  583. }
  584. if (empty($sub_element[$field]['#default_value'])) {
  585. $sub_element[$field]['#default_value'] = '-' . $label;
  586. }
  587. }
  588. }
  589. else {
  590. $sub_element[$field]['#type'] = 'select';
  591. $sub_element[$field]['#theme'] = 'date_select_element';
  592. $sub_element[$field]['#title'] = $label;
  593. $sub_element[$field]['#title_display'] = in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before';
  594. if ($element['#date_label_position'] == 'within') {
  595. $sub_element[$field]['#options'] = array(
  596. '' => '-' . $label) + $sub_element[$field]['#options'];
  597. }
  598. }
  599. }
  600. // Views exposed filters are treated as submitted even if not,
  601. // so force the #default value in that case. Make sure we set
  602. // a default that is in the option list.
  603. if (!empty($element['#force_value'])) {
  604. $options = $sub_element[$field]['#options'];
  605. $default = !empty($sub_element[$field]['#default_value']) ? $sub_element[$field]['#default_value'] : array_shift($options);
  606. $sub_element[$field]['#value'] = $default;
  607. }
  608. if (($hours_format == 'g' || $hours_format == 'h') && date_has_time($granularity)) {
  609. $label = theme('date_part_label_ampm', array('part_type' => 'ampm', 'element' => $element));
  610. $sub_element['ampm'] = array(
  611. '#type' => 'select',
  612. '#theme' => 'date_select_element',
  613. '#title' => $label,
  614. '#title_display' => in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before',
  615. '#default_value' => is_object($date) ? (date_format($date, 'G') >= 12 ? 'pm' : 'am') : '',
  616. '#options' => drupal_map_assoc(date_ampm($part_required)),
  617. '#required' => $part_required,
  618. '#weight' => 8,
  619. '#attributes' => array('class' => array('date-ampm')),
  620. );
  621. if ($element['#date_label_position'] == 'within') {
  622. $sub_element['ampm']['#options'] = array('' => '-' . $label) + $sub_element['ampm']['#options'];
  623. }
  624. }
  625. return $sub_element;
  626. }
  627. /**
  628. * Validation function for date selector.
  629. *
  630. * When used as a Views widget, the validation step always gets triggered,
  631. * even with no form submission. Before form submission $element['#value']
  632. * contains a string, after submission it contains an array.
  633. */
  634. function date_select_validate($element, &$form_state) {
  635. if (date_hidden_element($element)) {
  636. return;
  637. }
  638. if (is_string($element['#value'])) {
  639. return;
  640. }
  641. $input_exists = NULL;
  642. $input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
  643. // Strip field labels out of the results.
  644. foreach ($element['#value'] as $field => $field_value) {
  645. if (substr($field_value, 0, 1) == '-') {
  646. $input[$field] = '';
  647. }
  648. }
  649. drupal_alter('date_select_pre_validate', $element, $form_state, $input);
  650. $label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
  651. if (isset($input['ampm'])) {
  652. if ($input['ampm'] == 'pm' && $input['hour'] < 12) {
  653. $input['hour'] += 12;
  654. }
  655. elseif ($input['ampm'] == 'am' && $input['hour'] == 12) {
  656. $input['hour'] -= 12;
  657. }
  658. }
  659. unset($input['ampm']);
  660. $date = date_select_input_date($element, $input);
  661. // If the field has errors, display them.
  662. $error_field = implode('][', $element['#parents']);
  663. $entered = array_values(array_filter($input));
  664. if (empty($date) || !empty($date->errors)) {
  665. // The input created a date but it has errors.
  666. if (is_object($date) && !empty($date->errors)) {
  667. $message = t('The value input for field %field is invalid:', array('%field' => $label));
  668. $message .= '<br />' . implode('<br />', $date->errors);
  669. form_set_error($error_field, $message);
  670. return;
  671. }
  672. // Nothing was entered but the date is required.
  673. elseif (empty($entered) && $element['#required']) {
  674. $message = t('A valid date is required for %title.', array('%title' => $label));
  675. form_set_error($error_field, $message);
  676. return;
  677. }
  678. // Something was input but it wasn't enough to create a valid date.
  679. elseif (!empty($entered)) {
  680. $message = t('The value input for field %field is invalid.', array('%field' => $label));
  681. form_set_error($error_field, $message);
  682. return;
  683. }
  684. }
  685. $value = !empty($date) ? $date->format(DATE_FORMAT_DATETIME) : NULL;
  686. form_set_value($element, $value, $form_state);
  687. }
  688. /**
  689. * Helper function for creating a date object out of user input.
  690. */
  691. function date_select_input_date($element, $input) {
  692. // Was anything entered? If not, we have no date.
  693. if (!is_array($input)) {
  694. return NULL;
  695. }
  696. else {
  697. $entered = array_values(array_filter($input));
  698. if (empty($entered)) {
  699. return NULL;
  700. }
  701. }
  702. $granularity = date_format_order($element['#date_format']);
  703. if (isset($input['ampm'])) {
  704. if ($input['ampm'] == 'pm' && $input['hour'] < 12) {
  705. $input['hour'] += 12;
  706. }
  707. elseif ($input['ampm'] == 'am' && $input['hour'] == 12) {
  708. $input['hour'] -= 12;
  709. }
  710. }
  711. unset($input['ampm']);
  712. // Make the input match the granularity.
  713. foreach (date_nongranularity($granularity) as $part) {
  714. unset($input[$part]);
  715. }
  716. $date = new DateObject($input, $element['#date_timezone']);
  717. if (is_object($date)) {
  718. $date->limitGranularity($granularity);
  719. if ($date->validGranularity($granularity, $element['#date_flexible'])) {
  720. date_increment_round($date, $element['#date_increment']);
  721. }
  722. return $date;
  723. }
  724. return NULL;
  725. }