Index: extensions/GMaps/gmaps_ajax.php
===================================================================
--- extensions/GMaps/gmaps_ajax.php	(revision 7177)
+++ extensions/GMaps/gmaps_ajax.php	(revision 7308)
@@ -77,6 +77,5 @@
 
            $_REQUEST['ajaxfct']=='public.maps.init' or
-           $_REQUEST['ajaxfct']=='public.maps.getMarkers' //or
-           //$_REQUEST['ajaxfct']=='public.maps.getMarkerInfos'
+           $_REQUEST['ajaxfct']=='public.maps.getMarkers'
            )) $_REQUEST['ajaxfct']='';
 
@@ -133,16 +132,18 @@
           else
           {
-            if(!(isset($_REQUEST['datas']['displayType']) &&
-                 isset($_REQUEST['datas']['sizeMode']) &&
-                 isset($_REQUEST['datas']['name']) &&
-                 isset($_REQUEST['datas']['width']) &&
-                 isset($_REQUEST['datas']['height']) &&
-                 isset($_REQUEST['datas']['zoomLevel']) &&
-                 isset($_REQUEST['datas']['mapType']) &&
-                 isset($_REQUEST['datas']['mapTypeControl']) &&
-                 isset($_REQUEST['datas']['navigationControl']) &&
-                 isset($_REQUEST['datas']['scaleControl']) &&
-                 isset($_REQUEST['datas']['streetViewControl']) &&
-                 isset($_REQUEST['datas']['style'])))
+            if(!(isset($_REQUEST['datas']['displayType']) and
+                 isset($_REQUEST['datas']['sizeMode']) and
+                 isset($_REQUEST['datas']['name']) and
+                 isset($_REQUEST['datas']['width']) and
+                 isset($_REQUEST['datas']['height']) and
+                 isset($_REQUEST['datas']['zoomLevel']) and
+                 isset($_REQUEST['datas']['mapType']) and
+                 isset($_REQUEST['datas']['mapTypeControl']) and
+                 isset($_REQUEST['datas']['navigationControl']) and
+                 isset($_REQUEST['datas']['scaleControl']) and
+                 isset($_REQUEST['datas']['streetViewControl']) and
+                 isset($_REQUEST['datas']['style']) and
+                 isset($_REQUEST['datas']['zoomLevelMaxActivated'])
+                 ))
             {
               $_REQUEST['ajaxfct']='';
@@ -187,4 +188,7 @@
               if(!($_REQUEST['datas']['streetViewControl']=='y' or
                    $_REQUEST['datas']['streetViewControl']=='n')) $_REQUEST['datas']['streetViewControl']='n';
+
+              if(!($_REQUEST['datas']['zoomLevelMaxActivated']=='y' or
+                   $_REQUEST['datas']['zoomLevelMaxActivated']=='n')) $_REQUEST['datas']['zoomLevelMaxActivated']='n';
             }
           }
@@ -204,11 +208,11 @@
           else
           {
-            if(!(isset($_REQUEST['datas']['categoryId']) &&
-                 isset($_REQUEST['datas']['mapId']) &&
-                 isset($_REQUEST['datas']['applySubCat']) &&
-                 isset($_REQUEST['datas']['kmlFileId']) &&
-                 isset($_REQUEST['datas']['kmlFileUrl']) &&
-                 isset($_REQUEST['datas']['icon']) &&
-                 isset($_REQUEST['datas']['marker']) &&
+            if(!(isset($_REQUEST['datas']['categoryId']) and
+                 isset($_REQUEST['datas']['mapId']) and
+                 isset($_REQUEST['datas']['applySubCat']) and
+                 isset($_REQUEST['datas']['kmlFileId']) and
+                 isset($_REQUEST['datas']['kmlFileUrl']) and
+                 isset($_REQUEST['datas']['icon']) and
+                 isset($_REQUEST['datas']['marker']) and
                  isset($_REQUEST['datas']['title']) ))
             {
@@ -262,23 +266,17 @@
           else
           {
-            if(!(isset($_REQUEST['datas']['requestId']) &&
-                 isset($_REQUEST['datas']['bounds']) &&
-                 isset($_REQUEST['datas']['width']) &&
-                 isset($_REQUEST['datas']['height']) &&
-                 isset($_REQUEST['datas']['distanceTreshold']) &&
-                 isset($_REQUEST['datas']['bounds']['north']) &&
-                 isset($_REQUEST['datas']['bounds']['south']) &&
-                 isset($_REQUEST['datas']['bounds']['east']) &&
+            if(!(isset($_REQUEST['datas']['requestId']) and
+                 isset($_REQUEST['datas']['callId']) and
+                 isset($_REQUEST['datas']['bounds']) and
+                 isset($_REQUEST['datas']['width']) and
+                 isset($_REQUEST['datas']['height']) and
+                 isset($_REQUEST['datas']['distanceTreshold']) and
+                 isset($_REQUEST['datas']['bounds']['north']) and
+                 isset($_REQUEST['datas']['bounds']['south']) and
+                 isset($_REQUEST['datas']['bounds']['east']) and
                  isset($_REQUEST['datas']['bounds']['west']) ))
             {
               $_REQUEST['ajaxfct']='';
             }
-            else
-            {
-              if(!isset($_REQUEST['datas']['levelInfo'])) $_REQUEST['datas']['levelInfo']=1;
-
-              if(!($_REQUEST['datas']['levelInfo']==0 or
-                   $_REQUEST['datas']['levelInfo']==1)) $_REQUEST['datas']['levelInfo']=1;
-            }
           }
         }
@@ -293,8 +291,5 @@
             $_REQUEST['ajaxfct']='';
           }
-
-
-        }
-
+        }
       }
     } //checkRequest
