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

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

Admin interface + Gallery integration finished

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