Translated using Weblate (Slovenian)
[phpmyadmin.git] / gis_data_editor.php
blob61c377bbd68ba05e37f16119c3f3a7fe4155cb8f
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\gis\GISFactory;
9 use PMA\libraries\gis\GISVisualization;
10 use PMA\libraries\PMA_String;
12 /**
13 * Escapes special characters if the variable is set.
14 * Returns an empty string otherwise.
16 * @param string $variable variable to be escaped
18 * @return string escaped variable
20 function escape($variable)
22 return isset($variable) ? htmlspecialchars($variable) : '';
25 require_once 'libraries/common.inc.php';
27 // Get data if any posted
28 $gis_data = array();
29 if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
30 $gis_data = $_REQUEST['gis_data'];
33 $gis_types = array(
34 'POINT',
35 'MULTIPOINT',
36 'LINESTRING',
37 'MULTILINESTRING',
38 'POLYGON',
39 'MULTIPOLYGON',
40 'GEOMETRYCOLLECTION'
43 /** @var String $pmaString */
44 $pmaString = $GLOBALS['PMA_String'];
46 // Extract type from the initial call and make sure that it's a valid one.
47 // Extract from field's values if available, if not use the column type passed.
48 if (! isset($gis_data['gis_type'])) {
49 if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
50 $gis_data['gis_type'] = /*overload*/mb_strtoupper($_REQUEST['type']);
52 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
53 $start = (substr($_REQUEST['value'], 0, 1) == "'") ? 1 : 0;
54 $gis_data['gis_type'] = /*overload*/mb_substr(
55 $_REQUEST['value'],
56 $start,
57 /*overload*/mb_strpos($_REQUEST['value'], "(") - $start
60 if ((! isset($gis_data['gis_type']))
61 || (! in_array($gis_data['gis_type'], $gis_types))
62 ) {
63 $gis_data['gis_type'] = $gis_types[0];
66 $geom_type = $gis_data['gis_type'];
68 // Generate parameters from value passed.
69 $gis_obj = GISFactory::factory($geom_type);
70 if (isset($_REQUEST['value'])) {
71 $gis_data = array_merge(
72 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
76 // Generate Well Known Text
77 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
78 ? htmlspecialchars($gis_data['srid']) : 0;
79 $wkt = $gis_obj->generateWkt($gis_data, 0);
80 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
81 $result = "'" . $wkt . "'," . $srid;
83 // Generate PNG or SVG based visualization
84 $format = (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER <= 8)
85 ? 'png' : 'svg';
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($format);
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 = PMA\libraries\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 PMA_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 ? $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++) {
190 if ($geom_type == 'GEOMETRYCOLLECTION') {
191 echo '<br/><br/>';
192 printf(__('Geometry %d:'), $a + 1);
193 echo '<br/>';
194 if (isset($gis_data[$a]['gis_type'])) {
195 $type = $gis_data[$a]['gis_type'];
196 } else {
197 $type = $gis_types[0];
199 echo '<select name="gis_data[' . $a . '][gis_type]" class="gis_type">';
200 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
201 echo '<option value="' . $gis_type . '"';
202 if ($type == $gis_type) {
203 echo ' selected="selected"';
205 echo '>' . $gis_type . '</option>';
207 echo '</select>';
208 } else {
209 $type = $geom_type;
212 if ($type == 'POINT') {
213 echo '<br/>';
214 echo __('Point:');
215 echo '<label for="x">' . __("X") . '</label>';
216 echo '<input name="gis_data[' . $a . '][POINT][x]" type="text"'
217 . ' value="' . escape($gis_data[$a]['POINT']['x']) . '" />';
218 echo '<label for="y">' . __("Y") . '</label>';
219 echo '<input name="gis_data[' . $a . '][POINT][y]" type="text"'
220 . ' value="' . escape($gis_data[$a]['POINT']['y']) . '" />';
222 } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
223 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
224 ? $gis_data[$a][$type]['no_of_points'] : 1;
225 if ($type == 'LINESTRING' && $no_of_points < 2) {
226 $no_of_points = 2;
228 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
229 $no_of_points = 1;
232 if (isset($gis_data[$a][$type]['add_point'])) {
233 $no_of_points++;
235 echo '<input type="hidden" value="' . $no_of_points . '"'
236 . ' name="gis_data[' . $a . '][' . $type . '][no_of_points]" />';
238 for ($i = 0; $i < $no_of_points; $i++) {
239 echo '<br/>';
240 printf(__('Point %d'), $i + 1);
241 echo ': ';
242 echo '<label for="x">' . __("X") . '</label>';
243 echo '<input type="text"'
244 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][x]"'
245 . ' value="' . escape($gis_data[$a][$type][$i]['x']) . '" />';
246 echo '<label for="y">' . __("Y") . '</label>';
247 echo '<input type="text"'
248 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][y]"'
249 . ' value="' . escape($gis_data[$a][$type][$i]['y']) . '" />';
251 echo '<input type="submit"'
252 . ' name="gis_data[' . $a . '][' . $type . '][add_point]"'
253 . ' class="add addPoint" value="' . __("Add a point") . '" />';
255 } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
256 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
257 ? $gis_data[$a][$type]['no_of_lines'] : 1;
258 if ($no_of_lines < 1) {
259 $no_of_lines = 1;
261 if (isset($gis_data[$a][$type]['add_line'])) {
262 $no_of_lines++;
264 echo '<input type="hidden" value="' . $no_of_lines . '"'
265 . ' name="gis_data[' . $a . '][' . $type . '][no_of_lines]" />';
267 for ($i = 0; $i < $no_of_lines; $i++) {
268 echo '<br/>';
269 if ($type == 'MULTILINESTRING') {
270 printf(__('Linestring %d:'), $i + 1);
271 } else {
272 if ($i == 0) {
273 echo __('Outer ring:');
274 } else {
275 printf(__('Inner ring %d:'), $i);
279 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
280 ? $gis_data[$a][$type][$i]['no_of_points'] : 2;
281 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
282 $no_of_points = 2;
284 if ($type == 'POLYGON' && $no_of_points < 4) {
285 $no_of_points = 4;
287 if (isset($gis_data[$a][$type][$i]['add_point'])) {
288 $no_of_points++;
290 echo '<input type="hidden" value="' . $no_of_points . '"'
291 . ' name="gis_data[' . $a . '][' . $type . '][' . $i
292 . '][no_of_points]" />';
294 for ($j = 0; $j < $no_of_points; $j++) {
295 echo('<br/>');
296 printf(__('Point %d'), $j + 1);
297 echo ': ';
298 echo '<label for="x">' . __("X") . '</label>';
299 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
300 . $i . '][' . $j . '][x]" value="'
301 . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
302 echo '<label for="y">' . __("Y") . '</label>';
303 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
304 . $i . '][' . $j . '][y]"' . ' value="'
305 . escape($gis_data[$a][$type][$i][$j]['y']) . '" />';
307 echo '<input type="submit" name="gis_data[' . $a . '][' . $type . ']['
308 . $i . '][add_point]"'
309 . ' class="add addPoint" value="' . __("Add a point") . '" />';
311 $caption = ($type == 'MULTILINESTRING')
312 ? __('Add a linestring')
313 : __('Add an inner ring');
314 echo '<br/>';
315 echo '<input type="submit"'
316 . ' name="gis_data[' . $a . '][' . $type . '][add_line]"'
317 . ' class="add addLine" value="' . $caption . '" />';
319 } elseif ($type == 'MULTIPOLYGON') {
320 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
321 ? $gis_data[$a][$type]['no_of_polygons'] : 1;
322 if ($no_of_polygons < 1) {
323 $no_of_polygons = 1;
325 if (isset($gis_data[$a][$type]['add_polygon'])) {
326 $no_of_polygons++;
328 echo '<input type="hidden"'
329 . ' name="gis_data[' . $a . '][' . $type . '][no_of_polygons]"'
330 . ' value="' . $no_of_polygons . '" />';
332 for ($k = 0; $k < $no_of_polygons; $k++) {
333 echo '<br/>';
334 printf(__('Polygon %d:'), $k + 1);
335 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
336 ? $gis_data[$a][$type][$k]['no_of_lines'] : 1;
337 if ($no_of_lines < 1) {
338 $no_of_lines = 1;
340 if (isset($gis_data[$a][$type][$k]['add_line'])) {
341 $no_of_lines++;
343 echo '<input type="hidden"'
344 . ' name="gis_data[' . $a . '][' . $type . '][' . $k
345 . '][no_of_lines]"' . ' value="' . $no_of_lines . '" />';
347 for ($i = 0; $i < $no_of_lines; $i++) {
348 echo '<br/><br/>';
349 if ($i == 0) {
350 echo __('Outer ring:');
351 } else {
352 printf(__('Inner ring %d:'), $i);
355 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
356 ? $gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
357 if ($no_of_points < 4) {
358 $no_of_points = 4;
360 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
361 $no_of_points++;
363 echo '<input type="hidden"'
364 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
365 . '][no_of_points]"' . ' value="' . $no_of_points . '" />';
367 for ($j = 0; $j < $no_of_points; $j++) {
368 echo '<br/>';
369 printf(__('Point %d'), $j + 1);
370 echo ': ';
371 echo '<label for="x">' . __("X") . '</label>';
372 echo '<input type="text"'
373 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
374 . $i . '][' . $j . '][x]"'
375 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['x'])
376 . '" />';
377 echo '<label for="y">' . __("Y") . '</label>';
378 echo '<input type="text"'
379 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
380 . $i . '][' . $j . '][y]"'
381 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['y'])
382 . '" />';
384 echo '<input type="submit"'
385 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
386 . '][add_point]"'
387 . ' class="add addPoint" value="' . __("Add a point") . '" />';
389 echo '<br/>';
390 echo '<input type="submit"'
391 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][add_line]"'
392 . ' class="add addLine" value="' . __('Add an inner ring') . '" />';
393 echo '<br/>';
395 echo '<br/>';
396 echo '<input type="submit"'
397 . ' name="gis_data[' . $a . '][' . $type . '][add_polygon]"'
398 . ' class="add addPolygon" value="' . __('Add a polygon') . '" />';
401 if ($geom_type == 'GEOMETRYCOLLECTION') {
402 echo '<br/><br/>';
403 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
404 . 'class="add addGeom" value="' . __("Add geometry") . '" />';
406 echo '</div>';
407 echo '<!-- End of data section -->';
409 echo '<br/>';
410 echo '<input type="submit" name="gis_data[save]" value="' . __('Go') . '" />';
412 echo '<div id="gis_data_output">';
413 echo '<h3>' . __('Output') . '</h3>';
414 echo '<p>';
415 echo __(
416 'Choose "GeomFromText" from the "Function" column and paste the'
417 . ' string below into the "Value" field.'
419 echo '</p>';
420 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
421 echo $result;
422 echo '</textarea>';
423 echo '</div>';
425 echo '</div>';
426 echo '</form>';
428 PMA\libraries\Response::getInstance()->addJSON('gis_editor', ob_get_contents());
429 ob_end_clean();