date.module 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. $format = date_format_type_format($format_type, $langcode);
  411. break;
  412. }
  413. // A selected format might include timezone information.
  414. array_push($granularity, 'timezone');
  415. return date_limit_format($format, $granularity);
  416. }
  417. /**
  418. * Helper function to get the right format for a format type.
  419. *
  420. * Checks for locale-based format first.
  421. */
  422. function date_format_type_format($format_type, $langcode = NULL) {
  423. $static = &drupal_static(__FUNCTION__);
  424. if (!isset($static[$langcode][$format_type])) {
  425. $format = system_date_format_locale($langcode, $format_type);
  426. // If locale enabled and $format_type inexistent in {date_format_locale}
  427. // we receive (due to inconsistency of core api) an array of all (other)
  428. // formats available for $langcode in locale table.
  429. // However there's no guarantee that the key $format_type exists.
  430. // See http://drupal.org/node/1302358.
  431. if (!is_string($format)) {
  432. // If the configuration page at admin/config/regional/date-time was
  433. // never saved, the default core date format variables
  434. // ('date_format_short', 'date_format_medium', and 'date_format_long')
  435. // will not be stored in the database, so we have to define their
  436. // expected defaults here.
  437. switch ($format_type) {
  438. case 'short':
  439. $default = 'm/d/Y - H:i';
  440. break;
  441. case 'long':
  442. $default = 'l, F j, Y - H:i';
  443. break;
  444. // If it's not one of the core date types and isn't stored in the
  445. // database, we'll fall back on using the same default format as the
  446. // 'medium' type.
  447. case 'medium':
  448. default:
  449. // @todo: If a non-core module provides a date type and does not
  450. // variable_set() a default for it, the default assumed here may
  451. // not be correct (since the default format used by 'medium' may
  452. // not even be one of the allowed formats for the date type in
  453. // question). To fix this properly, we should really call
  454. // system_get_date_formats($format_type) and take the first
  455. // format from that list as the default. However, this function
  456. // is called often (on many different page requests), so calling
  457. // system_get_date_formats() from here would be a performance hit
  458. // since that function writes several records to the database
  459. // during each page request that calls it.
  460. $default = 'D, m/d/Y - H:i';
  461. break;
  462. }
  463. $format = variable_get('date_format_' . $format_type, $default);
  464. }
  465. $static[$langcode][$format_type] = $format;
  466. }
  467. return $static[$langcode][$format_type];
  468. }
  469. /**
  470. * Helper function to adapt entity date fields to formatter settings.
  471. */
  472. function date_prepare_entity($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display) {
  473. // If there are options to limit multiple values,
  474. // alter the entity values to match.
  475. $field_name = $field['field_name'];
  476. $options = $display['settings'];
  477. $max_count = $options['multiple_number'];
  478. // If no results should be shown, empty the values and return.
  479. if (is_numeric($max_count) && $max_count == 0) {
  480. $entity->{$field_name} = array();
  481. return $entity;
  482. }
  483. // Otherwise removed values that should not be displayed.
  484. if (!empty($options['multiple_from']) || !empty($options['multiple_to']) || !empty($max_count)) {
  485. $format = date_type_format($field['type']);
  486. include_once drupal_get_path('module', 'date_api') . '/date_api_sql.inc';
  487. $date_handler = new date_sql_handler($field);
  488. $arg0 = !empty($options['multiple_from']) ? $date_handler->arg_replace($options['multiple_from']) : variable_get('date_min_year', 100) . '-01-01T00:00:00';
  489. $arg1 = !empty($options['multiple_to']) ? $date_handler->arg_replace($options['multiple_to']) : variable_get('date_max_year', 4000) . '-12-31T23:59:59';
  490. if (!empty($arg0) && !empty($arg1)) {
  491. $arg = $arg0 . '--' . $arg1;
  492. }
  493. elseif (!empty($arg0)) {
  494. $arg = $arg0;
  495. }
  496. elseif (!empty($arg1)) {
  497. $arg = $arg1;
  498. }
  499. if (!empty($arg)) {
  500. $range = $date_handler->arg_range($arg);
  501. $start = date_format($range[0], $format);
  502. $end = date_format($range[1], $format);
  503. // Empty out values we don't want to see.
  504. $count = 0;
  505. foreach ($entity->{$field_name}[$langcode] as $delta => $value) {
  506. if (!empty($entity->date_repeat_show_all)) {
  507. break;
  508. }
  509. elseif ((!empty($max_count) && is_numeric($max_count) && $count >= $max_count) ||
  510. (!empty($value['value']) && $value['value'] < $start) ||
  511. (!empty($value['value2']) && $value['value2'] > $end)) {
  512. unset($entity->{$field_name}[$langcode][$delta]);
  513. }
  514. else {
  515. $count++;
  516. }
  517. }
  518. }
  519. }
  520. return $entity;
  521. }
  522. /**
  523. * Callback to alter the property info of date fields.
  524. *
  525. * @see date_field_info()
  526. */
  527. function date_entity_metadata_property_info_alter(&$info, $entity_type, $field, $instance, $field_type) {
  528. $name = $field['field_name'];
  529. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
  530. if ($field['type'] != 'datestamp' || $field['settings']['timezone_db'] != 'UTC') {
  531. // Add a getter callback to convert the date into the right format.
  532. $property['getter callback'] = 'date_entity_metadata_field_getter';
  533. $property['setter callback'] = 'date_entity_metadata_field_setter';
  534. unset($property['query callback']);
  535. }
  536. if (!empty($field['settings']['todate'])) {
  537. // Define a simple data structure containing both dates.
  538. $property['type'] = ($field['cardinality'] != 1) ? 'list<struct>' : 'struct';
  539. $property['auto creation'] = 'date_entity_metadata_struct_create';
  540. $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  541. $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  542. $property['property info'] = array(
  543. 'value' => array(
  544. 'type' => 'date',
  545. 'label' => t('Start date'),
  546. 'getter callback' => 'date_entity_metadata_struct_getter',
  547. 'setter callback' => 'date_entity_metadata_struct_setter',
  548. // The getter and setter callbacks for 'value' and 'value2'
  549. // will not provide the field name as $name, we'll add it to $info.
  550. 'field_name' => $field['field_name'],
  551. // Alert Microdata module that this value can be exposed in microdata.
  552. 'microdata' => TRUE,
  553. ),
  554. 'value2' => array(
  555. 'type' => 'date',
  556. 'label' => t('End date'),
  557. 'getter callback' => 'date_entity_metadata_struct_getter',
  558. 'setter callback' => 'date_entity_metadata_struct_setter',
  559. // The getter and setter callbacks for 'value' and 'value2'
  560. // will not provide the field name as $name, we'll add it to $info.
  561. 'field_name' => $field['field_name'],
  562. // Alert Microdata module that this value can be exposed in microdata.
  563. 'microdata' => TRUE,
  564. ),
  565. 'duration' => array(
  566. 'type' => 'duration',
  567. 'label' => t('Duration'),
  568. 'desription' => t('The duration of the time period given by the dates.'),
  569. 'getter callback' => 'date_entity_metadata_duration_getter',
  570. // No setter callback for duration.
  571. // The getter callback for duration will not provide the field name
  572. // as $name, we'll add it to $info.
  573. 'field_name' => $field['field_name'],
  574. ),
  575. );
  576. unset($property['query callback']);
  577. }
  578. else {
  579. // If this doesn't have a todate, it is handled as a date rather than a
  580. // struct. Enable microdata on the field itself rather than the properties.
  581. $property['microdata'] = TRUE;
  582. }
  583. }
  584. /**
  585. * Getter callback to return date values as datestamp in UTC from the field.
  586. */
  587. function date_entity_metadata_field_getter($entity, array $options, $name, $entity_type, &$context) {
  588. $return = entity_metadata_field_verbatim_get($entity, $options, $name, $entity_type, $context);
  589. $items = ($context['field']['cardinality'] == 1) ? array($return) : $return;
  590. foreach ($items as $key => $item) {
  591. $items[$key] = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $context);
  592. }
  593. return ($context['field']['cardinality'] == 1) ? $items[0] : $items;
  594. }
  595. /**
  596. * Getter callback to return date values as datestamp in UTC.
  597. */
  598. function date_entity_metadata_struct_getter($item, array $options, $name, $type, $info) {
  599. $value = trim($item[$name]);
  600. if (empty($value)) {
  601. return NULL;
  602. }
  603. $timezone_db = !empty($item['timezone_db']) ? $item['timezone_db'] : 'UTC';
  604. $date = new DateObject($value, $timezone_db);
  605. return !empty($date) ? date_format_date($date, 'custom', 'U') : NULL;
  606. }
  607. /**
  608. * Getter callback to return the duration of the time period given by the dates.
  609. */
  610. function date_entity_metadata_duration_getter($item, array $options, $name, $type, $info) {
  611. $value = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $info);
  612. $value2 = date_entity_metadata_struct_getter($item, $options, 'value2', 'struct', $info);
  613. if ($value && $value2) {
  614. return $value2 - $value;
  615. }
  616. }
  617. /**
  618. * Callback for setting field property values.
  619. *
  620. * Based on entity_metadata_field_property_set(), the original property setter,
  621. * adapted to transform non-timestamp date values to timestamps.
  622. */
  623. function date_entity_metadata_field_setter(&$entity, $name, $value, $langcode, $entity_type, $info) {
  624. $field = field_info_field($name);
  625. if (!isset($langcode)) {
  626. // Try to figure out the default language used by the entity.
  627. // @todo: Update once http://drupal.org/node/1260640 has been fixed.
  628. $langcode = isset($entity->language) ? $entity->language : LANGUAGE_NONE;
  629. }
  630. $values = $field['cardinality'] == 1 ? array($value) : (array) $value;
  631. $items = array();
  632. foreach ($values as $delta => $value) {
  633. // Make use of the struct setter to convert the date back to a timestamp.
  634. $info['field_name'] = $name;
  635. date_entity_metadata_struct_setter($items[$delta], 'value', $value, $langcode, 'struct', $info);
  636. }
  637. $entity->{$name}[$langcode] = $items;
  638. // Empty the static field language cache, so the field system picks up any
  639. // possible new languages.
  640. drupal_static_reset('field_language');
  641. }
  642. /**
  643. * Auto creation callback for fields which contain two date values in one.
  644. */
  645. function date_entity_metadata_struct_create($name, $property_info) {
  646. return array(
  647. 'date_type' => $property_info['field']['columns'][$name]['type'],
  648. 'timezone_db' => $property_info['field']['settings']['timezone_db'],
  649. );
  650. }
  651. /**
  652. * Callback for setting an individual field value if a to-date may be there too.
  653. *
  654. * Based on entity_property_verbatim_set().
  655. *
  656. * The passed in unix timestamp (UTC) is converted to the right value and format dependent on the field.
  657. *
  658. * $name is either 'value' or 'value2'.
  659. */
  660. function date_entity_metadata_struct_setter(&$item, $name, $value, $langcode, $type, $info) {
  661. if (!isset($value)) {
  662. $item[$name] = NULL;
  663. }
  664. else {
  665. $field = field_info_field($info['field_name']);
  666. $format = date_type_format($field['type']);
  667. $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  668. $date = new DateObject($value, 'UTC');
  669. if ($timezone_db != 'UTC') {
  670. date_timezone_set($date, timezone_open($timezone_db));
  671. }
  672. $item[$name] = $date->format($format);
  673. }
  674. }
  675. /**
  676. * Duplicate functionality of what is now date_all_day_field() in the Date All Day module.
  677. *
  678. * Copy left here to avoid breaking other modules that use this function.
  679. *
  680. * DEPRECATED!, will be removed at some time in the future.
  681. */
  682. function date_field_all_day($field, $instance, $date1, $date2 = NULL) {
  683. if (empty($date1) || !is_object($date1)) {
  684. return FALSE;
  685. }
  686. elseif (!date_has_time($field['settings']['granularity'])) {
  687. return TRUE;
  688. }
  689. if (empty($date2)) {
  690. $date2 = $date1;
  691. }
  692. $granularity = date_granularity_precision($field['settings']['granularity']);
  693. $increment = isset($instance['widget']['settings']['increment']) ? $instance['widget']['settings']['increment'] : 1;
  694. return date_is_all_day(date_format($date1, DATE_FORMAT_DATETIME), date_format($date2, DATE_FORMAT_DATETIME), $granularity, $increment);
  695. }
  696. /**
  697. * Generates a Date API SQL handler for the given date field.
  698. *
  699. * The handler will be set up to make the correct timezone adjustments
  700. * for the field settings.
  701. *
  702. * @param array $field
  703. * The $field array.
  704. * @param string $compare_tz
  705. * The timezone used for comparison values in the SQL.
  706. *
  707. * DEPRECATED!, will be removed at some time in the future.
  708. */
  709. function date_field_get_sql_handler($field, $compare_tz = NULL) {
  710. module_load_include('inc', 'date_api', 'date_api_sql');
  711. $db_info = date_api_database_info($field);
  712. // Create a DateAPI SQL handler class for this field type.
  713. $handler = new date_sql_handler($field['type']);
  714. // If this date field stores a timezone in the DB, tell the handler about it.
  715. if ($field['settings']['tz_handling'] == 'date') {
  716. $handler->db_timezone_field = $db_info['columns']['timezone']['column'];
  717. }
  718. else {
  719. $handler->db_timezone = date_get_timezone_db($field['settings']['tz_handling']);
  720. }
  721. if (empty($compare_tz)) {
  722. $compare_tz = date_get_timezone($field['settings']['tz_handling']);
  723. }
  724. $handler->local_timezone = $compare_tz;
  725. // Now that the handler is properly initialized, force the DB
  726. // to use UTC so no timezone conversions get added to things like
  727. // NOW() or FROM_UNIXTIME().
  728. $handler->set_db_timezone();
  729. return $handler;
  730. }
  731. /**
  732. * Implements hook_field_widget_properties_alter().
  733. *
  734. * Alters the widget properties of a field instance before it gets displayed.
  735. * Used here to flag new entities so we can later tell if they need default values.
  736. */
  737. function date_field_widget_properties_alter(&$widget, $context) {
  738. if (in_array($widget['type'], array('date_select', 'date_text', 'date_popup'))) {
  739. $entity_type = $context['entity_type'];
  740. $entity = $context['entity'];
  741. $info = entity_get_info($entity_type);
  742. $id = $info['entity keys']['id'];
  743. $widget['is_new'] = FALSE;
  744. if (empty($entity->$id)) {
  745. $widget['is_new'] = TRUE;
  746. }
  747. }
  748. }