Allow map definitions to provide a default icon
[project/leaflet.git] / leaflet.module
1 <?php
2 require_once ('leaflet.formatters.inc');
3
4 /**
5 * Implements hook_theme().
6 */
7 function leaflet_theme($existing, $type, $theme, $path) {
8 return array(
9 'leaflet_map' => array(
10 'arguments' => array('map_id' => NULL, 'height' => '400px'),
11 'template' => 'leaflet_map',
12 ),
13 );
14 }
15
16 /**
17 * Implements hook_library().
18 */
19 function leaflet_library() {
20 $libraries['leaflet'] = array(
21 'title' => 'Leaflet',
22 'website' => 'http://leaflet.cloudmade.com',
23 'version' => '0.4',
24 'js' => array(
25 array(
26 'type' => 'inline',
27 'data' => 'L_ROOT_URL = "' . base_path() . libraries_get_path('leaflet') . '/dist/";',
28 'group' => JS_LIBRARY,
29 'preprocess' => FALSE
30 ),
31 array(
32 'type' => 'file',
33 'data' => libraries_get_path('leaflet') . '/dist/leaflet.js',
34 'group' => JS_LIBRARY,
35 'preprocess' => FALSE
36 ),
37 ),
38 'css' => array(
39 libraries_get_path('leaflet') . '/dist/leaflet.css' => array(
40 'type' => 'file',
41 'media' => 'screen',
42 ),
43 libraries_get_path('leaflet') . '/dist/leaflet.ie.css' => array(
44 'browsers' => array(
45 'IE' => 'lte IE 8',
46 '!IE' => FALSE,
47 ),
48 ),
49 ),
50 );
51
52 $libraries['leaflet-drupal'] = array(
53 'title' => 'Leaflet initialization',
54 'website' => 'http://drupal.org/project/leaflet',
55 'version' => '7.x-1.x',
56 'js' => array(
57 drupal_get_path('module', 'leaflet') . '/leaflet.drupal.js' => array(),
58 ),
59 'dependencies' => array(
60 array('leaflet', 'leaflet'),
61 ),
62 );
63
64 return $libraries;
65 }
66
67 /**
68 * Load all Leaflet required client files and return markup for a map.
69 *
70 * @param array $map
71 * @param array $features
72 * @param string $height
73 *
74 * @return string map markup
75 */
76 function leaflet_render_map($map, $features = array(), $height = '400px') {
77 $map_id = drupal_html_id('leaflet_map');
78 drupal_add_library('leaflet', 'leaflet-drupal');
79
80 // Allow map definitions to provide a default icon
81 if (isset($map['icon']['iconUrl'])) {
82 foreach ($features as &$feature) {
83 if (!isset($feature['icon'])) {
84 $feature['icon'] = $map['icon'];
85 }
86 }
87 }
88
89 $settings = array(
90 'mapId' => $map_id,
91 'map' => $map,
92 'features' => $features,
93 );
94
95 drupal_add_js(array('leaflet' => array($settings)), 'setting');
96
97 return theme('leaflet_map', array('map_id' => $map_id, 'height' => $height));
98 }
99
100 /**
101 * Get all avaialble Leaflet map definitions.
102 *
103 * @param string $map
104 */
105 function leaflet_map_get_info($map = NULL) {
106 static $drupal_static_fast;
107 if (!isset($drupal_static_fast)) {
108 $drupal_static_fast['leaflet_map_info'] = &drupal_static(__FUNCTION__);
109 }
110 $map_info = &$drupal_static_fast['leaflet_map_info'];
111
112 if (empty($map_info)) {
113 if ($cache = cache_get("leaflet_map_info")) {
114 $map_info = $cache->data;
115 }
116 else {
117 $map_info = module_invoke_all('leaflet_map_info');
118
119 // Let other modules alter the map info.
120 drupal_alter('leaflet_map_info', $map_info);
121
122 cache_set("leaflet_map_info", $map_info);
123 }
124 }
125
126 if (empty($map)) {
127 return $map_info;
128 }
129 elseif (isset($map_info[$map])) {
130 return $map_info[$map];
131 }
132 }
133
134 /**
135 * Implements hook_leaflet_map_info() to return a default map.
136 *
137 * @return array
138 */
139 function leaflet_leaflet_map_info() {
140 return array(
141 'OSM Mapnik' =>
142 array(
143 'label' => 'OSM Mapnik',
144 'description' => t('Leaflet default map.'),
145 'settings' => array(
146 'zoom' => 18,
147 'minZoom' => 0,
148 'maxZoom' => 18,
149 'dragging' => TRUE,
150 'touchZoom' => TRUE,
151 'scrollWheelZoom' => TRUE,
152 'doubleClickZoom' => TRUE,
153 'zoomControl' => TRUE,
154 'attributionControl' => TRUE,
155 'trackResize' => TRUE,
156 'fadeAnimation' => TRUE,
157 'zoomAnimation' => TRUE,
158 'closePopupOnClick' => TRUE,
159 ),
160 'layers' => array(
161 'earth' => array(
162 'urlTemplate' => 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
163 'options' => array(
164 'attribution' => 'OSM Mapnik'
165 )
166 ),
167 ),
168 ),
169 );
170 }
171