/lib/class_agm_maps_widget.php

  1. <?php 
  2.  
  3. /** 
  4. * Sidebar widget for Google Maps Plugin. 
  5. */ 
  6. class AgmMapsWidget extends WP_Widget { 
  7. function AgmMapsWidget() { 
  8. parent::WP_Widget(false, $name = 'Google Maps Widget'); 
  9. $this->model = new AgmMapModel(); 
  10.  
  11. function form($instance) { 
  12. $title = esc_attr($instance['title']); 
  13. $height = esc_attr($instance['height']); 
  14. $width = esc_attr($instance['width']); 
  15. $query = esc_attr($instance['query']); 
  16. $query_custom = esc_attr($instance['query_custom']); 
  17. $network = esc_attr($instance['network']); 
  18. $map_id = esc_attr($instance['map_id']); 
  19. $show_as_one = esc_attr($instance['show_as_one']); 
  20. $show_map = esc_attr($instance['show_map']); 
  21. $show_markers = esc_attr($instance['show_markers']); 
  22. $show_images = esc_attr($instance['show_images']); 
  23. $show_posts = esc_attr($instance['show_posts']); 
  24. $zoom = esc_attr($instance['zoom']); 
  25.  
  26. // Set defaults 
  27. $height = $height ? $height : 200; 
  28. $width = $width ? $width : 200; 
  29. $query_custom = ('custom' == $query) ? $query_custom : ''; 
  30. $network = ('custom' == $query) ? $network : ''; 
  31. $show_as_one = (isset($instance['show_as_one'])) ? $show_as_one : 1; 
  32. $show_map = (isset($instance['show_map'])) ? $show_map : 1; 
  33. $show_markers = (isset($instance['show_markers'])) ? $show_markers : 1; 
  34. $show_images = $show_images ? $show_images : 0; 
  35. $show_posts = $show_posts ? $show_posts : 1; 
  36.  
  37. $zoom_items = array( 
  38. '1' => 'Earth',  
  39. '3' => 'Continent',  
  40. '5' => 'Region',  
  41. '7' => 'Nearby Cities',  
  42. '12' => 'City Plan',  
  43. '15' => 'Details',  
  44. ); 
  45.  
  46. // Load map titles/ids 
  47. $maps = $this->model->get_maps(); 
  48.  
  49. include(AGM_PLUGIN_BASE_DIR . '/lib/forms/widget_settings.php'); 
  50.  
  51. function update($new_instance, $old_instance) { 
  52. $instance = $old_instance; 
  53. $instance['title'] = strip_tags($new_instance['title']); 
  54. $instance['height'] = strip_tags($new_instance['height']); 
  55. $instance['width'] = strip_tags($new_instance['width']); 
  56. $instance['query'] = strip_tags($new_instance['query']); 
  57. $instance['query_custom'] = strip_tags($new_instance['query_custom']); 
  58. $instance['network'] = strip_tags($new_instance['network']); 
  59. $instance['map_id'] = strip_tags($new_instance['map_id']); 
  60. $instance['show_as_one'] = (int)$new_instance['show_as_one']; 
  61. $instance['show_map'] = (int)$new_instance['show_map']; 
  62. $instance['show_markers'] = (int)$new_instance['show_markers']; 
  63. $instance['show_images'] = (int)$new_instance['show_images']; 
  64. $instance['show_posts'] = (int)$new_instance['show_posts']; 
  65. $instance['zoom'] = (int)$new_instance['zoom']; 
  66. return $instance; 
  67.  
  68. function widget($args, $instance) { 
  69. extract($args); 
  70. $title = apply_filters('widget_title', $instance['title']); 
  71. $height = (int)$instance['height']; 
  72. $height = $height ? $height : 200; // Apply default 
  73. $width = (int)$instance['width']; 
  74. $width = $width ? $width : 200; // Apply default 
  75. $query = $instance['query']; 
  76. $query_custom = $instance['query_custom']; 
  77. $network = $instance['network']; 
  78. $map_id = $instance['map_id']; 
  79. $show_as_one = $instance['show_as_one']; 
  80. $show_map = $instance['show_map']; 
  81. $show_markers = $instance['show_markers']; 
  82. $show_images = $instance['show_images']; 
  83. $show_posts = $instance['show_posts']; 
  84. $zoom = (int)$instance['zoom']; 
  85.  
  86. $maps = $this->get_maps($query, $query_custom, $map_id, $show_as_one, $network); 
  87.  
  88. echo $before_widget; 
  89. if ($title) echo $before_title . $title . $after_title; 
  90. if (is_array($maps)) foreach ($maps as $map) { 
  91. $selector = 'agm_widget_map_' . md5(microtime() . rand()); 
  92. $map['show_posts'] = (int)$show_posts; 
  93. $map['height'] = $height; 
  94. $map['width'] = $width; 
  95. $map['show_map'] = $show_map; 
  96. $map['show_markers'] = $show_markers; 
  97. $map['show_images'] = $show_images; 
  98. if ($zoom) $map['zoom'] = $zoom; 
  99. echo '<div id="' . $selector . '"></div>'; 
  100. echo '<script type="text/javascript">_agmMaps[_agmMaps.length] = {selector: "#' . $selector . '", data: ' . json_encode($map) . '};</script>'; 
  101.  
  102. echo $after_widget; 
  103.  
  104. function get_maps ($query, $custom, $map_id, $show_as_one, $network) { 
  105. $ret = false; 
  106. switch ($query) { 
  107. case 'current': 
  108. $ret = $this->model->get_current_maps(); 
  109. break; 
  110. case 'all_posts': 
  111. $ret = $this->model->get_all_posts_maps(); 
  112. break; 
  113. case 'all': 
  114. $ret = $this->model->get_all_maps(); 
  115. break; 
  116. case 'random': 
  117. $ret = $this->model->get_random_map(); 
  118. break; 
  119. case 'custom': 
  120. $ret = $network ? $this->model->get_custom_network_maps($custom) : $this->model->get_custom_maps($custom); 
  121. break; 
  122. case 'id': 
  123. $ret = array($this->model->get_map($map_id)); 
  124. break; 
  125. default: 
  126. $ret = false; 
  127. break; 
  128. if ($ret && $show_as_one) return array($this->model->merge_markers($ret)); 
  129. return $ret; 
  130.  
.