summaryrefslogtreecommitdiffstats
path: root/mapboxjs.module
blob: 28e077a5e777e71e44701b804e69a5976aeae9d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php

module_load_include('inc', 'mapboxjs', 'includes/mapboxjs.field');

/**
 * Implements hook_requirements().
 *
 * @TODO - Rather than grabbing mapbox.js and mapbox.css directly, checkout project from GitHub
 *   at: https://github.com/mapbox/mapbox.js/tree/v0.6.7. Would then need to update mapboxjs.make.example.
 */
function mapboxjs_requirements($phase) {
  $requirements = array();

  // Ensure js library is available
  if ($phase == 'runtime' && !file_exists(libraries_get_path('mapboxjs') . '/mapbox.js')) {
    $requirements['mapboxjss'] = array(
      'title' => t('MapBox.js library not found'),
      'value' => t('The !mapboxjs javascript library was not found. Please !download it into the mapbox subdirectory of the libraries folder.',
        array(
          '!mapboxjs' => l('MapBox.js', 'http://mapbox.com/mapbox.js'),
          '!download' => l('download', 'http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.js'),
        )
      ),
      'severity' => REQUIREMENT_ERROR,
    );
  }
  // Ensure MapBox css file is available
  if ($phase == 'runtime' && !file_exists(libraries_get_path('mapboxjs') . '/mapbox.css')) {
    $requirements['mapboxjs'] = array(
      'title' => t('MapBox css not found'),
      'value' => t('The !mapboxjs css file was not found. Please !download it into the mapbox subdirectory of the libraries folder.',
        array(
          '!mapboxjs' => l('MapBox.js', 'http://mapbox.com/mapbox.js'),
          '!download' => l('download', 'http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.css'),
        )
      ),
      'severity' => REQUIREMENT_ERROR,
    );
  }

  return $requirements;
}

/**
 * Implements hook_library().
 */
function mapboxjs_library() {
  $libraries['mapboxjs'] = array(
    'title' => 'MapBox.js',
    'website' => 'http://mapbox.com/mapbox.js',
    'version' => '0.6.6',
    'js' => array(
      // Load the MapBox.js javascript library.
      libraries_get_path('mapboxjs') . '/mapbox.js' => array(
        'type' => 'file',
      ),
      // Load our javascript implementation of MapBox.js.
      drupal_get_path('module', 'mapboxjs') . '/js/mapboxjs.drupal.js' => array(
        'type' => 'file',
      ),
    ),
    'css' => array(
      libraries_get_path('mapboxjs') . '/mapbox.css' => array(
        'type' => 'file',
        'media' => 'screen',
        'group' => CSS_DEFAULT,
      ),
      drupal_get_path('module', 'mapboxjs') . '/css/mapboxjs.drupal.css' => array(
        'type' => 'file',
        'media' => 'screen',
        'group' => CSS_DEFAULT,
      )
    ),
  );
  return $libraries;
}

/**
 * Implements hook_permission().
 */