@@ -353,4 +348,7 @@
           $result=$this->ajax_gmaps_public_mapsGetMarkers($_REQUEST['datas']);
           break;
+        case 'public.maps.getMarkersInfos':
+          $result=$this->ajax_gmaps_public_mapsGetMarkersInfos($_REQUEST['datas']);
+          break;
       }
       GPCAjax::returnResult($result);
@@ -378,5 +376,7 @@
       $datas=Array();
 
-      $sql="SELECT id, name, displayType, sizeMode, width, height, zoomLevel, mapType, mapTypeControl, navigationControl, scaleControl, streetViewControl
+      $sql="SELECT id, name, displayType, sizeMode, width, height, zoomLevel,
+                   mapType, mapTypeControl, navigationControl, scaleControl,
+                   streetViewControl, zoomLevelMaxActivated
             FROM ".$this->tables['maps']."
             ORDER BY displayType ASC, name ASC";
@@ -443,5 +443,6 @@
                 $properties['scaleControl']."', '".
                 $properties['streetViewControl']."', '".
-                mysql_escape_string($properties['style'])."');";
+                mysql_escape_string($properties['style'])."', '".
+                $properties['zoomLevelMaxActivated']."');";
         $result=pwg_query($sql);
         $id=pwg_db_insert_id();
@@ -461,5 +462,6 @@
                 $properties['scaleControl']."', streetViewControl='".
                 $properties['streetViewControl']."', style='".
-                mysql_escape_string($properties['style'])."'
+                mysql_escape_string($properties['style'])."', zoomLevelMaxActivated='".
+                $properties['zoomLevelMaxActivated']."'
               WHERE id='$id';";
         $result=pwg_query($sql);
@@ -490,10 +492,20 @@
         'navigationControl' => 0,
         'scaleControl' => 'y',
-        'streetViewControl' => 'n'
+        'streetViewControl' => 'n',
+        'zoomLevelMaxActivated' => 'n',
+        'associations' => 0,
       );
 
-      $sql="SELECT id, displayType, sizeMode, name, width, height, zoomLevel, mapType, mapTypeControl, navigationControl, scaleControl, streetViewControl
-            FROM ".$this->tables['maps']."
-            WHERE id='$id';";
+      $sql="SELECT pgmm.id, pgmm.displayType, pgmm.sizeMode, pgmm.name,
+                   pgmm.width, pgmm.height, pgmm.zoomLevel, pgmm.mapType,
+                   pgmm.mapTypeControl, pgmm.navigationControl,
+                   pgmm.scaleControl, pgmm.streetViewControl,
+                   pgmm.zoomLevelMaxActivated,
+                   COUNT(pgcm.id) AS nbAssoc
+            FROM ".$this->tables['maps']." pgmm
+                  LEFT JOIN ".$this->tables['category_maps']." pgcm
+                    ON pgcm.mapId=pgmm.id
+            WHERE pgmm.id='$id'
+            GROUP BY pgmm.id;";
       $result=pwg_query($sql);
       if($result)
@@ -781,5 +793,5 @@
         while($row=pwg_db_fetch_assoc($result))
         {
-          $row['fileSize']=$this->formatOctet($row['fileSize']);
+          $row['fileSize']=GPCCore::formatOctet($row['fileSize']);
           $datas[]=$row;
         }
@@ -852,5 +864,5 @@
         }
 
-        $file=dirname(__FILE__).'/kml/'.$file;
+        $file=GPCCore::getPiwigoSystemPath().self::KML_DIRECTORY.$file;
 
         if(file_exists($file)) unlink($file);
