Do not use AJAX POST for forms that would normally use GET
[phpmyadmin.git] / gis_data_editor.php
blob0697bd7a2850e8dfa22036225052accffba8bf4d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Editor for Geometry data types.
6 * @package PhpMyAdmin
7 */
8 use PMA\libraries\Response;
9 use PMA\libraries\gis\GISFactory;
10 use PMA\libraries\gis\GISVisualization;
11 use PMA\libraries\URL;
13 /**
14 * Escapes special characters if the variable is set.
15 * Returns an empty string otherwise.
17 * @param string $variable variable to be escaped
19 * @return string escaped variable
21 function escape($variable)
23 return isset($variable) ? htmlspecialchars($variable) : '';
26 require_once 'libraries/common.inc.php';
28 if (! isset($_REQUEST['field'])) {
29 PMA\libraries\Util::checkParameters(array('field'));
32 // Get data if any posted
33 $gis_data = array();
34 if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
35 $gis_data = $_REQUEST['gis_data'];
38 $gis_types = array(
39 'POINT',
40 'MULTIPOINT',
41 'LINESTRING',
42 'MULTILINESTRING',
43 'POLYGON',
44 'MULTIPOLYGON',
45 'GEOMETRYCOLLECTION'
48 // Extract type from the initial call and make sure that it's a valid one.
49 // Extract from field's values if available, if not use the column type passed.
50 if (! isset($gis_data['gis_type'])) {
51 if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
52 $gis_data['gis_type'] = mb_strtoupper($_REQUEST['type']);
54 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
55 $start = (substr($_REQUEST['value'], 0, 1) == "'") ? 1 : 0;
56 $gis_data['gis_type'] = mb_substr(
57 $_REQUEST['value'],
58 $start,
59 mb_strpos($_REQUEST['value'], "(") - $start
62 if ((! isset($gis_data['gis_type']))
63 || (! in_array($gis_data['gis_type'], $gis_types))
64 ) {
65 $gis_data['gis_type'] = $gis_types[0];
68 $geom_type = htmlspecialchars($gis_data['gis_type']);
70 // Generate parameters from value passed.
71 $gis_obj = GISFactory::factory($geom_type);
72 if (isset($_REQUEST['value'])) {
73 $gis_data = array_merge(
74 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
78 // Generate Well Known Text
79 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
80 ? htmlspecialchars($gis_data['srid']) : 0;
81 $wkt = $gis_obj->generateWkt($gis_data, 0);
82 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
83 $result = "'" . $wkt . "'," . $srid;
85 // Generate SVG based visualization
86 $visualizationSettings = array(
87 'width' => 450,
88 'height' => 300,
89 'spatialColumn' => 'wkt'
91 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
92 $visualization = GISVisualization::getByData($data, $visualizationSettings)
93 ->toImage('svg');
95 $open_layers = GISVisualization::getByData($data, $visualizationSettings)
96 ->asOl();
98 // If the call is to update the WKT and visualization make an AJAX response
99 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
100 $extra_data = array(
101 'result' => $result,
102 'visualization' => $visualization,
103 'openLayers' => $open_layers,
105 $response = Response::getInstance();
106 $response->addJSON($extra_data);
107 exit;
110 ob_start();
112 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
113 echo '<input type="hidden" id="pmaThemeImage"'
114 , ' value="' , $GLOBALS['pmaThemeImage'] , '" />';
115 echo '<div id="gis_data_editor">';
117 echo '<h3>';
118 printf(
119 __('Value for the column "%s"'),
120 htmlspecialchars($_REQUEST['field'])
122 echo '</h3>';
124 echo '<input type="hidden" name="field" value="'
125 , htmlspecialchars($_REQUEST['field']) , '" />';
126 // The input field to which the final result should be added
127 // and corresponding null checkbox
128 if (isset($_REQUEST['input_name'])) {
129 echo '<input type="hidden" name="input_name" value="'
130 , htmlspecialchars($_REQUEST['input_name']) , '" />';
132 echo URL::getHiddenInputs();
134 echo '<!-- Visualization section -->';
135 echo '<div id="placeholder" style="width:450px;height:300px;'
136 , ($srid != 0 ? 'display:none;' : '') , '">';
137 echo $visualization;
138 echo '</div>';
140 echo '<div id="openlayersmap" style="width:450px;height:300px;'
141 , ($srid == 0 ? 'display:none;' : '') , '">';
142 echo '</div>';
144 echo '<div class="choice" style="float:right;clear:right;">';
145 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
146 , ($srid != 0 ? ' checked="checked"' : '') , '/>';
147 echo '<label for="choice">' , __("Use OpenStreetMaps as Base Layer") , '</label>';
148 echo '</div>';
150 echo '<script language="javascript" type="text/javascript">';
151 echo $open_layers;
152 echo '</script>';
153 echo '<!-- End of visualization section -->';
156 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
157 echo '<div id="gis_data_header">';
158 echo '<select name="gis_data[gis_type]" class="gis_type">';
159 foreach ($gis_types as $gis_type) {
160 echo '<option value="' , $gis_type , '"';
161 if ($geom_type == $gis_type) {
162 echo ' selected="selected"';
164 echo '>' , $gis_type , '</option>';
166 echo '</select>';
167 echo '&nbsp;&nbsp;&nbsp;&nbsp;';
168 /* l10n: Spatial Reference System Identifier */
169 echo '<label for="srid">' , __('SRID:') , '</label>';
170 echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
171 echo '</div>';
172 echo '<!-- End of header section -->';
174 echo '<!-- Data section -->';
175 echo '<div id="gis_data">';
177 $geom_count = 1;
178 if ($geom_type == 'GEOMETRYCOLLECTION') {
179 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
180 ? intval($gis_data[$geom_type]['geom_count']) : 1;
181 if (isset($gis_data[$geom_type]['add_geom'])) {
182 $geom_count++;
184 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
185 , ' value="' , $geom_count , '" />';
188 for ($a = 0; $a < $geom_count; $a++) {
189 if (! isset($gis_data[$a])) {
190 continue;
193 if ($geom_type == 'GEOMETRYCOLLECTION') {
194 echo '<br/><br/>';
195 printf(__('Geometry %d:'), $a + 1);
196 echo '<br/>';
197 if (isset($gis_data[$a]['gis_type'])) {
198 $type = htmlspecialchars($gis_data[$a]['gis_type']);
199 } else {
200 $type = $gis_types[0];
202 echo '<select name="gis_data[' , $a , '][gis_type]" class="gis_type">';
203 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
204 echo '<option value="' , $gis_type , '"';
205 if ($type == $gis_type) {
206 echo ' selected="selected"';
208 echo '>' , $gis_type , '</option>';
210 echo '</select>';
211 } else {
212 $type = $geom_type;
215 if ($type == 'POINT') {
216 echo '<br/>';
217 echo __('Point:');
218 echo '<label for="x">' , __("X") , '</label>';
219 echo '<input name="gis_data[' , $a , '][POINT][x]" type="text"'
220 , ' value="' , escape($gis_data[$a]['POINT']['x']) , '" />';
221 echo '<label for="y">' , __("Y") , '</label>';
222 echo '<input name="gis_data[' , $a , '][POINT][y]" type="text"'
223 , ' value="' , escape($gis_data[$a]['POINT']['y']) , '" />';
225 } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
226 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
227 ? intval($gis_data[$a][$type]['no_of_points']) : 1;
228 if ($type == 'LINESTRING' && $no_of_points < 2) {
229 $no_of_points = 2;
231 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
232 $no_of_points = 1;
235 if (isset($gis_data[$a][$type]['add_point'])) {
236 $no_of_points++;
238 echo '<input type="hidden" value="' , $no_of_points , '"'
239 , ' name="gis_data[' , $a , '][' , $type , '][no_of_points]" />';
241 for ($i = 0; $i < $no_of_points; $i++) {
242 echo '<br/>';
243 printf(__('Point %d'), $i + 1);
244 echo ': ';
245 echo '<label for="x">' , __("X") , '</label>';
246 echo '<input type="text"'
247 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][x]"'
248 , ' value="' , escape($gis_data[$a][$type][$i]['x']) , '" />';
249 echo '<label for="y">' , __("Y") , '</label>';
250 echo '<input type="text"'
251 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][y]"'
252 , ' value="' , escape($gis_data[$a][$type][$i]['y']) , '" />';
254 echo '<input type="submit"'
255 , ' name="gis_data[' , $a , '][' , $type , '][add_point]"'
256 , ' class="add addPoint" value="' , __("Add a point") , '" />';
258 } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
259 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
260 ? intval($gis_data[$a][$type]['no_of_lines']) : 1;
261 if ($no_of_lines < 1) {
262 $no_of_lines = 1;
264 if (isset($gis_data[$a][$type]['add_line'])) {
265 $no_of_lines++;
267 echo '<input type="hidden" value="' , $no_of_lines , '"'
268 , ' name="gis_data[' , $a , '][' , $type , '][no_of_lines]" />';
270 for ($i = 0; $i < $no_of_lines; $i++) {
271 echo '<br/>';
272 if ($type == 'MULTILINESTRING') {
273 printf(__('Linestring %d:'), $i + 1);
274 } else {
275 if ($i == 0) {
276 echo __('Outer ring:');
277 } else {
278 printf(__('Inner ring %d:'), $i);
282 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
283 ? intval($gis_data[$a][$type][$i]['no_of_points']) : 2;
284 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
285 $no_of_points = 2;
287 if ($type == 'POLYGON' && $no_of_points < 4) {
288 $no_of_points = 4;
290 if (isset($gis_data[$a][$type][$i]['add_point'])) {
291 $no_of_points++;
293 echo '<input type="hidden" value="' , $no_of_points , '"'
294 , ' name="gis_data[' , $a , '][' , $type , '][' , $i
295 , '][no_of_points]" />';
297 for ($j = 0; $j < $no_of_points; $j++) {
298 echo('<br/>');
299 printf(__('Point %d'), $j + 1);
300 echo ': ';
301 echo '<label for="x">' , __("X") , '</label>';
302 echo '<input type="text" name="gis_data[' , $a , '][' , $type . ']['
303 , $i , '][' , $j , '][x]" value="'
304 , escape($gis_data[$a][$type][$i][$j]['x']) , '" />';
305 echo '<label for="y">' , __("Y") , '</label>';
306 echo '<input type="text" name="gis_data[' , $a , '][' , $type , ']['
307 , $i , '][' , $j , '][y]"' , ' value="'
308 , escape($gis_data[$a][$type][$i][$j]['y']) , '" />';
310 echo '<input type="submit" name="gis_data[' , $a , '][' , $type , ']['
311 , $i , '][add_point]"'
312 , ' class="add addPoint" value="' , __("Add a point") , '" />';
314 $caption = ($type == 'MULTILINESTRING')
315 ? __('Add a linestring')
316 : __('Add an inner ring');
317 echo '<br/>';
318 echo '<input type="submit"'
319 , ' name="gis_data[' , $a , '][' , $type , '][add_line]"'
320 , ' class="add addLine" value="' , $caption , '" />';
322 } elseif ($type == 'MULTIPOLYGON') {
323 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
324 ? intval($gis_data[$a][$type]['no_of_polygons']) : 1;
325 if ($no_of_polygons < 1) {
326 $no_of_polygons = 1;
328 if (isset($gis_data[$a][$type]['add_polygon'])) {
329 $no_of_polygons++;
331 echo '<input type="hidden"'
332 , ' name="gis_data[' , $a , '][' , $type , '][no_of_polygons]"'
333 , ' value="' , $no_of_polygons , '" />';
335 for ($k = 0; $k < $no_of_polygons; $k++) {
336 echo '<br/>';
337 printf(__('Polygon %d:'), $k + 1);
338 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
339 ? intval($gis_data[$a][$type][$k]['no_of_lines']) : 1;
340 if ($no_of_lines < 1) {
341 $no_of_lines = 1;
343 if (isset($gis_data[$a][$type][$k]['add_line'])) {
344 $no_of_lines++;
346 echo '<input type="hidden"'
347 , ' name="gis_data[' , $a , '][' , $type , '][' , $k
348 , '][no_of_lines]"' , ' value="' , $no_of_lines , '" />';
350 for ($i = 0; $i < $no_of_lines; $i++) {
351 echo '<br/><br/>';
352 if ($i == 0) {
353 echo __('Outer ring:');
354 } else {
355 printf(__('Inner ring %d:'), $i);
358 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
359 ? intval($gis_data[$a][$type][$k][$i]['no_of_points']) : 4;
360 if ($no_of_points < 4) {
361 $no_of_points = 4;
363 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
364 $no_of_points++;
366 echo '<input type="hidden"'
367 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
368 , '][no_of_points]"' , ' value="' , $no_of_points , '" />';
370 for ($j = 0; $j < $no_of_points; $j++) {
371 echo '<br/>';
372 printf(__('Point %d'), $j + 1);
373 echo ': ';
374 echo '<label for="x">' , __("X") , '</label>';
375 echo '<input type="text"'
376 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
377 , $i , '][' , $j , '][x]"'
378 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['x'])
379 , '" />';
380 echo '<label for="y">' , __("Y") , '</label>';
381 echo '<input type="text"'
382 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
383 , $i , '][' , $j , '][y]"'
384 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['y'])
385 , '" />';
387 echo '<input type="submit"'
388 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
389 , '][add_point]"'
390 , ' class="add addPoint" value="' , __("Add a point") , '" />';
392 echo '<br/>';
393 echo '<input type="submit"'
394 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
395 , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
396 echo '<br/>';
398 echo '<br/>';
399 echo '<input type="submit"'
400 , ' name="gis_data[' , $a , '][' , $type , '][add_polygon]"'
401 , ' class="add addPolygon" value="' , __('Add a polygon') , '" />';
404 if ($geom_type == 'GEOMETRYCOLLECTION') {
405 echo '<br/><br/>';
406 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
407 , 'class="add addGeom" value="' , __("Add geometry") , '" />';
409 echo '</div>';
410 echo '<!-- End of data section -->';
412 echo '<br/>';
413 echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';
415 echo '<div id="gis_data_output">';
416 echo '<h3>' , __('Output') , '</h3>';
417 echo '<p>';
418 echo __(
419 'Choose "GeomFromText" from the "Function" column and paste the'
420 . ' string below into the "Value" field.'
422 echo '</p>';
423 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
424 echo htmlspecialchars($result);
425 echo '</textarea>';
426 echo '</div>';
428 echo '</div>';
429 echo '</form>';
431 Response::getInstance()->addJSON('gis_editor', ob_get_contents());
432 ob_end_clean();