source: extensions/GMaps/gmaps_pip.class.inc.php @ 7482

Last change on this file since 7482 was 7482, checked in by grum, 14 years ago

Maps are not displayed if user navigate with tag
bug:1967

  • Property svn:executable set to *
File size: 15.3 KB
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : GMaps
4  Author     : Grum
5    email    : grum@piwigo.org
6    website  : http://photos.grum.fr
7
8    << May the Little SpaceFrog be with you ! >>
9  ------------------------------------------------------------------------------
10  See main.inc.php for release information
11
12  GMaps_PIP : classe to manage plugin public pages
13
14  --------------------------------------------------------------------------- */
15
16include_once('gmaps_root.class.inc.php');
17
18class GMaps_PIP extends GMaps_root
19{
20  protected $category=array(
21    'id' => 0,
22    'bounds' => array(
23      'N' => -90,
24      'S' => 90,
25      'E' => -180,
26      'W' => 180
27    ),
28    'icon' => array(
29      'style' => true,
30      'file' => '',
31      'width' => -1,
32      'height' => -1
33    )
34  );
35  protected $picture=array(
36    'geolocated' => false,
37    'forceDisplay' => false,
38    'coords' => array('lat' => 0, 'lng' => 0),
39    'content' => array(
40      'I' => array(), // icon display mode
41      'M' => array(), // meta display mode
42    ),
43    'properties' => array(),
44    'icon' => array(
45      'style' => true,
46      'file' => '',
47      'width' => -1,
48      'height' => -1
49      )
50  );
51  protected $css2;
52
53  public function __construct($prefixeTable, $filelocation)
54  {
55    parent::__construct($prefixeTable, $filelocation);
56    $this->css2 = new GPCCss(dirname($this->getFileLocation()).'/'.$this->getPluginNameFiles()."2.css");
57    $this->loadConfig();
58    $this->initEvents();
59    $this->load_lang();
60  }
61
62  public function __destruct()
63  {
64    unset($maps);
65    unset($picture);
66    parent::__destruct();
67  }
68
69  /*
70    load language file
71  */
72  public function load_lang()
73  {
74    global $lang;
75
76    load_language('plugin.lang', GMAPS_PATH);
77  }
78
79  /*
80    initialize events call for the plugin
81  */
82  public function initEvents()
83  {
84    parent::initEvents();
85
86    add_event_handler('loc_begin_index', array(&$this, 'displayCategoryPageMap'));
87    add_event_handler('loc_begin_picture', array(&$this, 'displayPicturePageMap'), 55);
88    add_event_handler('amd_jpegMD_loaded', array(&$this, 'preparePictureMaps'));
89    add_event_handler('loc_end_page_header', array(&$this->css2, 'applyCSS'));
90  }
91
92
93
94  /* -------------------------------------------------------------------------
95    FUNCTIONS TO MANAGE GMAPS
96  ------------------------------------------------------------------------- */
97
98  /**
99   * this function display the map on the category page
100   */
101  public function displayCategoryPageMap()
102  {
103    global $page, $prefixeTable, $template, $user, $conf;
104
105    if($page['section']=='categories' or $page['section']=='tags')
106    {
107      if(isset($page['category']))
108      {
109        $this->category['id']=$page['category']['id'];
110        $this->buildMapList($page['category']['id'], 'C');
111
112        // check if there is picture with gps tag in the selected category
113        $sql="SELECT paut.tagId, MAX(CAST(pait.value AS DECIMAL(20,17))) AS maxValue, MIN(CAST(pait.value AS DECIMAL(20,17))) AS minValue
114              FROM (((".USER_CACHE_CATEGORIES_TABLE." pucc
115                LEFT JOIN ".CATEGORIES_TABLE." pct ON pucc.cat_id = pct.id)
116                LEFT JOIN ".IMAGE_CATEGORY_TABLE." pic ON pic.category_id = pucc.cat_id)
117                LEFT JOIN ".$prefixeTable."amd_images_tags pait ON pait.imageId = pic.image_id)
118                LEFT JOIN ".$prefixeTable."amd_used_tags paut ON pait.numId = paut.numId
119              WHERE pucc.user_id = '".$user['id']."'
120               AND (paut.tagId = 'magic.GPS.LatitudeNum' OR paut.tagId = 'magic.GPS.LongitudeNum')
121               AND pic.image_id IS NOT NULL
122               AND FIND_IN_SET(".$page['category']['id'].", pct.uppercats)!=0
123               GROUP BY paut.tagId";
124      }
125      elseif(isset($page['items']))
126      {
127        if(count($page['items'])==0) return(false);
128        // 'tags'
129        $sql="SELECT paut.tagId, MAX(CAST(pait.value AS DECIMAL(20,17))) AS maxValue, MIN(CAST(pait.value AS DECIMAL(20,17))) AS minValue
130              FROM ".$prefixeTable."amd_images_tags pait
131                    LEFT JOIN ".$prefixeTable."amd_used_tags paut ON pait.numId = paut.numId
132              WHERE (paut.tagId = 'magic.GPS.LatitudeNum' OR paut.tagId = 'magic.GPS.LongitudeNum')
133               AND pait.imageId IN (".implode(',', $page['items']).")
134               GROUP BY paut.tagId";
135        $this->category['id']=0;
136        $this->buildMapList(0, 'C');
137      }
138      else
139      {
140        // 'home'
141        $this->category['id']=0;
142        $this->buildMapList(0, 'C');
143        // check if there is picture with gps tag in the selected category
144        $sql="SELECT paut.tagId, MAX(CAST(pait.value AS DECIMAL(20,17))) AS maxValue, MIN(CAST(pait.value AS DECIMAL(20,17))) AS minValue
145              FROM (((".USER_CACHE_CATEGORIES_TABLE." pucc
146                LEFT JOIN ".CATEGORIES_TABLE." pct ON pucc.cat_id = pct.id)
147                LEFT JOIN ".IMAGE_CATEGORY_TABLE." pic ON pic.category_id = pucc.cat_id)
148                LEFT JOIN ".$prefixeTable."amd_images_tags pait ON pait.imageId = pic.image_id)
149                LEFT JOIN ".$prefixeTable."amd_used_tags paut ON pait.numId = paut.numId
150              WHERE pucc.user_id = '".$user['id']."'
151               AND (paut.tagId = 'magic.GPS.LatitudeNum' OR paut.tagId = 'magic.GPS.LongitudeNum')
152               AND pic.image_id IS NOT NULL
153               GROUP BY paut.tagId";
154      }
155
156      if(count($this->maps)>0)
157      {
158        $scripts=array();
159
160        $result=pwg_query($sql);
161        if($result)
162        {
163          $nb=0;
164          while($row=pwg_db_fetch_assoc($result))
165          {
166            switch($row['tagId'])
167            {
168              case 'magic.GPS.LatitudeNum':
169                $this->category['bounds']['N']=$row['maxValue'];
170                $this->category['bounds']['S']=$row['minValue'];
171                break;
172              case 'magic.GPS.LongitudeNum':
173                $this->category['bounds']['E']=$row['maxValue'];
174                $this->category['bounds']['W']=$row['minValue'];
175                break;
176            }
177            $nb++;
178          }
179
180          if($nb>0 or $this->forceDisplay>0)
181          {
182            /*
183             * prepare js script for each map
184             */
185
186            foreach($this->maps as $keyMap => $map)
187            {
188              if($nb>0 or
189                 $map['forceDisplay']=='y' and ($map['kmlFileUrl']!='' or $map['kmlFileId']!=0)
190                )
191              {
192                $scripts[]="
193                {
194                  id:'iGMapsIcon',
195                  zoomLevel:".$map['zoomLevel'].",
196                  markerImg:'".$map['marker']."',
197                  mapType:'".$map['mapType']."',
198                  mapTypeControl:'".$map['mapTypeControl']."',
199                  navigationControl:'".$map['navigationControl']."',
200                  scaleControl:'".$map['scaleControl']."',
201                  streetViewControl:'".$map['streetViewControl']."',
202                  kmlFileUrl:'".$map['kmlFileUrl']."',
203                  displayType:'".$map['displayType']."',
204                  sizeMode:'".$map['sizeMode']."',
205                  title:'".addslashes( ($map['title']=='')?l10n('gmaps_geolocation'):$map['title']  )."',
206                  markers:[],
207                  fitToBounds:true,
208                  zoomLevelMaxActivated:".($map['zoomLevelMaxActivated']=='y'?'true':'false')."
209                }";
210
211                preg_match('/^i(\d+)x(\d+).*/i', basename($map['icon']), $result);
212                $this->category['icon']['iconStyle']=$map['iconStyle'];
213                $this->category['icon']['file']=$map['icon'];
214                $this->category['icon']['width']=isset($result[1])?$result[1]:-1;
215                $this->category['icon']['height']=isset($result[2])?$result[2]:-1;
216              }
217            }
218
219            $template->assign('maps', $this->maps);
220            $template->set_filename('gmapsCatMap',
221                        dirname($this->getFileLocation()).'/templates/gmaps_category.tpl');
222            $template->append('footer_elements', $template->parse('gmapsCatMap', true), false);
223
224            if(is_array($this->category['icon']))
225            {
226              $template->assign('mapIcon', $this->category['icon']);
227              $template->set_filename('gmapsIconButton',
228                          dirname($this->getFileLocation()).'/templates/gmaps_category_iconbutton.tpl');
229              $template->concat('PLUGIN_INDEX_ACTIONS', $template->parse('gmapsIconButton', true), false);
230              $template->assign('mapIcon');
231            }
232
233
234            $template->append('head_elements',
235  "<script type=\"text/javascript\">
236  gmaps =
237    {
238      geolocated:".($nb>0?'true':'false').",
239      forceDisplay:".($nb==0?'true':'false').",
240      lang:{
241        boundmap:'".l10n('gmaps_i_boundmap')."',
242        boundkml:'".l10n('gmaps_i_boundkml')."',
243        loading:'".l10n('gmaps_loading')."'
244      },
245      requestId:'',
246      categoryId:".$this->category['id'].",
247      bounds:
248        {
249          north:".$this->category['bounds']['N'].",
250          south:".$this->category['bounds']['S'].",
251          east:".$this->category['bounds']['E'].",
252          west:".$this->category['bounds']['W']."
253        },
254      maps:
255      [".implode(',', $scripts)."],
256      popupAutomaticSize:".$this->config['popupAutomaticSize'].",
257      callId:0
258    }
259  </script>", false);
260
261          }
262        }
263      }
264    }
265  }
266
267
268
269  /**
270   * this function display the map on the picture page
271   *
272   * the 'amd_jpegMD_loaded' event is triggered before the 'loc_begin_picture'
273   * event so, when this function is called the $this->picture var was already
274   * initialized
275   */
276  public function displayPicturePageMap()
277  {
278    global $page, $template;
279
280    if($this->picture['geolocated']==false and $this->picture['forceDisplay']==false) return(false);
281    if(isset($this->picture['content']['MP']) and count($this->picture['content']['MP'])>0)
282    {
283      // there is maps in meta display mode
284      $template->set_filename('gmapsMeta',
285                  dirname($this->getFileLocation()).'/templates/gmaps_picture_meta.tpl');
286      $template->assign('maps', $this->picture['content']['MP']);
287
288      $metaTitle='';
289      foreach($this->picture['content']['MP'] as $map)
290      {
291        if($metaTitle=='') $metaTitle=$map['title'];
292      }
293
294      $metadata=array
295        (
296          'TITLE' => ($metaTitle=='')?l10n('gmaps_geolocation'):$metaTitle,
297          'lines' =>
298            array(
299              /* <!--rawContent-->  is a trick to display raw data in tabs
300               * for the gally template
301               *
302               * on the default template, the displayed content is done
303               * normally
304               */
305              '<!--rawContent-->' => $template->parse('gmapsMeta', true)
306            )
307        );
308      $template->append('metadata', $metadata, false);
309    }
310
311    if(isset($this->picture['content']['IP']) and count($this->picture['content']['IP'])>0)
312    {
313      // there is maps in icon display mode
314      $template->assign('map', $this->picture['content']['IP'][0]);
315      $template->assign('mapIcon', $this->picture['icon']);
316
317      $template->set_filename('gmapsIconMap',
318                  dirname($this->getFileLocation()).'/templates/gmaps_picture_icon.tpl');
319      $template->append('footer_elements', $template->parse('gmapsIconMap', true), false);
320
321      $template->set_filename('gmapsIconButton',
322                  dirname($this->getFileLocation()).'/templates/gmaps_picture_iconbutton.tpl');
323      $template->concat('PLUGIN_PICTURE_ACTIONS',  $template->parse('gmapsIconButton', true), false);
324    }
325
326    if(count($this->picture['properties'])>0)
327    {
328      $template->append('head_elements',
329"<script type=\"text/javascript\">
330  gmaps =
331    {
332      geolocated:".($this->picture['geolocated']?'true':'false').",
333      forceDisplay:".($this->picture['forceDisplay']?'true':'false').",
334      lang:{
335        centermap:'".l10n('gmaps_i_centermap')."',
336        boundkml:'".l10n('gmaps_i_boundkml')."'
337      },
338      coords:
339      {
340        latitude:'".$this->picture['coords']['lat']."',
341        longitude:'".$this->picture['coords']['lng']."',
342      },
343      maps:
344      [".implode(',', $this->picture['properties'])."],
345      popupAutomaticSize:".$this->config['popupAutomaticSize']."
346    }
347</script>", false);
348    }
349  }
350
351
352
353
354  /**
355   * prepare the maps for the picture page
356   *
357   * this function is called when the plugin AdvancedMetadata has finished to
358   * read the metadata ; if picture is not geolocated, there is no map to display
359   *
360   * @param JpegMetadata $jpegMD : a JpegMetadata object
361   */
362  public function preparePictureMaps($jpegMD)
363  {
364    global $template, $page, $user;
365
366    $isGeolocated=true;
367
368    if(is_null($jpegMD->getTag('magic.GPS.LatitudeNum')) or
369       is_null($jpegMD->getTag('magic.GPS.LongitudeNum'))) $isGeolocated=false;
370
371
372    if(isset($page['category']))
373    {
374      $this->buildMapList($page['category']['id'], 'P');
375    }
376    else
377    {
378      $sql="SELECT GROUP_CONCAT(pict.category_id)
379            FROM ".IMAGE_CATEGORY_TABLE." pict
380            WHERE pict.image_id=".$page['image_id'];
381      if($user['forbidden_categories']!='') $sql.=" AND pict.category_id NOT IN (".$user['forbidden_categories'].")";
382
383      $result=pwg_query($sql);
384      if($result)
385      {
386        while($row=pwg_db_fetch_row($result))
387        {
388          $cats=explode(',', $row[0]);
389        }
390      }
391
392      $cats[]=0;
393      $i=0;
394      while(count($this->maps)==0 and $i<count($cats))
395      {
396        $this->buildMapList($cats[$i], 'P');
397        $i++;
398      }
399    }
400
401    if($this->forceDisplay==0 and !$isGeolocated) return(false);
402
403    $this->picture['geolocated']=$isGeolocated;
404    $this->picture['forceDisplay']=!$isGeolocated;
405    if($isGeolocated)
406    {
407      $this->picture['coords']['lat']=$jpegMD->getTag('magic.GPS.LatitudeNum')->getValue();
408      $this->picture['coords']['lng']=$jpegMD->getTag('magic.GPS.LongitudeNum')->getValue();
409    }
410
411
412    foreach($this->maps as $map)
413    {
414      if($isGeolocated or
415         $map['forceDisplay']=='y' and ($map['kmlFileUrl']!='' or $map['kmlFileId']!=0)
416        )
417      {
418        if($map['displayType']=='IP')
419        {
420          preg_match('/^i(\d+)x(\d+).*/i', basename($map['icon']), $result);
421          $this->picture['icon']=array(
422            'iconStyle' => $map['iconStyle'],
423            'file' => $map['icon'],
424            'width' => isset($result[1])?$result[1]:-1,
425            'height' => isset($result[2])?$result[2]:-1
426          );
427        }
428
429
430        $this->picture['content'][$map['displayType']][]=array(
431          'id' => $map['id'],
432          'width' => $map['width'],
433          'height' => $map['height'],
434          'style' => $map['style'],
435          'displayType' => $map['displayType'],
436          'title' => $map['title'],
437        );
438
439        $this->picture['properties'][]="
440        {
441          id:'iGMaps".(($map['displayType']=='IP')?'Icon':$map['id'])."',
442          zoomLevel:".$map['zoomLevel'].",
443          markerImg:'".$map['marker']."',
444          mapType:'".$map['mapType']."',
445          mapTypeControl:'".$map['mapTypeControl']."',
446          navigationControl:'".$map['navigationControl']."',
447          scaleControl:'".$map['scaleControl']."',
448          streetViewControl:'".$map['streetViewControl']."',
449          kmlFileUrl:'".$map['kmlFileUrl']."',
450          displayType:'".$map['displayType']."',
451          sizeMode:'".$map['sizeMode']."',
452          title:'".addslashes( ($map['title']=='')?l10n('gmaps_geolocation'):$map['title']  )."'
453        }";
454      }
455    }
456  }
457
458
459
460
461} //class
462
463?>
Note: See TracBrowser for help on using the repository browser.