location_handler_field_location_distance.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @file
  4. * Coordinates field handler.
  5. */
  6. class location_handler_field_location_distance extends views_handler_field {
  7. function option_definition() {
  8. $options = parent::option_definition();
  9. $options['origin'] = array('default' => 'user');
  10. $options['units'] = array('default' => 'km');
  11. $options['latitude'] = array('default' => '');
  12. $options['longitude'] = array('default' => '');
  13. $options['postal_code'] = array('default' => '');
  14. $options['country'] = array('default' => '');
  15. $options['php_code'] = array('default' => '');
  16. $options['nid_arg'] = array('default' => '');
  17. $options['nid_loc_field'] = array('default' => 'node');
  18. $options['uid_arg'] = array('default' => '');
  19. return $options;
  20. }
  21. function has_extra_options() {
  22. return TRUE;
  23. }
  24. function extra_options_form(&$form, &$form_state) {
  25. $form['units'] = array(
  26. '#type' => 'radios',
  27. '#title' => t('Units'),
  28. '#options' => array(
  29. 'km' => t('Kilometers'),
  30. 'mi' => t('Miles'),
  31. ),
  32. '#default_value' => $this->options['units'],
  33. );
  34. $form['origin'] = array(
  35. '#type' => 'select',
  36. '#title' => t('Origin'),
  37. '#options' => array(
  38. 'user' => t("User's Latitude / Longitude (blank if unset)"),
  39. 'hybrid' => t("User's Latitude / Longitude (fall back to static if unset)"),
  40. 'static' => t('Static Latitude / Longitude'),
  41. 'tied' => t("Use Distance / Proximity filter"),
  42. 'postal' => t('Postal Code / Country'),
  43. 'postal_default' => t('Postal Code (assume default country)'),
  44. 'php' => t('Use PHP code to determine latitude/longitude'),
  45. 'nid_arg' => t("Node's Latitude / Longitude from views nid argument"),
  46. 'uid_arg' => t("User's Latitude / Longitude from views uid argument"),
  47. 'distance_arg' => t("Latitude / Longitude from views argument"),
  48. ),
  49. '#description' => t("This will be the way the latitude/longitude of origin is determined. When using the user's latitude / longitude, if a user has multiple locations the first will be used."),
  50. '#default_value' => $this->options['origin'],
  51. );
  52. $form['latitude'] = array(
  53. '#type' => 'textfield',
  54. '#title' => t('Latitude'),
  55. '#default_value' => $this->options['latitude'],
  56. '#process' => array('ctools_dependent_process'),
  57. '#dependency' => array('edit-options-origin' => array('hybrid', 'static')),
  58. );
  59. $form['longitude'] = array(
  60. '#type' => 'textfield',
  61. '#title' => t('Longitude'),
  62. '#default_value' => $this->options['longitude'],
  63. '#process' => array('ctools_dependent_process'),
  64. '#dependency' => array('edit-options-origin' => array('hybrid', 'static')),
  65. );
  66. $form['postal_code'] = array(
  67. '#type' => 'textfield',
  68. '#title' => t('Postal code'),
  69. '#default_value' => $this->options['postal_code'],
  70. '#process' => array('ctools_dependent_process'),
  71. '#dependency' => array('edit-options-origin' => array('postal', 'postal_default')),
  72. '#maxlength' => 16
  73. );
  74. $form['country'] = array(
  75. '#type' => 'select',
  76. '#title' => t('Country'),
  77. '#options' => array('' => '') + location_get_iso3166_list(),
  78. '#default_value' => $this->options['country'],
  79. '#process' => array('ctools_dependent_process'),
  80. '#dependency' => array('edit-options-origin' => array('postal')),
  81. );
  82. $form['php_code'] = array(
  83. '#type' => 'textarea',
  84. '#title' => t('PHP code for latitude, longitude'),
  85. '#default_value' => $this->options['php_code'],
  86. '#process' => array('ctools_dependent_process'),
  87. '#dependency' => array('edit-options-origin' => array('php')),
  88. '#description' => t("Enter PHP code that returns a latitude/longitude. Do not use &lt;?php ?&gt;. You must return only an array with float values set for the 'latitude' and 'longitude' keys."),
  89. );
  90. list($nid_argument_options, $uid_argument_options) = location_views_proximity_get_argument_options($this->view);
  91. $nid_loc_field_options = location_views_proximity_get_location_field_options();
  92. $form['nid_arg'] = array(
  93. '#type' => 'select',
  94. '#title' => t('Node ID argument to use'),
  95. '#options' => $nid_argument_options,
  96. '#default_value' => $this->options['nid_arg'],
  97. '#description' => empty($nid_argument_options) ? t("Select which of the view's arguments to use as the node ID. The latitude / longitude of the first location of that node will be used as the origin. Use the 'Global: Null' argument if you don't want to also restrict results to that node ID. You must have added arguments to the view to use this option.") : t("Select which of the view's arguments to use as the node ID. The latitude / longitude of the first location of that node will be used as the origin. Use the 'Global: Null' argument if you don't want to also restrict results to that node ID."),
  98. '#process' => array('ctools_dependent_process'),
  99. '#dependency' => array('edit-options-origin' => array('nid_arg')),
  100. );
  101. $form['nid_loc_field'] = array(
  102. '#type' => 'select',
  103. '#title' => t('Location to use'),
  104. '#options' => $nid_loc_field_options,
  105. '#default_value' => $this->options['nid_loc_field'],
  106. '#description' => t("Select which of the node's locations to use as the origin. Either the node locations or a CCK location field. If the location supports multiple entries the first one will be used."),
  107. '#process' => array('ctools_dependent_process'),
  108. '#dependency' => array('edit-options-origin' => array('nid_arg')),
  109. );
  110. $form['uid_arg'] = array(
  111. '#type' => 'select',
  112. '#title' => t('User ID argument to use'),
  113. '#options' => $uid_argument_options,
  114. '#default_value' => $this->options['uid_arg'],
  115. '#description' => empty($uid_argument_options) ? t("Select which of the view's arguments to use as the user ID. The latitude / longitude of the first location of that user will be used as the origin. Use the 'Global: Null' argument if you don't want to also restrict results to that user ID. You must have added arguments to the view to use this option.") : t("Select which of the view's arguments to use as the user ID. The latitude / longitude of the first location of that user will be used as the origin. Use the 'Global: Null' argument if you don't want to also restrict results to that user ID."),
  116. '#process' => array('ctools_dependent_process'),
  117. '#dependency' => array('edit-options-origin' => array('uid_arg')),
  118. );
  119. }
  120. function click_sort($order) {
  121. $location = location_views_proximity_get_reference_location($this->view, $this->options);
  122. if ($location) {
  123. $this->query->add_orderby(NULL, earth_distance_sql($location['longitude'], $location['latitude'], $this->table_alias), $order, $this->field_alias);
  124. }
  125. }
  126. function render($values) {
  127. if (empty($values->{$this->field_alias}) || $values->{$this->field_alias} == 'Unknown') {
  128. // Unset location, empty display.
  129. return;
  130. }
  131. $dist = (float)$values->{$this->field_alias};
  132. if ($this->options['units'] == 'km') {
  133. $dist = $dist / 1000.0;
  134. return theme('location_distance', array('distance' => $dist, 'units' => 'km'));
  135. }
  136. else {
  137. $dist = $dist / 1609.347;
  138. return theme('location_distance', array('distance' => $dist, 'units' => 'mi'));
  139. }
  140. }
  141. function query() {
  142. $this->ensure_my_table();
  143. $coordinates = location_views_proximity_get_reference_location($this->view, $this->options);
  144. if (!empty($coordinates)) {
  145. $this->field_alias = $this->query->add_field(NULL, earth_distance_sql($coordinates['longitude'], $coordinates['latitude'], $this->table_alias), $this->table_alias .'_'. $this->field);
  146. }
  147. else {
  148. $this->field_alias = $this->query->add_field(NULL, "'Unknown'", $this->table_alias .'_'. $this->field);
  149. }
  150. }
  151. }