Change settings into a serialized array rather than individual properties.
[project/mapboxjs.git] / mapboxjs.module
1 <?php
2
3 module_load_include('inc', 'mapboxjs', 'includes/mapboxjs.field');
4
5 /**
6 * Implements hook_requirements().
7 *
8 * @TODO - Rather than grabbing mapbox.js and mapbox.css directly, checkout project from GitHub
9 * at: https://github.com/mapbox/mapbox.js/tree/v0.6.7. Would then need to update mapboxjs.make.example.
10 */
11 function mapboxjs_requirements($phase) {
12 $requirements = array();
13
14 // Ensure js library is available
15 if ($phase == 'runtime' && !file_exists(libraries_get_path('mapboxjs') . '/mapbox.js')) {
16 $requirements['mapboxjss'] = array(
17 'title' => t('MapBox.js library not found'),
18 'value' => t('The !mapboxjs javascript library was not found. Please !download it into the mapbox subdirectory of the libraries folder.',
19 array(
20 '!mapboxjs' => l('MapBox.js', 'http://mapbox.com/mapbox.js'),
21 '!download' => l('download', 'http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.js'),
22 )
23 ),
24 'severity' => REQUIREMENT_ERROR,
25 );
26 }
27 // Ensure MapBox css file is available
28 if ($phase == 'runtime' && !file_exists(libraries_get_path('mapboxjs') . '/mapbox.css')) {
29 $requirements['mapboxjs'] = array(
30 'title' => t('MapBox css not found'),
31 'value' => t('The !mapboxjs css file was not found. Please !download it into the mapbox subdirectory of the libraries folder.',
32 array(
33 '!mapboxjs' => l('MapBox.js', 'http://mapbox.com/mapbox.js'),
34 '!download' => l('download', 'http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.css'),
35 )
36 ),
37 'severity' => REQUIREMENT_ERROR,
38 );
39 }
40
41 return $requirements;
42 }
43
44 /**
45 * Implements hook_library().
46 */
47 function mapboxjs_library() {
48 $libraries['mapboxjs'] = array(
49 'title' => 'MapBox.js',
50 'website' => 'http://mapbox.com/mapbox.js',
51 'version' => '0.6.6',
52 'js' => array(
53 // Load the MapBox.js javascript library.
54 libraries_get_path('mapboxjs') . '/mapbox.js' => array(
55 'type' => 'file',
56 ),
57 // Load our javascript implementation of MapBox.js.
58 drupal_get_path('module', 'mapboxjs') . '/js/mapboxjs.drupal.js' => array(
59 'type' => 'file',
60 ),
61 ),
62 'css' => array(
63 libraries_get_path('mapboxjs') . '/mapbox.css' => array(
64 'type' => 'file',
65 'media' => 'screen',
66 'group' => CSS_DEFAULT,
67 ),
68 drupal_get_path('module', 'mapboxjs') . '/css/mapboxjs.drupal.css' => array(
69 'type' => 'file',
70 'media' => 'screen',
71 'group' => CSS_DEFAULT,
72 )
73 ),
74 );
75 return $libraries;
76 }
77
78 /**
79 * Implements hook_permission().
80 */
81 function mapboxjs_permission() {
82 return array(
83 'administer mapboxjs presets' => array(
84 'title' => t('administer mapboxjs presets'),
85 'description' => t('Administer MapBox.js map presets.'),
86 'restrict access' => TRUE,
87 ),
88 );
89 }
90
91 /**
92 * Access callback for managing MapBox.js map presets.
93 *
94 * @return bool
95 */
96 function mapboxjs_access() {
97 if (user_access('administer mapboxjs presets')) {
98 return TRUE;
99 }
100 return FALSE;
101 }
102
103 /**
104 * Implements hook_entity_info().
105 *
106 * Provides our custom entity type/bundle for MapBox.js map presets.
107 */
108 function mapboxjs_entity_info() {
109 $return = array(
110 'mapboxjs_preset' => array(
111 'label' => t('MapBox.js map preset'),
112 'controller class' => 'EntityAPIControllerExportable',
113 'entity class' => 'MapboxjsPreset',
114 'base table' => 'mapboxjs_preset',
115 'uri callback' => 'entity_class_uri',
116 'fieldable' => TRUE,
117 'exportable' => TRUE,
118 'module' => 'mapboxjs',
119 'entity keys' => array(
120 'id' => 'mapboxjs_preset_id',
121 'name' => 'name',
122 'label' => 'label'
123 ),
124 'bundles' => array(
125 'mapboxjs_preset' => array(
126 'label' => 'Mapbox.js map preset',
127 ),
128 ),
129 // Enable the entity API's admin UI.
130 'admin ui' => array(
131 'path' => 'admin/structure/mapboxjs/presets',
132 'file' => 'mapboxjs.admin.inc',
133 'file path' => drupal_get_path('module', 'mapboxjs') . '/includes',
134 ),
135 'access callback' => 'mapboxjs_access',
136 'label callback' => 'entity_class_label',
137 ),
138 );
139
140 return $return;
141 }
142
143 /**
144 * Gets an array of all registration types, keyed by the name.
145 *
146 * @param string $name
147 * If set, the type with the given name is returned.
148 */
149 function mapboxjs_get_presets($name = NULL) {
150 $presets = entity_load_multiple_by_name('mapboxjs_preset', isset($name) ? array($name) : FALSE);
151 return isset($name) ? reset($presets) : $presets;
152 }
153
154 /**
155 * Render a preset as a map.
156 *
157 * @param MapboxjsPreset $preset
158 *
159 * @return MapBox.js map as a renderable array
160 */
161 function mapboxjs_render_preset(MapboxjsPreset $preset) {
162 $map_id = 'map-' . $preset->name;
163 // @TODO - Provide unique ID for each MapBox map so that we can act on multiple maps on a single page.
164
165 // Grab our required base tileset(s).
166 $base_tileset_items = field_get_items('mapboxjs_preset', $preset, 'field_base_tilesets');
167 $base_tilesets = array();
168 foreach ($base_tileset_items as $item) {
169 $base_tilesets[] = array('title' => $item['title'], 'url' => $item['url']);
170 }
171 // Grab our optional tilesets to be laid on top of base tileset(s).
172 $optional_tileset_items = field_get_items('mapboxjs_preset', $preset, 'field_optional_tilesets');
173 if (!empty($optional_tileset_items)) {
174 $optional_tilesets = array();
175 foreach ($optional_tileset_items as $item) {
176 $optional_tilesets[] = array(
177 'title' => $item['title'],
178 'url' => $item['url']
179 );
180 }
181 }
182
183 $settings = array('mapID' => $map_id);
184 foreach ($preset->settings as $key => $value) {
185 $settings['configuration'][$key] = $value;
186 }
187
188 // Pass our settings to Drupal's global js variable.
189 drupal_add_js(array('mapboxjs' => $settings), 'setting');
190
191 // Optionally, start adding extra controls.
192 $extras = NULL;
193 // Add a base tile switcher if we have more than one base tileset defined.
194 if (count($base_tilesets) > 1) {
195 $switcher = '';
196 // @TODO - Generate markup using renderable array.
197 foreach ($base_tilesets as $key => $tileset) {
198 $switcher .= "<li><a href='#' id='base-tile-" . $key . "'>" . $tileset['title'] . "</a></li>";
199 }
200 $switcher = "<div id='map-switcher'><ul>" . $switcher . "</ul></div>";
201 $extras .= $switcher;
202 }
203 // Add additional markup layer toggle controls option selected.
204 if ($preset->layer_toggle == TRUE) {
205 $extras .= "<ul id='map-toggle'></ul>";
206 }
207
208 return mapboxjs_render_map($map_id, $preset->height, $extras);
209 }
210
211
212 /**
213 * Implements hook_theme().
214 */
215 function mapboxjs_theme($existing, $type, $theme, $path) {
216 return array(
217 'mapboxjs_map' => array(
218 'arguments' => array(
219 'map_id' => NULL,
220 'height' => 400,
221 'extras' => ''
222 ),
223 'template' => 'mapboxjs_map',
224 ),
225 );
226 }
227
228 /**
229 * Load all MapBox.js required client files and return markup for a map.
230 *
231 * @param string $map_id
232 * @param string $height
233 * @param string $extras
234 * Contains additional markup or controls embedded in the map element.
235 *
236 * @return string map markup
237 *
238 * @TODO - Provide some additional default map settings that can be overridden.
239 */
240 function mapboxjs_render_map($map_id = 'mapboxjs-map', $height = 400, $extras = NULL) {
241 // Add javascript and css dependencies.
242 drupal_add_library('mapboxjs', 'mapboxjs');
243 return theme('mapboxjs_map', array(
244 'map_id' => $map_id,
245 'height' => $height,
246 'extras' => $extras
247 ));
248 }