geofield.schemaorg.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Schema.org format.
  4. *
  5. * Formats shapes for output in metadata using Schema.org format. This is
  6. * different from the WKT format provided by GeoGenerator, so just use a one
  7. * off function.
  8. */
  9. function geofield_schemaorg_shape($item) {
  10. $output = '';
  11. $bottom = $item['bottom'];
  12. $left = $item['left'];
  13. $right = $item['right'];
  14. $top = $item['top'];
  15. switch ($item['geo_type']) {
  16. case 'polygon':
  17. $output = $bottom . ',' . $left . ' ';
  18. $output .= $bottom . ',' . $right . ' ';
  19. $output .= $top . ',' . $right . ' ';
  20. $output .= $top . ',' . $left . ' ';
  21. $output .= $bottom . ',' . $left;
  22. break;
  23. case 'linestring':
  24. $output = $bottom . ',' . $left . ' ';
  25. $output .= $bottom . ',' . $right . ' ';
  26. $output .= $top . ',' . $right . ' ';
  27. $output .= $top . ',' . $left;
  28. break;
  29. }
  30. return $output;
  31. }
  32. /**
  33. * Callback to alter the property info of geofield fields.
  34. *
  35. * @see geofield_field_info()
  36. */
  37. function geofield_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
  38. $name = $field['field_name'];
  39. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
  40. $property['type'] = ($field['cardinality'] != 1) ? 'list<geofield>' : 'geofield';
  41. $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  42. $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  43. // $property['auto creation'] = 'geofield_default_values';
  44. $property['property info'] = geofield_data_property_info('Geofield');
  45. unset($property['query callback']);
  46. }
  47. /**
  48. * Defines info for the properties of the geofield field data structure.
  49. */
  50. function geofield_data_property_info($name = NULL) {
  51. // Build an array of basic property information for the geofield field.
  52. // If any of these should have individual options to set microdata
  53. // properties is not clear. Lat, Lon work fine. Other data is more
  54. // complicated. Geo_type relates (not 1:1) with the itemprop within
  55. // a GeoShape, with an alternate representation of the wkt for the content
  56. // (at its simplest).
  57. $properties = array(
  58. 'geom' => array(
  59. 'label' => 'Well-known text',
  60. 'type' => 'text',
  61. 'microdata' => FALSE,
  62. ),
  63. 'geo_type' => array(
  64. 'label' => 'Geo Type',
  65. 'options list' => '_geofield_geo_types_options_callback',
  66. 'required' => TRUE,
  67. 'microdata' => FALSE,
  68. ),
  69. 'lat' => array(
  70. 'label' => 'Latitude',
  71. 'type' => 'decimal',
  72. 'required' => TRUE,
  73. 'setter callback' => 'entity_property_verbatim_set',
  74. 'microdata' => TRUE,
  75. ),
  76. 'lon' => array(
  77. 'label' => 'Longitude',
  78. 'type' => 'decimal',
  79. 'required' => TRUE,
  80. 'setter callback' => 'entity_property_verbatim_set',
  81. 'microdata' => TRUE,
  82. ),
  83. 'left' => array(
  84. 'label' => 'Left Latitude',
  85. 'type' => 'decimal',
  86. 'setter callback' => 'entity_property_verbatim_set',
  87. 'microdata' => FALSE,
  88. ),
  89. 'top' => array(
  90. 'label' => 'Top Longitude',
  91. 'type' => 'decimal',
  92. 'setter callback' => 'entity_property_verbatim_set',
  93. 'microdata' => FALSE,
  94. ),
  95. 'right' => array(
  96. 'label' => 'Right Latitude',
  97. 'type' => 'decimal',
  98. 'setter callback' => 'entity_property_verbatim_set',
  99. 'microdata' => FALSE,
  100. ),
  101. 'bottom' => array(
  102. 'label' => 'Bottom Longitude',
  103. 'type' => 'decimal',
  104. 'setter callback' => 'entity_property_verbatim_set',
  105. 'microdata' => FALSE,
  106. ),
  107. 'srid' => array(
  108. 'label' => 'Projection (SRID)',
  109. 'type' => 'integer',
  110. 'microdata' => FALSE,
  111. ),
  112. 'latlon' => array(
  113. 'label' => 'LatLong Pair',
  114. 'type' => 'string',
  115. 'getter callback' => 'geofield_return_latlon',
  116. 'microdata' => FALSE,
  117. ),
  118. 'schemaorg_shape' => array(
  119. 'label' => 'Schema.org Shape',
  120. 'type' => 'string',
  121. 'getter callback' => 'geofield_return_schemaorg_shape',
  122. 'microdata' => TRUE,
  123. ),
  124. );
  125. // Add the default values for each of the geofield properties.
  126. foreach ($properties as $key => &$value) {
  127. $value += array(
  128. 'description' => !empty($name) ? t('!label of field %name', array('!label' => $value['label'], '%name' => $name)) : '',
  129. 'getter callback' => 'entity_property_verbatim_get',
  130. );
  131. }
  132. return $properties;
  133. }
  134. function _geofield_geo_types_options_callback() {
  135. $geophp = geophp_load();
  136. if (!$geophp) {
  137. return;
  138. }
  139. return geoPHP::geometryList();
  140. }
  141. /**
  142. * Gets the a latlong property.
  143. */
  144. function geofield_return_latlon($data, array $options, $name) {
  145. if ((is_array($data) || (is_object($data) && $data instanceof ArrayAccess)) && !is_null($data['lat']) && !is_null($data['lon'])) {
  146. return $data['lat'] . ',' . $data['lon'];
  147. }
  148. return NULL;
  149. }
  150. /**
  151. * Gets the Schema.org formatted shape value.
  152. */
  153. function geofield_return_schemaorg_shape($data, array $options, $name) {
  154. if ((is_array($data) || (is_object($data) && $data instanceof ArrayAccess))) {
  155. return geofield_schemaorg_shape($data);
  156. }
  157. return NULL;
  158. }