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