Advertisement
KeyDog

osm.php for wordpress

Jan 5th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 33.09 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: OSM
  4. Plugin URI: http://www.Fotomobil.at/wp-osm-plugin
  5. Description: Embeds maps in your blog and adds geo data to your posts.  Find samples and a forum on the <a href="http://www.Fotomobil.at/wp-osm-plugin">OSM plugin page</a>.  Simply create the shortcode to add it in your post at [<a href="options-general.php?page=osm.php">Settings</a>]
  6. Version: 1.0
  7. Author: MiKa
  8. Author URI: http://www.HanBlog.net
  9. Minimum WordPress Version Required: 2.5.1
  10. */
  11.  
  12. /*  (c) Copyright 2011  Michael Kang
  13.  
  14.     This program is free software; you can redistribute it and/or modify
  15.     it under the terms of the GNU General Public License as published by
  16.     the Free Software Foundation; either version 2 of the License, or
  17.     (at your option) any later version.
  18.  
  19.     This program is distributed in the hope that it will be useful,
  20.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.     GNU General Public License for more details.
  23.  
  24.     You should have received a copy of the GNU General Public License
  25.     along with this program; if not, write to the Free Software
  26.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  27. */
  28. load_plugin_textdomain('OSM-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
  29.  
  30. define ("PLUGIN_VER", "V1.0");
  31.  
  32. // modify anything about the marker for tagged posts here
  33. // instead of the coding.
  34. define ("POST_MARKER_PNG", "marker_posts.png");
  35. define (POST_MARKER_PNG_HEIGHT, 2);
  36. define (POST_MARKER_PNG_WIDTH, 2);
  37.  
  38. define ("GCSTATS_MARKER_PNG", "geocache.png");
  39. define (GCSTATS_MARKER_PNG_HEIGHT, 25);
  40. define (GCSTATS_MARKER_PNG_WIDTH, 25);
  41.  
  42. define ("INDIV_MARKER", "marker_blue.png");
  43. define (INDIV_MARKER_PNG_HEIGHT, 25);
  44. define (INDIV_MARKER_PNG_WIDTH, 25);
  45.  
  46. // these defines are given by OpenStreetMap.org
  47. define ("URL_INDEX", "http://www.openstreetmap.org/index.html?");
  48. define ("URL_LAT","&mlat=");
  49. define ("URL_LON","&mlon=");
  50. define ("URL_ZOOM_01","&zoom=[");
  51. define ("URL_ZOOM_02","]");
  52. define (ZOOM_LEVEL_MAX,18); // standard is 17, only mapnik is 18
  53. define (ZOOM_LEVEL_MIN,1);
  54.  
  55. // other geo plugin defines
  56. // google-maps-geocoder
  57. define ("WPGMG_LAT", "lat");
  58. define ("WPGMG_LON", "lng");
  59.  
  60. // some general defines
  61. define (LAT_MIN,-90);
  62. define (LAT_MAX,90);
  63. define (LON_MIN,-180);
  64. define (LON_MAX,180);
  65.  
  66. // tracelevels
  67. define (DEBUG_OFF, 0);
  68. define (DEBUG_ERROR, 1);
  69. define (DEBUG_WARNING, 2);
  70. define (DEBUG_INFO, 3);
  71. define (HTML_COMMENT, 10);
  72.  
  73. // Load OSM library mode
  74. define (SERVER_EMBEDDED, 1);
  75. define (SERVER_WP_ENQUEUE, 2);
  76.  
  77.  
  78. if ( ! defined( 'WP_CONTENT_URL' ) )
  79.       define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
  80. if ( ! defined( 'WP_CONTENT_DIR' ) )
  81.       define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
  82. if ( ! defined( 'WP_PLUGIN_URL' ) )
  83.       define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
  84. if ( ! defined( 'WP_PLUGIN_DIR' ) )
  85.       define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
  86. define ("OSM_PLUGIN_URL", WP_PLUGIN_URL."/osm/");
  87. define ("OSM_PLUGIN_ICONS_URL", OSM_PLUGIN_URL."icons/");
  88. define ("URL_POST_MARKER", OSM_PLUGIN_URL.POST_MARKER_PNG);
  89.  
  90. global $wp_version;
  91. if (version_compare($wp_version,"2.5.1","<")){
  92.   exit('[OSM plugin - ERROR]: At least Wordpress Version 2.5.1 is needed for this plugin!');
  93. }
  94.    
  95. // get the configuratin by
  96. // default or costumer settings
  97. if (@(!include('osm-config.php'))){
  98.   include ('osm-config-sample.php');
  99. }
  100.  
  101. // do not edit this
  102. define ("Osm_TraceLevel", DEBUG_ERROR);
  103.  
  104. include('osm-openlayers.php');
  105.        
  106. // let's be unique ...
  107. // with this namespace
  108. class Osm
  109. {
  110.     function Osm() {
  111.         $this->localizionName = 'Osm';
  112.     //$this->TraceLevel = DEBUG_INFO;
  113.         $this->ErrorMsg = new WP_Error();
  114.         $this->initErrorMsg();
  115.    
  116.     // add the WP action
  117.     add_action('wp_head', array(&$this, 'wp_head'));
  118.     add_action('admin_menu', array(&$this, 'admin_menu'));
  119.     add_action('wp_print_scripts',array(&$this, 'show_enqueue_script'));
  120.  
  121.     // add the WP shortcode
  122.     add_shortcode('osm_map',array(&$this, 'sc_showMap'));
  123.     }
  124.  
  125.   function initErrorMsg()
  126.   {
  127.     include('osm-error-msg.php');  
  128.   }
  129.  
  130.   function traceErrorMsg($e = '')
  131.   {
  132.    if ($this == null){
  133.      return $e;
  134.    }
  135.    $EMsg = $this->ErrorMsg->get_error_message($e);
  136.    if ($EMsg == null){
  137.      return $e;
  138.      //return__("Unknown errormessage",$this->localizionName);
  139.    }
  140.    return $EMsg;
  141.   }
  142.  
  143.   function traceText($a_Level, $a_String)
  144.   {
  145.     $TracePrefix = array(
  146.       DEBUG_ERROR =>'[OSM-Plugin-Error]:',
  147.       DEBUG_WARNING=>'[OSM-Plugin-Warning]:',
  148.       DEBUG_INFO=>'[OSM-Plugin-Info]:');
  149.      
  150.     if ($a_Level == DEBUG_ERROR){    
  151.       echo '<div class="osm_error_msg"><p><strong style="color:red">'.$TracePrefix[$a_Level].Osm::traceErrorMsg($a_String).'</strong></p></div>';
  152.     }
  153.     else if ($a_Level <= Osm_TraceLevel){
  154.       echo $TracePrefix[$a_Level].$a_String.'<br>';
  155.     }
  156.     else if ($a_Level == HTML_COMMENT){
  157.       echo "<!-- ".$a_String." --> \n";
  158.     }
  159.   }
  160.  
  161.     // add it to the Settings page
  162.     function options_page_osm()
  163.     {
  164.         if(isset($_POST['Options'])){
  165.  
  166.       // 0 = no error;
  167.       // 1 = error occured
  168.       $Option_Error = 0;
  169.            
  170.       // get the zoomlevel for the external link
  171.       // and inform the user if the level was out of range    
  172.       update_option('osm_custom_field',$_POST['osm_custom_field']);
  173.      
  174.       if ($_POST['osm_zoom_level'] >= ZOOM_LEVEL_MIN && $_POST['osm_zoom_level'] <= ZOOM_LEVEL_MAX){
  175.         update_option('osm_zoom_level',$_POST['osm_zoom_level']);
  176.       }
  177.       else {
  178.         $Option_Error = 1;
  179.         Osm::traceText(DEBUG_ERROR, "e_zoomlevel_range");
  180.       }
  181.       // Let the user know whether all was fine or not
  182.       if ($Option_Error  == 0){
  183.         Osm::traceText(DEBUG_INFO, "i_options_updated");
  184.       }
  185.       else{
  186.          Osm::traceText(DEBUG_ERROR, "e_options_not_updated");
  187.       }
  188.    
  189.         }
  190.         else{
  191.             add_option('osm_custom_field', 0);
  192.             add_option('osm_zoom_level', 0);
  193.         }
  194.    
  195.     // name of the custom field to store Long and Lat
  196.     // for the geodata of the post
  197.         $osm_custom_field  = get_option('osm_custom_field');                                                  
  198.  
  199.     // zoomlevel for the link the OSM page
  200.     $osm_zoom_level    = get_option('osm_zoom_level');
  201.            
  202.     include('osm-options.php');
  203.     }
  204.    
  205.   // put meta tags into the head section
  206.     function wp_head($not_used)
  207.     {
  208.         global $wp_query;
  209.    
  210.     $CustomField = get_option('osm_custom_field');
  211.  
  212.     if (get_post_meta($wp_query->post->ID, $CustomField, true)){
  213.       $PostLatLon = get_post_meta($wp_query->post->ID, $CustomField, true);
  214.           list($lat, $lon) = explode(',', $PostLatLon[0]);
  215.     }
  216.         if(is_single() && ($lat != '') && ($lon != '')){
  217.             $title = convert_chars(strip_tags(get_bloginfo("name")))." - ".$wp_query->post->post_title;
  218.       $this->traceText(HTML_COMMENT, 'OSM plugin '.PLUGIN_VER.': adding geo meta tags:');
  219.         }
  220.         else{
  221.       $this->traceText(HTML_COMMENT, 'OSM plugin '.PLUGIN_VER.': did not add geo meta tags.');
  222.             return;
  223.         }
  224.    
  225.     // let's store geo data with W3 standard
  226.         echo "<meta name=\"ICBM\" content=\"{$lat}, {$lon}\" />\n";
  227.         echo "<meta name=\"DC.title\" content=\"{$wp_query->post->post_title}\" />\n";
  228.     echo "<meta name=\"geo.placename\" content=\"{$wp_query->post->post_title}\"/>\n";
  229.         echo "<meta name=\"geo.position\"  content=\"{$lat};{$lon}\" />\n";
  230.     }
  231.    
  232.   function createMarkerList($a_import, $a_import_UserName, $a_Customfield, $a_import_osm_cat_incl_name,  $a_import_osm_cat_excl_name)
  233.   {
  234.      $this->traceText(DEBUG_INFO, "createMarkerList(".$a_import.",".$a_import_UserName.",".$a_Customfield.")");
  235.        global $post;
  236.      $post_org = $post;
  237.      
  238.      // make a dummymarker to you use icon.clone later
  239.      if ($a_import == 'gcstats'){
  240.        $this->traceText(DEBUG_INFO, "Requesting data from gcStats-plugin");
  241.        include('osm-import.php');
  242.      }
  243.      else if ($a_import == 'ecf'){
  244.        $this->traceText(DEBUG_INFO, "Requesting data from comments");
  245.        include('osm-import.php');
  246.      }
  247.      else if ($a_import == 'osm' || $a_import == 'osm_l'){
  248.        // let's see which posts are using our geo data ...
  249.        $this->traceText(DEBUG_INFO, "check all posts for osm geo custom fields");
  250.        $CustomFieldName = get_settings('osm_custom_field');        
  251.        $recentPosts = new WP_Query();
  252.        $recentPosts->query('meta_key='.$CustomFieldName.'&post_status=publish'.'&showposts=-1');
  253. //     $recentPosts->query('meta_key='.$CustomFieldName.'&post_status=publish'.'&post_type=page');
  254.        while ($recentPosts->have_posts()) : $recentPosts->the_post();
  255.          list($temp_lat, $temp_lon) = explode(',', get_post_meta($post->ID, $CustomFieldName, true));
  256. //         echo $post->ID.'Lat: '.$temp_lat.'Long '.$temp_lon.'<br>';
  257.  
  258.          // check if a filter is set and geodata are set
  259.          // if filter is set and set then pretend there are no geodata
  260.          if (($a_import_osm_cat_incl_name  != 'Osm_All' || $a_import_osm_cat_excl_name  != 'Osm_None')&&($temp_lat != '' && $temp_lon != '')){
  261.            $categories = wp_get_post_categories($post->ID);
  262.            foreach( $categories as $catid ) {
  263.                 $cat = get_category($catid);
  264.               if (($a_import_osm_cat_incl_name  != 'Osm_All') && (strtolower($cat->name) != (strtolower($a_import_osm_cat_incl_name)))){
  265.                 $temp_lat = '';
  266.                 $temp_lon = '';
  267.               }
  268.               if (strtolower($cat->name) == (strtolower($a_import_osm_cat_excl_name))){
  269.                 $temp_lat = '';
  270.                 $temp_lon = '';
  271.               }
  272.            }
  273.          }
  274.  
  275.  
  276.          if ($temp_lat != '' && $temp_lon != '') {
  277.            list($temp_lat, $temp_lon) = $this->checkLatLongRange('$marker_all_posts',$temp_lat, $temp_lon);
  278.            if ($a_import == 'osm_l' ){  
  279.              $categories = wp_get_post_categories($post->ID);
  280.                // take the last one but ignore those without a specific category
  281.              foreach( $categories as $catid ) {
  282.                   $cat = get_category($catid);
  283.                 if ((strtolower($cat->name) == 'uncategorized') || (strtolower($cat->name) == 'allgemein')){
  284.                   $Category_Txt = '';
  285.                 }
  286.                 else{
  287.                   $Category_Txt = $cat->name.': ';
  288.                 }
  289.              }
  290.            $Marker_Txt = '<a href="'.get_permalink($post->ID).'">'.$Category_Txt.get_the_title($post->ID).'  </a>';
  291.            $MarkerArray[] = array('lat'=> $temp_lat,'lon'=>$temp_lon,'popup_height'=>'100', 'popup_width'=>'150', 'marker'=>$Icon[name], 'text'=>$Marker_Txt);
  292.            }     
  293.            else{ // plain osm without link to the post
  294.              $Marker_Txt = ' ';
  295.              $MarkerArray[] = array('lat'=> $temp_lat,'lon'=>$temp_lon,'popup_height'=>'100', 'popup_width'=>'150', 'marker'=>$Icon[name], 'text'=>$Marker_Txt);
  296.            }
  297.            }  
  298.        endwhile;
  299.      }
  300.      else if ($a_import == 'wpgmg'){
  301.        // let's see which posts are using our geo data ...
  302.        $this->traceText(DEBUG_INFO, "check all posts for wpgmg geo custom fields");
  303.        $recentPosts = new WP_Query();
  304.        $recentPosts->query('meta_key='.WPGMG_LAT.'&meta_key='.WPGMG_LON.'&showposts=-1');
  305.        while ($recentPosts->have_posts()) : $recentPosts->the_post();
  306.          include('osm-import.php');
  307.          if ($temp_lat != '' && $temp_lon != '') {
  308.            list($temp_lat, $temp_lon) = $this->checkLatLongRange('$marker_all_posts',$temp_lat, $temp_lon);          
  309.            $MarkerArray[] = array('lat'=> $temp_lat,'lon'=>$temp_lon,'marker'=>$Icon[name],'popup_height'=>'100', 'popup_width'=>'200');
  310.         }  
  311.        endwhile;
  312.      }
  313.  
  314.      $post = $post_org;
  315.      return $MarkerArray;
  316.   }
  317.  
  318.   // if you miss a colour, just add it
  319.   function checkStyleColour($a_colour){
  320.     if ($a_colour != 'red' && $a_colour != 'blue' && $a_colour != 'black' && $a_colour != 'green'){
  321.       return "blue";
  322.     }
  323.     return $a_colour;
  324.   }
  325.  
  326.   // if you miss a colour, just add it
  327.   function getImportLayer($a_import_type, $a_import_UserName, $Icon, $a_import_osm_cat_incl_name,  $a_import_osm_cat_excl_name){
  328.  
  329.     if ($a_import_type  == 'osm_l'){
  330.       $LayerName = 'TaggedPosts';
  331.       if ($Icon[name] != 'NoName'){ // <= ToDo
  332.         $PopUp = 'true';    
  333.       }
  334.       else {
  335.         $PopUp = 'false';
  336.       }
  337.      
  338.     }    
  339.    
  340.     // import data from tagged posts
  341.     else if ($a_import_type  == 'osm'){
  342.       $LayerName = 'TaggedPosts';
  343.       $PopUp = 'false';
  344.     }
  345.  
  346.     // import data from wpgmg
  347.     else if ($a_import_type  == 'wpgmg'){
  348.       $LayerName = 'TaggedPosts';
  349.       $PopUp = 'false';
  350.     }
  351.     // import data from gcstats
  352.     else if ($a_import_type == 'gcstats'){
  353.       $LayerName     = 'GeoCaches';
  354.       $PopUp = 'true';
  355.       $Icon = Osm::getIconsize(GCSTATS_MARKER_PNG);
  356.       $Icon[name] = GCSTATS_MARKER_PNG;
  357.     }
  358.     // import data from ecf
  359.     else if ($a_import_type == 'ecf'){
  360.       $LayerName = 'Comments';
  361.       $PopUp = 'true';
  362.       $Icon = Osm::getIconsize(INDIV_MARKER);
  363.       $Icon[name] = INDIV_MARKER;
  364.     }
  365.     else{
  366.       $this->traceText(DEBUG_ERROR, "e_import_unknwon");
  367.     }
  368.     $MarkerArray = $this->createMarkerList($a_import_type, $a_import_UserName,'Empty', $a_import_osm_cat_incl_name,  $a_import_osm_cat_excl_name);
  369.     return Osm_OpenLayers::addMarkerListLayer($LayerName, $Icon, $MarkerArray, $PopUp);
  370.   }
  371.  
  372.  // check Lat and Long
  373.   function getMapCenter($a_Lat, $a_Long, $a_import, $a_import_UserName){
  374.     if ($a_import == 'wpgmg'){
  375.       $a_Lat  = OSM_getCoordinateLat($a_import);
  376.       $a_Long = OSM_getCoordinateLong($a_import);
  377.     }
  378.     else if ($a_import == 'gcstats'){
  379.       if (function_exists('gcStats__getInterfaceVersion')) {
  380.         $Val = gcStats__getMinMaxLat($a_import_UserName);
  381.         $a_Lat = ($Val[min] + $Val[max]) / 2;
  382.         $Val = gcStats__getMinMaxLon($a_import_UserName);
  383.         $a_Long = ($Val[min] + $Val[max]) / 2;
  384.       }
  385.       else{
  386.        $this->traceText(DEBUG_WARNING, "getMapCenter() could not connect to gcStats plugin");
  387.        $a_Lat  = 0;$a_Long = 0;
  388.       }
  389.     }
  390.     else if ($a_Lat == '' || $a_Long == ''){
  391.       $a_Lat  = OSM_getCoordinateLat('osm');
  392.       $a_Long = OSM_getCoordinateLong('osm');
  393.     }
  394.     return array($a_Lat,$a_Long);
  395.   }
  396.    
  397.   // check Lat and Long
  398.   function checkLatLongRange($a_CallingId, $a_Lat, $a_Long)
  399.   {
  400.     if ($a_Lat >= LAT_MIN && $a_Lat <= LAT_MAX && $a_Long >= LON_MIN && $a_Long <= LON_MAX &&
  401.                     preg_match('!^[^0-9]+$!', $a_Lat) != 1 && preg_match('!^[^0-9]+$!', $a_Long) != 1){
  402.       return array($a_Lat,$a_Long);              
  403.     }
  404.     else{
  405.       $this->traceText(DEBUG_ERROR, "e_lat_lon_range");
  406.       $this->traceText(DEBUG_INFO, "Error: ".$a_CallingId." Lat".$a_Lat." or Long".$a_Long);
  407.       $a_Lat  = 0;$a_Long = 0;
  408.     }
  409.   }
  410.  
  411.  function isOsmIcon($a_IconName)
  412.  {
  413.  
  414.    if ($a_IconName == "airport.png" || $a_IconName == "bicycling.png" ||
  415.     $a_IconName == "bus.png" || $a_IconName == "camping.png" ||
  416.     $a_IconName == "car.png" || $a_IconName == "friends.png" ||
  417.     $a_IconName == "geocache.png" || $a_IconName == "guest_house.png" ||
  418.     $a_IconName == "home.png" || $a_IconName == "hostel.png" ||
  419.     $a_IconName == "hotel.png"|| $a_IconName == "marker_blue.png" ||
  420.     $a_IconName == "motorbike.png" || $a_IconName == "restaurant.png" ||
  421.     $a_IconName == "services.png" || $a_IconName == "styria_linux.png" ||
  422.     $a_IconName == "marker_posts.png" || $a_IconName == "restaurant.png" ||
  423.     $a_IconName == "toilets.png" || $a_IconName == "wpttemp-yellow.png" ||
  424.     $a_IconName == "wpttemp-green.png" || $a_IconName == "wpttemp-red.png"){
  425.     return 1;
  426.    }
  427.    else {
  428.     return 0;
  429.    }
  430.  }
  431.  
  432.  function getIconsize($a_IconName)
  433.  {
  434.   $Icons = array(
  435.     "airport.png"        => array("height"=>32,"width"=>"31","offset_height"=>"-16","offset_width"=>"-16"),
  436.     "bicycling.png"      => array("height"=>19,"width"=>"32","offset_height"=>"-9","offset_width"=>"-16"),
  437.     "bus.png"            => array("height"=>32,"width"=>"26","offset_height"=>"-16","offset_width"=>"-13"),
  438.     "camping.png"        => array("height"=>32,"width"=>"32","offset_height"=>"-16","offset_width"=>"-16"),
  439.     "car.png"            => array("height"=>18,"width"=>"32","offset_height"=>"-16","offset_width"=>"-9"),
  440.     "friends.png"        => array("height"=>32,"width"=>"32","offset_height"=>"-16","offset_width"=>"-16"),
  441.     "geocache.png"       => array("height"=>25,"width"=>"25","offset_height"=>"-12","offset_width"=>"-12"),
  442.     "guest_house.png"    => array("height"=>32,"width"=>"32","offset_height"=>"-16","offset_width"=>"-16"),
  443.     "home.png"           => array("height"=>32,"width"=>"32","offset_height"=>"-16","offset_width"=>"-16"),
  444.     "hostel.png"         => array("height"=>24,"width"=>"24","offset_height"=>"-12","offset_width"=>"-12"),
  445.     "hotel.png"          => array("height"=>32,"width"=>"32","offset_height"=>"-16","offset_width"=>"-16"),
  446.     "marker_blue.png"    => array("height"=>24,"width"=>"24","offset_height"=>"-12","offset_width"=>"-12"),
  447.     "motorbike.png"      => array("height"=>23,"width"=>"32","offset_height"=>"-12","offset_width"=>"-16"),
  448.     "restaurant.png"     => array("height"=>24,"width"=>"24","offset_height"=>"-12","offset_width"=>"-12"),
  449.     "services.png"       => array("height"=>28,"width"=>"32","offset_height"=>"-14","offset_width"=>"-16"),
  450.     "styria_linux.png"   => array("height"=>50,"width"=>"36","offset_height"=>"-25","offset_width"=>"-18"),
  451.     "marker_posts.png"   => array("height"=>2,"width"=>"2","offset_height"=>"-1","offset_width"=>"-1"),
  452.     "restaurant.png"     => array("height"=>24,"width"=>"24","offset_height"=>"-12","offset_width"=>"-12"),
  453.     "toilets.png"        => array("height"=>32,"width"=>"32","offset_height"=>"-16","offset_width"=>"-16"),
  454.     "wpttemp-yellow.png" => array("height"=>24,"width"=>"24","offset_height"=>"-24","offset_width"=>"0"),
  455.     "wpttemp-green.png"  => array("height"=>24,"width"=>"24","offset_height"=>"-24","offset_width"=>"0"),
  456.     "wpttemp-red.png"    => array("height"=>24,"width"=>"24","offset_height"=>"-24","offset_width"=>"0"),
  457.   );
  458.  
  459.   if ($Icons[$a_IconName][height] == ''){
  460.     $Icon = array("height"=>24,"width"=>"24");
  461.     $this->traceText(DEBUG_ERROR, "e_unknown_icon");
  462.     $this->traceText(DEBUG_INFO, "Error: (marker_name: ".$a_IconName.")!");
  463.   }
  464.   else {
  465.     $Icon = $Icons[$a_IconName];
  466.   }
  467.   return $Icon;
  468.  }
  469.  
  470.   function getGPXName($filepath){
  471.     $file = basename($filepath, ".gpx"); // $file is set to "index"
  472.     return $file;
  473.   }
  474.  
  475.   // execute the java script to display
  476.   // the OpenStreetMap
  477.   function sc_showMap($atts) {
  478.     // let's get the shortcode arguments
  479.     extract(shortcode_atts(array(
  480.     // size of the map
  481.     'width'     => '450', 'height' => '300',
  482.     // address of the center in the map
  483.         'lat'       => '', 'long'  => '',    
  484.     // the zoomlevel of the map
  485.     'zoom'      => '7',    
  486.     // Osmarender, Mapnik, CycleMap, ...          
  487.     'type'      => 'AllOsm',
  488.     // track info
  489.     'gpx_file'  => 'NoFile',           // 'absolut address'          
  490.     'gpx_file_proxy'  => 'NoFile',           // 'absolut address'          
  491.     'gpx_colour'=> 'NoColour',
  492.     'gpx_file_list'   => 'NoFileList',
  493.     'gpx_colour_list' => 'NoColourList',
  494.     'kml_file'  => 'NoFile',           // 'absolut address'          
  495.     'kml_colour'=> 'NoColour',
  496.     // are there markers in the map wished loaded from a file
  497.     'marker_file'     => 'NoFile', // 'absolut address'
  498.     'marker_file_proxy'     => 'NoFile', // 'absolut address'
  499.     // are there markers in the map wished loaded from post tags
  500.     'marker_all_posts'=> 'n',      // 'y' or 'Y'
  501.     'marker_name'     => 'NoName',
  502.     'marker_height'   => '0',
  503.     'marker_width'    => '0',
  504.     'marker_focus'    => '0',
  505.     'ov_map'          => '-1',         // zoomlevel of overviewmap
  506.     'import'          => 'No',
  507.     'import_osm_cat_incl_name'  => 'Osm_All',
  508.     'import_osm_cat_excl_name'  => 'Osm_None',
  509.     'marker'          => 'No',
  510.     'msg_box'         => 'No',
  511.     'custom_field'    => 'No',
  512.     'control'             => 'No',
  513.       'extmap_type'     => 'No',
  514.       'extmap_name'     => 'No',
  515.       'extmap_address'  => 'No',
  516.       'extmap_init'     => 'No',
  517.     'map_border'      => 'none'
  518.       ), $atts));
  519.    
  520.     if ($zoom < ZOOM_LEVEL_MIN || $zoom > ZOOM_LEVEL_MAX){
  521.       $this->traceText(DEBUG_ERROR, "e_zoomlevel_range");
  522.       $this->traceText(DEBUG_INFO, "Error: (Zoomlevel: ".$zoom.")!");
  523.       $zoom = 0;  
  524.     }
  525.     if ($width < 1 || $height < 1){
  526.       Osm::traceText(DEBUG_ERROR, "e_map_size");
  527.       Osm::traceText(DEBUG_INFO, "Error: ($width: ".$width." $height: ".$height.")!");
  528.       $width = 450; $height = 300;
  529.     }
  530.  
  531.     if ($marker_name == 'NoName'){
  532.       $marker_name  = POST_MARKER_PNG;
  533.     }
  534.  
  535.     if (Osm::isOsmIcon($marker_name) == 1){
  536.        $Icon = Osm::getIconsize($marker_name);
  537.        $Icon[name]  = $marker_name;
  538.     }
  539.     else  {
  540.  
  541.       $Icon[height] = $marker_height;
  542.       $Icon[width]  = $marker_width;
  543.       $Icon[name]  = $marker_name;
  544.       if ($marker_focus == 0){ // center is default
  545.         $Icon[offset_height] = round(-$marker_height/2);
  546.         $Icon[offset_width] = round(-$marker_width/2);
  547.       }
  548.       else if ($marker_focus == 1){ // left bottom
  549.         $Icon[offset_height] = -$marker_height;
  550.         $Icon[offset_width]  = 0;
  551.       }
  552.       else if ($marker_focus == 2){ // left top
  553.         $Icon[offset_height] = 0;
  554.         $Icon[offset_width]  = 0;
  555.       }
  556.       else if ($marker_focus == 3){ // right top
  557.         $Icon[offset_height] = 0;
  558.         $Icon[offset_width]  = -$marker_width;
  559.       }
  560.       else if ($marker_focus == 4){ // right bottom
  561.         $Icon[offset_height] = -$marker_height;
  562.         $Icon[offset_width]  = -$marker_width;
  563.       }
  564.       if ($Icon[height] == 0 || $Icon[width] == 0){
  565.         Osm::traceText(DEBUG_ERROR, "e_marker_size"); //<= ToDo
  566.         $Icon[height] = 24;
  567.         $Icon[width]  = 24;
  568.       }
  569.     }
  570.  
  571.     list($import_type, $import_UserName) = explode(',', $import);
  572.     if ($import_UserName == ''){
  573.       $import_UserName = 'DummyName';
  574.     }
  575.     $import_type = strtolower($import_type);
  576.       $array_control = explode( ',', $control);
  577.    
  578.     list($lat, $long) = Osm::getMapCenter($lat, $long, $import_type, $import_UserName);
  579.     list($lat, $long) = Osm::checkLatLongRange('MapCenter',$lat, $long);
  580.     $gpx_colour       = Osm::checkStyleColour($gpx_colour);
  581.     $kml_colour       = Osm::checkStyleColour($kml_colour);
  582.     $type             = Osm_OpenLayers::checkMapType($type);
  583.     $ov_map           = Osm_OpenLayers::checkOverviewMapZoomlevels($ov_map);
  584.      
  585.     $array_control    = Osm_OpenLayers::checkControlType($array_control);
  586.  
  587.     // to manage several maps on the same page
  588.     // create names with index
  589.     static  $MapCounter = 0;
  590.     $MapCounter += 1;
  591.     $MapName = 'map_'.$MapCounter;
  592.     $GpxName = 'GPX_'.$MapCounter;
  593.     $KmlName = 'KML_'.$MapCounter;
  594.    
  595.     Osm::traceText(DEBUG_INFO, "MapCounter = ".$MapCounter);
  596.      
  597.     // if we came up to here, let's load the map
  598.     $output = '';  
  599.       $output .= '<style type="text/css">';
  600.     $output .= '.entry .olMapViewport img {max-width: none; max-height: none;}';
  601.    
  602.     $output .= '.entry-content img, .widget img {max-width: none; max-height: none;}';
  603.    
  604.     $output .= '.olControlAttribution {bottom: 0 !important;}';
  605.     $output .= 'div.olControlMousePosition {bottom: 1em !important;}';
  606.      
  607.       $output .= '#'.$MapName.' {clear: both; padding: 0px; margin: 0px; border: 0px; width: 100%; height: 100%; margin-top:0px; margin-right:0px;margin-left:0px; margin-bottom:0px; left: 0px;}';
  608.     $output .= '#'.$MapName.' img{clear: both; padding: 0px; margin: 0px; border: 0px; width: 100%; height: 100%; position: absolute; margin-top:0px; margin-right:0px;margin-left:0px; margin-bottom:0px;}';
  609.       $output .= '</style>';
  610.  
  611.     $output .= '<div id="'.$MapName.'" style="width:'.$width.'px; height:'.$height.'px; overflow:hidden;padding:0px;border:'.$map_border.';">';
  612.  
  613.    
  614.         if (Osm_LoadLibraryMode == SERVER_EMBEDDED){
  615.           if (OL_LIBS_LOADED == 0) {
  616.             $output .= '<script type="text/javascript" src="'.Osm_OL_LibraryLocation.'"></script>';
  617.           define (OL_LIBS_LOADED, 1);
  618.         }
  619.  
  620.         if ($type == 'Mapnik' || $type == 'Osmarender' || $type == 'CycleMap' || $type == 'All' || $type == 'AllOsm' || $type == 'Ext'){
  621.             if (OSM_LIBS_LOADED == 0) {
  622.             $output .= '<script type="text/javascript" src="'.Osm_OSM_LibraryLocation.'"></script>';
  623.             define (OSM_LIBS_LOADED, 1);
  624.           }
  625.         }
  626.  
  627.         if ($type == 'GooglePhysical' || $type == 'GoogleStreet' || $type == 'GoogleHybrid' || $type == 'GoogleSatellite' || $type == 'All' || $type == 'AllGoogle' || $a_type == 'Ext' || $type == 'Google Physical' || $type == 'Google Street' || $type == 'Google Hybrid' || $type == 'Google Satellite'){
  628.             if (GOOGLE_LIBS_LOADED == 0) {
  629.             $output .= '<script type="text/javascript" src="'.Osm_GOOGLE_LibraryLocation.'"></script>';
  630.             define (GOOGLE_LIBS_LOADED, 1);
  631.           }
  632.         }
  633.       }
  634.         elseif (Osm_LoadLibraryMode == SERVER_WP_ENQUEUE){
  635.         // registered and loaded by WordPress
  636.         }
  637.         else{
  638.           $this->traceText(DEBUG_ERROR, "e_library_config");
  639.         }
  640.      
  641.     $output .= '<script type="text/javascript">';
  642.     $output .= '/* <![CDATA[ */';
  643.     //$output .= 'jQuery(document).ready(';
  644.     //$output .= 'function($) {';
  645.     $output .= '(function($) {';
  646.     $output .= Osm_OpenLayers::addOsmLayer($MapName, $type, $ov_map, $array_control, $extmap_type, $extmap_name, $extmap_address, $extmap_init);
  647.  
  648.     // add a clickhandler if needed
  649.     $msg_box = strtolower($msg_box);
  650.     if ( $msg_box == 'sc_gen' || $msg_box == 'lat_long'){
  651.       $output .= Osm_OpenLayers::AddClickHandler($msg_box);
  652.     }
  653.     // set center and zoom of the map
  654.     $output .= Osm_OpenLayers::setMapCenterAndZoom($lat, $long, $zoom);
  655.  
  656.     // Add the Layer with GPX Track
  657.     if ($gpx_file_proxy != 'NoFile'){
  658.       $GpxName = basename($gpx_file_proxy, ".gpx");
  659.       $output .= Osm_OpenLayers::addGmlLayer($GpxName, OSM_PLUGIN_URL."osm-proxy.php?url=".$gpx_file_proxy, $gpx_colour,'GPX');
  660.     }
  661.  
  662.     if ($gpx_file != 'NoFile'){
  663.       $GpxName = basename($gpx_file, ".gpx");
  664.       $output .= Osm_OpenLayers::addGmlLayer($GpxName, $gpx_file,$gpx_colour,'GPX');
  665.     }
  666.  
  667.     if ($gpx_file_list != 'NoFileList'){
  668.       $GpxFileListArray   = explode( ',', $gpx_file_list );
  669.       $GpxColourListArray = explode( ',', $gpx_colour_list);
  670.       $this->traceText(DEBUG_INFO, "(NumOfGpxFiles: ".sizeof($GpxFileListArray)." NumOfGpxColours: ".sizeof($GpxColourListArray).")!");
  671.       if (sizeof($GpxFileListArray) == sizeof($GpxColourListArray)){
  672.         for($x=0;$x<sizeof($GpxFileListArray);$x++){
  673.           $GpxName = basename($GpxFileListArray[$x], ".gpx");
  674.           $output .= Osm_OpenLayers::addGmlLayer($GpxName, $GpxFileListArray[$x],$GpxColourListArray[$x],'GPX');
  675.         }
  676.       }
  677.       else {
  678.         $this->traceText(DEBUG_ERROR, "e_gpx_list_error");
  679.       }
  680.     }
  681.    
  682.     // Add the Layer with KML Track
  683.     if ($kml_file != 'NoFile'){
  684.       $output .= Osm_OpenLayers::addGmlLayer($KmlName, $kml_file,$kml_colour,'KML');
  685.     }
  686.  
  687.     // Add the marker here which we get from the file
  688.     if ($marker_file_proxy != 'NoFile'){
  689.       $MarkerName = basename($marker_file_proxy, ".txt");
  690.       $output .= Osm_OpenLayers::addTextLayer($MarkerName, OSM_PLUGIN_URL."osm-proxy.php?url=".$marker_file_proxy);
  691.     }  
  692.    
  693.     if ($marker_file != 'NoFile'){    
  694.       $MarkerName = basename($marker_file, ".txt");
  695.       $output .= Osm_OpenLayers::addTextLayer($MarkerName, $marker_file);
  696.     }  
  697.  
  698.     $marker_all_posts = strtolower($marker_all_posts);
  699.     if ($marker_all_posts == 'y'){
  700.       //$this->traceText(DEBUG_ERROR, "e_use_marker_all_posts");
  701.       $import_type  = 'osm';
  702.     }
  703.  
  704.     if ($import_type  != 'no'){
  705.       $output .= Osm::getImportLayer($import_type, $import_UserName, $Icon, $import_osm_cat_incl_name,  $import_osm_cat_excl_name);
  706.     }
  707.  
  708.    // just add single marker
  709.    if ($marker  != 'No'){  
  710.      global $post;
  711.      $DoPopUp = 'true';
  712.      list($temp_lat, $temp_lon, $temp_popup_custom_field) = explode(',', $marker);
  713.        if ($temp_popup_custom_field == ''){
  714.            $temp_popup_custom_field = 'osm_dummy';
  715.        $DoPopUp = 'false';
  716.        }
  717.      $temp_popup_custom_field = trim($temp_popup_custom_field);
  718.      $temp_popup = get_post_meta($post->ID, $temp_popup_custom_field, true);
  719.      list($temp_lat, $temp_lon) = Osm::checkLatLongRange('Marker',$temp_lat, $temp_lon);
  720.      $MarkerArray[] = array('lat'=> $temp_lat,'lon'=>$temp_lon,'text'=>$temp_popup,'popup_height'=>'150', 'popup_width'=>'150');
  721.      $output .= Osm_OpenLayers::addMarkerListLayer('Marker', $Icon,$MarkerArray,$DoPopUp);
  722.     }
  723.  
  724.     //$output .= '}';
  725.     //$output .= ');';
  726.     $output .= '})(jQuery)';
  727.     $output .= '/* ]]> */';
  728.     $output .= ' </script>';
  729.     $output .= '</div>';
  730.     return $output;
  731.     }
  732.  
  733.    
  734.     // add OSM-config page to Settings
  735.     function admin_menu($not_used){
  736.     // place the info in the plugin settings page
  737.         add_options_page(__('OpenStreetMap Manager', 'Osm'), __('OSM', 'Osm'), 5, basename(__FILE__), array('Osm', 'options_page_osm'));
  738.     }
  739.  
  740.   // ask WP to handle the loading of scripts
  741.   // if it is not admin area
  742.   function show_enqueue_script() {
  743.     wp_enqueue_script(array ('jquery'));
  744.    
  745.     if (Osm_LoadLibraryMode == SERVER_EMBEDDED){
  746.       // it is loaded when the map is displayed
  747.     }
  748.     elseif (Osm_LoadLibraryMode == SERVER_WP_ENQUEUE){
  749.       //wp_enqueue_script('OlScript', 'http://www.openlayers.org/api/OpenLayers.js');
  750.       //wp_enqueue_script('OsnScript', 'http://www.openstreetmap.org/openlayers/OpenStreetMap.js');
  751.         wp_enqueue_script('OlScript',Osm_OL_LibraryLocation);
  752.       wp_enqueue_script('OsnScript',Osm_OSM_LibraryLocation);
  753.       wp_enqueue_script('OsnScript',Osm_GOOGLE_LibraryLocation);
  754.       define (OSM_LIBS_LOADED, 1);
  755.       define (OL_LIBS_LOADED, 1);
  756.       define (GOOGLE_LIBS_LOADED, 1);
  757.     }
  758.     else{
  759.       // Errormsg is traced at another place
  760.     }  
  761.   }
  762. }   // End class Osm
  763.  
  764. $pOsm = new Osm();
  765.  
  766. // This is meant to be the interface used
  767. // in your WP-template
  768. // returns Lat data of coordination
  769. function OSM_getCoordinateLat($a_import)
  770. {
  771.     global $post;
  772.  
  773.   $a_import = strtolower($a_import);
  774.   if ($a_import == 'osm' || $a_import == 'osm_l'){
  775.       list($lat, $lon) = explode(',', get_post_meta($post->ID, get_settings('osm_custom_field'), true));
  776.   }
  777.   else if ($a_import == 'wpgmg'){
  778.       $lat = get_post_meta($post->ID, WPGMG_LAT, true);
  779.   }
  780.   else {
  781.     $this->traceText(DEBUG_ERROR, "e_php_getlat_missing_arg");
  782.     $lat = 0;
  783.   }
  784.   if ($lat != '') {
  785.       return trim($lat);
  786.   }
  787.   return '';
  788. }
  789.  
  790. // returns Lon data
  791. function OSM_getCoordinateLong($a_import)
  792. {
  793.     global $post;
  794.  
  795.   $a_import = strtolower($a_import);
  796.   if ($a_import == 'osm' || $a_import == 'osm_l'){
  797.       list($lat, $lon) = explode(',', get_post_meta($post->ID, get_settings('osm_custom_field'), true));
  798.   }
  799.   else if ($a_import == 'wpgmg'){
  800.       list($lon) = get_post_meta($post->ID,WPGMG_LON, true);
  801.   }
  802.   else {
  803.     $this->traceText(DEBUG_ERROR, "e_php_getlon_missing_arg");
  804.     $lon = 0;
  805.   }
  806.   if ($lon != '') {
  807.       return trim($lon);
  808.   }
  809.   return '';
  810. }
  811.  
  812. function OSM_getOpenStreetMapUrl() {
  813.   $zoom_level = get_settings('osm_zoom_level');  
  814.     $lat = $lat == ''? OSM_getCoordinateLat('osm') : $lat;
  815.     $lon = $lon == ''? OSM_getCoordinateLong('osm'): $lon;
  816.   return URL_INDEX.URL_LAT.$lat.URL_LON.$lon.URL_ZOOM_01.$zoom_level.URL_ZOOM_02;
  817. }
  818.  
  819. function OSM_echoOpenStreetMapUrl(){
  820.   echo OSM_getOpenStreetMapUrl() ;
  821. }
  822. // functions to display a map in your theme
  823. // by using the custom fields
  824. // default values should be set only at sc_showMap()
  825. function OSM_displayOpenStreetMap($a_widht, $a_hight, $a_zoom, $a_type){
  826.  
  827.   $atts = array ('width'        => $a_widht,
  828.                  'height'       => $a_hight,
  829.                  'type'         => $a_type,
  830.                  'zoom'         => $a_zoom,
  831.                    'control'          => 'off');
  832.  
  833.   if ((OSM_getCoordinateLong("osm"))&&(OSM_getCoordinateLat("osm"))) {
  834.     echo OSM::sc_showMap($atts);
  835.   }
  836. }
  837.  
  838. function OSM_displayOpenStreetMapExt($a_widht, $a_hight, $a_zoom, $a_type, $a_control, $a_marker_name, $a_marker_height, $a_marker_width, $a_marker_text, $a_ov_map, $a_marker_focus = 0){
  839.  
  840.   $atts = array ('width'        => $a_widht,
  841.                  'height'       => $a_hight,
  842.                  'type'         => $a_type,
  843.                  'zoom'         => $a_zoom,
  844.                  'ov_map'       => $a_ov_map,
  845.                  'marker_name'  => $a_marker_name,
  846.                  'marker_height'=> $a_marker_height,
  847.                  'marker_width' => $a_marker_width,
  848.                  'marker'       => OSM_getCoordinateLat("osm") . ',' . OSM_getCoordinateLong("osm") . ',' . $a_marker_text,
  849.                    'control'          => $a_control,
  850.                  'marker_focus' => $a_marker_focus);
  851.  
  852.   if ((OSM_getCoordinateLong("osm"))&&(OSM_getCoordinateLat("osm"))) {
  853.     echo OSM::sc_showMap($atts);
  854.   }
  855. }
  856. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement