--- C:\Documents and Settings\Aldo\Local Settings\Temp\TCV33aa.tmp\gmap_location.1.8.module	Thu Jun 15 23:07:15 2006
+++ D:\drupal cvs\contributions\modules\gmap\gmap_location.module	Thu Jun 15 23:07:03 2006
@@ -39,16 +39,19 @@
   $items=array();
 
   if ($may_cache) {
-      $items[] = array('path' => 'map/users',
+    $items[] = array(
+      'path' => 'map/user',
         'type' => MENU_NORMAL_ITEM,
-        'title' => t('User locations'),
+      'title' => t('user locations'),
         'access' => user_access('show user map'),
-        'callback' => 'gmap_location_users_page');
+      'callback' => 'gmap_location_user_page'
+    );
       $items[] = array('path' => 'map/node',
         'type' => MENU_NORMAL_ITEM,
-        'title' => t('Node locations'),
+      'title' => t('node locations'),
         'access' => user_access('show node map'),
-        'callback' => 'gmap_location_node_page');
+      'callback' => 'gmap_location_node_page'
+    );
   }
 
   return $items;
@@ -59,13 +62,10 @@
  *
  */
 
-function gmap_location_users_page() {
-  global $user, $base_url;
+function gmap_location_user_page() {
+  global $user;
   $output ='<p>'.variable_get('gmap_user_map_header', t('This map illustrates the extent of users of this website. Each marker indicates a user that has entered their locations.'))."</p>\n";
 
-  $result=db_query("SELECT * FROM {location} WHERE (longitude !=0 OR latitude !=0) AND type='user'");
-  $thismap = gmap_parse_macro(variable_get('gmap_user_map', '[gmap|id=usermap|center=30,0|zoom=2|width=100%|height=400px]'));
-
   // Find default marker
   $default_marker = variable_get('gmap_user_map_marker', 'drupal');
 
@@ -78,12 +78,13 @@
     }
   }
 
-
+  $thismap = gmap_parse_macro(variable_get('gmap_user_map', '[gmap|id=usermap|center=30,0|zoom=2|width=100%|height=400px]'));
   if (empty($thismap['markers'])) {
     $thismap['markers']=array();
   }
