location_handler_sort_location_distance.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * @file
  4. * Coordinates sort handler.
  5. */
  6. class location_handler_sort_location_distance extends views_handler_sort {
  7. function option_definition() {
  8. $options = parent::option_definition();
  9. $options['origin'] = array('default' => 'user');
  10. $options['latitude'] = array('default' => '');
  11. $options['longitude'] = array('default' => '');
  12. $options['postal_code'] = array('default' => '');
  13. $options['country'] = array('default' => '');
  14. $options['php_code'] = array('default' => '');
  15. $options['nid_arg'] = array('default' => '');
  16. $options['nid_loc_field'] = array('default' => 'node');
  17. $options['uid_arg'] = array('default' => '');
  18. return $options;
  19. }
  20. function has_extra_options() {
  21. return TRUE;
  22. }
  23. function extra_options_form(&$form, &$form_state) {
  24. $form['origin'] = array(
  25. '#type' => 'select',
  26. '#title' => t('Origin'),
  27. '#options' => array(
  28. 'user' => t("User's Latitude / Longitude (blank if unset)"),
  29. 'hybrid' => t("User's Latitude / Longitude (fall back to static if unset)"),
  30. 'static' => t('Static Latitude / Longitude'),
  31. 'tied' => t("Use Distance / Proximity filter"),
  32. 'postal' => t('Postal Code / Country'),
  33. 'postal_default' => t('Postal Code (assume default country)'),
  34. 'php' => t('Use PHP code to determine latitude/longitude'),
  35. 'nid_arg' => t("Node's Latitude / Longitude from views nid argument"),
  36. 'uid_arg' => t("User's Latitude / Longitude from views uid argument"),
  37. ),
  38. '#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."),
  39. '#default_value' => $this->options['origin'],
  40. );
  41. $form['latitude'] = array(
  42. '#type' => 'textfield',
  43. '#title' => t('Latitude'),
  44. '#default_value' => $this->options['latitude'],
  45. '#process' => array('ctools_dependent_process'),
  46. '#dependency' => array('edit-options-origin' => array('hybrid', 'static')),
  47. );
  48. $form['longitude'] = array(
  49. '#type' => 'textfield',
  50. '#title' => t('Longitude'),
  51. '#default_value' => $this->options['longitude'],
  52. '#process' => array('ctools_dependent_process'),
  53. '#dependency' => array('edit-options-origin' => array('hybrid', 'static')),
  54. );
  55. $form['postal_code'] = array(
  56. '#type' => 'textfield',
  57. '#title' => t('Postal code'),
  58. '#default_value' => $this->options['postal_code'],
  59. '#process' => array('ctools_dependent_process'),
  60. '#dependency' => array('edit-options-origin' => array('postal', 'postal_default')),
  61. '#maxlength' => 16
  62. );
  63. $form['country'] = array(
  64. '#type' => 'select',
  65. '#title' => t('Country'),
  66. '#options' => array('' => '') + location_get_iso3166_list(),
  67. '#default_value' => $this->options['country'],
  68. '#process' => array('ctools_dependent_process'),
  69. '#dependency' => array('edit-options-origin' => array('postal')),
  70. );
  71. $form['php_code'] = array(
  72. '#type' => 'textarea',
  73. '#title' => t('PHP code for latitude, longitude'),
  74. '#default_value' => $this->options['php_code'],
  75. '#process' => array('ctools_dependent_process'),
  76. '#dependency' => array('edit-options-origin' => array('php')),
  77. '#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."),
  78. );
  79. list($nid_argument_options, $uid_argument_options) = location_views_proximity_get_argument_options($this->view);
  80. $nid_loc_field_options = location_views_proximity_get_location_field_options();
  81. $form['nid_arg'] = array(
  82. '#type' => 'select',
  83. '#title' => t('Node ID argument to use'),
  84. '#options' => $nid_argument_options,
  85. '#default_value' => $this->options['nid_arg'],
  86. '#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."),
  87. '#process' => array('ctools_dependent_process'),
  88. '#dependency' => array('edit-options-origin' => array('nid_arg')),
  89. );
  90. $form['nid_loc_field'] = array(
  91. '#type' => 'select',
  92. '#title' => t('Location to use'),
  93. '#options' => $nid_loc_field_options,
  94. '#default_value' => $this->options['nid_loc_field'],
  95. '#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."),
  96. '#process' => array('ctools_dependent_process'),
  97. '#dependency' => array('edit-options-origin' => array('nid_arg')),
  98. );
  99. $form['uid_arg'] = array(
  100. '#type' => 'select',
  101. '#title' => t('User ID argument to use'),
  102. '#options' => $uid_argument_options,
  103. '#default_value' => $this->options['uid_arg'],
  104. '#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."),
  105. '#process' => array('ctools_dependent_process'),
  106. '#dependency' => array('edit-options-origin' => array('uid_arg')),
  107. );
  108. }
  109. function query() {
  110. $coordinates = location_views_proximity_get_reference_location($this->view, $this->options);
  111. $this->ensure_my_table();
  112. // OK, so this part will need a little explanation.
  113. // Since the distance calculation is so icky, we try quite hard
  114. // to save some work for the database.
  115. // If someone has added a field that matches the sort, we just sort on that column!
  116. $alias = $this->table_alias . '_' . $this->field . '_sort';
  117. foreach ($this->view->field as $filter) {
  118. if ($filter->table == 'location' && $filter->field == 'distance' && $filter->options['relationship'] == $this->options['relationship']) {
  119. if ($filter->options['origin'] == $this->options['origin']
  120. && $filter->options['latitude'] == $this->options['latitude']
  121. && $filter->options['longitude'] == $this->options['longitude']) {
  122. // We have a match! Sync aliases to make it easier on the database.
  123. $alias = $filter->field_alias;
  124. }
  125. }
  126. }
  127. if (empty($coordinates)) {
  128. // We don't know the distance.
  129. // Therefore, we don't need to sort on it.
  130. }
  131. else {
  132. // This is done exactly the same as the field version.
  133. // Views is ok with us redefining the formula for a field.
  134. // If ANYTHING differs in the configuration, we will use a new alias.
  135. $this->query->add_orderby(NULL, earth_distance_sql($coordinates['longitude'], $coordinates['latitude'], $this->table_alias), $this->options['order'], $alias);
  136. }
  137. }
  138. }