date.inc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for date
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets().
  8. *
  9. * @todo Only provides "end date" target if field allows it.
  10. */
  11. function date_feeds_processor_targets($entity_type, $bundle_name) {
  12. $targets = array();
  13. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  14. $info = field_info_field($name);
  15. if (in_array($info['type'], array('date', 'datestamp', 'datetime'))) {
  16. $targets[$name . ':start'] = array(
  17. 'name' => t('@name: Start', array('@name' => $instance['label'])),
  18. 'callback' => 'date_feeds_set_target',
  19. 'description' => t('The start date for the @name field. Also use if mapping both start and end.', array('@name' => $instance['label'])),
  20. 'real_target' => $name,
  21. );
  22. $targets[$name . ':end'] = array(
  23. 'name' => t('@name: End', array('@name' => $instance['label'])),
  24. 'callback' => 'date_feeds_set_target',
  25. 'description' => t('The end date for the @name field.', array('@name' => $instance['label'])),
  26. 'real_target' => $name,
  27. );
  28. }
  29. }
  30. return $targets;
  31. }
  32. /**
  33. * Callback for setting date values.
  34. */
  35. function date_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
  36. list($field_name, $sub_field) = explode(':', $target, 2);
  37. $delta = 0;
  38. foreach ($values as $value) {
  39. if (!($value instanceof FeedsDateTimeElement)) {
  40. if (empty($value) || !is_numeric($value) && is_string($value) && !date_create($value)) {
  41. $value = new FeedsDateTimeElement(NULL, NULL);
  42. }
  43. elseif ($sub_field == 'end') {
  44. $value = new FeedsDateTimeElement(NULL, $value);
  45. }
  46. else {
  47. $value = new FeedsDateTimeElement($value, NULL);
  48. }
  49. }
  50. $value->buildDateField($entity, $field_name, $delta, $mapping['language']);
  51. $delta++;
  52. }
  53. }