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

Last change on this file since 7479 was 7479, checked in by grum, 13 years ago

implement feature:1950

  • Property svn:executable set to *
File size: 13.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')
106    {
107      if(isset($page['category']))
108      {
109        $this->category['id']=$page['category']['id'];
110        $this->buildMapList($page['category']['id'], 'C');
111        $sqlCatRestrict="AND FIND_IN_SET(".$page['category']['id'].", pct.uppercats)!=0";
112      }
113      else
114      {
115        $this->category['id']=0;
116        $this->buildMapList(0, 'C');
117        $sqlCatRestrict="";
118      }
119
120      if(count($this->maps)>0)
121      {
122        $scripts=array();
123
124        // check if there is picture with gps tag in the selected category
125        $sql="SELECT paut.tagId, MAX(CAST(pait.value AS DECIMAL(20,17))) AS maxValue, MIN(CAST(pait.value AS DECIMAL(20,17))) AS minValue
126              FROM (((".USER_CACHE_CATEGORIES_TABLE." pucc
127                LEFT JOIN ".CATEGORIES_TABLE." pct ON pucc.cat_id = pct.id)
128                LEFT JOIN ".IMAGE_CATEGORY_TABLE." pic ON pic.category_id = pucc.cat_id)
129                LEFT JOIN ".$prefixeTable."amd_images_tags pait ON pait.imageId = pic.image_id)
130                LEFT JOIN ".$prefixeTable."amd_used_tags paut ON pait.numId = paut.numId
131              WHERE pucc.user_id = '".$user['id']."'
132               AND (paut.tagId = 'magic.GPS.LatitudeNum' OR paut.tagId = 'magic.GPS.LongitudeNum')
133               AND pic.image_id IS NOT NULL
134               $sqlCatRestrict
135               GROUP BY paut.tagId";
136
137        $result=pwg_query($sql);
138        if($result)
139        {
140          $nb=0;
141          while($row=pwg_db_fetch_assoc($result))
142          {
143            switch($row['tagId'])
144            {
145              case 'magic.GPS.LatitudeNum':
146                $this->category['bounds']['N']=$row['maxValue'];
147                $this->category['bounds']['S']=$row['minValue'];
148                break;
149              case 'magic.GPS.LongitudeNum':
150                $this->category['bounds']['E']=$row['maxValue'];
151                $this->category['bounds']['W']=$row['minValue'];
152                break;
153            }
154            $nb++;
155          }
156
157          if($nb>0 or $this->forceDisplay>0)
158          {
159            /*
160             * prepare js script for each map
161             */
162
163            foreach($this->maps as $keyMap => $map)
164            {
165              if($nb>0 or
166                 $map['forceDisplay']=='y' and ($map['kmlFileUrl']!='' or $map['kmlFileId']!=0)
167                )
168              {
169                $scripts[]="
170                {
171                  id:'iGMapsIcon',
172                  zoomLevel:".$map['zoomLevel'].",
173                  markerImg:'".$map['marker']."',
174                  mapType:'".$map['mapType']."',
175                  mapTypeControl:'".$map['mapTypeControl']."',
176                  navigationControl:'".$map['navigationControl']."',
177                  scaleControl:'".$map['scaleControl']."',
178                  streetViewControl:'".$map['streetViewControl']."',
179                  kmlFileUrl:'".$map['kmlFileUrl']."',
180                  displayType:'".$map['displayType']."',
181                  sizeMode:'".$map['sizeMode']."',
182                  title:'".addslashes( ($map['title']=='')?l10n('gmaps_geolocation'):$map['title']  )."',
183                  markers:[],
184                  fitToBounds:true,
185                  zoomLevelMaxActivated:".($map['zoomLevelMaxActivated']=='y'?'true':'false')."
186                }";
187
188                preg_match('/^i(\d+)x(\d+).*/i', basename($map['icon']), $result);
189                $this->category['icon']['iconStyle']=$map['iconStyle'];
190                $this->category['icon']['file']=$map['icon'];
191                $this->category['icon']['width']=isset($result[1])?$result[1]:-1;
192                $this->category['icon']['height']=isset($result[2])?$result[2]:-1;
193              }
194            }
195
196            $template->assign('maps', $this->maps);
197            $template->set_filename('gmapsCatMap',
198                        dirname($this->getFileLocation()).'/templates/gmaps_category.tpl');
199            $template->append('footer_elements', $template->parse('gmapsCatMap', true), false);
200
201            if(is_array($this->category['icon']))
202            {
203              $template->assign('mapIcon', $this->category['icon']);
204              $template->set_filename('gmapsIconButton',
205                          dirname($this->getFileLocation()).'/templates/gmaps_category_iconbutton.tpl');
206              $template->concat('PLUGIN_INDEX_ACTIONS', $template->parse('gmapsIconButton', true), false);
207              $template->assign('mapIcon');
208            }
209
210
211            $template->append('head_elements',
212  "<script type=\"text/javascript\">
213  gmaps =
214    {
215      geolocated:".($nb>0?'true':'false').",
216      forceDisplay:".($nb==0?'true':'false').",
217      lang:{
218        boundmap:'".l10n('gmaps_i_boundmap')."',
219        boundkml:'".l10n('gmaps_i_boundkml')."',
220        loading:'".l10n('gmaps_loading')."'
221      },
222      requestId:'',
223      categoryId:".$this->category['id'].",
224      bounds:
225        {
226          north:".$this->category['bounds']['N'].",
227          south:".$this->category['bounds']['S'].",
228          east:".$this->category['bounds']['E'].",
229          west:".$this->category['bounds']['W']."
230        },
231      maps:
232      [".implode(',', $scripts)."],
233      popupAutomaticSize:".$this->config['popupAutomaticSize'].",
234      callId:0
235    }
236  </script>", false);
237
238          }
239        }
240      }
241    }
242  }
243
244
245
246  /**
247   * this function display the map on the picture page
248   *
249   * the 'amd_jpegMD_loaded' event is triggered before the 'loc_begin_picture'
250   * event so, when this function is called the $this->picture var was already
251   * initialized
252   */
253  public function displayPicturePageMap()
254  {
255    global $page, $template;
256
257    if($this->picture['geolocated']==false and $this->picture['forceDisplay']==false) return(false);
258
259    if(isset($this->picture['content']['MP']) and count($this->picture['content']['MP'])>0)
260    {
261      // there is maps in meta display mode
262      $template->set_filename('gmapsMeta',
263                  dirname($this->getFileLocation()).'/templates/gmaps_picture_meta.tpl');
264      $template->assign('maps', $this->picture['content']['MP']);
265
266      $metaTitle='';
267      foreach($this->picture['content']['MP'] as $map)
268      {
269        if($metaTitle=='') $metaTitle=$map['title'];
270      }
271
272      $metadata=array
273        (
274          'TITLE' => ($metaTitle=='')?l10n('gmaps_geolocation'):$metaTitle,
275          'lines' =>
276            array(
277              /* <!--rawContent-->  is a trick to display raw data in tabs
278               * for the gally template
279               *
280               * on the default template, the displayed content is done
281               * normally
282               */
283              '<!--rawContent-->' => $template->parse('gmapsMeta', true)
284            )
285        );
286      $template->append('metadata', $metadata, false);
287    }
288
289    if(isset($this->picture['content']['IP']) and count($this->picture['content']['IP'])>0)
290    {
291      // there is maps in icon display mode
292      $template->assign('map', $this->picture['content']['IP'][0]);
293      $template->assign('mapIcon', $this->picture['icon']);
294
295      $template->set_filename('gmapsIconMap',
296                  dirname($this->getFileLocation()).'/templates/gmaps_picture_icon.tpl');
297      $template->append('footer_elements', $template->parse('gmapsIconMap', true), false);
298
299      $template->set_filename('gmapsIconButton',
300                  dirname($this->getFileLocation()).'/templates/gmaps_picture_iconbutton.tpl');
301      $template->concat('PLUGIN_PICTURE_ACTIONS',  $template->parse('gmapsIconButton', true), false);
302    }
303
304    if(count($this->picture['properties'])>0)
305    {
306      $template->append('head_elements',
307"<script type=\"text/javascript\">
308  gmaps =
309    {
310      geolocated:".($this->picture['geolocated']?'true':'false').",
311      forceDisplay:".($this->picture['forceDisplay']?'true':'false').",
312      lang:{
313        centermap:'".l10n('gmaps_i_centermap')."',
314        boundkml:'".l10n('gmaps_i_boundkml')."'
315      },
316      coords:
317      {
318        latitude:'".$this->picture['coords']['lat']."',
319        longitude:'".$this->picture['coords']['lng']."',
320      },
321      maps:
322      [".implode(',', $this->picture['properties'])."],
323      popupAutomaticSize:".$this->config['popupAutomaticSize']."
324    }
325</script>", false);
326    }
327  }
328
329
330
331
332  /**
333   * prepare the maps for the picture page
334   *
335   * this function is called when the plugin AdvancedMetadata has finished to
336   * read the metadata ; if picture is not geolocated, there is no map to display
337   *
338   * @param JpegMetadata $jpegMD : a JpegMetadata object
339   */
340  public function preparePictureMaps($jpegMD)
341  {
342    global $template, $page;
343
344    $isGeolocated=true;
345
346    if(is_null($jpegMD->getTag('magic.GPS.LatitudeNum')) or
347       is_null($jpegMD->getTag('magic.GPS.LongitudeNum')) or
348       $page['section']!='categories') $isGeolocated=false;
349
350
351    if(isset($page['category']))
352    {
353      $this->buildMapList($page['category']['id'], 'P');
354    }
355    else
356    {
357      $this->buildMapList(0, 'P');
358    }
359
360    if($this->forceDisplay==0 and !$isGeolocated) return(false);
361
362    $this->picture['geolocated']=$isGeolocated;
363    $this->picture['forceDisplay']=!$isGeolocated;
364    if($isGeolocated)
365    {
366      $this->picture['coords']['lat']=$jpegMD->getTag('magic.GPS.LatitudeNum')->getValue();
367      $this->picture['coords']['lng']=$jpegMD->getTag('magic.GPS.LongitudeNum')->getValue();
368    }
369
370
371    foreach($this->maps as $map)
372    {
373      if($isGeolocated or
374         $map['forceDisplay']=='y' and ($map['kmlFileUrl']!='' or $map['kmlFileId']!=0)
375        )
376      {
377        if($map['displayType']=='IP')
378        {
379          preg_match('/^i(\d+)x(\d+).*/i', basename($map['icon']), $result);
380          $this->picture['icon']=array(
381            'iconStyle' => $map['iconStyle'],
382            'file' => $map['icon'],
383            'width' => isset($result[1])?$result[1]:-1,
384            'height' => isset($result[2])?$result[2]:-1
385          );
386        }
387
388
389        $this->picture['content'][$map['displayType']][]=array(
390          'id' => $map['id'],
391          'width' => $map['width'],
392          'height' => $map['height'],
393          'style' => $map['style'],
394          'displayType' => $map['displayType'],
395          'title' => $map['title'],
396        );
397
398        $this->picture['properties'][]="
399        {
400          id:'iGMaps".(($map['displayType']=='IP')?'Icon':$map['id'])."',
401          zoomLevel:".$map['zoomLevel'].",
402          markerImg:'".$map['marker']."',
403          mapType:'".$map['mapType']."',
404          mapTypeControl:'".$map['mapTypeControl']."',
405          navigationControl:'".$map['navigationControl']."',
406          scaleControl:'".$map['scaleControl']."',
407          streetViewControl:'".$map['streetViewControl']."',
408          kmlFileUrl:'".$map['kmlFileUrl']."',
409          displayType:'".$map['displayType']."',
410          sizeMode:'".$map['sizeMode']."',
411          title:'".addslashes( ($map['title']=='')?l10n('gmaps_geolocation'):$map['title']  )."'
412        }";
413      }
414    }
415  }
416
417
418
419
420} //class
421
422?>
Note: See TracBrowser for help on using the repository browser.