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

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

add marker style management + minor bugs fixed

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