location.tokens.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @file
  4. * Make tokens about location available.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function location_token_info() {
  10. $type = array(
  11. 'name' => t('Location', array(), array('context' => 'geolocation')),
  12. 'description' => t('Tokens related to location.'),
  13. 'needs-data' => 'location',
  14. );
  15. $location = array();
  16. // Core tokens for location.
  17. $location['name'] = array(
  18. 'name' => t("Location Name"),
  19. 'description' => t('Location Name (If there are multiple locations, N is the iteration, starting with 0).'),
  20. 'dynamic' => TRUE,
  21. );
  22. $location['street'] = array(
  23. 'name' => t("Street"),
  24. 'description' => t("Street (If there are multiple locations, N is the iteration, starting with 0)."),
  25. 'dynamic' => TRUE,
  26. );
  27. $location['additional'] = array(
  28. 'name' => t("Additional"),
  29. 'description' => t("Additional (If there are multiple locations, N is the iteration, starting with 0)."),
  30. 'dynamic' => TRUE,
  31. );
  32. $location['city'] = array(
  33. 'name' => t("City"),
  34. 'description' => t("City (If there are multiple locations, N is the iteration, starting with 0)."),
  35. 'dynamic' => TRUE,
  36. );
  37. $location['province'] = array(
  38. 'name' => t("State/Province"),
  39. 'description' => t("State/Province (If there are multiple locations, N is the iteration, starting with 0)."),
  40. 'dynamic' => TRUE,
  41. );
  42. $location['province_name'] = array(
  43. 'name' => t("State/Province Name"),
  44. 'description' => t("State/Province Name (If there are multiple locations, N is the iteration, starting with 0)."),
  45. 'dynamic' => TRUE,
  46. );
  47. $location['postal_code'] = array(
  48. 'name' => t("Postal Code"),
  49. 'description' => t("Postal Code (If there are multiple locations, N is the iteration, starting with 0)."),
  50. 'dynamic' => TRUE,
  51. );
  52. $location['latitude'] = array(
  53. 'name' => t("Latitude"),
  54. 'description' => t("Latitude (If there are multiple locations, N is the iteration, starting with 0)."),
  55. 'dynamic' => TRUE,
  56. );
  57. $location['longitude'] = array(
  58. 'name' => t("Longitude"),
  59. 'description' => t("Longitude (If there are multiple locations, N is the iteration, starting with 0)."),
  60. 'dynamic' => TRUE,
  61. );
  62. $location['country'] = array(
  63. 'name' => t("Country"),
  64. 'description' => t("Country (If there are multiple locations, N is the iteration, starting with 0)."),
  65. 'dynamic' => TRUE,
  66. );
  67. $location['country_name'] = array(
  68. 'name' => t("Country Name"),
  69. 'description' => t("Country Name (If there are multiple locations, N is the iteration, starting with 0)."),
  70. 'dynamic' => TRUE,
  71. );
  72. $node = array(
  73. 'location' => array(
  74. 'name' => t('Location', array(), array('context' => 'geolocation')),
  75. 'description' => t("The location for this node."),
  76. 'type' => 'location',
  77. )
  78. );
  79. $user = array(
  80. 'location' => array(
  81. 'name' => t('Location', array(), array('context' => 'geolocation')),
  82. 'description' => t("The location for this user."),
  83. 'type' => 'location',
  84. )
  85. );
  86. return array(
  87. 'types' => array('location' => $type),
  88. 'tokens' => array('location' => $location, 'user' => $user, 'node' => $node),
  89. );
  90. }
  91. /**
  92. * Implements hook_tokens().
  93. */
  94. function location_tokens($type, $tokens, array $data = array(), array $options = array()) {
  95. $replacements = array();
  96. if ($type == 'node' && ($location_tokens = token_find_with_prefix($tokens, 'location'))) {
  97. $locations = isset($data['node']->locations) ? $data['node']->locations : array();
  98. $replacements += token_generate('location', $location_tokens, array('location' => $locations), $options);
  99. }
  100. elseif ($type == 'user' && ($location_tokens = token_find_with_prefix($tokens, 'location'))) {
  101. $locations = isset($data['user']->locations) ? $data['user']->locations : array();
  102. $replacements += token_generate('location', $location_tokens, array('location' => $locations), $options);
  103. }
  104. elseif ($type == 'location' && isset($data['location'])) {
  105. $locations = $data['location'];
  106. // Make sure that this is an array of locations and not a single location.
  107. if (isset($locations['lid'])) {
  108. $locations = array($locations);
  109. }
  110. // Handle all tokens that are dynamic. For example, location:name:1. This does
  111. // not handle location:name. That is handled lower.
  112. $tokens_helper = array(
  113. 'name' => token_find_with_prefix($tokens, 'name'),
  114. 'street' => token_find_with_prefix($tokens, 'street'),
  115. 'additional' => token_find_with_prefix($tokens, 'additional'),
  116. 'city' => token_find_with_prefix($tokens, 'city'),
  117. 'province' => token_find_with_prefix($tokens, 'province'),
  118. 'province_name' => token_find_with_prefix($tokens, 'province_name'),
  119. 'postal_code' => token_find_with_prefix($tokens, 'postal_code'),
  120. 'latitude' => token_find_with_prefix($tokens, 'latitude'),
  121. 'longitude' => token_find_with_prefix($tokens, 'longitude'),
  122. 'country' => token_find_with_prefix($tokens, 'country'),
  123. 'country_name' => token_find_with_prefix($tokens, 'country_name'),
  124. );
  125. foreach ($tokens_helper as $key => $tokens_find) {
  126. foreach ($tokens_find as $val => $original) {
  127. $replacements[$original] = _locations_token_helper($locations, $key, $val, $options);
  128. }
  129. }
  130. // Handle values that do not have a specific location on them. For example,
  131. // handle location:city but not location:city:0 as it is handled above.
  132. foreach ($tokens as $key => $original) {
  133. if (isset($tokens_helper[$key])) {
  134. $replacements[$original] = _locations_token_helper($locations, $key, 0, $options);
  135. }
  136. }
  137. }
  138. return $replacements;
  139. }
  140. /**
  141. * Callback function to handle getting the correct location token.
  142. */
  143. function _locations_token_helper($locations, $location_key, $which = 0, array $options = array()) {
  144. $sanitize = !empty($options['sanitize']);
  145. // That location doesn't exist.
  146. if (!isset($locations[$which])) {
  147. return '';
  148. }
  149. // Load the country name and return it.
  150. if ($location_key == 'country_name') {
  151. if (!empty($locations[$which]['country'])) {
  152. $val = location_country_name($locations[$which]['country']);
  153. return $sanitize ? check_plain($val) : $val;
  154. }
  155. return '';
  156. }
  157. // Load the province name and return it.
  158. if ($location_key == 'province_name') {
  159. if (!empty($locations[$which]['country']) && !empty($locations[$which]['province'])) {
  160. $val = location_province_name($locations[$which]['country'], $locations[$which]['province']);
  161. return $sanitize ? check_plain($val) : $val;
  162. }
  163. return '';
  164. }
  165. // Handle all other values.
  166. if (!empty($locations[$which][$location_key])) {
  167. return $sanitize ? check_plain($locations[$which][$location_key]) : $locations[$which][$location_key];
  168. }
  169. return '';
  170. }