Page 1 of 1

PostPosted: Tue May 15, 2007 4:34 am
by maddin
hi Steve...(and all the other helpful df users)

similar to the posting of Markus I'd like to populate some fields automatically.
for my current project I need to add some geocode to my user records (to make them displayed in a google map)
my plan is to call the google-map api in an beforeSave trigger.
but ...
since the record is not yet written to the database when this function is called, I don't really know how to get the values (simply 3 input fields in the user table : zipcode,city,street) for the $valid _adr string which I have to pass to the google api.
is it possible to get these values before they are saved?
or do I have to change the function to be triggered after save, and then update the lat and lng fields?



setValue('lat', $lat);
$record->setValue('lng', $lng);
}
}
?>

any ideas are welcome..
cheers
martin

PostPosted: Tue May 15, 2007 4:38 am
by maddin
oops some important parts are missing in the above post...

--------------users.php----------------------------
class tables_users{

function beforeSave(&$record){
$auth =& Dataface_AuthenticationTool::getInstance();

//google-api-call
$key='ABQIAAAAooZ_SZteboYmTOL4jxD62BSOvuGUx-vwd1f9BNBwMyfFwZh75RRNqIYghOSJherZZaxKd-Rvj_PH1Q';
// valid_adr = zipcode + city + street
$valid_adr =
// --------------- here is where I'm stuck----------
$geocodeUrl = "http://maps.google.com/maps/geo?q=$valid_adr&key=
$key&output=csv";
$f = fopen($geocodeUrl,'r');
// separate values eg: 200,6,42.730070,-73.690570
list($weisnicht, $resolution, $lat, $lng) = explode(",", $f);
//assign to lat and lng field
$record->setValue('lat', $lat);
$record->setValue('lng', $lng);
}
}
?>
------------------------------------------------------

PostPosted: Tue May 15, 2007 7:43 am
by shannah

Hi Martin,

You are on the right track.Ê This will work perfectly if you are using Dataface 0.7 or later.Ê In 0.6.x you would have to do this in the afterSave() trigger in a trickier way.

If you have input the address values that you need on the new record form, then they will be available via the $record object.Ê E.g.:

$address = $record->strval('address');

If the address is actually a combination of a few fields (e.g. address, city, state, zip) sometimes I like to create a calculated field that outputs the address how I like it.Ê E.g.:

function field__full_address(&$record){
ÊÊÊ return $record->strval('address').' '.$record->strval('city').', '.$record->strval('state').', '.$record->val('zip');
}


Then you can use this just like another field.Ê E.g.

$full_address = $record->val('full_address');

Hope this helps a little.

-Steve


PostPosted: Tue May 15, 2007 12:36 pm
by maddin
hi steve

thanks for your help

I slighthly modified the function, now everything works fine
exept that I now get a:
Warning: Cannot modify header information - headers already sent by (output started at /data/springer/fisch-im-schuh.de/www/html/dataface-0.7/admin/tables/users/users.php:29) in /data/springer/fisch-im-schuh.de/www/html/dataface-0.7/actions/edit.php on line 167

but since this seems to be a cosmetic issue, I can live with that.... the lat and lng fields are poulated as expected.

for all beginners (like me) who might need a geocode http request exammple , here is the working code (to be placed in the delegate class of your table:
---------snip---------------------------------

strval('strasse').'+'.$record->strval('plz').'+'.$record->strval('ort');
}

function beforeSave(&$record){
$auth =& Dataface_AuthenticationTool::getInstance();

$key='ABQIAAAAooZ_SZteboYmTOL4jxD62BSOvuGUx-vwd1f9BNBwMyfFwZh75RRNqIYghOSJherZZaxKd-Rvj_PH1Q';
$valid_adr = $record->val('full_address');
$url ="http://maps.google.com/maps/geo?q=$valid_adr&key=$key&output=csv";
$geo = file_get_contents($url,'r');
$geo_array = explode(',',$geo);

$lat = $geo_array[3];
$lng = $geo_array[2];


$record->setValue('lat', $lat);
$record->setValue('lng', $lng);
}
}
?>


