You are here

function gmap_location_node_page in GMap Module 7

Same name and namespace in other branches
  1. 5 gmap_location.module \gmap_location_node_page()
  2. 6.2 gmap_location.module \gmap_location_node_page()
  3. 6 gmap_location.module \gmap_location_node_page()
  4. 7.2 gmap_location.module \gmap_location_node_page()

Draws a page with a google map with the node on it, or if no node is set all of the nodes on it.

Parameters

$nid: The node nid to draw on the map. If this is not set, or is null then all of the nodes will be drawn.

1 string reference to 'gmap_location_node_page'
gmap_location_menu in ./gmap_location.module
Implementation of hook_menu().

File

./gmap_location.module, line 248
GMap Location module is a module to add some gmap funcationality based on location.modules information.

Code

function gmap_location_node_page($nid = NULL) {
  $nodemap = variable_get('gmap_node_map', _gmap_location_node_map_defaults());
  $markertypes = variable_get('gmap_node_markers', array());
  $map = array_merge(gmap_defaults(), gmap_parse_macro($nodemap['macro']));
  $mode = $nodemap['markermode'];
  $map['rmtcallback'] = url('map/node/load');
  $map['markermode'] = $nodemap['markermode'];
  if (!isset($map['markers']) || !is_array($map['markers'])) {
    $map['markers'] = array();
  }
  $query = db_select('node', 'n');
  $query
    ->condition('n.status', 1)
    ->condition(db_or()
    ->condition('l.latitude', 0, '!=')
    ->condition('l.longitude', 0, '!='))
    ->fields('n', array(
    'nid',
    'type',
    'title',
  ))
    ->fields('l', array(
    'latitude',
    'longitude',
  ));
  $query
    ->innerjoin('location_instance', 'i', 'n.vid = i.vid');
  $query
    ->innerjoin('location', 'l', 'l.lid = i.lid');
  if (is_numeric($nid) && $nid > 0) {
    $query
      ->condition('n.nid', $nid);
  }
  if (module_exists('gmap_taxonomy')) {
    $query
      ->fields('m', array(
      'marker',
    ))
      ->leftjoin('gmap_taxonomy_node', 'm', 'n.nid = m.nid');
  }
  $query
    ->addTag('node_access');
  $result = $query
    ->execute();
  $count = 0;
  foreach ($result as $row) {
    $count++;
    $newmarker = array();
    if ($mode == 1) {

      // Popup
      $newmarker['rmt'] = $row->nid . '/0';
    }
    elseif ($mode == 2) {

      // Link
      $newmarker['link'] = url('node/' . $row->nid);
    }
    $newmarker['latitude'] = $row->latitude;
    $newmarker['longitude'] = $row->longitude;
    $newmarker['markername'] = isset($markertypes[$row->type]) ? $markertypes[$row->type] : 'drupal';
    if (isset($row->marker) && !empty($row->marker)) {
      $newmarker['markername'] = $row->marker;
    }
    $newmarker['opts']['title'] = $row->title;
    $map['markers'][] = $newmarker;
  }

  // Special stuff for single marker
  if ($count == 1) {

    // Center map on only marker.
    $map['latitude'] = $map['markers'][0]['latitude'];
    $map['longitude'] = $map['markers'][0]['longitude'];

    // Autoclick in single marker case.
    if ($mode == 1) {
      $map['markers'][0]['autoclick'] = TRUE;
    }
  }

  // Special cases for single node view.
  if (is_numeric($nid) && ($node = node_load($nid))) {

    // Organic groups. Group nodes are displayed as a map of the users who belong to the group.
    if (user_access('view user location details') && function_exists('og_is_group_type') && og_is_group_type($node->type)) {
      $rolemarkers = variable_get('gmap_role_markers', array());
      $map['markers'] = array();

      // Reset markers.
      $result = db_query("\n        SELECT\n          MAX(r.rid) as role, i.uid, l.latitude, l.longitude\n        FROM\n          {og_uid} o\n        INNER JOIN {location_instance} i\n          ON i.uid = o.uid\n        INNER JOIN {location} l\n          ON l.lid = i.lid\n        LEFT JOIN {users_roles} r\n          ON i.uid = r.uid\n        WHERE\n          o.nid = :nid\n        AND\n          o.is_active >= 1\n        AND\n          (l.latitude != 0 OR l.longitude != 0)\n        GROUP BY\n          o.uid", array(
        ':nid' => $nid,
      ));
      foreach ($result as $row) {
        $newmarker = array();
        $newmarker['rmt'] = $nid . '/' . $row->uid;

        // Determine marker type to show.
        $newmarker['markername'] = $markertypes[DRUPAL_AUTHENTICATED_RID];
        if ($row->role && isset($rolemarkers[$row->role])) {
          $newmarker['markername'] = $rolemarkers[$row->role];
        }
        $newmarker['latitude'] = $row->latitude;
        $newmarker['longitude'] = $row->longitude;
        $map['markers'][] = $newmarker;
      }
    }
  }
  $element = array(
    '#type' => 'gmap',
    '#gmap_settings' => $map,
  );
  return theme('gmap_location_node_page', array(
    'count' => $count,
    'header' => $nodemap['header'],
    'map' => drupal_render($element),
    'footer' => $nodemap['footer'],
  ));
}