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

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

Fix bugs on install process ; add street view control management

  • Property svn:executable set to *
File size: 12.0 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 $maps = array(); //list of maps
21  protected $category=array(
22    'id' => 0,
23    'bounds' => array(
24      'N' => -90,
25      'S' => 90,
26      'E' => -180,
27      'W' => 180
28    ),
29    'icon' => array(
30      'style' => true,
31      'file' => '',
32      'width' => -1,
33      'height' => -1
34    )
35  );
36  protected $picture=array(
37    'geolocated' => 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)
158          {
159            /*
160             * prepare js script for each map
161             */
162
163            foreach($this->maps as $keyMap => $map)
164            {
165              $scripts[]="
166              {
167                id:'iGMapsIcon',
168                zoomLevel:".$map['zoomLevel'].",
169                markerImg:'".$map['marker']."',
170                mapType:'".$map['mapType']."',
171                mapTypeControl:'".$map['mapTypeControl']."',
172                navigationControl:'".$map['navigationControl']."',
173                scaleControl:'".$map['scaleControl']."',
174                streetViewControl:'".$map['streetViewControl']."',
175                kmlFileUrl:'".$map['kmlFileUrl']."',
176                displayType:'".$map['displayType']."',
177                sizeMode:'".$map['sizeMode']."',
178                title:'".addslashes( ($map['title']=='')?l10n('gmaps_geolocation'):$map['title']  )."',
179                markers:[],
180                fitToBounds:true
181              }";
182
183              preg_match('/^i(\d+)x(\d+).*/i', basename($map['icon']), $result);
184              $this->category['icon']['iconStyle']=$map['iconStyle'];
185              $this->category['icon']['file']=$map['icon'];
186              $this->category['icon']['width']=isset($result[1])?$result[1]:-1;
187              $this->category['icon']['height']=isset($result[2])?$result[2]:-1;
188            }
189
190
191            $template->assign('maps', $this->maps);
192            $template->set_filename('gmapsCatMap',
193                        dirname($this->getFileLocation()).'/templates/gmaps_category.tpl');
194            $template->append('footer_elements', $template->parse('gmapsCatMap', true), false);
195
196            if(is_array($this->category['icon']))
197            {
198              $template->assign('mapIcon', $this->category['icon']);
199              $template->set_filename('gmapsIconButton',
200                          dirname($this->getFileLocation()).'/templates/gmaps_category_iconbutton.tpl');
201              $template->concat('PLUGIN_INDEX_ACTIONS', $template->parse('gmapsIconButton', true), false);
202              $template->assign('mapIcon');
203            }
204
205
206            $template->append('head_elements',
207  "<script type=\"text/javascript\">
208  gmaps =
209    {
210      lang:{
211        boundmap:'".l10n('gmaps_i_boundmap')."',
212        boundkml:'".l10n('gmaps_i_boundkml')."',
213        loading:'".l10n('gmaps_loading')."'
214      },
215      requestId:'',
216      categoryId:".$this->category['id'].",
217      bounds:
218        {
219          north:".$this->category['bounds']['N'].",
220          south:".$this->category['bounds']['S'].",
221          east:".$this->category['bounds']['E'].",
222          west:".$this->category['bounds']['W']."
223        },
224      maps:
225      [".implode(',', $scripts)."],
226    }
227  </script>", false);
228
229          }
230        }
231      }
232    }
233  }
234
235
236
237  /**
238   * this function display the map on the picture page
239   *
240   * the 'amd_jpegMD_loaded' event is triggered before the 'loc_begin_picture'
241   * event so, when this function is called the $this->picture var was already
242   * initialized
243   */
244  public function displayPicturePageMap()
245  {
246    global $page, $template;
247
248    if($this->picture['geolocated']==false) return(false);
249
250    if(isset($this->picture['content']['MP']) and count($this->picture['content']['MP'])>0)
251    {
252      // there is maps in meta display mode
253      $template->set_filename('gmapsMeta',
254                  dirname($this->getFileLocation()).'/templates/gmaps_picture_meta.tpl');
255      $template->assign('maps', $this->picture['content']['MP']);
256
257      $metadata=array
258        (
259          'TITLE' => l10n('gmaps_geolocation'),
260          'lines' =>
261            array(
262              /* <!--rawContent-->  is a trick to display raw data in tabs
263               * for the gally template
264               *
265               * on the default template, the displayed content is done
266               * normally
267               */
268              '<!--rawContent-->' => $template->parse('gmapsMeta', true)
269            )
270        );
271      $template->append('metadata', $metadata, false);
272    }
273
274    if(isset($this->picture['content']['IP']) and count($this->picture['content']['IP'])>0)
275    {
276      // there is maps in icon display mode
277      $template->assign('map', $this->picture['content']['IP'][0]);
278      $template->assign('mapIcon', $this->picture['icon']);
279
280      $template->set_filename('gmapsIconMap',
281                  dirname($this->getFileLocation()).'/templates/gmaps_picture_icon.tpl');
282      $template->append('footer_elements', $template->parse('gmapsIconMap', true), false);
283
284      $template->set_filename('gmapsIconButton',
285                  dirname($this->getFileLocation()).'/templates/gmaps_picture_iconbutton.tpl');
286      $template->concat('PLUGIN_PICTURE_ACTIONS',  $template->parse('gmapsIconButton', true), false);
287    }
288
289    if(count($this->picture['properties'])>0)
290    {
291      $template->append('head_elements',
292"<script type=\"text/javascript\">
293  gmaps =
294    {
295      lang:{
296        centermap:'".l10n('gmaps_i_centermap')."',
297        boundkml:'".l10n('gmaps_i_boundkml')."'
298      },
299      coords:
300      {
301        latitude:'".$this->picture['coords']['lat']."',
302        longitude:'".$this->picture['coords']['lng']."',
303      },
304      maps:
305      [".implode(',', $this->picture['properties'])."],
306    }
307</script>", false);
308    }
309  }
310
311
312
313
314  /**
315   * prepare the maps for the picture page
316   *
317   * this function is called when the plugin AdvancedMetadata has finished to
318   * read the metadata ; if picture is not geolocated, there is no map to display
319   *
320   * @param JpegMetadata $jpegMD : a JpegMetadata object
321   */
322  public function preparePictureMaps($jpegMD)
323  {
324    global $template, $page;
325
326    if(is_null($jpegMD->getTag('magic.GPS.LatitudeNum')) or
327       is_null($jpegMD->getTag('magic.GPS.LongitudeNum')) or
328       $page['section']!='categories') return(false);
329
330
331    $this->picture['geolocated']=true;
332    $this->picture['coords']['lat']=$jpegMD->getTag('magic.GPS.LatitudeNum')->getValue();
333    $this->picture['coords']['lng']=$jpegMD->getTag('magic.GPS.LongitudeNum')->getValue();
334
335
336    if(isset($page['category']))
337    {
338      $this->buildMapList($page['category']['id'], 'P');
339    }
340    else
341    {
342      $this->buildMapList(0, 'P');
343    }
344
345
346    foreach($this->maps as $map)
347    {
348      if($map['displayType']=='IP')
349      {
350        preg_match('/^i(\d+)x(\d+).*/i', basename($map['icon']), $result);
351        $this->picture['icon']=array(
352          'iconStyle' => $map['iconStyle'],
353          'file' => $map['icon'],
354          'width' => isset($result[1])?$result[1]:-1,
355          'height' => isset($result[2])?$result[2]:-1
356        );
357      }
358
359
360      $this->picture['content'][$map['displayType']][]=array(
361        'id' => $map['id'],
362        'width' => $map['width'],
363        'height' => $map['height'],
364        'style' => $map['style'],
365        'displayType' => $map['displayType']
366      );
367
368      $this->picture['properties'][]="
369      {
370        id:'iGMaps".(($map['displayType']=='IP')?'Icon':$map['id'])."',
371        zoomLevel:".$map['zoomLevel'].",
372        markerImg:'".$map['marker']."',
373        mapType:'".$map['mapType']."',
374        mapTypeControl:'".$map['mapTypeControl']."',
375        navigationControl:'".$map['navigationControl']."',
376        scaleControl:'".$map['scaleControl']."',
377        streetViewControl:'".$map['streetViewControl']."',
378        kmlFileUrl:'".$map['kmlFileUrl']."',
379        displayType:'".$map['displayType']."',
380        sizeMode:'".$map['sizeMode']."',
381        title:'".addslashes( ($map['title']=='')?l10n('gmaps_geolocation'):$map['title']  )."'
382      }";
383    }
384  }
385
386
387
388
389} //class
390
391?>
Note: See TracBrowser for help on using the repository browser.