You are here

function location_search_results in Location 5

1 call to location_search_results()
location_search_view in ./location.module

File

./location.module, line 163

Code

function location_search_results($location_params, $proximity_params) {

  // DEBUG: commented code for testing/debugging purposes
  $start_time = microtime();

  // First things first: find lat/lon for submitted postal_code
  // TODO: will need to replace be able to get lat/lon of search point for more than just postal_code
  $latlon = location_latlon_rough($location_params);
  if (!$latlon['lat'] || !$latlon['lon']) {
    drupal_set_message(t('No search results; could not determine the location of the submitted postal code.'));
    return '';
  }

  // If the distance parameters did not make sense, return an empty search result set.
  $distance_float = _location_convert_distance_to_meters($proximity_params['distance'], $proximity_params['unit']);

  // Find the pairs of lats and lons that define the corners of a square that is $distance X $distance and centered on $latlon
  $latrange = earth_latitude_range($latlon['lon'], $latlon['lat'], $distance_float);
  $lonrange = earth_longitude_range($latlon['lon'], $latlon['lat'], $distance_float);
  $count_query = 'SELECT COUNT(n.nid) AS count FROM {node} n INNER JOIN {location} l ON n.vid = l.eid WHERE l.type =\'node\' AND l.latitude > %f AND l.latitude < %f AND l.longitude > %f AND l.longitude < %f AND ' . earth_distance_sql($latlon['lon'], $latlon['lat']) . ' < %f';
  $query = 'SELECT n.nid, l.*, ' . earth_distance_sql($latlon['lon'], $latlon['lat'], 'l') . ' as distance FROM {node} n INNER JOIN {location} l ON n.vid = l.eid WHERE l.type =\'node\' AND l.latitude > %f AND l.latitude < %f AND l.longitude > %f AND l.longitude < %f AND ' . earth_distance_sql($latlon['lon'], $latlon['lat']) . ' < %f GROUP BY n.nid ORDER by distance';
  $query_args = array(
    $latrange[0],
    $latrange[1],
    $lonrange[0],
    $lonrange[1],
    $distance_float,
  );
  $pager_count_query = 'SELECT COUNT(DISTINCT nid) AS count FROM {node} n INNER JOIN {location} l ON n.vid = l.eid WHERE l.type =\'node\' AND l.latitude > ' . $latrange[0] . ' AND l.latitude < ' . $latrange[1] . ' AND l.longitude > ' . $lonrange[0] . ' AND l.longitude < ' . $lonrange[1] . ' AND ' . earth_distance_sql($latlon['lon'], $latlon['lat']) . ' < ' . $distance_float;
  $pager_query = 'SELECT n.nid, n.vid, l.*, ' . earth_distance_sql($latlon['lon'], $latlon['lat'], 'l') . ' AS distance FROM {node} n INNER JOIN {location} l ON n.vid = l.eid WHERE l.type =\'node\' AND l.latitude > ' . $latrange[0] . ' AND l.latitude < ' . $latrange[1] . ' AND l.longitude > ' . $lonrange[0] . ' AND l.longitude < ' . $lonrange[1] . ' AND ' . earth_distance_sql($latlon['lon'], $latlon['lat']) . ' < ' . $distance_float . ' GROUP BY n.nid ORDER by distance';
  $result = pager_query(db_rewrite_sql($query), 10, 0, db_rewrite_sql($count_query), $query_args);
  $result_count = db_result(db_query(db_rewrite_sql($count_query), $query_args));
  if (!$result_count) {
    $output = theme('box', t('Your search yielded no results.'), '');
  }
  else {
    $results_offset = isset($_GET['from']) ? $_GET['from'] : 0;
    $page_count = db_num_rows($result);
    $output = '<p>' . t('Displaying results %a - %b of %count for search on %c', array(
      '%a' => $results_offset + 1,
      '%b' => $results_offset + $page_count,
      '%count' => $result_count,
      '%c' => filter_xss($_GET['postal_code']),
    )) . '</p>';
    while ($row = db_fetch_object($result)) {
      $extra = array();
      unset($location_line);
      if ($row->postal_code || $row->city) {
        $location_line = t('Local to ');
        if ($row->postal_code && $row->city) {
          if ($row->postal_code) {
            $location_line .= $row->postal_code;
          }
          if ($row->postal_code && $row->city) {
            $location_line .= ' (' . $row->city;
            $location_line .= $row->province ? ', ' . $row->province : '';
            $location_line .= ')';
          }
          elseif ($row->city) {
            $location_line .= $row->city . ($row->province ? ', ' . $row->province : '');
          }
        }
      }
      if ($location_line) {
        $extra['location'] = $location_line;
      }
      if ($row->postal_code == $location_params['postal_code'] && $row->country == $location_params['country'] || $row->distance < 10) {
        $extra['distance'] = t('Result is <strong>also from %postal_code</strong>', array(
          '%postal_code' => $location_params['postal_code'],
        ));
      }
      else {
        $adjusted_distance = round($row->distance / ($proximity_params['unit'] == 'km' ? 1000.0 : 1609.347), 1);
        if ($adjusted_distance != 1) {
          $distance_unit = $edit['distance_unit'] == 'km' ? t('km') : t('miles');
        }
        else {
          $distance_unit = $edit['distance_unit'] == 'km' ? t('km') : t('mile');
        }
        $extra['distance'] = t('Approximately %distance %distanceunit from <strong>%location</strong> ', array(
          '%distance' => round($adjusted_distance, 1),
          '%distanceunit' => $distance_unit,
          '%location' => $location_params['postal_code'],
        ));
      }
      $node = node_load($row->nid);
      $output .= theme('search_item', array(
        'link' => url('node/' . $row->nid),
        'title' => $node->title,
        'type' => $node->type,
        'user' => db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $node->uid)),
        'date' => $node->created,
        'snippet' => $row->teaser,
        'extra' => $extra,
      ), 'node');
    }
    $output .= theme('pager');
  }
  return $output;
}