GeoMashup
[ class tree: GeoMashup ] [ index: GeoMashup ] [ all elements ]

Source for file render-map.php

Documentation is available at render-map.php

  1. <?php
  2. /**
  3.  * Respond to a requests for a Geo Mashup map.
  4.  *
  5.  * @package GeoMashup
  6.  */
  7.  
  8. /**
  9.  * Static class for map rendering namespace
  10.  * 
  11.  * @access public
  12.  * @since 1.4
  13.  * @package GeoMashup
  14.  * @static
  15.  */
  16.     /**
  17.      * Map data built for a request.
  18.      * @var array 
  19.      */
  20.     private static $map_data = null;
  21.  
  22.     /**
  23.      * Template tag for the current map script.
  24.      *
  25.      * @since 1.4
  26.      *
  27.      * @param string $element_id The DOM id of the map container.
  28.      * @return string The script tag to generate the map for this request.
  29.      */
  30.     public static function map_script( $element_id ) {
  31.         return '<script type="text/javascript">' . "\n" .
  32.             'GeoMashup.createMap(document.getElementById("' . $element_id . '"), { ' .
  33.             GeoMashup::implode_assoc( ':', ',', self::$map_data ) . ' });' .
  34.             "\n" . '</script>';
  35.     }
  36.  
  37.     /**
  38.      * Enqueue a style to be included in the map frame.
  39.      *
  40.      * Register external styles with wp_register_style().
  41.      * Register local styles with GeoMashup::register_style().
  42.      *
  43.      * @since 1.4
  44.      *
  45.      * @param string $handle The handle used to register the style.
  46.      */
  47.     public static function enqueue_style( $handle ) {
  48.         $styles = self::map_property( 'styles' );
  49.         if ( is_null( $styles ) ) {
  50.             $styles = array();
  51.         }
  52.         $styles[] = $handle;
  53.         self::map_property( 'styles', $styles );
  54.     }
  55.  
  56.     /**
  57.      * Enqueue a script to be included in the map frame.
  58.      *
  59.      * Register external scripts with wp_register_script().
  60.      * Register local scripts with GeoMashup::register_script().
  61.      *
  62.      * @since 1.4
  63.      *
  64.      * @param string $handle The handle used to register the script.
  65.      */
  66.     public static function enqueue_script( $handle ) {
  67.         $scripts = self::map_property( 'scripts' );
  68.         if ( is_null( $scripts ) ) {
  69.             $scripts = array();
  70.         }
  71.         $scripts[] = $handle;
  72.         self::map_property( 'scripts', $scripts );
  73.     }
  74.  
  75.     /**
  76.      * Print only resources queued for the Geo Mashup map frame.
  77.      * 
  78.      * Can be replaced with wp_head() to include all blog resources in
  79.      * map frames.
  80.      * 
  81.      * @since 1.4
  82.      */
  83.     public static function head() {
  84.         $styles = self::map_property( 'styles' );
  85.         wp_print_styles( $styles );
  86.         $scripts = self::map_property( 'scripts' );
  87.         wp_print_scripts( $scripts );
  88.     }
  89.  
  90.     /**
  91.      * Template tag to get a property of the map for the current request.
  92.      *
  93.      * Current properties available: height and width for inline map styles.
  94.      *
  95.      * @since 1.4
  96.      *
  97.      * @param string $name The name of the property to get.
  98.      * @param string $new_value Optional, sets the property if supplied.
  99.      * @return string|nullThe property value or null if not found.
  100.      */
  101.     public static function map_property( $name, $new_value = null ) {
  102.         static $properties = array();
  103.         if ( !is_null( $new_value ) ) {
  104.             $properties[$name] = $new_value;
  105.         }
  106.         return ( isset( $properties[$name] ) ? $properties[$name] : null );
  107.     }
  108.  
  109.     /**
  110.      * Make sure a non-javascript item is double-quoted.
  111.      *
  112.      * @since 1.3
  113.      *
  114.      * @param mixed $item The value in question, may be modified.
  115.      * @param string $key The JSON key.
  116.      */
  117.     private static function add_double_quotes(&$item,$key) {
  118.         $quoted_keys = array ( 'background_color', 'show_future', 'map_control', 'map_content', 'map_type', 'legend_format', 'template' );
  119.         if ( $key == 'post_data' ) {
  120.             // don't quote
  121.         } else if ( !is_numeric( $item ) && empty ( $item ) ) {
  122.             $item = '""';
  123.         } else if ( is_array( $item ) && isset( $item[0] ) ) {
  124.             $item = '["' . implode( '","', $item ) . '"]';
  125.         } else if ( is_string( $item ) && $item[0] != '{' && $item[0] != '[' ) {
  126.             $item = '"'.$item.'"';
  127.         }
  128.     }
  129.  
  130.     /**
  131.      * Render the requested map.
  132.      *
  133.      * @since 1.4
  134.      */
  135.     public static function render_map() {
  136.         global $geo_mashup_options, $geo_mashup_custom;
  137.  
  138.         // Include theme stylesheet if requested
  139.         if ( $geo_mashup_options->get('overall', 'theme_stylesheet_with_maps' ) == 'true' ) {
  140.             wp_enqueue_style( 'theme-style', get_stylesheet_uri() );
  141.             self::enqueue_style( 'theme-style' );
  142.         }
  143.  
  144.         // Resolve map style
  145.         $style_file_path = path_join( get_stylesheet_directory(), 'map-style.css' );
  146.         $style_url_path = '';
  147.         if ( is_readable( $style_file_path ) ) {
  148.             $style_url_path = path_join( get_stylesheet_directory_uri(), 'map-style.css' );
  149.         } else if ( isset( $geo_mashup_custom ) ) {
  150.             $style_url_path = $geo_mashup_custom->file_url( 'map-style.css' );
  151.         }
  152.         if ( empty( $style_url_path ) ) {
  153.             GeoMashup::register_style( 'geo-mashup-map-style', 'css/map-style-default.css' );
  154.         } else {
  155.              wp_register_style( 'geo-mashup-map-style', $style_url_path );
  156.         }
  157.         wp_enqueue_style( 'geo-mashup-map-style' );
  158.         self::enqueue_style( 'geo-mashup-map-style' );
  159.  
  160.         if ( isset( $_GET['map_data_key'] ) ) {
  161.             // Map data is cached in a transient
  162.             $map_data = get_transient( 'gmm' . $_GET['map_data_key'] );
  163.             if ( !$map_data ) {
  164.                 $map_parameters = get_transient( 'gmp' . $_GET['map_data_key'] );
  165.                 if ( $map_parameters )
  166.                     $map_data = GeoMashup::build_map_data( $map_parameters );
  167.                 else
  168.                     $map_data = GeoMashup::build_map_data( '' );
  169.             }
  170.         } else {
  171.             // Try building map data from the query string
  172.             $map_data = GeoMashup::build_map_data( $_GET );
  173.         }
  174.  
  175.         // Queue scripts
  176.         $mashup_dependencies = array( 'jquery' );
  177.         $language_code = GeoMashup::get_language_code();
  178.         if ( 'google' == $map_data['map_api'] ) {
  179.             // Google v2 base
  180.             $google_2_url = 'http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=' .
  181.                 $geo_mashup_options->get( 'overall', 'google_key' );
  182.             if ( ! empty( $language_code ) ) {
  183.                 $google_2_url .= '&amp;hl=' . $language_code;
  184.             }
  185.             wp_register_script( 'google-maps-2', $google_2_url );
  186.             $mashup_dependencies[] = 'google-maps-2';
  187.             $mashup_script = 'geo-mashup-google';
  188.  
  189.             if ( !empty( $map_data['cluster_max_zoom'] ) ) {
  190.                 // Queue clustering scripts
  191.                 if ( 'clustermarker' == $map_data['cluster_lib'] ) {
  192.                     GeoMashup::register_script( 'mapiconmaker', 'js/mapiconmaker.js', array( 'google-maps-2' ), '1.1' );
  193.                     GeoMashup::register_script( 'clustermarker', 'js/ClusterMarker.js', array( 'mapiconmaker' ), '1.3.2' );
  194.                     $mashup_dependencies[] = 'clustermarker';
  195.                 } else {
  196.                     $map_data['cluster_lib'] = 'markerclusterer';
  197.                     GeoMashup::register_script( 'markerclusterer', 'js/markerclusterer.js', array( 'google-maps-2', 'geo-mashup-google' ), '1.0' );
  198.                     wp_enqueue_script( 'markerclusterer' );
  199.                     self::enqueue_script( 'markerclusterer' );
  200.                 }
  201.             }
  202.         } else {
  203.             // Mapstraction base
  204.             $mashup_script = 'geo-mashup-mxn';
  205.             GeoMashup::register_script( 'mxn', 'js/mxn/mxn.js', null, GEO_MASHUP_VERSION );
  206.             GeoMashup::register_script( 'mxn-core', 'js/mxn/mxn.core.js', array( 'mxn' ), GEO_MASHUP_VERSION );
  207.         }
  208.  
  209.         // Mapstraction providers
  210.         if ( 'openlayers' == $map_data['map_api'] ) {
  211.             wp_register_script( 'openlayers', 'http://openlayers.org/api/OpenLayers.js', null, 'latest' );
  212.             wp_register_script( 'openstreetmap', 'http://www.openstreetmap.org/openlayers/OpenStreetMap.js', array( 'openlayers' ), 'latest' );
  213.             GeoMashup::register_script( 'mxn-openlayers', 'js/mxn/mxn.openlayers.core.js', array( 'mxn-core', 'openstreetmap' ), GEO_MASHUP_VERSION );
  214.             GeoMashup::register_script( 'mxn-openlayers-gm', 'js/mxn/mxn.openlayers.geo-mashup.js', array( 'mxn-openlayers' ), GEO_MASHUP_VERSION );
  215.             $mashup_dependencies[] = 'mxn-openlayers-gm';
  216.         } else if ( 'googlev3' == $map_data['map_api'] ) {
  217.             $google_3_url = 'http://maps.google.com/maps/api/js?sensor=false';
  218.             if ( ! empty( $language_code ) ) {
  219.                 $google_3_url .= '&amp;language=' . $language_code;
  220.             }
  221.             wp_register_script( 'google-maps-3', $google_3_url );
  222.             GeoMashup::register_script( 'mxn-googlev3', 'js/mxn/mxn.googlev3.core.js', array( 'mxn-core', 'google-maps-3' ), GEO_MASHUP_VERSION );
  223.             $mashup_dependencies[] = 'mxn-googlev3';
  224.         }
  225.  
  226.         // Geo Mashup scripts
  227.         GeoMashup::register_script( 'geo-mashup', 'js/geo-mashup.js', $mashup_dependencies, GEO_MASHUP_VERSION );
  228.         GeoMashup::register_script( $mashup_script, 'js/' . $mashup_script . '.js', array( 'geo-mashup' ), GEO_MASHUP_VERSION );
  229.         wp_enqueue_script( $mashup_script );
  230.         self::enqueue_script( $mashup_script );
  231.  
  232.         // Custom javascript
  233.         $custom_js_url_path = '';
  234.         if ( isset( $geo_mashup_custom ) ) {
  235.             $custom_js_url_path = $geo_mashup_custom->file_url( 'custom-' . $map_data['map_api'] . '.js' );
  236.             if ( !$custom_js_url_path and 'google' != $map_data['map_api'] )
  237.                 $custom_js_url_path = $geo_mashup_custom->file_url( 'custom-mxn.js' );
  238.             if ( !$custom_js_url_path )
  239.                 $custom_js_url_path = $geo_mashup_custom->file_url( 'custom.js' );
  240.         } else if ( is_readable( 'custom.js' ) ) {
  241.             $custom_js_url_path = path_join( GEO_MASHUP_URL_PATH, 'custom.js' );
  242.         }
  243.         if ( ! empty( $custom_js_url_path ) ) {
  244.             wp_enqueue_script( 'geo-mashup-custom', $custom_js_url_path, array( $mashup_script ) );
  245.             self::enqueue_script( 'geo-mashup-custom' );
  246.         }
  247.  
  248.         // Set height and width properties for the template
  249.         $width = $map_data['width'];
  250.         if ( substr( $width, -1 ) != '%' ) {
  251.             $width .= 'px';
  252.         }
  253.         unset( $map_data['width'] );
  254.         self::map_property( 'width', $width );
  255.  
  256.         $height = $map_data['height'];
  257.         if ( substr( $height, -1 ) != '%' ) {
  258.             $height .= 'px';
  259.         }
  260.         unset( $map_data['height'] );
  261.         self::map_property( 'height', $height );
  262.  
  263.         if ( isset( $map_data['object_data'] ) and is_array( $map_data['object_data'] ) )
  264.             $map_data['object_data'] = json_encode( $map_data['object_data'] );
  265.         array_walk( $map_data, array( 'GeoMashupRenderMap', 'add_double_quotes' ) );
  266.  
  267.         if ( 'single' == $map_data['map_content'] ) {
  268.             $category_opts = '{}';
  269.         } else {
  270.             $categories = get_categories( array( 'hide_empty' => false ) );
  271.             $category_opts = '{';
  272.             if (is_array($categories))
  273.             {
  274.                 $cat_comma = '';
  275.                 $category_color = $geo_mashup_options->get('global_map', 'category_color');
  276.                 $category_line_zoom = $geo_mashup_options->get('global_map', 'category_line_zoom');
  277.                 foreach($categories as $category) {
  278.                     $category_opts .= $cat_comma.'"'.$category->term_id.'":{"name":"' . esc_js( $category->name ) . '"';
  279.                     $parent_id = '';
  280.                     if ( !empty( $category->parent ) ) {
  281.                         $parent_id = $category->parent;
  282.                     }
  283.                     $category_opts .= ',"parent_id":"' . $parent_id . '"';
  284.                     if ( !empty( $category_color[$category->slug] ) ) {
  285.                         $category_opts .= ',"color_name":"'.$category_color[$category->slug].'"';
  286.                     }
  287.                     if ( !empty( $category_line_zoom[$category->slug] ) ) {
  288.                         $category_opts .= ',"max_line_zoom":"'.$category_line_zoom[$category->slug].'"';
  289.                     }
  290.                     $category_opts .= '}';
  291.                     $cat_comma = ',';
  292.                 }
  293.             }
  294.             $category_opts .= '}';
  295.         }
  296.         $map_data['category_opts'] = $category_opts;
  297.  
  298.         // Store the properties for use by the template tag GeoMashupRenderMap::map_script
  299.         self::$map_data = $map_data;
  300.  
  301.         // Load the template
  302.         status_header ( 200 );
  303.         load_template( GeoMashup::locate_template( 'map-frame' ) );
  304.     }
  305. }

Documentation generated on Sat, 09 Jul 2011 21:54:59 -0700 by phpDocumentor 1.4.3