function mapboxjs_permission() {
  return array(
    'administer mapboxjs presets' => array(
      'title' => t('administer mapboxjs presets'),
      'description' => t('Administer MapBox.js map presets.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Access callback for managing MapBox.js map presets.
 *
 * @return bool
 */
function mapboxjs_access() {
  if (user_access('administer mapboxjs presets')) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implements hook_entity_info().
 *
 * Provides our custom entity type/bundle for MapBox.js map presets.
 */
function mapboxjs_entity_info() {
  $return = array(
    'mapboxjs_preset' => array(
      'label' => t('MapBox.js map preset'),
      'controller class' => 'EntityAPIControllerExportable',
      'entity class' => 'MapboxjsPreset',
      'base table' => 'mapboxjs_preset',
      'uri callback' => 'entity_class_uri',
      'fieldable' => TRUE,
      'exportable' => TRUE,
      'module' => 'mapboxjs',
      'entity keys' => array(
        'id' => 'mapboxjs_preset_id',
        'name' => 'name',
        'label' => 'label'
      ),
      'bundles' => array(
        'mapboxjs_preset' => array(
          'label' => 'Mapbox.js map preset',
        ),
      ),
      // Enable the entity API's admin UI.
      'admin ui' => array(
        'path' => 'admin/structure/mapboxjs/presets',
        'file' => 'mapboxjs.admin.inc',
        'file path' => drupal_get_path('module', 'mapboxjs') . '/includes',
      ),
      'access callback' => 'mapboxjs_access',
      'label callback' => 'entity_class_label',
    ),
  );

  return $return;
}

/**
 * Gets an array of all registration types, keyed by the name.
 *
 * @param string $name
 *   If set, the type with the given name is returned.
 */
function mapboxjs_get_presets($name = NULL) {
  $presets = entity_load_multiple_by_name('mapboxjs_preset', isset($name) ? array($name) : FALSE);
  return isset($name) ? reset($presets) : $presets;
}

/**
 * Render a preset as a map.
 *
 * @param MapboxjsPreset $preset
 *
 * @return MapBox.js map as a renderable array
 */
function mapboxjs_render_preset(MapboxjsPreset $preset) {
  $map_id = 'map-' . $preset->name;
  // @TODO - Provide unique ID for each MapBox map so that we can act on multiple maps on a single page.

  // Grab our required base tileset(s).
  $base_tileset_items = field_get_items('mapboxjs_preset', $preset, 'field_base_tilesets');
  $base_tilesets = array();
  foreach ($base_tileset_items as $item) {
    $base_tilesets[] = array('title' => $item['title'], 'url' => $item['url']);
  }
  // Grab our optional tilesets to be laid on top of base tileset(s).
  $optional_tileset_items = field_get_items('mapboxjs_preset', $preset, 'field_optional_tilesets');
  if (!empty($optional_tileset_items)) {
    $optional_tilesets = array();
    foreach ($optional_tileset_items as $item) {
      $optional_tilesets[] = array(
        'title' => $item['title'],
        'url' => $item['url']
      );
    }
  }

  $settings = array('mapID' => $map_id);
  foreach ($preset->settings as $key => $value) {
    $settings['configuration'][$key] = $value;
  }

  // Pass our settings to Drupal's global js variable.
  drupal_add_js(array('mapboxjs' => $settings), 'setting');

  // Optionally, start adding extra controls.
  $extras = NULL;
  // Add a base tile switcher if we have more than one base tileset defined.
  if (count($base_tilesets) > 1) {
    $switcher = '';
    // @TODO - Generate markup using renderable array.
    foreach ($base_tilesets as $key => $tileset) {
      $switcher .= "<li><a href='#' id='base-tile-" . $key . "'>" . $tileset['title'] . "</a></li>";
    }
    $switcher = "<div id='map-switcher'><ul>" . $switcher . "</ul></div>";
    $extras .= $switcher;
  }
  // Add additional markup layer toggle controls option selected.
  if ($preset->layer_toggle == TRUE) {
    $extras .= "<ul id='map-toggle'></ul>";
  }

  return mapboxjs_render_map($map_id, $preset->height, $extras);
}


/**
 * Implements hook_theme().
 */
function mapboxjs_theme($existing, $type, $theme, $path) {
  return array(
    'mapboxjs_map' => array(
      'arguments' => array(
        'map_id' => NULL,
        'height' => 400,
        'extras' => ''
      ),
      'template' => 'mapboxjs_map',
    ),
  );
}

/**
 * Load all MapBox.js required client files and return markup for a map.
 *
 * @param string $map_id
 * @param string $height
 * @param string $extras
 *    Contains additional markup or controls embedded in the map element.
 *
 * @return string map markup
 *
 * @TODO - Provide some additional default map settings that can be overridden.
 */
function mapboxjs_render_map($map_id = 'mapboxjs-map', $height = 400, $extras = NULL) {
  // Add javascript and css dependencies.
  drupal_add_library('mapboxjs', 'mapboxjs');
  return theme('mapboxjs_map', array(
    'map_id' => $map_id,
    'height' => $height,
    'extras' => $extras
  ));
}