Not submitted a patch before, so not sure of the protocol, but:

http://drupal.org/node/118817 explains the original problem, which was that users couldn't set their location via a google map through their 'my account' link (in use with the location module.) The primary key needed a new value.

With this addition, users can now set their location via their 'my account' link. Line 622-632 in gmap_location.module. The problem was that the primary key (lid in the 'location' table) wasn't being set, so I just pull out the maximum value of that field and add one. Only other change, then, is to add lid into the INSERT query.

$query = db_query("SELECT MAX(lid) FROM {location}");

	$lid = db_result($query);
		
	$lid++;
	
        db_query("INSERT INTO {location} (eid, lid, type, latitude, longitude, source) VALUES (%d, %d, 'user', %s, %s, 1)",
          $user->uid,
          $lid,
	  gmap_decimal($edit['gmap_location_latitude']),
          gmap_decimal($edit['gmap_location_longitude']));

Comments

danaktivix’s picture

Having done the above fix, I discovered a related problem: new users have an entry made for them in the location table, but the location values do no exist yet - so it enters 0 for both. This means when the new user goes to set their location via their accounts page, they initially find themselves just south of Africa!

Anyway, here's a fix. I imagine it doesn't really fit into the overall gmap.location methods, but it works. Note: I've hard-wired my default location values in, but I guess there's a way to access the default values set in the gmap.location settings page. (I don't need to do this, so didn't bother, sorry!)

Anyway, in gmap_location.module from line 622. It checks to see if user->id exists in the table and, if so, whether latitude is null or zero. If so, we presume its a new user and set the default values into {location} rather than use the $edit array.

//Get the last lid - which is the primary key in the location table - 
	//and inc by one for the next row.
	      
	$query = db_query("SELECT MAX(lid) FROM {location}");

	$lid = db_result($query);
		
	$lid++;
	
	//Need to check:
	//1. Does the user->uid exist in the location table yet? If not (e.g. latitude = null or zero) use default values.
	//2. Does the $user->uid in the table have a lat value of 0 or NULL? If not, default
	
	$setlatlongdefault = false;
	
	$query = db_query("SELECT eid, latitude FROM {location} WHERE eid=%d", $user->id);
	
	//Did we get a result? If not, they're in the table...
	
	$field = db_result($query);
	
	if (is_null($field->latitude) || $field->latitude ==0) {$setlatlongdefault = true;}
	
		
		if ($setlatlongdefault) {
			
			db_query("INSERT INTO {location} (eid, lid, type, latitude, longitude, source) VALUES (%d, %d, 'user', %s, %s, 1)",
			$user->uid,
			$lid,
			54.5,
			-3.5);}
					
			else {
	
			db_query("INSERT INTO {location} (eid, lid, type, latitude, longitude, source) VALUES (%d, %d, 'user', %s, %s, 1)",
			$user->uid,
			$lid,
			gmap_decimal($edit['gmap_location_latitude']),
			gmap_decimal($edit['gmap_location_longitude']));
		      
			} //end of else 664
brady747’s picture

Excuse me, as I'm not as knowledgeable as I could be. I was about to put the patches above in place as I was having the user location problem referenced when I realized my problem is slightly different. I don't even have a lid column in my location table. I checked gmap_location.install and it doesn't create the lid column, so where should that column come from? I believe I have all relevant modules installed and enabled to allow users to add their location under my account and all the fields exist for their location info, I just get errors stating the lid column is unknown which I can verify in phpmyadmin. Sorry if I should start a new topic somewhere else, I just thought the problem was similar in natures and others who are new (like me) might not notice the difference in errors at first glance and might wonder why the patch above doesn't solve their problem.

Thanks for the above efforts, I hope to figure out why I don't have a lid column so i can put the patches to use.

brady

danaktivix’s picture

Hia,

First, I have to say my patch is, er, patchy! There may well be a much simpler fix for the problem elsewhere in the code, and I'm not at all sure why you're install shouldn't have created the lid column - sorry. I'm also new to this module, and just trying to get it to work.

I've also discovered some whole new problems: when an admin creates a new account, a {location} entry is made for the user. When a user creates their own account, it isn't. This means that my previous test for setting default location values wasn't working. Here's a fix again - it checks the user table to see if the user has accessed their account. If they haven't, admin made it (and so values already exist for location.) If they have, when they access their location setter, they don't want the values set to default, but the one they select.

