Updates map on address edition.
authorStéphane Jacob <sj@m4x.org>
Wed, 1 Jun 2011 12:40:34 +0000 (14:40 +0200)
committerStéphane Jacob <sj@m4x.org>
Wed, 1 Jun 2011 12:40:34 +0000 (14:40 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
classes/gmapsgeocoder.php
htdocs/javascript/profile.js
modules/geoloc.php
plugins/insert.getStaticMapURL.php [new file with mode: 0644]
templates/geoloc/form.address.tpl

index 5321c72..2e44a01 100644 (file)
@@ -30,6 +30,19 @@ class GMapsGeocoder extends Geocoder {
     // Maximum number of Geocoding calls to the Google Maps API.
     const MAX_GMAPS_RPC_CALLS = 5;
 
+    static public function buildStaticMapURL($latitude, $longitude, $color, $separator = '&')
+    {
+        $parameters = array(
+            'size'    => '300x100',
+            'markers' => 'color:' . $color . '|' . $latitude . ',' . $longitude,
+            'zoom'    => '12',
+            'sensor'  => 'false'
+        );
+        global $globals;
+
+        return Platal::globals()->maps->static_map . '?' . http_build_query($parameters, '', $separator);
+    }
+
     public function getGeocodedAddress(Address $address, $defaultLanguage = null, $forceLanguage = false) {
         $this->prepareAddress($address);
         $textAddress = $this->getTextToGeocode($address->text);
index 3a10c17..beb65ee 100644 (file)
@@ -328,9 +328,13 @@ function addAddress()
                                               checkCurrentAddress());
 }
 
-function addressChanged(prefid)
+function addressChanged(prefid, color)
 {
+    var text = $('#' + prefid + '_cont').find("[name*='[text]']").val();
     $('#' + prefid + '_cont').find('[name*=changed]').val("1");
+    $.xpost('map_url/', { text:text, color:color }, function(data) {
+        $('.static_map_url').find('img').attr('src', data);
+    });
 }
 
 function deleteGeocoding(prefid, hrpid)
index c0402bf..762e1ac 100644 (file)
@@ -24,7 +24,8 @@ class GeolocModule extends PLModule
     function handlers()
     {
         return array(
-            'map' => $this->make_hook('map', AUTH_COOKIE)
+            'map'     => $this->make_hook('map', AUTH_COOKIE),
+            'map_url' => $this->make_hook('map_url', AUTH_COOKIE)
         );
     }
 
@@ -68,6 +69,20 @@ class GeolocModule extends PLModule
             self::prepare_map($page);
         }
     }
+
+    function handler_map_url($page)
+    {
+        pl_content_headers('text/plain');
+
+        if (Post::has('text')) {
+            $address = new Address(array('text' => Post::t('text')));
+            $gmapsGeocoder = new GMapsGeocoder();
+            $gmapsGeocoder->getGeocodedAddress($address);
+            echo GMapsGeocoder::buildStaticMapURL($address->latitude, $address->longitude, Post::t('color'));
+        }
+
+        exit();
+    }
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
diff --git a/plugins/insert.getStaticMapURL.php b/plugins/insert.getStaticMapURL.php
new file mode 100644 (file)
index 0000000..8f64637
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+function smarty_insert_getStaticMapURL(array $data)
+{
+     return GMapsGeocoder::buildStaticMapURL($data['latitude'], $data['longitude'], $data['color'], '&amp;');
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
index b05ac49..8bde680 100644 (file)
@@ -26,7 +26,7 @@
 <tr{if t($class)} class="{$class}"{/if}>
   <td>
 {/if}
-    <textarea name="{$prefname}[text]" cols="30" rows="4" onkeyup="addressChanged('{$prefid}')">{$address.text}</textarea>
+    <textarea name="{$prefname}[text]" cols="30" rows="4" onchange="addressChanged('{$prefid}','{$profile->promoColor()}')">{$address.text}</textarea>
     <input type="hidden" name="{$prefname}[postalText]" value="{$address.postalText}" />
     <input type="hidden" name="{$prefname}[types]" value="{$address.types}" />
     <input type="hidden" name="{$prefname}[formatted_address]" value="{$address.formatted_address}" />
@@ -52,9 +52,8 @@
   </td>
   <td>
 {/if}
-  {if t($address.latitude)}
-    <img src="{#globals.maps.static_map#}?size=300x100&amp;markers=color:{$profile->promoColor()}%7C{$address.latitude},{$address.longitude}&amp;zoom=12&amp;sensor=false"
-         alt="Position de l'adresse" />
+  <div class="static_map_url" {if !t($address.latitude)}style="display: none"{/if}>
+    <img src="{insert name="getStaticMapURL" latitude=$address.latitude longitude=$address.longitude color=$profile->promoColor()}" alt="Position de l'adresse" />
     {if t($geocoding_removal)}
     <br />
     <small id="{$prefid}_geocoding_removal">
@@ -65,7 +64,7 @@
     {/if}
     </small>
     {/if}
-  {/if}
+  </div>
 {if t($validation)}
 </div>
 <div style="clear: both"></div>