Moving to GitHub.
[platal.git] / modules / geoloc.php
index ae45b22..d731e6a 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -24,136 +24,76 @@ class GeolocModule extends PLModule
     function handlers()
     {
         return array(
-            'geoloc'             => $this->make_hook('default', AUTH_COOKIE),
-            'admin/geoloc'           => $this->make_hook('admin', AUTH_MDP, 'admin'),
-            'admin/geoloc/dynamap'   => $this->make_hook('admin_dynamap', AUTH_MDP, 'admin'),
-            'admin/geoloc/country'   => $this->make_hook('admin_country',  AUTH_MDP, 'admin')
+            'map'     => $this->make_hook('map',     AUTH_COOKIE, 'user'),
+            'map_url' => $this->make_hook('map_url', AUTH_COOKIE, 'user')
         );
     }
 
-    function handler_default(&$page, $action = null, $subaction = null)
+    static public function prepare_map(PlPage $page)
     {
         global $globals;
-
-        $set = new UserSet();
-        $set->addMod('geoloc', 'Geolocalisation', true);
-        $set->apply('geoloc', $page, $action, $subaction);
+        $page->changeTpl('geoloc/index.tpl');
+        $map_url = $globals->maps->dynamic_map . '?&sensor=false&v=' . $globals->maps->api_version . '&language=' . $globals->maps->language;
+        $page->addJsLink($map_url, false);
+        $page->addJsLink('maps.js');
+        $page->addJsLink('markerclusterer.js');
+        $page->assign('pl_extra_header', '<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />');
     }
 
