Translated using Weblate (German)
[phpmyadmin.git] / gis_data_editor.php
blob9af4d7fdc49af12a1a1b36feee032f116e83511a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Editor for Geometry data types.
6 * @package PhpMyAdmin
7 */
9 /**
10 * Escapes special characters if the variable is set.
11 * Returns an empty string otherwise.
13 * @param string $variable variable to be escaped
15 * @return string escaped variable
17 function escape($variable)
19 return isset($variable) ? htmlspecialchars($variable) : '';
22 require_once 'libraries/common.inc.php';
23 require_once 'libraries/gis/GIS_Factory.class.php';
24 require_once 'libraries/gis/GIS_Visualization.class.php';
26 // Get data if any posted
27 $gis_data = array();
28 if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
29 $gis_data = $_REQUEST['gis_data'];
32 $gis_types = array(
33 'POINT',
34 'MULTIPOINT',
35 'LINESTRING',
36 'MULTILINESTRING',
37 'POLYGON',
38 'MULTIPOLYGON',
39 'GEOMETRYCOLLECTION'
42 /** @var PMA_String $pmaString */
43 $pmaString = $GLOBALS['PMA_String'];
45 // Extract type from the initial call and make sure that it's a valid one.
46 // Extract from field's values if available, if not use the column type passed.
47 if (! isset($gis_data['gis_type'])) {
48 if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
49 $gis_data['gis_type'] = /*overload*/mb_strtoupper($_REQUEST['type']);
51 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
52 $start = (substr($_REQUEST['value'], 0, 1) == "'") ? 1 : 0;
53 $gis_data['gis_type'] = /*overload*/mb_substr(
54 $_REQUEST['value'],
55 $start,
56 /*overload*/mb_strpos($_REQUEST['value'], "(") - $start
59 if ((! isset($gis_data['gis_type']))
60 || (! in_array($gis_data['gis_type'], $gis_types))
61 ) {
62 $gis_data['gis_type'] = $gis_types[0];
65 $geom_type = $gis_data['gis_type'];
67 // Generate parameters from value passed.
68 $gis_obj = PMA_GIS_Factory::factory($geom_type);
69 if (isset($_REQUEST['value'])) {
70 $gis_data = array_merge(
71 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
75 // Generate Well Known Text
76 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
77 ? htmlspecialchars($gis_data['srid']) : 0;
78 $wkt = $gis_obj->generateWkt($gis_data, 0);
79 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
80 $result = "'" . $wkt . "'," . $srid;
82 // Generate PNG or SVG based visualization
83 $format = (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER <= 8)
84 ? 'png' : 'svg';
85 $visualizationSettings = array(
86 'width' => 450,
87 'height' => 300,
88 'spatialColumn' => 'wkt'
90 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
91 $visualization = PMA_GIS_Visualization::getByData($data, $visualizationSettings)->toImage($format);
93 $open_layers = PMA_GIS_Visualization::getByData($data, $visualizationSettings)->asOl();
95 // If the call is to update the WKT and visualization make an AJAX response
96 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
97 $extra_data = array(
98 'result' => $result,
99 'visualization' => $visualization,
100 'openLayers' => $open_layers,
102 $response = PMA_Response::getInstance();
103 $response->addJSON($extra_data);
104 exit;
107 ob_start();
109 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
110 echo '<input type="hidden" id="pmaThemeImage"'
111 . ' value="' . $GLOBALS['pmaThemeImage'] . '" />';
112 echo '<div id="gis_data_editor">';
114 echo '<h3>';
115 printf(
116 __('Value for the column "%s"'),
117 htmlspecialchars($_REQUEST['field'])
119 echo '</h3>';
121 echo '<input type="hidden" name="field" value="'
122 . htmlspecialchars($_REQUEST['field']) . '" />';
123 // The input field to which the final result should be added
124 // and corresponding null checkbox
125 if (isset($_REQUEST['input_name'])) {
126 echo '<input type="hidden" name="input_name" value="'
127 . htmlspecialchars($_REQUEST['input_name']) . '" />';
129 echo PMA_URL_getHiddenInputs();
131 echo '<!-- Visualization section -->';
132 echo '<div id="placeholder" style="width:450px;height:300px;'
133 . ($srid != 0 ? 'display:none;' : '') . '">';
134 echo $visualization;
135 echo '</div>';
137 echo '<div id="openlayersmap" style="width:450px;height:300px;'
138 . ($srid == 0 ? 'display:none;' : '') . '">';
139 echo '</div>';
141 echo '<div class="choice" style="float:right;clear:right;">';
142 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
143 . ($srid != 0 ? ' checked="checked"' : '') . '/>';
144 echo '<label for="choice">' . __("Use OpenStreetMaps as Base Layer") . '</label>';
145 echo '</div>';
147 echo '<script language="javascript" type="text/javascript">';
148 echo $open_layers;
149 echo '</script>';
150 echo '<!-- End of visualization section -->';
153 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
154 echo '<div id="gis_data_header">';
155 echo '<select name="gis_data[gis_type]" class="gis_type">';
156 foreach ($gis_types as $gis_type) {
157 echo '<option value="' . $gis_type . '"';
158 if ($geom_type == $gis_type) {
159 echo ' selected="selected"';
161 echo '>' . $gis_type . '</option>';
163 echo '</select>';
164 echo '&nbsp;&nbsp;&nbsp;&nbsp;';
165 /* l10n: Spatial Reference System Identifier */
166 echo '<label for="srid">' . __('SRID:') . '</label>';
167 echo '<input name="gis_data[srid]" type="text" value="' . $srid . '" />';
168 echo '</div>';
169 echo '<!-- End of header section -->';
171 echo '<!-- Data section -->';
172 echo '<div id="gis_data">';
174 $geom_count = 1;
175 if ($geom_type == 'GEOMETRYCOLLECTION') {
176 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
177 ? $gis_data[$geom_type]['geom_count'] : 1;
178 if (isset($gis_data[$geom_type]['add_geom'])) {
179 $geom_count++;
181 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
182 . ' value="' . $geom_count . '" />';
185 for ($a = 0; $a < $geom_count; $a++) {
187 if ($geom_type == 'GEOMETRYCOLLECTION') {
188 echo '<br/><br/>';
189 printf(__('Geometry %d:'), $a + 1);
190 echo '<br/>';
191 if (isset($gis_data[$a]['gis_type'])) {
192 $type = $gis_data[$a]['gis_type'];
193 } else {
194 $type = $gis_types[0];
196 echo '<select name="gis_data[' . $a . '][gis_type]" class="gis_type">';
197 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
198 echo '<option value="' . $gis_type . '"';
199 if ($type == $gis_type) {
200 echo ' selected="selected"';
202 echo '>' . $gis_type . '</option>';
204 echo '</select>';
205 } else {
206 $type = $geom_type;
209 if ($type == 'POINT') {
210 echo '<br/>';
211 echo __('Point:');
212 echo '<label for="x">' . __("X") . '</label>';
213 echo '<input name="gis_data[' . $a . '][POINT][x]" type="text"'
214 . ' value="' . escape($gis_data[$a]['POINT']['x']) . '" />';
215 echo '<label for="y">' . __("Y") . '</label>';
216 echo '<input name="gis_data[' . $a . '][POINT][y]" type="text"'
217 . ' value="' . escape($gis_data[$a]['POINT']['y']) . '" />';
219 } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
220 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
221 ? $gis_data[$a][$type]['no_of_points'] : 1;
222 if ($type == 'LINESTRING' && $no_of_points < 2) {
223 $no_of_points = 2;
225 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
226 $no_of_points = 1;
229 if (isset($gis_data[$a][$type]['add_point'])) {
230 $no_of_points++;
232 echo '<input type="hidden" value="' . $no_of_points . '"'
233 . ' name="gis_data[' . $a . '][' . $type . '][no_of_points]" />';
235 for ($i = 0; $i < $no_of_points; $i++) {
236 echo '<br/>';
237 printf(__('Point %d'), $i + 1);
238 echo ': ';
239 echo '<label for="x">' . __("X") . '</label>';
240 echo '<input type="text"'
241 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][x]"'
242 . ' value="' . escape($gis_data[$a][$type][$i]['x']) . '" />';
243 echo '<label for="y">' . __("Y") . '</label>';
244 echo '<input type="text"'
245 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][y]"'
246 . ' value="' . escape($gis_data[$a][$type][$i]['y']) . '" />';
248 echo '<input type="submit"'
249 . ' name="gis_data[' . $a . '][' . $type . '][add_point]"'
250 . ' class="add addPoint" value="' . __("Add a point") . '" />';
252 } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
253 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
254 ? $gis_data[$a][$type]['no_of_lines'] : 1;
255 if ($no_of_lines < 1) {
256 $no_of_lines = 1;
258 if (isset($gis_data[$a][$type]['add_line'])) {
259 $no_of_lines++;
261 echo '<input type="hidden" value="' . $no_of_lines . '"'
262 . ' name="gis_data[' . $a . '][' . $type . '][no_of_lines]" />';
264 for ($i = 0; $i < $no_of_lines; $i++) {
265 echo '<br/>';
266 if ($type == 'MULTILINESTRING') {
267 printf(__('Linestring %d:'), $i + 1);
268 } else {
269 if ($i == 0) {
270 echo __('Outer ring:');
271 } else {
272 printf(__('Inner ring %d:'), $i);
276 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
277 ? $gis_data[$a][$type][$i]['no_of_points'] : 2;
278 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
279 $no_of_points = 2;
281 if ($type == 'POLYGON' && $no_of_points < 4) {
282 $no_of_points = 4;
284 if (isset($gis_data[$a][$type][$i]['add_point'])) {
285 $no_of_points++;
287 echo '<input type="hidden" value="' . $no_of_points . '"'
288 . ' name="gis_data[' . $a . '][' . $type . '][' . $i
289 . '][no_of_points]" />';
291 for ($j = 0; $j < $no_of_points; $j++) {
292 echo('<br/>');
293 printf(__('Point %d'), $j + 1);
294 echo ': ';
295 echo '<label for="x">' . __("X") . '</label>';
296 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
297 . $i . '][' . $j . '][x]" value="'
298 . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
299 echo '<label for="y">' . __("Y") . '</label>';
300 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
301 . $i . '][' . $j . '][y]"' . ' value="'
302 . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
304 echo '<input type="submit" name="gis_data[' . $a . '][' . $type . ']['
305 . $i . '][add_point]"'
306 . ' class="add addPoint" value="' . __("Add a point") . '" />';
308 $caption = ($type == 'MULTILINESTRING')
309 ? __('Add a linestring')
310 : __('Add an inner ring');
311 echo '<br/>';
312 echo '<input type="submit"'
313 . ' name="gis_data[' . $a . '][' . $type . '][add_line]"'
314 . ' class="add addLine" value="' . $caption . '" />';
316 } elseif ($type == 'MULTIPOLYGON') {
317 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
318 ? $gis_data[$a][$type]['no_of_polygons'] : 1;
319 if ($no_of_polygons < 1) {
320 $no_of_polygons = 1;
322 if (isset($gis_data[$a][$type]['add_polygon'])) {
323 $no_of_polygons++;
325 echo '<input type="hidden"'
326 . ' name="gis_data[' . $a . '][' . $type . '][no_of_polygons]"'
327 . ' value="' . $no_of_polygons . '" />';
329 for ($k = 0; $k < $no_of_polygons; $k++) {
330 echo '<br/>';
331 printf(__('Polygon %d:'), $k + 1);
332 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
333 ? $gis_data[$a][$type][$k]['no_of_lines'] : 1;
334 if ($no_of_lines < 1) {
335 $no_of_lines = 1;
337 if (isset($gis_data[$a][$type][$k]['add_line'])) {
338 $no_of_lines++;
340 echo '<input type="hidden"'
341 . ' name="gis_data[' . $a . '][' . $type . '][' . $k
342 . '][no_of_lines]"' . ' value="' . $no_of_lines . '" />';
344 for ($i = 0; $i < $no_of_lines; $i++) {
345 echo '<br/><br/>';
346 if ($i == 0) {
347 echo __('Outer ring:');
348 } else {
349 printf(__('Inner ring %d:'), $i);
352 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
353 ? $gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
354 if ($no_of_points < 4) {
355 $no_of_points = 4;
357 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
358 $no_of_points++;
360 echo '<input type="hidden"'
361 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
362 . '][no_of_points]"' . ' value="' . $no_of_points . '" />';
364 for ($j = 0; $j < $no_of_points; $j++) {
365 echo '<br/>';
366 printf(__('Point %d'), $j + 1);
367 echo ': ';
368 echo '<label for="x">' . __("X") . '</label>';
369 echo '<input type="text"'
370 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
371 . $i . '][' . $j . '][x]"'
372 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['x'])
373 . '" />';
374 echo '<label for="y">' . __("Y") . '</label>';
375 echo '<input type="text"'
376 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
377 . $i . '][' . $j . '][y]"'
378 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['y'])
379 . '" />';
381 echo '<input type="submit"'
382 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
383 . '][add_point]"'
384 . ' class="add addPoint" value="' . __("Add a point") . '" />';
386 echo '<br/>';
387 echo '<input type="submit"'
388 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][add_line]"'
389 . ' class="add addLine" value="' . __('Add an inner ring') . '" />';
390 echo '<br/>';
392 echo '<br/>';
393 echo '<input type="submit"'
394 . ' name="gis_data[' . $a . '][' . $type . '][add_polygon]"'
395 . ' class="add addPolygon" value="' . __('Add a polygon') . '" />';
398 if ($geom_type == 'GEOMETRYCOLLECTION') {
399 echo '<br/><br/>';
400 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
401 . 'class="add addGeom" value="' . __("Add geometry") . '" />';
403 echo '</div>';
404 echo '<!-- End of data section -->';
406 echo '<br/>';
407 echo '<input type="submit" name="gis_data[save]" value="' . __('Go') . '" />';
409 echo '<div id="gis_data_output">';
410 echo '<h3>' . __('Output') . '</h3>';
411 echo '<p>';
412 echo __(
413 'Choose "GeomFromText" from the "Function" column and paste the'
414 . ' string below into the "Value" field.'
416 echo '</p>';
417 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
418 echo $result;
419 echo '</textarea>';
420 echo '</div>';
422 echo '</div>';
423 echo '</form>';
425 PMA_Response::getInstance()->addJSON('gis_editor', ob_get_contents());
426 ob_end_clean();