geofield.feeds.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * @file
  4. * Provides integration with Feeds module (http://drupal.org/project/feeds)
  5. */
  6. /**
  7. * Implementation of hook_feeds_parser_sources_alter().
  8. *
  9. * @see geofield_feeds_combined_source()
  10. */
  11. function geofield_feeds_parser_sources_alter(&$sources, $content_type) {
  12. $sources['geofield'] = array(
  13. 'name' => t('Geofield (combined)'),
  14. 'description' => t('All geographic information from the item.'),
  15. 'callback' => 'geofield_feeds_combined_source',
  16. );
  17. }
  18. /**
  19. * Callback; Provides a source combining geo information from items.
  20. *
  21. * Currently handles geo output from:
  22. * - simplepie 1.3
  23. * - common syndication parser (feeds 7.x-2.x)
  24. *
  25. * @param $source
  26. * The FeedsSource object being imported.
  27. * @param $result
  28. * The FeedsParserResult object being mapped from.
  29. * @param $key
  30. * The key specified in the $sources array in
  31. * hook_feeds_parser_sources_alter().
  32. *
  33. * @return
  34. * The value to be extracted from the source.
  35. *
  36. * @see geofield_feeds_parser_sources_alter()
  37. */
  38. function geofield_feeds_combined_source($source, FeedsParserResult $result, $key) {
  39. $values = array();
  40. $item = $result->currentItem();
  41. // Simplepie 1.3 output
  42. if (isset($item['location_latitude'])) {
  43. // Lon X; lat Y
  44. foreach ($item['location_latitude'] as $key => $lat) {
  45. $point = array('lat' => $lat, 'lon' => $item['location_longitude'][$key]);
  46. $values[] = geofield_compute_values($point, 'latlon');
  47. }
  48. }
  49. // Common Syndication Parser
  50. elseif (isset($item['geolocations'][0]) && is_a($item['geolocations'][0], 'FeedsGeoTermElement')) {
  51. // Presently Common Syndication Parser is just parsing points?
  52. // and is creating FeedsGeoTermElements, which is possibly not so useful.
  53. // Maybe better if we could read access the original item, or add in to the item parsing?
  54. foreach ($item['geolocations'] as $geolocation) {
  55. $point = array('lat' => $geolocation->lat, 'lon' => $geolocation->lon);
  56. $values[] = geofield_compute_values($point, 'latlon');
  57. }
  58. }
  59. return $values;
  60. }
  61. /**
  62. * Implements hook_feeds_node_processor_targets_alter().
  63. */
  64. function geofield_feeds_processor_targets_alter(&$targets, $entity_type,
  65. $bundle_name) {
  66. foreach (field_info_instances($entity_type, $bundle_name) as
  67. $name => $instance) {
  68. $info = field_info_field($name);
  69. if ($info['type'] == 'geofield') {
  70. $targets[$info['field_name'] . ':wkt'] = array(
  71. 'name' => t($instance['label'] . ' WKT'),
  72. 'callback' => 'geofield_set_target_wkt',
  73. 'real_target' => $info['field_name'],
  74. );
  75. $targets[$info['field_name'] . ':lat'] = array(
  76. 'name' => t($instance['label'] . ' Latitude'),
  77. 'callback' => 'geofield_set_target_simple',
  78. 'real_target' => $info['field_name'],
  79. );
  80. $targets[$info['field_name'] . ':lon'] = array(
  81. 'name' => t($instance['label'] . ' Longitude'),
  82. 'callback' => 'geofield_set_target_simple',
  83. 'real_target' => $info['field_name'],
  84. );
  85. $targets[$info['field_name'] . ':left'] = array(
  86. 'name' => t($instance['label'] . ' Left Latitude'),
  87. 'callback' => 'geofield_set_target_simple',
  88. 'real_target' => $info['field_name'],
  89. );
  90. $targets[$info['field_name'] . ':top'] = array(
  91. 'name' => t($instance['label'] . ' Top Longitude'),
  92. 'callback' => 'geofield_set_target_simple',
  93. 'real_target' => $info['field_name'],
  94. );
  95. $targets[$info['field_name'] . ':right'] = array(
  96. 'name' => t($instance['label'] . ' Right Latitude'),
  97. 'callback' => 'geofield_set_target_simple',
  98. 'real_target' => $info['field_name'],
  99. );
  100. $targets[$info['field_name'] . ':bottom'] = array(
  101. 'name' => t($instance['label'] . ' Bottom Longitude'),
  102. 'callback' => 'geofield_set_target_simple',
  103. 'real_target' => $info['field_name'],
  104. );
  105. $targets[$info['field_name'] . ':combined'] = array(
  106. 'name' => t($instance['label'] . ' (combined)'),
  107. 'callback' => 'geofield_set_target_combined',
  108. 'real_target' => $info['field_name'],
  109. );
  110. }
  111. }
  112. }
  113. /**
  114. * Example callback specified in hook_feeds_processor_targets_alter().
  115. *
  116. * @param $source
  117. * Field mapper source settings.
  118. * @param $entity
  119. * An entity object, for instance a node object.
  120. * @param $target
  121. * A string identifying the target on the node.
  122. * @param $value
  123. * The value to populate the target with.
  124. *
  125. */
  126. function geofield_set_target_simple($source, $entity, $target, $value) {
  127. list($field_name, $sub_field) = explode(':', $target, 2);
  128. $entity->{$field_name}['und'][0][$sub_field] = $value;
  129. }
  130. /**
  131. * Feeds processor target callback from the already combined source.
  132. *
  133. * @see geofield_feeds_parser_sources_alter()
  134. *
  135. * @param $source
  136. * Field mapper source settings.
  137. * @param $entity
  138. * An entity object, for instance a node object.
  139. * @param $target
  140. * A string identifying the target on the node.
  141. * @param $value
  142. * The value to populate the target with.
  143. *
  144. */
  145. function geofield_set_target_combined($source, $entity, $target, $value) {
  146. $field_name = substr($target, 0, strpos($target, ':'));
  147. _geofield_set_target($source, $entity, $field_name, $value);
  148. }
  149. /**
  150. * Feeds processor target callback for WKT source.
  151. */
  152. function geofield_set_target_wkt($source, $entity, $target, $value) {
  153. $field_name = substr($target, 0, strpos($target, ':'));
  154. $geofield_values = array();
  155. if (is_array($value)) {
  156. foreach ($value as $key => $wkt) {
  157. $field = array('wkt' => $wkt);
  158. $geofield_values[] = geofield_compute_values($field, 'wkt');
  159. }
  160. }
  161. else {
  162. $field = array('wkt' => $value);
  163. $geofield_values[] = geofield_compute_values($field, 'wkt');
  164. }
  165. _geofield_set_target($source, $entity, $field_name, $geofield_values);
  166. }
  167. /**
  168. * Helper function to set values and respect ordinality of field.
  169. *
  170. * Based on _field_feeds_set_target(). But type set, more keys than just value.
  171. *
  172. * @param $source
  173. * A FeedsSource object.
  174. * @param $entity
  175. * The entity to map to.
  176. * @param $target
  177. * The target key on $entity to map to.
  178. * @param $value
  179. * The value to map. MUST be an array.
  180. */
  181. function _geofield_set_target($source, $entity, $target, $value) {
  182. if (empty($value)) {
  183. return;
  184. }
  185. $info = field_info_field($target);
  186. // Iterate over all values.
  187. $i = 0;
  188. $field = isset($entity->$target) ? $entity->$target : array();
  189. foreach ($value as $v) {
  190. // Check if field value is empty.
  191. if(empty($v)){
  192. continue;
  193. }
  194. if (is_array($v) || is_object($v)) {
  195. $field['und'][$i] = $v;
  196. }
  197. if ($info['cardinality'] == 1) {
  198. break;
  199. }
  200. $i++;
  201. }
  202. $entity->{$target} = $field;
  203. }