Translated using Weblate (Slovenian)
[phpmyadmin.git] / gis_data_editor.php
blob190a802c10c00b830615c3257e912b1c1fd9deff
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Editor for Geometry data types.
6 * @package PhpMyAdmin
7 */
8 declare(strict_types=1);
10 use PhpMyAdmin\Core;
11 use PhpMyAdmin\Gis\GisFactory;
12 use PhpMyAdmin\Gis\GisVisualization;
13 use PhpMyAdmin\Response;
14 use PhpMyAdmin\Url;
16 /**
17 * Escapes special characters if the variable is set.
18 * Returns an empty string otherwise.
20 * @param string $variable variable to be escaped
22 * @return string escaped variable
24 function escape($variable)
26 return isset($variable) ? htmlspecialchars($variable) : '';
29 require_once 'libraries/common.inc.php';
31 if (! isset($_REQUEST['field'])) {
32 PhpMyAdmin\Util::checkParameters(array('field'));
35 // Get data if any posted
36 $gis_data = array();
37 if (Core::isValid($_REQUEST['gis_data'], 'array')) {
38 $gis_data = $_REQUEST['gis_data'];
41 $gis_types = array(
42 'POINT',
43 'MULTIPOINT',
44 'LINESTRING',
45 'MULTILINESTRING',
46 'POLYGON',
47 'MULTIPOLYGON',
48 'GEOMETRYCOLLECTION'
51 // Extract type from the initial call and make sure that it's a valid one.
52 // Extract from field's values if available, if not use the column type passed.
53 if (! isset($gis_data['gis_type'])) {
54 if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
55 $gis_data['gis_type'] = mb_strtoupper($_REQUEST['type']);
57 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
58 $start = (substr($_REQUEST['value'], 0, 1) == "'") ? 1 : 0;
59 $gis_data['gis_type'] = mb_substr(
60 $_REQUEST['value'],
61 $start,
62 mb_strpos($_REQUEST['value'], "(") - $start
65 if ((! isset($gis_data['gis_type']))
66 || (! in_array($gis_data['gis_type'], $gis_types))
67 ) {
68 $gis_data['gis_type'] = $gis_types[0];
71 $geom_type = htmlspecialchars($gis_data['gis_type']);
73 // Generate parameters from value passed.
74 $gis_obj = GisFactory::factory($geom_type);
75 if (isset($_REQUEST['value'])) {
76 $gis_data = array_merge(
77 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
81 // Generate Well Known Text
82 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
83 ? htmlspecialchars($gis_data['srid']) : 0;
84 $wkt = $gis_obj->generateWkt($gis_data, 0);
85 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
86 $result = "'" . $wkt . "'," . $srid;
88 // Generate SVG based visualization
89 $visualizationSettings = array(
90 'width' => 450,
91 'height' => 300,
92 'spatialColumn' => 'wkt'
94 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
95 $visualization = GisVisualization::getByData($data, $visualizationSettings)
96 ->toImage('svg');
98 $open_layers = GisVisualization::getByData($data, $visualizationSettings)
99 ->asOl();
101 // If the call is to update the WKT and visualization make an AJAX response
102 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
103 $extra_data = array(
104 'result' => $result,
105 'visualization' => $visualization,
106 'openLayers' => $open_layers,
108 $response = Response::getInstance();
109 $response->addJSON($extra_data);
110 exit;
113 ob_start();
115 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
116 echo '<input type="hidden" id="pmaThemeImage"'
117 , ' value="' , $GLOBALS['pmaThemeImage'] , '" />';
118 echo '<div id="gis_data_editor">';
120 echo '<h3>';
121 printf(
122 __('Value for the column "%s"'),
123 htmlspecialchars($_REQUEST['field'])
125 echo '</h3>';
127 echo '<input type="hidden" name="field" value="'
128 , htmlspecialchars($_REQUEST['field']) , '" />';
129 // The input field to which the final result should be added
130 // and corresponding null checkbox
131 if (isset($_REQUEST['input_name'])) {
132 echo '<input type="hidden" name="input_name" value="'
133 , htmlspecialchars($_REQUEST['input_name']) , '" />';
135 echo Url::getHiddenInputs();
137 echo '<!-- Visualization section -->';
138 echo '<div id="placeholder" '
139 , ($srid != 0 ? 'class="hide' : '') , '">';
140 echo $visualization;
141 echo '</div>';
143 echo '<div id="openlayersmap" '
144 , ($srid == 0 ? 'class="hide' : '') , '">';
145 echo '</div>';
147 echo '<div class="choice floatright">';
148 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
149 , ($srid != 0 ? ' checked="checked"' : '') , '/>';
150 echo '<label for="choice">' , __("Use OpenStreetMaps as Base Layer") , '</label>';
151 echo '</div>';
153 echo '<script language="javascript" type="text/javascript">';
154 echo $open_layers;
155 echo '</script>';
156 echo '<!-- End of visualization section -->';
159 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
160 echo '<div id="gis_data_header">';
161 echo '<select name="gis_data[gis_type]" class="gis_type">';
162 foreach ($gis_types as $gis_type) {
163 echo '<option value="' , $gis_type , '"';
164 if ($geom_type == $gis_type) {
165 echo ' selected="selected"';
167 echo '>' , $gis_type , '</option>';
169 echo '</select>';
170 echo '&nbsp;&nbsp;&nbsp;&nbsp;';
171 /* l10n: Spatial Reference System Identifier */
172 echo '<label for="srid">' , __('SRID:') , '</label>';
173 echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
174 echo '</div>';
175 echo '<!-- End of header section -->';
177 echo '<!-- Data section -->';
178 echo '<div id="gis_data">';
180 $geom_count = 1;
181 if ($geom_type == 'GEOMETRYCOLLECTION') {
182 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
183 ? intval($gis_data[$geom_type]['geom_count']) : 1;
184 if (isset($gis_data[$geom_type]['add_geom'])) {
185 $geom_count++;
187 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
188 , ' value="' , $geom_count , '" />';
191 for ($a = 0; $a < $geom_count; $a++) {
192 if (! isset($gis_data[$a])) {
193 continue;
196 if ($geom_type == 'GEOMETRYCOLLECTION') {
197 echo '<br/><br/>';
198 printf(__('Geometry %d:'), $a + 1);
199 echo '<br/>';
200 if (isset($gis_data[$a]['gis_type'])) {
201 $type = htmlspecialchars($gis_data[$a]['gis_type']);
202 } else {
203 $type = $gis_types[0];
205 echo '<select name="gis_data[' , $a , '][gis_type]" class="gis_type">';
206 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
207 echo '<option value="' , $gis_type , '"';
208 if ($type == $gis_type) {
209 echo ' selected="selected"';
211 echo '>' , $gis_type , '</option>';
213 echo '</select>';
214 } else {
215 $type = $geom_type;
218 if ($type == 'POINT') {
219 echo '<br/>';
220 echo __('Point:');
221 echo '<label for="x">' , __("X") , '</label>';
222 echo '<input name="gis_data[' , $a , '][POINT][x]" type="text"'
223 , ' value="' , escape($gis_data[$a]['POINT']['x']) , '" />';
224 echo '<label for="y">' , __("Y") , '</label>';
225 echo '<input name="gis_data[' , $a , '][POINT][y]" type="text"'
226 , ' value="' , escape($gis_data[$a]['POINT']['y']) , '" />';
228 } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
229 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
230 ? intval($gis_data[$a][$type]['no_of_points']) : 1;
231 if ($type == 'LINESTRING' && $no_of_points < 2) {
232 $no_of_points = 2;
234 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
235 $no_of_points = 1;
238 if (isset($gis_data[$a][$type]['add_point'])) {
239 $no_of_points++;
241 echo '<input type="hidden" value="' , $no_of_points , '"'
242 , ' name="gis_data[' , $a , '][' , $type , '][no_of_points]" />';
244 for ($i = 0; $i < $no_of_points; $i++) {
245 echo '<br/>';
246 printf(__('Point %d'), $i + 1);
247 echo ': ';
248 echo '<label for="x">' , __("X") , '</label>';
249 echo '<input type="text"'
250 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][x]"'
251 , ' value="' , escape($gis_data[$a][$type][$i]['x']) , '" />';
252 echo '<label for="y">' , __("Y") , '</label>';
253 echo '<input type="text"'
254 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][y]"'
255 , ' value="' , escape($gis_data[$a][$type][$i]['y']) , '" />';
257 echo '<input type="submit"'
258 , ' name="gis_data[' , $a , '][' , $type , '][add_point]"'
259 , ' class="add addPoint" value="' , __("Add a point") , '" />';
261 } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
262 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
263 ? intval($gis_data[$a][$type]['no_of_lines']) : 1;
264 if ($no_of_lines < 1) {
265 $no_of_lines = 1;
267 if (isset($gis_data[$a][$type]['add_line'])) {
268 $no_of_lines++;
270 echo '<input type="hidden" value="' , $no_of_lines , '"'
271 , ' name="gis_data[' , $a , '][' , $type , '][no_of_lines]" />';
273 for ($i = 0; $i < $no_of_lines; $i++) {
274 echo '<br/>';
275 if ($type == 'MULTILINESTRING') {
276 printf(__('Linestring %d:'), $i + 1);
277 } else {
278 if ($i == 0) {
279 echo __('Outer ring:');
280 } else {
281 printf(__('Inner ring %d:'), $i);
285 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
286 ? intval($gis_data[$a][$type][$i]['no_of_points']) : 2;
287 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
288 $no_of_points = 2;
290 if ($type == 'POLYGON' && $no_of_points < 4) {
291 $no_of_points = 4;
293 if (isset($gis_data[$a][$type][$i]['add_point'])) {
294 $no_of_points++;
296 echo '<input type="hidden" value="' , $no_of_points , '"'
297 , ' name="gis_data[' , $a , '][' , $type , '][' , $i
298 , '][no_of_points]" />';
300 for ($j = 0; $j < $no_of_points; $j++) {
301 echo('<br/>');
302 printf(__('Point %d'), $j + 1);
303 echo ': ';
304 echo '<label for="x">' , __("X") , '</label>';
305 echo '<input type="text" name="gis_data[' , $a , '][' , $type . ']['
306 , $i , '][' , $j , '][x]" value="'
307 , escape($gis_data[$a][$type][$i][$j]['x']) , '" />';
308 echo '<label for="y">' , __("Y") , '</label>';
309 echo '<input type="text" name="gis_data[' , $a , '][' , $type , ']['
310 , $i , '][' , $j , '][y]"' , ' value="'
311 , escape($gis_data[$a][$type][$i][$j]['y']) , '" />';
313 echo '<input type="submit" name="gis_data[' , $a , '][' , $type , ']['
314 , $i , '][add_point]"'
315 , ' class="add addPoint" value="' , __("Add a point") , '" />';
317 $caption = ($type == 'MULTILINESTRING')
318 ? __('Add a linestring')
319 : __('Add an inner ring');
320 echo '<br/>';
321 echo '<input type="submit"'
322 , ' name="gis_data[' , $a , '][' , $type , '][add_line]"'
323 , ' class="add addLine" value="' , $caption , '" />';
325 } elseif ($type == 'MULTIPOLYGON') {
326 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
327 ? intval($gis_data[$a][$type]['no_of_polygons']) : 1;
328 if ($no_of_polygons < 1) {
329 $no_of_polygons = 1;
331 if (isset($gis_data[$a][$type]['add_polygon'])) {
332 $no_of_polygons++;
334 echo '<input type="hidden"'
335 , ' name="gis_data[' , $a , '][' , $type , '][no_of_polygons]"'
336 , ' value="' , $no_of_polygons , '" />';
338 for ($k = 0; $k < $no_of_polygons; $k++) {
339 echo '<br/>';
340 printf(__('Polygon %d:'), $k + 1);
341 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
342 ? intval($gis_data[$a][$type][$k]['no_of_lines']) : 1;
343 if ($no_of_lines < 1) {
344 $no_of_lines = 1;
346 if (isset($gis_data[$a][$type][$k]['add_line'])) {
347 $no_of_lines++;
349 echo '<input type="hidden"'
350 , ' name="gis_data[' , $a , '][' , $type , '][' , $k
351 , '][no_of_lines]"' , ' value="' , $no_of_lines , '" />';
353 for ($i = 0; $i < $no_of_lines; $i++) {
354 echo '<br/><br/>';
355 if ($i == 0) {
356 echo __('Outer ring:');
357 } else {
358 printf(__('Inner ring %d:'), $i);
361 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
362 ? intval($gis_data[$a][$type][$k][$i]['no_of_points']) : 4;
363 if ($no_of_points < 4) {
364 $no_of_points = 4;
366 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
367 $no_of_points++;
369 echo '<input type="hidden"'
370 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
371 , '][no_of_points]"' , ' value="' , $no_of_points , '" />';
373 for ($j = 0; $j < $no_of_points; $j++) {
374 echo '<br/>';
375 printf(__('Point %d'), $j + 1);
376 echo ': ';
377 echo '<label for="x">' , __("X") , '</label>';
378 echo '<input type="text"'
379 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
380 , $i , '][' , $j , '][x]"'
381 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['x'])
382 , '" />';
383 echo '<label for="y">' , __("Y") , '</label>';
384 echo '<input type="text"'
385 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
386 , $i , '][' , $j , '][y]"'
387 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['y'])
388 , '" />';
390 echo '<input type="submit"'
391 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
392 , '][add_point]"'
393 , ' class="add addPoint" value="' , __("Add a point") , '" />';
395 echo '<br/>';
396 echo '<input type="submit"'
397 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
398 , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
399 echo '<br/>';
401 echo '<br/>';
402 echo '<input type="submit"'
403 , ' name="gis_data[' , $a , '][' , $type , '][add_polygon]"'
404 , ' class="add addPolygon" value="' , __('Add a polygon') , '" />';
407 if ($geom_type == 'GEOMETRYCOLLECTION') {
408 echo '<br/><br/>';
409 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
410 , 'class="add addGeom" value="' , __("Add geometry") , '" />';
412 echo '</div>';
413 echo '<!-- End of data section -->';
415 echo '<br/>';
416 echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';
418 echo '<div id="gis_data_output">';
419 echo '<h3>' , __('Output') , '</h3>';
420 echo '<p>';
421 echo __(
422 'Choose "GeomFromText" from the "Function" column and paste the'
423 . ' string below into the "Value" field.'
425 echo '</p>';
426 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
427 echo htmlspecialchars($result);
428 echo '</textarea>';
429 echo '</div>';
431 echo '</div>';
432 echo '</form>';
434 Response::getInstance()->addJSON('gis_editor', ob_get_contents());
435 ob_end_clean();