-    function handler_admin(&$page, $action = false) {
-        $page->changeTpl('geoloc/admin.tpl');
-        require_once("geoloc.inc.php");
-        $page->setTitle('Administration - Geolocalisation');
-
-        $nb_synchro = 0;
-
-        if (Env::has('id') && is_numeric(Env::v('id'))) {
-            if (synchro_city(Env::v('id'))) $nb_synchro ++;
+    static public function assign_json_to_map(PlPage $page, $pids = null)
+    {
+        if (!is_null($pids)) {
+            $where = XDB::format(' AND pa.pid IN {?}', $pids);
+        } else {
+            $where = '';
         }
 
-        if ($action == 'missinglat') {
-            $res = XDB::iterRow("SELECT id FROM geoloc_city WHERE lat = 0 AND lon = 0");
-            while ($a = $res->next()) if (synchro_city($a[0])) $nb_synchro++;
+        if (!S::logged() || !S::user()->checkPerms('directory_ax')) {
+            $where .= " AND pa.pub = 'public'";
+            $name_publicity = 'public';
+        } else if (!S::user()->checkPerms('directory_private')) {
+            $where .= " AND pa.pub = 'ax'";
+            $name_publicity = 'public';
+        } else {
+            $name_publicity = 'private';
         }
 
-        if ($nb_synchro)
-            $page->trigSuccess(($nb_synchro > 1)?($nb_synchro." villes ont été synchronisées"):"Une ville a été synchronisée");
-
-        $res = XDB::query("SELECT COUNT(*) FROM geoloc_city WHERE lat = 0 AND lon = 0");
-        $page->assign("nb_missinglat", $res->fetchOneCell());
+        $data = XDB::rawFetchAllAssoc('SELECT  pa.latitude, pa.longitude, GROUP_CONCAT(DISTINCT p.hrpid SEPARATOR \',\') AS hrpid,
+                                               GROUP_CONCAT(pd.promo SEPARATOR \',\') AS promo,
+                                               GROUP_CONCAT(DISTINCT pd.' . $name_publicity . '_name, \' (\', pd.promo, \')\' SEPARATOR \', \') AS name,
+                                               GROUP_CONCAT(DISTINCT pa.pid SEPARATOR \',\') AS pid
+                                         FROM  profile_addresses AS pa
+                                   INNER JOIN  profiles          AS p  ON (pa.pid = p.pid)
+                                   INNER JOIN  profile_display   AS pd ON (pd.pid = pa.pid)
+                                        WHERE  pa.type = \'home\' AND p.deathdate IS NULL AND pa.latitude IS NOT NULL AND pa.longitude IS NOT NULL' . $where . '
+                                     GROUP BY  pa.latitude, pa.longitude');
+        $page->jsonAssign('data', $data);
     }
 
-    function handler_admin_dynamap(&$page, $action = false) {
-        $page->changeTpl('geoloc/admin_dynamap.tpl');
-
-        if ($action == 'cities_not_on_map') {
-            require_once('geoloc.inc.php');
-            if (!fix_cities_not_on_map(20))
-                $page->trigError("Impossible d'accéder au webservice");
-            else
-                $refresh = true;
-        }
-
-        if ($action == 'smallest_maps') {
-            require_once('geoloc.inc.php');
-            set_smallest_levels();
-        }
-
-        if ($action == 'precise_coordinates') {
-            XDB::execute("UPDATE  adresses AS a
-                      INNER JOIN  geoloc_city AS c ON(a.cityid = c.id)
-                             SET  a.glat = c.lat / 100000, a.glng = c.lon / 100000");
-        }
-
-        if ($action == 'newmaps') {
-            require_once('geoloc.inc.php');
-            if (!get_new_maps(Env::v('url')))
-                $page->trigError("Impossible d'accéder aux nouvelles cartes");
-        }
-
-        $countMissing = XDB::query("SELECT COUNT(*) FROM geoloc_city AS c LEFT JOIN geoloc_city_in_maps AS m ON(c.id = m.city_id) WHERE m.city_id IS NULL");
-        $missing = $countMissing->fetchOneCell();
-
-        $countNoSmallest = XDB::query("SELECT SUM(IF(infos = 'smallest',1,0)) AS n FROM geoloc_city_in_maps GROUP BY city_id ORDER BY n");
-        $noSmallest = $countNoSmallest->fetchOneCell() == 0;
-
-        $countNoCoordinates = XDB::query("SELECT COUNT(*) FROM adresses WHERE cityid IS NOT NULL AND glat = 0 AND glng = 0");
-        $noCoordinates = $countNoCoordinates->fetchOneCell();
-
-        if (isset($refresh) && $missing) {
-            $page->assign("pl_extra_header", "<meta http-equiv='Refresh' content='3'/>");
+    function handler_map($page)
+    {
+        if (Get::b('ajax')) {
+            self::assign_json_to_map($page);
+            return PL_JSON;
+        } else {
+            self::prepare_map($page);
         }
-        $page->assign("nb_cities_not_on_map", $missing);
-        $page->assign("no_smallest", $noSmallest);
-        $page->assign("no_coordinates", $noCoordinates);
     }
 
-    function handler_admin_country(&$page, $action = 'list', $id = null)
+    function handler_map_url($page)
     {
-        $page->assign('xorg_title', 'Polytechnique.org - Administration - Pays');
-        $page->assign('title', 'Gestion des pays');
-        $table_editor = new PLTableEditor('admin/geoloc/country', 'geoloc_pays', 'a2', true);
-        $table_editor->describe('a2', 'alpha-2', true);
-        $table_editor->describe('a3', 'alpha-3', false);
-        $table_editor->describe('n3', 'ISO numeric', false);
-        $table_editor->describe('num', 'num', false);
-        $table_editor->describe('worldrgn', 'Continent', false);
-        $table_editor->describe('subd', 'Subdivisions territoriales', false);
-        $table_editor->describe('post', 'post', false);
-        $table_editor->describe('pays', 'Nom (fr)', true);
-        $table_editor->describe('country', 'Nom (en)', true);
-        $table_editor->describe('phoneprf', 'Préfixe téléphonique', true);
-        $table_editor->describe('phoneformat', 'Format du téléphone (ex: (+p) ### ## ## ##)', false);
-        $table_editor->describe('capital', 'Capitale', true);
-        $table_editor->describe('nat', 'Nationalité', true);
-        $table_editor->describe('display', 'Format des adresses', false);
+        pl_content_headers('text/plain');
 
-        if ($action == 'update') {
-            if (Post::has('a2') && (Post::v('a2') == $id) && Post::has('phoneprf') && (Post::v('phoneprf') != '')) {
-                if (Post::has('phoneformat')) {
-                    $new_format = Post::v('phoneformat');
-                } else {
-                    $new_format = '';
-                }
-                $res = XDB::query("SELECT phoneformat
-                                     FROM geoloc_pays
-                                    WHERE phoneprf = {?}
-                                    LIMIT 1",
-                                  Post::v('phoneprf'));
-                $old_format = $res->fetchOneCell();
-                if ($new_format != $old_format) {
-                    require_once("profil.func.inc.php");
-                    XDB::execute("UPDATE  geoloc_pays
-                                     SET  phoneformat = {?}
-                                   WHERE  phoneprf = {?}",
-                                 $new_format, Post::v('phoneprf'));
-                }
-            }
+        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'));
         }
-        $table_editor->apply($page, $action, $id);
-    }
 
+        exit();
+    }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>