summaryrefslogtreecommitdiffstats
path: root/includes/mapboxjs.admin.inc
blob: 42821d749c639183acb5ef162f68ae55213159ea (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
<?php

define('MAPBOXJS_PRESET_NAME_LENGTH', 128);

/**
 * Return a form for a mapbox.js map preset entity.
 *
 * @param $form
 * @param $form_state
 * @param null $preset
 *
 * @return mixed
 */
function mapboxjs_preset_form($form, &$form_state, MapboxjsPreset $preset = NULL, $op = 'edit') {
  if ($op == 'clone') {
    $preset->label .= ' (cloned)';
    $preset->name = '';
  }
  $form['#id'] = 'maboxjs_preset_form';
  $form_state['maboxjs_preset'] = $preset;

  $form['label'] = array(
    '#title' => t('Label'),
    '#type' => 'textfield',
    '#default_value' => isset($preset) ? $preset->label : '',
    '#description' => t('The human-readable name of this MapBox.js map preset.'),
    '#required' => TRUE,
    '#maxlength' => MAPBOXJS_PRESET_NAME_LENGTH,
    '#size' => 30,
    '#weight' => -10,
  );
  $form['name'] = array(
    '#title' => t('Name'),
    '#type' => 'machine_name',
    '#description' => t(''),
    '#default_value' => isset($preset) ? $preset->name : '',
    '#disabled' => !empty($preset->name),
    '#machine_name' => array(
      'exists' => 'mapboxjs_get_presets',
      'source' => array('label')
    ),
    '#weight' => -10,
  );

  $form['settings'] = array(
    '#title' => t('Settings'),
    '#type' => 'container',
    '#description' => t(''),
    '#tree' => TRUE
  );

  // Enable/disable layer toggle control for optional layers.
  $form['settings']['layer_toggle'] = array(
    '#type' => 'checkbox',
    '#title' => t('Provide layer toggle controls for optional layers'),
    '#description' => t('Select this option to present controls to toggle on/off the optional tileset layers provided above.'),
    '#default_value' => isset($preset) ? $preset->settings['layer_toggle'] : FALSE,
  );
  // Provide preset administrator with control over the map container height.
  $form['settings']['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Mabox map height'),
    '#description' => t('Enter the height in pixels for this map.'),
    '#default_value' => isset($preset) ? $preset->settings['height'] : '500',
    '#size' => '5',
    '#field_suffix' => t('px'),
    '#element_validate' => array('element_validate_integer_positive'),
  );
  // Set the lat/lon center points.
  $form['settings']['center'] = array(
    '#type' => 'fieldset',
    '#title' => t('Center point'),
    '#description' => t('Enter the lat/long center point of this map in decimal degrees.'),
    '#collapsible' => TRUE,
  );
  $form['settings']['center']['lat'] = array(
    '#type' => 'textfield',
    '#title' => t('Latitude'),
    '#default_value' => isset($preset) ? $preset->settings['center']['lat'] : 0,
  );
  $form['settings']['center']['lon'] = array(
    '#type' => 'textfield',
    '#title' => t('Longitude'),
    '#default_value' => isset($preset) ? $preset->settings['center']['lon'] : 0,
  );
  // Set default zoom level.
  $form['settings']['zoom'] = array(
    '#type' => 'select',
    '#title' => t('Zoom level'),
    '#description' => t('Select the default zoom level for this map.'),
    '#options' => drupal_map_assoc(array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)),
    '#default_value' => isset($preset) ? $preset->settings['zoom'] : 8,
  );
  // Enable/disable zoom controls.
  $form['settings']['zoomer'] = array(
    '#type' => 'checkbox',
    '#title' => t('Provide zoom controls'),
    '#default_value' => isset($preset) ? $preset->settings['zoomer'] : TRUE,
  );
  // Enable/disable fullscreen control.
  $form['settings']['fullscreen'] = array(
    '#type' => 'checkbox',
    '#title' => t('Provide fullscreen map option'),
    '#default_value' => isset($preset) ? $preset->settings['fullscreen'] : FALSE,
  );
  // Enable/disable map legend.
  $form['settings']['legend'] = array(
    '#type' => 'checkbox',
    '#title' => t('Provide a map legend, if included with MapBox tileset(s).'),
    '#default_value' => isset($preset) ? $preset->settings['legend'] : FALSE,
  );
  // Enable/disable tileset compositing.
  $form['settings']['composite'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable tileset compositing'),
    '#description' => t('By default, multiple tilesets will be returned by the MapBox API as a single, composited tileset. This improves performance and reduces the number of tile requests to the MapBox API. In certain cases, however, it is helpful to disable compositing for smoother transitions when toggling on/off one or more tilesets.'),
    '#default_value' => isset($preset) ? $preset->settings['composite'] : TRUE,
  );

  // Attach our locked link fields.
  field_attach_form('mapboxjs_preset', $preset, $form, $form_state);

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#value' => t('Save preset'),
    '#type' => 'submit',
  );

  return $form;
}

/**
 * Helper function to validate base tileset URL.
 *
 * @TODO - Consider adding this validation to mapboxjs_preset_form_validate().
 */
function mapboxjs_base_layer_validate($element, &$form_state, $form) {
  if (!empty($element['#value']) && !(valid_url($element['#value'], TRUE))) {
    form_error($element, t('Enter a valid URL or leave this field blank.'));
  }
}

/**
 * Validation handler for mapboxjs_preset_form().
 */
function mapboxjs_preset_form_validate($form, &$form_state) {
  // Validate lat/lon values.
  $lat = $form_state['values']['lat'];
  $lon = $form_state['values']['lon'];
  if (!empty($lat) & (!is_numeric($lat) || $lat > 90 || $lat < -90)) {
    form_set_error('lat', t('Enter the latitude of the center point as a decimal between -90 and 90.'));
  }
  if (!empty($lon) & (!is_numeric($lon) || $lon > 180 || $lon < -180)) {
    form_set_error('lon', t('Enter the longitude of the center point as a decimal between -180 and 180.'));
  }
  // Call to field_attach_form_validate() ensures that locked link field is validated.
  $preset = $form_state['mapboxjs_preset'];
  field_attach_form_validate('mapboxjs_preset', $preset, $form, $form_state);
  // @FIXME - For some reason, link field URL is not getting validated.
  // @TODO - Consider custom validation to check for .jsonp URL extension.
}

/**
 * Submit handler for mapboxjs_preset_form().
 *
 * @param $form
 * @param $form_state
 */
function mapboxjs_preset_form_submit($form, &$form_state) {

  $values = $form_state['values'];

  if ($form_state['mapboxjs_preset']->name) {
    $preset = $form_state['mapboxjs_preset'];
    foreach ($values as $key => $value) {
      $preset->{$key} = $value;
    }
  }
  else {
    $preset = entity_create('mapboxjs_preset', $values);
  }

  // Make sure that attached link field is saved with our custom entity.
  field_attach_submit('mapboxjs_preset', $preset, $form, $form_state);

  $preset->save();

  drupal_set_message(t('MapBox.js map preset saved.'));
}