---------snap-----------------------------------
gmaps php-mysql tutorial
http://code.google.com/support/bin/answer.py?answer=65622
cheers
martin

PostPosted: Tue May 15, 2007 1:28 pm
by shannah

Hi Martin,

Glad it's working.Ê The errors are likely caused by extra whitespace at the end of your file. (after the ?>;)

Best regards

Steve

PostPosted: Wed May 16, 2007 6:39 am
by maddin
hi steve
whitespace at the end of file was a direct hit , thanks
‡ propos whitespace...
to make this delegate class really foolproof, i need to strip all of the whitespace and replace it with a plus sign (+)
I was experimenting with validation this morning but the built-in validation rules of quickform don't really match.

Then I went over to the delegate class of the users table to implement a php replacement eg:

seach = array(" ", ".");
$replace = array("+", "+");
$valid_adr = str_replace($seach, $replace, $nonvalid_adr);

but this leads to the following error:
Wrong parameter count for str_replace()

The whole function looks like this:
-----------------------------------------------------------------------
strval('strasse').'+'.$record->strval('hausnr').'+'.$record->strval('plz').'+'.$record->strval('ort'));
}

function beforeSave(&$record){
$auth =& Dataface_AuthenticationTool::getInstance();

$key='ABQIAAAAooZ_SZteboYmTOL4jxD62BSOvuGUx-vwd1f9BNBwMyfFwZh75RRNqIYghOSJherZZaxKd-Rvj_PH1Q';
$nonvalid_adr = $record->val('full_address');
// ----------------------------
$seach = array(" ", ".");
$replace = array("+", "+");
$valid_adr = str_replace($seach, $replace, $nonvalid_adr);
// --------------------------------------------------------
$url ="http://maps.google.com/maps/geo?q=$valid_adr&key=$key&output=csv";
$geo = file_get_contents($url,'r');
$geo_array = explode(',',$geo);

$lat = $geo_array[2];
$lng = $geo_array[3];


$record->setValue('lat', $lat);
$record->setValue('lng', $lng);
}
}
?>
---------------------------------------------------------------------
well... i'cant see any syntactical errors (that doesnt't mean much)
maybe you with your "PHP eagle eye" have an idea?
cheers
martin

PostPosted: Wed May 16, 2007 6:45 am
by maddin
mm..
for some reason parts of the delegate class are chopped..
maybe there are some tags filtered out in this forum..

here is the missing part of the function:

-------------snip-----------------

class tables_users{
//get values from user-form
function field__full_address(&$record){
return str_replace($record->strval('strasse').'+'.$record->strval('hausnr').'+'.$record->strval('plz').'+'.$record->strval('ort'));
}

------------snap-----------------

PostPosted: Wed May 16, 2007 8:17 am
by shannah

Ahh.. there is the problem.Ê in the field__full_address method you have a str_replace function operating on only one parameter (it is a long concatenated string, but still only one parameter).Ê str_replace needs 3 parameters..Ê I think that in this function you probably don't even want to be using str_replace at all.


-Steve

PostPosted: Wed May 16, 2007 8:20 am
by shannah

Oh.. another note... I think what you are trying to do is actually urlencode the address.Ê Try doing this, instead of explicitly calling str_replace to replace the spaces - because there are other characters other than spaces that will cause you problems too.Ê The urlencode function is made for this sort of thing:

$url ="http://maps.google.com/maps/geo?q=".urlencode($valid_adr)."&key=".urlencode($key)."&output=csv";

This should get things rolling.

Best regards


Steve

PostPosted: Wed May 16, 2007 8:44 am
by maddin
Ahh.. there is the problem. in the field__full_address method you have a str_replace function operating on only one parameter

stupid me..
i simply forgot it there ,its a leftover from my first attempt ...(rolleyes)...

thanks again -- over and out