@@ -858,5 +870,5 @@
         {
           $currentInfo=$this->ajax_gmaps_admin_kmlFilesGetFile($id, false);
-          if(file_exists(dirname(__FILE__).'/kml/'.$currentInfo['file'])) unlink(dirname(__FILE__).'/kml/'.$currentInfo['file']);
+          if(file_exists(self::KML_DIRECTORY.$currentInfo['file'])) unlink(GPCCore::getPiwigoSystemPath().self::KML_DIRECTORY.$currentInfo['file']);
         }
 
@@ -933,5 +945,5 @@
         while($row=pwg_db_fetch_assoc($result))
         {
-          $row['fileSizeUnits']=$this->formatOctet($row['fileSize']);
+          $row['fileSizeUnits']=GPCCore::formatOctet($row['fileSize']);
           $returned=$row;
         }
@@ -989,7 +1001,7 @@
       if($result)
       {
-        if($file!='' and file_exists(dirname(__FILE__).'/kml/'.$file))
-        {
-          unlink(dirname(__FILE__).'/kml/'.$file);
+        if($file!='' and file_exists(GPCCore::getPiwigoSystemPath().self::KML_DIRECTORY.$file))
+        {
+          unlink(GPCCore::getPiwigoSystemPath().self::KML_DIRECTORY.$file);
         }
         return('ok');
@@ -1092,5 +1104,5 @@
               'latitude' => $coords[0],
               'longitude' => $coords[1],
-              'imageName' => mysql_escape_string($row['imageName']),
+              'imageName' => GPCCore::getUserLanguageDesc(mysql_escape_string($row['imageName'])),
               'imageTnFile' => dirname($row['path']).'/'.$conf['dir_thumbnail'].'/'.$conf['prefix_thumbnail'].get_filename_wo_extension(basename($row['path'])).'.'.$row['tn_ext'],
               'imageCatsId' => $row['imageCatsId'],
@@ -1119,6 +1131,4 @@
      *                       'width' : width of maps in pixels
      *                       'height' : height of maps in pixels
-     *                       'levelInfo' : 0 = don't return pictures info (name, url, ...)
-     *                                     1 = return pictures informations
      * @return Array|String : a JSON string of an array of points (nbPictures, lat, lng)
      */
@@ -1128,4 +1138,5 @@
 
       $returned=array(
+        'callId' => $datas['callId'],
         'markers' => array(),
         'datas' => array(
@@ -1200,42 +1211,21 @@
         // works with the highest ratio
         $ratio=max($ratioW, $ratioH);
-
-
-        $points=array();
+        $datas['distanceTreshold']=pow($datas['distanceTreshold']*$ratio,2);
+
+
         $groups=array();
 
-        while($row=pwg_db_fetch_assoc($result))
-        {
-          $points[]=$row;
-        }
 
         /*
          *  build groups
          */
-        while(count($points)>0)
-        {
-          $currentGroup=array();
-          $currentGroup[]=array_shift($points);
-
-          $i=0;
-          while($i<count($points))
-          {
-            $dist=sqrt(pow($points[$i]['latitude']-$currentGroup[0]['latitude'],2) + pow($points[$i]['longitude']-$currentGroup[0]['longitude'],2))/$ratio;
-
-            if($dist <= $datas['distanceTreshold'])
-            {
-              $tmp=array_splice($points, $i, 1);
-              $currentGroup[]=$tmp[0];
-              $points=array_values($points); // reset array index...
-            }
-            else
-            {
-              $i++;
-            }
-          }
-
-          $groups[]=$currentGroup;
-          unset($currentGroup);
-        }
+        while($row=pwg_db_fetch_assoc($result))
+        {
+          if(!$this->checkInGroup($row, $groups, $datas['distanceTreshold']))
+          {
+            $groups[]=array($row);
+          }
+        }
+
 
         /*
@@ -1247,5 +1237,5 @@
           $coords=array(
             'nbImgTxt' => '',
-            'nbImages' => 0,
+            'nbImg' => 0,
             'lat' => 0,                   // 'lat' rather than 'latitude'
             'lng' => 0,                   // 'lng' rather than 'longitude'
@@ -1270,5 +1260,5 @@
                   'category' => array(
                     'id' => $id,
-                    'name' => $tmpCatsNames[$key],
+                    'name' => GPCCore::getUserLanguageDesc($tmpCatsNames[$key]),
                     'permalink' => (substr($tmpCatsPLinks[$key],0,1)=='*')?'':$tmpCatsPLinks[$key],
                   )
@@ -1279,22 +1269,19 @@
             if(count($tmpCatsId)<=1) $tmpCatsNames='';
 
-            $coords['nbImages']++;
+            $coords['nbImg']++;
 
             $coords['lat']+=$point['latitude'];
             $coords['lng']+=$point['longitude'];
 
-            if($datas['levelInfo']==1)
-            {
-              $coords['imgTn'][]=substr($point['imageTnFile'],12);
-              $coords['imgCatsNames'][]=$tmpCatsNames;
-              $coords['imgName'][]=$point['imageName'];
-              $coords['imgCatsUrl'][]=$tmpCatsUrl;
-            }
+            $coords['imgTn'][]=substr($point['imageTnFile'],12);
+            $coords['imgCatsNames'][]=$tmpCatsNames;
+            $coords['imgName'][]=GPCCore::getUserLanguageDesc($point['imageName']);
+            $coords['imgCatsUrl'][]=$tmpCatsUrl;
           }
           $coords['lat']=$coords['lat']/count($group);
           $coords['lng']=$coords['lng']/count($group);
-          $coords['uniqueId']=md5($coords['lat'].$coords['lng']);
-
-          if($coords['nbImages']==1)
+          $coords['uId']=md5($coords['lat'].$coords['lng']);
+
+          if($coords['nbImg']==1)
           {
             $coords['nbImgTxt']=l10n('gmaps_1_picture');
@@ -1302,9 +1289,9 @@
           else
           {
-            $coords['nbImgTxt']=sprintf(l10n('gmaps_nb_pictures'), $coords['nbImages']);
+            $coords['nbImgTxt']=sprintf(l10n('gmaps_nb_pictures'), $coords['nbImg']);
           }
 
           $returned['markers'][]=$coords;
-          $returned['datas']['nbPhotos']+=$coords['nbImages'];
+          $returned['datas']['nbPhotos']+=$coords['nbImg'];
         }
       }
@@ -1324,82 +1311,27 @@
 
       return(json_encode($returned));
+    } //ajax_gmaps_public_mapsGetMarkers
+
+    /**
+     * check if a point is inside an existing group, and add it
+     * @param Array $point : the point
+     * @param Array &$groups : the groups
+     * @param Float $maxDist : maximum distance to be inside group
+     * @return Bool : true if added in a group, otherwise false
+     */
+    private function checkInGroup($point, &$groups, $maxDist)
+    {
+      foreach($groups as $key=>$group)
+      {
+        $dist=pow($point['latitude']-$group[0]['latitude'],2) + pow($point['longitude']-$group[0]['longitude'],2);
+
+        if($dist<=$maxDist)
+        {
+          $groups[$key][]=$point;
+          return(true);
+        }
+      }
+      return(false);
     }
-
-
-
-
-     /**
-      * formats a file size into a human readable size
-      *
-      * @param String $format : "A"  : auto
-      *                         "Ai" : auto (io)
-      *                         "O"  : o
-      *                         "K"  : Ko
-      *                         "M"  : Mo
-      *                         "G"  : Go
-      *                         "Ki" : Kio
-      *                         "Mi" : Mio
-      *                         "Gi" : Gio
-      * @param String $thsep : thousand separator
-      * @param Integer $prec : number of decimals
-      * @param Bool $visible : display or not the unit
-      * @return String : a formatted file size
-      */
-     private function formatOctet($octets, $format="Ai", $thsep="", $prec=2, $visible=true)
-     {
-      if($format=="Ai")
-      {
-       if($octets<1024)
-       { $format="O"; }
-       elseif($octets<1024000)
-       { $format="Ki"; }
-       elseif($octets<1024000000)
-       { $format="Mi"; }
-       else
-       { $format="Gi"; }
-      }
-      elseif($format=="A")
-      {
-       if($octets<1000)
-       { $format="O"; }
-       elseif($octets<1000000)
-       { $format="Ki"; }
-       elseif($octets<1000000000)
-       { $format="Mi"; }
-       else
-       { $format="Gi"; }
-      }
-
-      switch($format)
-      {
-       case "O":
-        $unit="o"; $div=1;
-        break;
-       case "K":
-        $unit="Ko"; $div=1000;
-        break;
-       case "M":
-        $unit="Mo"; $div=1000000;
-        break;
-       case "G":
-        $unit="Go"; $div=1000000000;
-        break;
-       case "Ki":
-        $unit="Kio"; $div=1024;
-        break;
-       case "Mi":
-        $unit="Mio"; $div=1024000;
-        break;
-       case "Gi":
-        $unit="Gio"; $div=1024000000;
-        break;
-      }
-
-      $returned=number_format($octets/$div, $prec, '.', $thsep);
-      if($visible) $returned.=' '.$unit;
-      return($returned);
-     } //function formatOctet
-
-
 
   } //class
