date.module 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. <?php
  2. /**
  3. * @file
  4. * Defines date/time field types.
  5. */
  6. module_load_include('theme', 'date', 'date');
  7. module_load_include('inc', 'date', 'date.field');
  8. module_load_include('inc', 'date', 'date_elements');
  9. /**
  10. * Helper function to figure out the bundle name for an entity.
  11. */
  12. function date_get_entity_bundle($entity_type, $entity) {
  13. switch ($entity_type) {
  14. case 'field_collection_item':
  15. $bundle = $entity->field_name;
  16. break;
  17. default:
  18. $bundle = field_extract_bundle($entity_type, $entity);
  19. break;
  20. }
  21. // If there is no bundle name, field_info() uses the entity name as the bundle
  22. // name in its arrays.
  23. if (empty($bundle)) {
  24. $bundle = $entity_type;
  25. }
  26. return $bundle;
  27. }
  28. /**
  29. * Gets the default date format for the given field widget.
  30. */
  31. function date_default_format($type) {
  32. // Example input formats must show all possible date parts, so add seconds.
  33. $default_format = str_replace('i', 'i:s', variable_get('date_format_short', 'm/d/Y - H:i'));
  34. return $default_format;
  35. }
  36. /**
  37. * Wrapper function around each of the widget types for creating a date object.
  38. */
  39. function date_input_date($field, $instance, $element, $input) {
  40. // Trim extra spacing off user input of text fields.
  41. if (isset($input['date'])) {
  42. $input['date'] = trim($input['date']);
  43. }
  44. switch ($instance['widget']['type']) {
  45. case 'date_text':
  46. $function = 'date_text_input_date';
  47. break;
  48. case 'date_popup':
  49. $function = 'date_popup_input_date';
  50. break;
  51. default:
  52. $function = 'date_select_input_date';
  53. }
  54. return $function($element, $input);
  55. }
  56. /**
  57. * Implements hook_theme().
  58. */
  59. function date_theme() {
  60. $path = drupal_get_path('module', 'date');
  61. module_load_include('theme', 'date', 'date');
  62. $base = array(
  63. 'file' => 'date.theme',
  64. 'path' => "$path",
  65. );
  66. $themes = array(
  67. 'date_combo' => $base + array('render element' => 'element'),
  68. 'date_form_element' => $base + array('render element' => 'element'),
  69. 'date_text_parts' => $base + array('render element' => 'element'),
  70. 'date' => $base + array('render element' => 'element'),
  71. 'date_display_single' => $base + array(
  72. 'variables' => array(
  73. 'date' => NULL,
  74. 'timezone' => NULL,
  75. 'dates' => NULL,
  76. 'attributes' => array(),
  77. 'rdf_mapping' => NULL,
  78. 'add_rdf' => NULL,
  79. 'microdata' => NULL,
  80. 'add_microdata' => NULL,
  81. ),
  82. ),
  83. 'date_display_range' => $base + array(
  84. 'variables' => array(
  85. 'date1' => NULL,
  86. 'date2' => NULL,
  87. 'timezone' => NULL,
  88. 'dates' => NULL,
  89. // HTML attributes that will be applied to both the start and end dates
  90. // (unless overridden).
  91. 'attributes' => array(),
  92. // HTML attributes that will be applied to the start date only.
  93. 'attributes_start' => array(),
  94. // HTML attributes that will be applied to the end date only.
  95. 'attributes_end' => array(),
  96. 'rdf_mapping' => NULL,
  97. 'add_rdf' => NULL,
  98. 'microdata' => NULL,
  99. 'add_microdata' => NULL,
  100. ),
  101. ),
  102. 'date_display_remaining' => $base + array(
  103. 'variables' => array(
  104. 'remaining_days' => NULL,
  105. ),
  106. ),
  107. 'date_display_combination' => $base + array(
  108. 'variables' => array(
  109. 'entity_type' => NULL,
  110. 'entity' => NULL,
  111. 'field' => NULL,
  112. 'instance' => NULL,
  113. 'langcode' => NULL,
  114. 'item' => NULL,
  115. 'delta' => NULL,
  116. 'display' => NULL,
  117. 'dates' => NULL,
  118. 'attributes' => array(),
  119. 'rdf_mapping' => NULL,
  120. 'add_rdf' => NULL,
  121. 'microdata' => NULL,
  122. 'add_microdata' => NULL,
  123. ),
  124. ),
  125. 'date_display_interval' => $base + array(
  126. 'variables' => array(
  127. 'entity_type' => NULL,
  128. 'entity' => NULL,
  129. 'field' => NULL,
  130. 'instance' => NULL,
  131. 'langcode' => NULL,
  132. 'item' => NULL,
  133. 'delta' => NULL,
  134. 'display' => NULL,
  135. 'dates' => NULL,
  136. 'attributes' => array(),
  137. 'rdf_mapping' => NULL,
  138. 'add_rdf' => NULL,
  139. ),
  140. ),
  141. );
  142. return $themes;
  143. }
  144. /**
  145. * Implements hook_element_info().
  146. *
  147. * date_combo will create a 'start' and optional 'end' date, along with
  148. * an optional 'timezone' column for date-specific timezones. Each
  149. * 'start' and 'end' date will be constructed from date_select or date_text.
  150. */
  151. function date_element_info() {
  152. $type = array();
  153. $type['date_combo'] = array(
  154. '#input' => TRUE,
  155. '#delta' => 0,
  156. '#columns' => array('value', 'value2', 'timezone', 'offset', 'offset2'),
  157. '#process' => array('date_combo_element_process'),
  158. '#element_validate' => array('date_combo_validate'),
  159. '#theme_wrappers' => array('date_combo'),
  160. );
  161. if (module_exists('ctools')) {
  162. $type['date_combo']['#pre_render'] = array('ctools_dependent_pre_render');
  163. }
  164. return $type;
  165. }
  166. /**
  167. * Helper function for creating formatted date arrays from a formatter.
  168. *
  169. * Use the Date API to get an object representation of a date field.
  170. *
  171. * @param string $formatter
  172. * The date formatter.
  173. * @param string $entity_type
  174. * The entity_type for the instance
  175. * @param object $entity
  176. * The entity object.
  177. * @param array $field
  178. * The field info array.
  179. * @param array $instance
  180. * The field instance array.
  181. * @param string $langcode
  182. * The language code used by this field.
  183. * @param array $item
  184. * An entity field item, like $entity->myfield[0].
  185. * @param array $display
  186. * The instance display settings.
  187. *
  188. * @return array
  189. * An array that holds the Start and End date objects.
  190. * Each date object looks like:
  191. * date [value] => array (
  192. * [db] => array ( // the value stored in the database
  193. * [object] => the datetime object
  194. * [datetime] => 2007-02-15 20:00:00
  195. * )
  196. * [local] => array ( // the local representation of that value
  197. * [object] => the datetime object
  198. * [datetime] => 2007-02-15 14:00:00
  199. * [timezone] => US/Central
  200. * [offset] => -21600
  201. * )
  202. * )
  203. */
  204. function date_formatter_process($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display) {
  205. $dates = array();
  206. $timezone = date_default_timezone();
  207. if (empty($timezone)) {
  208. return $dates;
  209. }
  210. $granularity = date_granularity($field);
  211. $settings = $display['settings'];
  212. $field_name = $field['field_name'];
  213. $format = date_formatter_format($formatter, $settings, $granularity, $langcode);
  214. if (!isset($field['settings']['tz_handling']) || $field['settings']['tz_handling'] !== 'utc') {
  215. $timezone = isset($item['timezone']) ? $item['timezone'] : '';
  216. $timezone = date_get_timezone($field['settings']['tz_handling'], $timezone);
  217. }
  218. $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  219. $db_format = date_type_format($field['type']);
  220. $process = date_process_values($field);
  221. foreach ($process as $processed) {
  222. if (empty($item[$processed])) {
  223. $dates[$processed] = NULL;
  224. }
  225. else {
  226. // Create a date object with a GMT timezone from the database value.
  227. $dates[$processed] = array();
  228. // Check to see if this date was already created by date_field_load().
  229. if (isset($item['db'][$processed])) {
  230. $date = $item['db'][$processed];
  231. }
  232. else {
  233. $date = new DateObject($item[$processed], $timezone_db, $db_format);
  234. $date->limitGranularity($field['settings']['granularity']);
  235. }
  236. $dates[$processed]['db']['object'] = $date;
  237. $dates[$processed]['db']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
  238. date_timezone_set($date, timezone_open($timezone));
  239. $dates[$processed]['local']['object'] = $date;
  240. $dates[$processed]['local']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
  241. $dates[$processed]['local']['timezone'] = $timezone;
  242. $dates[$processed]['local']['offset'] = date_offset_get($date);
  243. // Format the date, special casing the 'interval' format which doesn't
  244. // need to be processed.
  245. $dates[$processed]['formatted'] = '';
  246. $dates[$processed]['formatted_iso'] = date_format_date($date, 'custom', 'c');
  247. if (is_object($date)) {
  248. if ($format == 'format_interval') {
  249. $dates[$processed]['interval'] = date_format_interval($date);
  250. }
  251. elseif ($format == 'format_calendar_day') {
  252. $dates[$processed]['calendar_day'] = date_format_calendar_day($date);
  253. }
  254. elseif ($format == 'U' || $format == 'r' || $format == 'c') {
  255. $dates[$processed]['formatted'] = date_format_date($date, 'custom', $format);
  256. $dates[$processed]['formatted_date'] = date_format_date($date, 'custom', $format);
  257. $dates[$processed]['formatted_time'] = '';
  258. $dates[$processed]['formatted_timezone'] = '';
  259. }
  260. elseif (!empty($format)) {
  261. $formats = _get_custom_date_format($date, $format);
  262. $dates[$processed]['formatted'] = $formats['formatted'];
  263. $dates[$processed]['formatted_date'] = $formats['date'];
  264. $dates[$processed]['formatted_time'] = $formats['time'];
  265. $dates[$processed]['formatted_timezone'] = $formats['zone'];
  266. }
  267. }
  268. }
  269. }
  270. if (empty($dates['value2'])) {
  271. $dates['value2'] = $dates['value'];
  272. }
  273. // Allow other modules to alter the date values.
  274. $context = array(
  275. 'field' => $field,
  276. 'instance' => $instance,
  277. 'format' => $format,
  278. 'entity_type' => $entity_type,
  279. 'entity' => $entity,
  280. 'langcode' => $langcode,
  281. 'item' => $item,
  282. 'display' => $display,
  283. );
  284. drupal_alter('date_formatter_dates', $dates, $context);
  285. $dates['format'] = $format;
  286. return $dates;
  287. }
  288. /**
  289. * Get a custom date format.
  290. */
  291. function _get_custom_date_format($date, $format) {
  292. $custom = array();
  293. $custom['granularities'] = array(
  294. 'date' => array('year', 'month', 'day'),
  295. 'time' => array('hour', 'minute', 'second'),
  296. 'zone' => array('timezone'),
  297. );
  298. $custom['limits'] = array(
  299. 'date' => date_limit_format($format, $custom['granularities']['date']),
  300. 'time' => date_limit_format($format, $custom['granularities']['time']),
  301. 'zone' => date_limit_format($format, $custom['granularities']['zone']),
  302. );
  303. return array(
  304. 'formatted' => date_format_date($date, 'custom', $format),
  305. 'date' => date_format_date($date, 'custom', $custom['limits']['date']),
  306. 'time' => date_format_date($date, 'custom', $custom['limits']['time']),
  307. 'zone' => date_format_date($date, 'custom', $custom['limits']['zone']),
  308. );
  309. }
  310. /**
  311. * Retrieves the granularity for a field.
  312. *
  313. * $field['settings']['granularity'] will contain an array like
  314. * ('hour' => 'hour', 'month' => 0) where the values turned on return their own
  315. * names and the values turned off return a zero need to reconfigure this into
  316. * simple array of the turned on values
  317. *
  318. * @param array $field
  319. * The field array.
  320. */
  321. function date_granularity($field) {
  322. if (!is_array($field) || !is_array($field['settings']['granularity'])) {
  323. $granularity = drupal_map_assoc(array('year', 'month', 'day'));
  324. $field['settings']['granularity'] = $granularity;
  325. }
  326. return array_values(array_filter($field['settings']['granularity']));
  327. }
  328. /**
  329. * Helper function to create an array of the date values in a field that need to be processed.
  330. */
  331. function date_process_values($field) {
  332. return $field['settings']['todate'] ? array('value', 'value2') : array('value');
  333. }
  334. /**
  335. * Implements hook_form_FORM_ID_alter() for field_ui_field_edit_form().
  336. */
  337. function date_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  338. $field = $form['#field'];
  339. $instance = $form['#instance'];
  340. if (!in_array($field['type'], array('date', 'datetime', 'datestamp'))) {
  341. return;
  342. }
  343. // Reorganize the instance settings and widget settings sections into a more
  344. // intuitive combined fieldset.
  345. $form['instance']['defaults'] = array(
  346. '#type' => 'fieldset',
  347. '#title' => t('More settings and values'),
  348. '#collapsible' => TRUE,
  349. '#collapsed' => TRUE,
  350. );
  351. $form['instance']['date_format'] = array(
  352. '#type' => 'fieldset',
  353. '#title' => t('Date entry'),
  354. '#collapsible' => TRUE,
  355. '#collapsed' => FALSE,
  356. );
  357. $form['instance']['default_values'] = array(
  358. '#type' => 'fieldset',
  359. '#title' => t('Default values'),
  360. '#collapsible' => TRUE,
  361. '#collapsed' => FALSE,
  362. );
  363. $form['instance']['years_back_and_forward'] = array(
  364. '#type' => 'fieldset',
  365. '#title' => t('Starting and ending year'),
  366. '#collapsible' => TRUE,
  367. '#collapsed' => FALSE,
  368. );
  369. $form['instance']['#pre_render'][] = 'date_field_ui_field_edit_form_pre_render';
  370. }
  371. /**
  372. * Rearrange form elements into fieldsets for presentation only.
  373. */
  374. function date_field_ui_field_edit_form_pre_render($form) {
  375. foreach ($form as $name => $element) {
  376. if (is_array($element) && isset($element['#fieldset'])) {
  377. $fieldset = $element['#fieldset'];
  378. $form[$fieldset][$name] = $element;
  379. unset($form[$name]);
  380. }
  381. }
  382. foreach (array('date_format', 'default_values', 'years_back_and_forward') as $name) {
  383. if (element_children($form[$name])) {
  384. // Force the items in the fieldset to be resorted now that the instance
  385. // and widget settings are combined.
  386. $form[$name]['#sorted'] = FALSE;
  387. $form['defaults'][$name] = $form[$name];
  388. }
  389. unset($form[$name]);
  390. }
  391. return $form;
  392. }
  393. /**
  394. * Implements hook_field_widget_error().
  395. */
  396. function date_field_widget_error($element, $error, $form, &$form_state) {
  397. form_error($element[$error['error']], $error['message']);
  398. }
  399. /**
  400. * Retrieve a date format string from formatter settings.
  401. */
  402. function date_formatter_format($formatter, $settings, $granularity = array(), $langcode = NULL) {
  403. $format_type = !empty($settings['format_type']) ? $settings['format_type'] : 'format_interval';
  404. switch ($formatter) {
  405. case 'format_interval':
  406. return 'format_interval';
  407. case 'date_plain':
  408. return 'date_plain';
  409. default:
  410. if ($format_type == 'custom') {
  411. $format = $settings['custom_date_format'];
  412. }
  413. else {
  414. $format = date_format_type_format($format_type, $langcode);
  415. }
  416. break;
  417. }
  418. // A selected format might include timezone information.
  419. array_push($granularity, 'timezone');
  420. return date_limit_format($format, $granularity);
  421. }
  422. /**
  423. * Helper function to get the right format for a format type.
  424. *
  425. * Checks for locale-based format first.
  426. */
  427. function date_format_type_format($format_type, $langcode = NULL) {
  428. $static = &drupal_static(__FUNCTION__);
  429. if (!isset($static[$langcode][$format_type])) {
  430. $format = system_date_format_locale($langcode, $format_type);
  431. // If locale enabled and $format_type inexistent in {date_format_locale}
  432. // we receive (due to inconsistency of core api) an array of all (other)
  433. // formats available for $langcode in locale table.
  434. // However there's no guarantee that the key $format_type exists.
  435. // See http://drupal.org/node/1302358.
  436. if (!is_string($format)) {
  437. // If the configuration page at admin/config/regional/date-time was
  438. // never saved, the default core date format variables
  439. // ('date_format_short', 'date_format_medium', and 'date_format_long')
  440. // will not be stored in the database, so we have to define their
  441. // expected defaults here.
  442. switch ($format_type) {
  443. case 'short':
  444. $default = 'm/d/Y - H:i';
  445. break;
  446. case 'long':
  447. $default = 'l, F j, Y - H:i';
  448. break;
  449. // If it's not one of the core date types and isn't stored in the
  450. // database, we'll fall back on using the same default format as the
  451. // 'medium' type.
  452. case 'medium':
  453. default:
  454. // @todo: If a non-core module provides a date type and does not
  455. // variable_set() a default for it, the default assumed here may
  456. // not be correct (since the default format used by 'medium' may
  457. // not even be one of the allowed formats for the date type in
  458. // question). To fix this properly, we should really call
  459. // system_get_date_formats($format_type) and take the first
  460. // format from that list as the default. However, this function
  461. // is called often (on many different page requests), so calling
  462. // system_get_date_formats() from here would be a performance hit
  463. // since that function writes several records to the database
  464. // during each page request that calls it.
  465. $default = 'D, m/d/Y - H:i';
  466. break;
  467. }
  468. $format = variable_get('date_format_' . $format_type, $default);
  469. }
  470. $static[$langcode][$format_type] = $format;
  471. }
  472. return $static[$langcode][$format_type];
  473. }
  474. /**
  475. * Helper function to adapt entity date fields to formatter settings.
  476. */
  477. function date_prepare_entity($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display) {
  478. // If there are options to limit multiple values,
  479. // alter the entity values to match.
  480. $field_name = $field['field_name'];
  481. $options = $display['settings'];
  482. $max_count = $options['multiple_number'];
  483. // If no results should be shown, empty the values and return.
  484. if (is_numeric($max_count) && $max_count == 0) {
  485. $entity->{$field_name} = array();
  486. return $entity;
  487. }
  488. // Otherwise removed values that should not be displayed.
  489. if (!empty($options['multiple_from']) || !empty($options['multiple_to']) || !empty($max_count)) {
  490. $format = date_type_format($field['type']);
  491. include_once drupal_get_path('module', 'date_api') . '/date_api_sql.inc';
  492. $date_handler = new date_sql_handler($field);
  493. $arg0 = !empty($options['multiple_from']) ? $date_handler->arg_replace($options['multiple_from']) : variable_get('date_min_year', 100) . '-01-01T00:00:00';
  494. $arg1 = !empty($options['multiple_to']) ? $date_handler->arg_replace($options['multiple_to']) : variable_get('date_max_year', 4000) . '-12-31T23:59:59';
  495. if (!empty($arg0) && !empty($arg1)) {
  496. $arg = $arg0 . '--' . $arg1;
  497. }
  498. elseif (!empty($arg0)) {
  499. $arg = $arg0;
  500. }
  501. elseif (!empty($arg1)) {
  502. $arg = $arg1;
  503. }
  504. if (!empty($arg)) {
  505. $range = $date_handler->arg_range($arg);
  506. $start = date_format($range[0], $format);
  507. $end = date_format($range[1], $format);
  508. // Empty out values we don't want to see.
  509. $count = 0;
  510. foreach ($entity->{$field_name}[$langcode] as $delta => $value) {
  511. if (!empty($entity->date_repeat_show_all)) {
  512. break;
  513. }
  514. elseif ((!empty($max_count) && is_numeric($max_count) && $count >= $max_count) ||
  515. (!empty($value['value']) && $value['value'] < $start) ||
  516. (!empty($value['value2']) && $value['value2'] > $end)) {
  517. unset($entity->{$field_name}[$langcode][$delta]);
  518. }
  519. else {
  520. $count++;
  521. }
  522. }
  523. }
  524. }
  525. return $entity;
  526. }
  527. /**
  528. * Callback to alter the property info of date fields.
  529. *
  530. * @see date_field_info()
  531. */
  532. function date_entity_metadata_property_info_alter(&$info, $entity_type, $field, $instance, $field_type) {
  533. $name = $field['field_name'];
  534. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
  535. if ($field['type'] != 'datestamp' || $field['settings']['timezone_db'] != 'UTC') {
  536. // Add a getter callback to convert the date into the right format.
  537. $property['getter callback'] = 'date_entity_metadata_field_getter';
  538. $property['setter callback'] = 'date_entity_metadata_field_setter';
  539. unset($property['query callback']);
  540. }
  541. if (!empty($field['settings']['todate'])) {
  542. // Define a simple data structure containing both dates.
  543. $property['type'] = ($field['cardinality'] != 1) ? 'list<struct>' : 'struct';
  544. $property['auto creation'] = 'date_entity_metadata_struct_create';
  545. $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  546. $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  547. $property['property info'] = array(
  548. 'value' => array(
  549. 'type' => 'date',
  550. 'label' => t('Start date'),
  551. 'getter callback' => 'date_entity_metadata_struct_getter',
  552. 'setter callback' => 'date_entity_metadata_struct_setter',
  553. // The getter and setter callbacks for 'value' and 'value2'
  554. // will not provide the field name as $name, we'll add it to $info.
  555. 'field_name' => $field['field_name'],
  556. // Alert Microdata module that this value can be exposed in microdata.
  557. 'microdata' => TRUE,
  558. ),
  559. 'value2' => array(
  560. 'type' => 'date',
  561. 'label' => t('End date'),
  562. 'getter callback' => 'date_entity_metadata_struct_getter',
  563. 'setter callback' => 'date_entity_metadata_struct_setter',
  564. // The getter and setter callbacks for 'value' and 'value2'
  565. // will not provide the field name as $name, we'll add it to $info.
  566. 'field_name' => $field['field_name'],
  567. // Alert Microdata module that this value can be exposed in microdata.
  568. 'microdata' => TRUE,
  569. ),
  570. 'duration' => array(
  571. 'type' => 'duration',
  572. 'label' => t('Duration'),
  573. 'desription' => t('The duration of the time period given by the dates.'),
  574. 'getter callback' => 'date_entity_metadata_duration_getter',
  575. // No setter callback for duration.
  576. // The getter callback for duration will not provide the field name
  577. // as $name, we'll add it to $info.
  578. 'field_name' => $field['field_name'],
  579. ),
  580. );
  581. unset($property['query callback']);
  582. }
  583. else {
  584. // If this doesn't have a todate, it is handled as a date rather than a
  585. // struct. Enable microdata on the field itself rather than the properties.
  586. $property['microdata'] = TRUE;
  587. }
  588. }
  589. /**
  590. * Getter callback to return date values as datestamp in UTC from the field.
  591. */
  592. function date_entity_metadata_field_getter($entity, array $options, $name, $entity_type, &$context) {
  593. $return = entity_metadata_field_verbatim_get($entity, $options, $name, $entity_type, $context);
  594. $items = ($context['field']['cardinality'] == 1) ? array($return) : $return;
  595. foreach ($items as $key => $item) {
  596. $items[$key] = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $context);
  597. }
  598. return ($context['field']['cardinality'] == 1) ? $items[0] : $items;
  599. }
  600. /**
  601. * Getter callback to return date values as datestamp in UTC.
  602. */
  603. function date_entity_metadata_struct_getter($item, array $options, $name, $type, $info) {
  604. $value = trim($item[$name]);
  605. if (empty($value)) {
  606. return NULL;
  607. }
  608. $timezone_db = !empty($item['timezone_db']) ? $item['timezone_db'] : 'UTC';
  609. $date = new DateObject($value, $timezone_db);
  610. return !empty($date) ? date_format_date($date, 'custom', 'U') : NULL;
  611. }
  612. /**
  613. * Getter callback to return the duration of the time period given by the dates.
  614. */
  615. function date_entity_metadata_duration_getter($item, array $options, $name, $type, $info) {
  616. $value = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $info);
  617. $value2 = date_entity_metadata_struct_getter($item, $options, 'value2', 'struct', $info);
  618. if ($value && $value2) {
  619. return $value2 - $value;
  620. }
  621. }
  622. /**
  623. * Callback for setting field property values.
  624. *
  625. * Based on entity_metadata_field_property_set(), the original property setter,
  626. * adapted to transform non-timestamp date values to timestamps.
  627. */
  628. function date_entity_metadata_field_setter(&$entity, $name, $value, $langcode, $entity_type, $info) {
  629. $field = field_info_field($name);
  630. if (!isset($langcode)) {
  631. // Try to figure out the default language used by the entity.
  632. // @todo: Update once http://drupal.org/node/1260640 has been fixed.
  633. $langcode = isset($entity->language) ? $entity->language : LANGUAGE_NONE;
  634. }
  635. $values = $field['cardinality'] == 1 ? array($value) : (array) $value;
  636. $items = array();
  637. foreach ($values as $delta => $value) {
  638. // Make use of the struct setter to convert the date back to a timestamp.
  639. $info['field_name'] = $name;
  640. date_entity_metadata_struct_setter($items[$delta], 'value', $value, $langcode, 'struct', $info);
  641. }
  642. $entity->{$name}[$langcode] = $items;
  643. // Empty the static field language cache, so the field system picks up any
  644. // possible new languages.
  645. drupal_static_reset('field_language');
  646. }
  647. /**
  648. * Auto creation callback for fields which contain two date values in one.
  649. */
  650. function date_entity_metadata_struct_create($name, $property_info) {
  651. return array(
  652. 'date_type' => $property_info['field']['columns'][$name]['type'],
  653. 'timezone_db' => $property_info['field']['settings']['timezone_db'],
  654. );
  655. }
  656. /**
  657. * Callback for setting an individual field value if a to-date may be there too.
  658. *
  659. * Based on entity_property_verbatim_set().
  660. *
  661. * The passed in unix timestamp (UTC) is converted to the right value and format dependent on the field.
  662. *
  663. * $name is either 'value' or 'value2'.
  664. */
  665. function date_entity_metadata_struct_setter(&$item, $name, $value, $langcode, $type, $info) {
  666. if (!isset($value)) {
  667. $item[$name] = NULL;
  668. }
  669. else {
  670. $field = field_info_field($info['field_name']);
  671. $format = date_type_format($field['type']);
  672. $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  673. $date = new DateObject($value, 'UTC');
  674. if ($timezone_db != 'UTC') {
  675. date_timezone_set($date, timezone_open($timezone_db));
  676. }
  677. $item[$name] = $date->format($format);
  678. }
  679. }
  680. /**
  681. * Duplicate functionality of what is now date_all_day_field() in the Date All Day module.
  682. *
  683. * Copy left here to avoid breaking other modules that use this function.
  684. *
  685. * DEPRECATED!, will be removed at some time in the future.
  686. */
  687. function date_field_all_day($field, $instance, $date1, $date2 = NULL) {
  688. if (empty($date1) || !is_object($date1)) {
  689. return FALSE;
  690. }
  691. elseif (!date_has_time($field['settings']['granularity'])) {
  692. return TRUE;
  693. }
  694. if (empty($date2)) {
  695. $date2 = $date1;
  696. }
  697. $granularity = date_granularity_precision($field['settings']['granularity']);
  698. $increment = isset($instance['widget']['settings']['increment']) ? $instance['widget']['settings']['increment'] : 1;
  699. return date_is_all_day(date_format($date1, DATE_FORMAT_DATETIME), date_format($date2, DATE_FORMAT_DATETIME), $granularity, $increment);
  700. }
  701. /**
  702. * Generates a Date API SQL handler for the given date field.
  703. *
  704. * The handler will be set up to make the correct timezone adjustments
  705. * for the field settings.
  706. *
  707. * @param array $field
  708. * The $field array.
  709. * @param string $compare_tz
  710. * The timezone used for comparison values in the SQL.
  711. *
  712. * DEPRECATED!, will be removed at some time in the future.
  713. */
  714. function date_field_get_sql_handler($field, $compare_tz = NULL) {
  715. module_load_include('inc', 'date_api', 'date_api_sql');
  716. $db_info = date_api_database_info($field);
  717. // Create a DateAPI SQL handler class for this field type.
  718. $handler = new date_sql_handler($field['type']);
  719. // If this date field stores a timezone in the DB, tell the handler about it.
  720. if ($field['settings']['tz_handling'] == 'date') {
  721. $handler->db_timezone_field = $db_info['columns']['timezone']['column'];
  722. }
  723. else {
  724. $handler->db_timezone = date_get_timezone_db($field['settings']['tz_handling']);
  725. }
  726. if (empty($compare_tz)) {
  727. $compare_tz = date_get_timezone($field['settings']['tz_handling']);
  728. }
  729. $handler->local_timezone = $compare_tz;
  730. // Now that the handler is properly initialized, force the DB
  731. // to use UTC so no timezone conversions get added to things like
  732. // NOW() or FROM_UNIXTIME().
  733. $handler->set_db_timezone();
  734. return $handler;
  735. }
  736. /**
  737. * Implements hook_field_widget_properties_alter().
  738. *
  739. * Alters the widget properties of a field instance before it gets displayed.
  740. * Used here to flag new entities so we can later tell if they need default values.
  741. */
  742. function date_field_widget_properties_alter(&$widget, $context) {
  743. if (in_array($widget['type'], array('date_select', 'date_text', 'date_popup'))) {
  744. $entity_type = $context['entity_type'];
  745. $entity = $context['entity'];
  746. $info = entity_get_info($entity_type);
  747. $id = $info['entity keys']['id'];
  748. $widget['is_new'] = FALSE;
  749. if (empty($entity->$id)) {
  750. $widget['is_new'] = TRUE;
  751. }
  752. }
  753. }