Change settings into a serialized array rather than individual properties.
[project/mapboxjs.git] / includes / mapboxjs.admin.inc
1 <?php
2
3 define('MAPBOXJS_PRESET_NAME_LENGTH', 128);
4
5 /**
6 * Return a form for a mapbox.js map preset entity.
7 *
8 * @param $form
9 * @param $form_state
10 * @param null $preset
11 *
12 * @return mixed
13 */
14 function mapboxjs_preset_form($form, &$form_state, MapboxjsPreset $preset = NULL, $op = 'edit') {
15 if ($op == 'clone') {
16 $preset->label .= ' (cloned)';
17 $preset->name = '';
18 }
19 $form['#id'] = 'maboxjs_preset_form';
20 $form_state['maboxjs_preset'] = $preset;
21
22 $form['label'] = array(
23 '#title' => t('Label'),
24 '#type' => 'textfield',
25 '#default_value' => isset($preset) ? $preset->label : '',
26 '#description' => t('The human-readable name of this MapBox.js map preset.'),
27 '#required' => TRUE,
28 '#maxlength' => MAPBOXJS_PRESET_NAME_LENGTH,
29 '#size' => 30,
30 '#weight' => -10,
31 );
32 $form['name'] = array(
33 '#title' => t('Name'),
34 '#type' => 'machine_name',
35 '#description' => t(''),
36 '#default_value' => isset($preset) ? $preset->name : '',
37 '#disabled' => !empty($preset->name),
38 '#machine_name' => array(
39 'exists' => 'mapboxjs_get_presets',
40 'source' => array('label')
41 ),
42 '#weight' => -10,
43 );
44
45 $form['settings'] = array(
46 '#title' => t('Settings'),
47 '#type' => 'container',
48 '#description' => t(''),
49 '#tree' => TRUE
50 );
51
52 // Enable/disable layer toggle control for optional layers.
53 $form['settings']['layer_toggle'] = array(
54 '#type' => 'checkbox',
55 '#title' => t('Provide layer toggle controls for optional layers'),
56 '#description' => t('Select this option to present controls to toggle on/off the optional tileset layers provided above.'),
57 '#default_value' => isset($preset) ? $preset->settings['layer_toggle'] : FALSE,
58 );
59 // Provide preset administrator with control over the map container height.
60 $form['settings']['height'] = array(
61 '#type' => 'textfield',
62 '#title' => t('Mabox map height'),
63 '#description' => t('Enter the height in pixels for this map.'),
64 '#default_value' => isset($preset) ? $preset->settings['height'] : '500',
65 '#size' => '5',
66 '#field_suffix' => t('px'),
67 '#element_validate' => array('element_validate_integer_positive'),
68 );
69 // Set the lat/lon center points.
70 $form['settings']['center'] = array(
71 '#type' => 'fieldset',
72 '#title' => t('Center point'),
73 '#description' => t('Enter the lat/long center point of this map in decimal degrees.'),
74 '#collapsible' => TRUE,
75 );
76 $form['settings']['center']['lat'] = array(
77 '#type' => 'textfield',
78 '#title' => t('Latitude'),
79 '#default_value' => isset($preset) ? $preset->settings['center']['lat'] : 0,
80 );
81 $form['settings']['center']['lon'] = array(
82 '#type' => 'textfield',
83 '#title' => t('Longitude'),
84 '#default_value' => isset($preset) ? $preset->settings['center']['lon'] : 0,
85 );
86 // Set default zoom level.
87 $form['settings']['zoom'] = array(
88 '#type' => 'select',
89 '#title' => t('Zoom level'),
90 '#description' => t('Select the default zoom level for this map.'),
91 '#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)),
92 '#default_value' => isset($preset) ? $preset->settings['zoom'] : 8,
93 );
94 // Enable/disable zoom controls.
95 $form['settings']['zoomer'] = array(
96 '#type' => 'checkbox',
97 '#title' => t('Provide zoom controls'),
98 '#default_value' => isset($preset) ? $preset->settings['zoomer'] : TRUE,
99 );
100 // Enable/disable fullscreen control.
101 $form['settings']['fullscreen'] = array(
102 '#type' => 'checkbox',
103 '#title' => t('Provide fullscreen map option'),
104 '#default_value' => isset($preset) ? $preset->settings['fullscreen'] : FALSE,
105 );
106 // Enable/disable map legend.
107 $form['settings']['legend'] = array(
108 '#type' => 'checkbox',
109 '#title' => t('Provide a map legend, if included with MapBox tileset(s).'),
110 '#default_value' => isset($preset) ? $preset->settings['legend'] : FALSE,
111 );
112 // Enable/disable tileset compositing.
113 $form['settings']['composite'] = array(
114 '#type' => 'checkbox',
115 '#title' => t('Enable tileset compositing'),
116 '#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.'),
117 '#default_value' => isset($preset) ? $preset->settings['composite'] : TRUE,
118 );
119
120 // Attach our locked link fields.
121 field_attach_form('mapboxjs_preset', $preset, $form, $form_state);
122
123 $form['actions'] = array('#type' => 'actions');
124 $form['actions']['submit'] = array(
125 '#value' => t('Save preset'),
126 '#type' => 'submit',
127 );
128
129 return $form;
130 }
131
132 /**
133 * Helper function to validate base tileset URL.
134 *
135 * @TODO - Consider adding this validation to mapboxjs_preset_form_validate().
136 */
137 function mapboxjs_base_layer_validate($element, &$form_state, $form) {
138 if (!empty($element['#value']) && !(valid_url($element['#value'], TRUE))) {
139 form_error($element, t('Enter a valid URL or leave this field blank.'));
140 }
141 }
142
143 /**
144 * Validation handler for mapboxjs_preset_form().
145 */
146 function mapboxjs_preset_form_validate($form, &$form_state) {
147 // Validate lat/lon values.
148 $lat = $form_state['values']['lat'];
149 $lon = $form_state['values']['lon'];
150 if (!empty($lat) & (!is_numeric($lat) || $lat > 90 || $lat < -90)) {
151 form_set_error('lat', t('Enter the latitude of the center point as a decimal between -90 and 90.'));
152 }
153 if (!empty($lon) & (!is_numeric($lon) || $lon > 180 || $lon < -180)) {
154 form_set_error('lon', t('Enter the longitude of the center point as a decimal between -180 and 180.'));
155 }
156 // Call to field_attach_form_validate() ensures that locked link field is validated.
157 $preset = $form_state['mapboxjs_preset'];
158 field_attach_form_validate('mapboxjs_preset', $preset, $form, $form_state);
159 // @FIXME - For some reason, link field URL is not getting validated.
160 // @TODO - Consider custom validation to check for .jsonp URL extension.
161 }
162
163 /**
164 * Submit handler for mapboxjs_preset_form().
165 *
166 * @param $form
167 * @param $form_state
168 */
169 function mapboxjs_preset_form_submit($form, &$form_state) {
170
171 $values = $form_state['values'];
172
173 if ($form_state['mapboxjs_preset']->name) {
174 $preset = $form_state['mapboxjs_preset'];
175 foreach ($values as $key => $value) {
176 $preset->{$key} = $value;
177 }
178 }
179 else {
180 $preset = entity_create('mapboxjs_preset', $values);
181 }
182
183 // Make sure that attached link field is saved with our custom entity.
184 field_attach_submit('mapboxjs_preset', $preset, $form, $form_state);
185
186 $preset->save();
187
188 drupal_set_message(t('MapBox.js map preset saved.'));
189 }