-  while ($u=db_fetch_object($result)) {
+  $result=db_query("SELECT * FROM {location} WHERE (longitude !=0 OR latitude !=0) AND type='user'");
 
+  while ($u=db_fetch_object($result)) {
     $account = user_load(array('uid' => $u->oid));
     $dupes = array_intersect($account->roles, $roles_override);
     if (empty($dupes)) {
@@ -93,7 +94,6 @@
       $key = key($dupes);
       $marker = variable_get("gmap_role_map_marker_$key", $default_marker);
     }
-
     if (user_access('user locations')) {
       $newmarker['label'] = theme('gmap_location_user_html',$account);
       $newmarker['point']= $u->latitude.','.$u->longitude;
@@ -118,8 +118,10 @@
       $buddies= buddylist_get_buddies($key);
       foreach ($buddies as $bkey=>$bvalue) {
         if ($bkey > $key && isset($locationbyuser[$bkey])) {
-          $thismap['shape'][]= array('points'=>array($locationbyuser[$key],$locationbyuser[$bkey]),
-                                      'type'=>'line');
+          $thismap['shape'][]= array(
+            'points'=>array($locationbyuser[$key],$locationbyuser[$bkey]),
+            'type'=>'line'
+          );
         }
       }
     }
@@ -140,12 +142,12 @@
  */
 
 
-function gmap_location_node_page($nn=null) {
-  if ($nn && $n=node_load($nn)){
+function gmap_location_node_page($nid=null) {
+  if ($nid && $n=node_load($nid)){
     if (node_access('view',$n)) {
       $output .='<p>'.variable_get('gmap_node_map_header', t('This map illustrates the extent of nodes of this website. '))."</p>\n";
       $thismap = gmap_parse_macro(variable_get('gmap_node_map', '[gmap|id=nodemap|center=30,0|zoom=2|width=100%|height=400px]'));
-      $thismap = gmap_location_node_map($n,$thismap);
+      $thismap = gmap_location_node_map($n,$thismap,true);
       $output .= '<p>'.gmap_draw_map($thismap);
       return $output;
     }
@@ -156,17 +158,18 @@
 
   $output .='<p>'.variable_get('gmap_node_map_header', t('This map illustrates the extent of nodes of this website.'))."</p>\n";
 
-  $result=db_query("SELECT * FROM {location} WHERE (longitude!=0 OR latitude !=0) AND type='node'");
+  $result=db_query("SELECT oid, longitude, latitude FROM {location} WHERE (longitude!=0 OR latitude !=0) AND type='node'");
   $thismap = gmap_parse_macro(variable_get('gmap_node_map', '[gmap|id=usermap|center=30,0|zoom=2|width=100%|height=400px]'));
   if (empty($thismap['markers'])) {
     $thismap['markers']=array();
   }
   while ($locn=db_fetch_object($result)) {
-    $n=node_load(array('vid'=>$locn->oid));
+    $n=node_load($locn->oid);
     if ($n && node_access('view',$n)) {
       $thismap=gmap_location_node_map($n,$thismap,false);
     }
   }
+
   $output .= '<p>'.gmap_draw_map($thismap);
   return $output;
 }
@@ -176,7 +179,7 @@
  * Adds the location information from a node for a gmap.
  *
  * @param $n
- * The node number to add to the map.
+ * The node object to add to the map.
  *
  * @param $thismap
  * A gmap var with the map that will be used as the basemap
@@ -190,16 +193,15 @@
  */
 
 function gmap_location_node_map($n,$thismap,$single=false){
-
-  if (isset($n->location['latitude']) && isset($n->location['longitude'])){
+  if (isset($n->gmap_location_latitude) && isset($n->gmap_location_longitude)){
     $newmarker=array();
     $width=0;
 
     if (!($newmarker['text'] = theme(strtr($n->type,'-','_').'_gmapnodelabel',$n))) {
       $newmarker['text'] = theme('gmapnodelabel',$n);
     }
-    $newmarker['point']= $n->location['latitude'].','.$n->location['longitude'];
-    $newmarker['markername']=variable_get('gmap_node_map_marker_'.$n->type, '');
+    $newmarker['point']= $n->gmap_location_latitude.','.$n->gmap_location_longitude;
+    $newmarker['markername']=variable_get('gmap_node_marker_'.$n->type, '');
     $newmarker['tooltip']=$n->title;
     $newmarker['link']=url('node/'.$n->nid);
     switch ($n->type) {
@@ -224,7 +226,7 @@
               $newmarker=array();
               $newmarker['label'] = theme('user_picture', $user);
               $newmarker['label'] .= theme('username', $user);
-              $newmarker['point']= $n->location['latitude'].','.$n->location['longitude'];
+              $newmarker['point']= $n->gmap_location_latitude.','.$n->gmap_location_longitude;
               $newmarker['markername']=variable_get('gmap_user_map_marker', 'drupal');
               $thismap['markers'][]=$newmarker;
             }
@@ -251,33 +253,58 @@
    $markerdir=variable_get('gmap_markerfiles','modules/gmap/markers');
   // Retrieve and sort a list of available markers
   $marker_images = file_scan_directory($markerdir, '.*\.png$',array('shadow.png','.','..'),0,true, 'filename');
-//  $marker_images=array();
   foreach ($marker_images as $marker) {
     $thisfile=substr($marker->filename,strlen($markerdir)+1,-4);
     $markers[$thisfile] = $thisfile;
   }
   asort($markers);
 
-  $form['user']=array('#type' => 'fieldset', '#title' => t('Location settings for users'));
-  $form['user']['gmap_user_map']=array('#type'=>'textfield',
+  $form['user'] = array(
+    '#type' => 'fieldset', 
+    '#title' => t('Location settings for users'),
+  );
+  $form['user']['gmap_user'] = array(
+    '#type'=>'checkbox',
+    '#title'=>t('Profile map'),
+    '#default_value'=>variable_get('gmap_user', true),
+    '#description'=>t('Let users set/edit their location in their profile.'),
+  );
+  $form['user']['gmap_user_profile_category'] = array(
+    '#type'=>'textfield',
+    '#title'=>t('Profile category title'),
+    '#default_value'=>variable_get('gmap_user_profile_category', "Location map"),
+    '#size'=>50,
+    '#maxlength'=>50,
+    '#description'=>t('Let users set/edit their location in their profile.'),
+  );
+  $form['user']['gmap_user_map'] = array(
+    '#type'=>'textfield',
                                        '#title'=>t('Default user map'),
                                        '#default_value'=>variable_get('gmap_user_map', '[gmap |id=usermap|center=40,0|zoom=3|width=100%|height=400px]'),
                                        '#size'=>50,
                                        '#maxlength'=>500,
-                                       '#description'=>t('The gmap macro where the user information will be diplayed on.'));
-  $form['user']['gmap_user_map_header']=array('#type'=>'textarea',
+    '#description'=>t('The gmap macro where the user information will be diplayed on.'),
+  );
+  $form['user']['gmap_user_map_header'] = array(
+    '#type'=>'textarea',
                                               '#title'=>t('Text at the top of the map/users page'),
                                               '#default_value'=>variable_get('gmap_user_map_header', t('This map illustrates the extent of users of this website. Each marker indicates a user that has entered their locations.')),
                                               '#cols'=>50,
-                                              '#rows'=>6 );
-  $form['user']['gmap_user_map_marker']=array('#type'=>'select',
+    '#rows'=>6 ,
+  );
+  $form['user']['gmap_user_map_marker'] = array(
+    '#type'=>'select',
                                               '#title'=>t('Marker for users'),
                                               '#default_value'=>variable_get('gmap_user_map_marker', 'drupal'),
-                                              '#options'=>$markers);
+    '#options'=>$markers,
+  );
+
   // Option to use a different marker for each role
-  $form['user']['roles']=array('#type'=>'fieldset',
+  $form['user']['roles'] = array(
+    '#type'=>'fieldset',
                                '#title'=>t('Markers per role'),
-                               '#description'=>t('Use a different marker to denote users in the following roles.'));
+    '#description'=>t('Use a different marker to denote users in the following roles.'),
+  );
 
   // Retrieve and sort list of roles, sans anonymous user
   $roles = user_roles(TRUE);
@@ -285,33 +312,44 @@
 
   // Create a selection box per role
   foreach ($roles as $rid => $role) {
-    $form['user']['roles']["gmap_role_map_marker_$rid"]=array('#type'=>'select',
+    $form['user']['roles']["gmap_role_map_marker_$rid"] = array(
+      '#type'=>'select',
                                                               '#title'=>$role,
                                                               '#default_value'=>variable_get("gmap_role_map_marker_$rid", variable_get('gmap_user_map_marker', 'drupal')),
-                                                              '#options'=>$markers);
+      '#options'=>$markers,
+    );
   }
 
 
-  $form['node']=array('#type' => 'fieldset', '#title' => t('Location settings for nodes'));
-  $form['node']['gmap_node_map']=array('#type'=>'textfield',
+  $form['node'] = array(
+    '#type' => 'fieldset', 
+    '#title' => t('Location settings for nodes'),
+  );
+  $form['node']['gmap_node_map'] = array(
+    '#type'=>'textfield',
                                        '#title'=>t('Default node map'),
-                                       '#default_value'=>variable_get('gmap_node_map',
-                                       '[gmap |id=nodemap|center=40,0|zoom=3|width=100%|height=400px]'),
+    '#default_value'=>variable_get('gmap_node_map', '[gmap |id=nodemap|center=40,0|zoom=3|width=100%|height=400px]'),
                                        '#size'=>50,
                                        '#maxlength'=>500,
-                                       '#description'=>t('The gmap macro where the user information will be diplayed on.'));
-  $form['node']['gmap_node_map_header']=array('#type'=>'textarea',
+    '#description'=>t('The gmap macro where the user information will be diplayed on.'),
+  );
+  $form['node']['gmap_node_map_header'] = array(
+    '#type'=>'textarea',
                                               '#title'=>t('Text at the top of the map/nodes page'),
                                               '#default_value'=>variable_get('gmap_node_map_header', t('This map illustrates the locations of the nodes on this website. Each marker indicates a node associated with a specific location.')),
                                               '#cols'=>50,
-                                              '#rows'=>6 );
+    '#rows'=>6 
+  );
+
   $ntypes=node_get_types();
   foreach ($ntypes as $key => $value) {
     if (variable_get('location_'. $key, 0)) {
-      $form['node']['gmap_node_map_marker_'.$key]=array('#type'=>'select',
+      $form['node']['gmap_node_marker_'.$key]=array(
+        '#type'=>'select',
                                                         '#title'=>t('Marker for '.$value),
-                                                        '#default_value'=>variable_get('gmap_node_map_marker_'.$key, ''),
-                                                        '#options'=>$markers);
+        '#default_value'=>variable_get('gmap_node_marker_'.$key, ''),
+        '#options'=>$markers,
+      );
     }
   }
   return $form;
@@ -346,7 +384,8 @@
                                                           '#title' => t('Map Macro for '.$value),
                                                           '#size' => 60,
                                                           '#maxlength'=>500,
-                                                          '#default_value' => variable_get('gmap_location_block_macro_'.$key, ''),        );
+             '#default_value' => variable_get('gmap_location_block_macro_'.$key, ''),
+            );
           }
         }
       }
@@ -405,7 +444,7 @@
     $thismap['markers']=array();
   }
   $newmarker['point']= $node->location['latitude'].','.$node->location['longitude'];
-  $newmarker['markername']=variable_get('gmap_node_map_marker_'.$node->type, '');
+  $newmarker['markername']=variable_get('gmap_node_marker_'.$node->type, '');
   $newmarker['label']=$label;
   $basemap['markers'][]=$newmarker;
   $basemap['center']= $node->location['latitude'].','.$node->location['longitude'];
@@ -442,3 +481,199 @@
   $returntxt .= theme('username', $account);
   return $returntxt;
 }
+
+function gmap_location_user($op, &$edit, &$user, $category = NULL) {
+  if (module_exist('gmap') && variable_get('gmap_user', 0)) {
+    $object->gmap_location_latitude = $edit['gmap_location_latitude'];
+    $object->gmap_location_longitude = $edit['gmap_location_longitude'];
+    $object->oid = $user->uid;
+
+    switch ($op) {
+      case 'categories':
+        return array(array('name'=>'gmap_user', 'title'=> variable_get('gmap_user_profile_category', t('location map')),'weight'=>5));
+      case 'validate':
+        _gmap_location_validate_form($object);
+        return;
+      case 'load':
+        $res=db_query("SELECT * FROM {location} WHERE oid=%d AND type='user'",$user->uid);
+        if ($gmap_user=db_fetch_array($res)) {
+          if ($gmap_user['latitude'] !=0) $user->gmap_location_latitude=  $gmap_user['latitude'];
+          if ($gmap_user['longitude']!=0) $user->gmap_location_longitude= $gmap_user['longitude'];
+          $user->gmap_location_set=true;
+        }
+        return;
+      case 'insert':
+        _gmap_location_store_location($object, 'user', false);
+        return;
+
+      case 'update':
+        _gmap_location_store_location($object, 'user', isset($user->gmap_location_set));
+        return;
+
+      case 'delete':
+        db_query ("DELETE from {location} WHERE oid=%d AND type='user'", $user->uid);
+        return;
+
+      case 'form':
+        if($category == 'gmap_user') _gmap_location_map_form(&$form, $edit, 'user');
+
+        return  $form;
+    }
+  }
+}
+
+
+function gmap_location_form_alter($form_id, &$form) {
+  $type = $form['type']['#value'];
+  $node = $form['#node'];
+
+  switch ($form_id) {
+    case $type .'_node_settings':
+
+      $markerdir=variable_get('gmap_markerfiles','modules/gmap/markers');
+      // Retrieve and sort a list of available markers
+      $marker_images = file_scan_directory($markerdir, '.*\.png$',array('shadow.png','.','..'),0,true, 'filename');
+      foreach ($marker_images as $marker) {
+        $thisfile=substr($marker->filename,strlen($markerdir)+1,-4);
+        $markers[$thisfile] = $thisfile;
+      }
+      asort($markers);
+
+      $form['gmap'] = array(
+        '#type' => 'fieldset', 
+        '#title' => t('Google Maps'));
+      $form['gmap']['gmap_node_'. $type] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Allow users to add Google Maps info to this node type'),
+        '#default_value' => variable_get('gmap_node_'. $type, 0),
+        '#description' => t('The location information will be available to the google maps module, and can be shown in a block.'),
+        '#required' => FALSE,
+      );
+      $form['gmap']['gmap_node_marker_'.$type] = array(
+        '#type'=>'select',
+        '#title'=>t('Marker for this nodetype'),
+        '#default_value'=>variable_get('gmap_node_marker_'.$type, variable_get('gmap_user_map_marker', 'drupal')),
+        '#options'=>$markers,
+      );
+      break;
+    case $type .'_node_form':
+      if(variable_get('gmap_node_'. $type, 0))
+        $edit = array();
+        _gmap_location_map_form(&$form, $edit, 'node');
+
+      break;
+    default:
+      // break;
+  }
+}
+
+function gmap_location_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
+  if(variable_get('gmap_node_'. $node->type, 0)) {
+    $object->gmap_location_latitude = $node->gmap_location_longitude;
+    $object->gmap_location_longitude = $node->gmap_location_longitude;
+    $object->oid = $node->nid;
+
+    switch ($op) {
+      case 'load':
+        $res=db_query("SELECT * FROM {location} WHERE oid=%d AND type='node'",$node->nid);
+        if ($gmap_node=db_fetch_array($res)) {
+          if ($gmap_node['latitude'] !=0) $node->gmap_location_latitude= $gmap_node['latitude'];
+          if ($gmap_node['longitude']!=0) $node->gmap_location_longitude=$gmap_node['longitude'];
+          $node->gmap_location_set=true;
+        }
+        return;
+
+      case 'validate':
+        _gmap_location_validate_form($object);
+        return;
+
+      case 'insert':
+        _gmap_location_store_location($object, 'node', false);
+        return;
+
+      case 'update':
+        _gmap_location_store_location($object, 'node', isset($node->gmap_location_set));
+        return;
+
+      case 'delete':
+        db_query ("DELETE from {location} WHERE oid=%d AND type='node'", $node->nid);
+        return;
+
+    }
+  }
+}
+
+function _gmap_location_map_form(&$form, &$edit, $type) {
+  if($type=="user") {
+    $latitude = $edit['gmap_location_latitude'];
+    $longitude = $edit['gmap_location_longitude'];
+  }
+  else {
+    $latitude = $form['#node']->gmap_location_latitude;
+    $longitude = $form['#node']->gmap_location_longitude;
+  }
+
+  $form['coordinates']=array(
+    '#type' => 'fieldset',
+    '#title' => t('Coordinates'),
+    '#weight' => 5,
+    '#collapsible' => $type!='user', 
+    '#collapsed' => $type!='user', 
+  );
+  $form['coordinates']['gmap_location_latitude']=array(
+    '#type'=>'textfield',
+    '#id'=>'gmap-latitude',
+    '#title'=>t('Latitude'),
+    '#default_value'=>$latitude,
+    '#size'=>30,
+    '#maxlength'=>120,
+    '#attributes'=>array('onchange'=>'gmap_textchange();'),
+  );
+  $form['coordinates']['gmap_location_longitude']=array(
+    '#type'=>'textfield',
+    '#title'=>t('Longitude'),
+    '#default_value'=>$longitude,
+    '#size'=>30,
+    '#maxlength'=>120,
+    '#description'=>t('The latitude and longitude will be entered here when you click on a location in the interactive map below. You can also fill in the values manually.'),
+    '#attributes'=>array('onchange'=>'gmap_textchange();'),
+  );
+  $form['coordinates']['gmap_node']=array(
+    '#type'=>'markup',
+    '#value'=> gmap_set_location(
+       variable_get('gmap_'.$type.'_map', '[gmap|id='.$type.'map|center=0,30|control=Large|type=Hybrid|zoom=16|width=100%|height=400px]'),
+       $form['coordinates']['gmap_location_longitude'],
+       $form['coordinates']['gmap_location_latitude'] 
+    ),
+  );
+}
+
+function _gmap_location_validate_form(&$object) {
+  if (isset($object->latitude)){
+    if (!is_numeric($object->latitude) || abs($object->latitude)>90) {
+      form_set_error('gmap_location_latitude', t('Latitude must be between -90 and 90'));
+    }
+  }
+  if (isset($object->longitude)){
+    if (!is_numeric($object->longitude) || abs($object->longitude)>180) {
+      form_set_error('gmap_location_longitude', t('Longitude must be between -180 and 180'));
+    }
+  }
+}
+
+function _gmap_location_store_location(&$object, $type, $update = false) {
+  if (isset($object->gmap_location_latitude) && isset($object->gmap_location_longitude)) {
+    if ($update) {
+      db_query("UPDATE {location} SET latitude='%f', longitude='%f', source='%d' WHERE oid='%d' AND type='%s'",  
+      $object->gmap_location_latitude,$object->gmap_location_longitude, 1,$object->oid, $type);
+    }
+    else {
+      // based on location.module LOCATION_LATLON_USER_SUBMITTED=1
+      db_query("INSERT INTO {location} (oid, type, latitude, longitude, source) VALUES (%d, '%s', '%f', '%f', '%d')", 
+      $object->oid, $type, $object->gmap_location_latitude,$object->gmap_location_longitude, 1); 
+    }
+    unset($object->gmap_location_latitude);
+    unset($object->gmap_location_longitude);
+    unset($object->gmap_location_set);
+  }
+}
\ No newline at end of file