I'm hoping someone who knows about this project is going to come along and say why this is a complicated solution, and that there's something much simpler...! But needs must, and this has worked for me. (Sorry I can't help with the missing column conundrum...)

Remember, those are my default lat and long values... I haven't taken them from Gmap's own. From line 622 of gmap_location again:

<?php

$query = db_query("SELECT MAX(lid) FROM {location}");

	$lid = db_result($query);
		
	$lid++;
	
	//Need to check:
	//1. Does the user->uid exist in the location table yet? (e.g. latitude = null or zero) If not, then:
	//Has the user accessed their account? If not, then  use default values.
	
	$setlatlongdefault = false;
	
	$query = db_query("SELECT latitude FROM {location} WHERE eid=%d", $user->uid);
		
	$field = db_result($query);
	
	$querytwo = db_query("SELECT access FROM {users} WHERE uid=%d", $user->uid);

	$fieldtwo = db_result($querytwo);
	
	if ((is_null($field) || $field ==0) && (is_null($fieldtwo))||$fieldtwo==0 ) {$setlatlongdefault = true;} 
	
		if ($setlatlongdefault) {
			
			db_query("INSERT INTO {location} (eid, lid, type, latitude, longitude, source) VALUES (%d, %d, 'user', %s, %s, 1)",
			$user->uid,
			$lid,
			54.5,
			-3.5);}
					
			else {
	
			db_query("INSERT INTO {location} (eid, lid, type, latitude, longitude, source) VALUES (%d, %d, 'user', %s, %s, 1)",
			$user->uid,
			$lid,
			gmap_decimal($edit['gmap_location_latitude']),
			gmap_decimal($edit['gmap_location_longitude']));
	?>
danaktivix’s picture

Just thought I'd add: I'm not sure anyone should be using these patches until we hear back from someone more experienced with this project. They've worked for me - but then I also keep on finding new problems as a result of my attempted fixes! The last one does now seem to be working, but some other problems might turn up...

brady747’s picture

Thanks. Im just working on a test site, so i wont hold you accountable ;). I just added the lid field myself with phpmyadmin, although I had to guess at the fact its an integer field and a few of the other values. Anyhow, thats interesting re: the admin created user vs. user created user situation. I have been adding test users thru admin ao Im glad you posted this difference.

Brady

brady747’s picture

Damn. Im getting:
Parse error: syntax error, unexpected '<' in xxxxxxxxxxxxxdrupal/modules/gmap/gmap_location.module on line 621

When I place in your code. I guess I need to learn some php and figure out why I couldn't just cut and paste your code in. Maybe you removed a bracket or something?

Off to look a php syntax i go...

brady747’s picture

Ok. So I removed and when I paste in the code and added an extra bracket at the end and now its working!! Not sure why I needed that extra bracket. Initially I didn't put in the and but I was getting an unexpected T_CASE error. Then the and were giving me the other syntax error and eventually I just started matching brackets and for some reason your code (or my code) seemed a bracket short. Quite possibly I removed on in my cutting and pasting I gather. Oh well, THANKS for putting this stuff up even if its a temp fix - it got me up and running for the moment and I learned a bit :)
Brady

Hopefully we can get someone to clarify this patch, but it seems good to me.

danaktivix’s picture

Ah! I think I can explain this one, sorry, should have said:

1. You have to put the tags around code when pasting to the forum, otherwise it won't make it nice and pretty coloured - but you should leave them out of the pasting in.

2. And, just to make things even more odd, drupal modules don't use both PHP tags. You're supposed to open with <?php - but not close it again at the end of the code.

Hope that might help. I also hope you haven't spent five hours trying to work this out. Ah, the joys of the open source community...

brady747’s picture

Thanks, yeah...that should be obvious I guess. Like I said, I left the php out initially because I assumed it didn't belong, but got errors because I somehow was missing a bracket. Time to get a real php editor to help me sort out code late at night.....lol

danaktivix’s picture

Jedit is a nice, light editor that has PHP highlighting: I've enjoed using it lots.

http://www.jedit.org/

(You need to turn on the PHP parser in the plug-ins...)

bdragon’s picture

I recently purchased a copy of Komodo IDE 4, and I love it.

The editor component of it is available for free.

http://www.activestate.com/products/komodo_edit/

Anyway, gmap_location.module should no longer be attempting to touch the location table directly if at all possible. Unfortunately, fixing user locations is a bit low priority at the moment for me, I still have so much other stuff to finish... :-/

nvoyageur’s picture

I just installed the latest build from March 8 and still have problems with setting/updating the user location that is found at "My Account > Edit > Location Map"

This problem of being able to add/update the user location on the gmap gives the following error.

user warning: Duplicate entry '0' for key 1 query: INSERT INTO location (eid, type, latitude, longitude, source) VALUES (1, 'user', 0.01441955551185579, -0.064544677734375, 1) in /var/www/drupal/includes/database.mysql.inc on line 172.

I guess it's trying to do an INSERT into the table when the field already exists. It should be checking if the field exists then doing an UPDATE if it does. I have not looked at the code to see where the logic/fix should be (still learning the Drupal way).

Thanks again for all the hard work going into this pivital module.

emeij’s picture

denney’s picture

I to can confirm this error when updating a profile.

The initial setting of my location (being admin) worked fine but when someone else tried to set their location, and when I tried to to update my location, they received this error.

Also, just subscribing so I know when a fix is available. BTW... the link above, by 'emeij' contains code that doesn't exist or has moved from the latest version of this module (updated 13.03.07).

bdragon’s picture

Status: Needs review » Closed (duplicate)

Marking as duplicate of http://drupal.org/node/118817.

Thank you.
--Brandon