Translated using Weblate (Slovenian)
[phpmyadmin.git] / gis_data_editor.php
blob8f710703d8b978c1a1ceec1ac8e35c8592dcc628
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\URL;
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 if (! isset($_REQUEST['field'])) {
28 PMA\libraries\Util::checkParameters(array('field'));
31 // Get data if any posted
32 $gis_data = array();
33 if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
34 $gis_data = $_REQUEST['gis_data'];
37 $gis_types = array(
38 'POINT',
39 'MULTIPOINT',
40 'LINESTRING',
41 'MULTILINESTRING',
42 'POLYGON',
43 'MULTIPOLYGON',
44 'GEOMETRYCOLLECTION'
47 // Extract type from the initial call and make sure that it's a valid one.
48 // Extract from field's values if available, if not use the column type passed.
49 if (! isset($gis_data['gis_type'])) {
50 if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
51 $gis_data['gis_type'] = mb_strtoupper($_REQUEST['type']);
53 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
54 $start = (substr($_REQUEST['value'], 0, 1) == "'") ? 1 : 0;
55 $gis_data['gis_type'] = mb_substr(
56 $_REQUEST['value'],
57 $start,
58 mb_strpos($_REQUEST['value'], "(") - $start
61 if ((! isset($gis_data['gis_type']))
62 || (! in_array($gis_data['gis_type'], $gis_types))
63 ) {
64 $gis_data['gis_type'] = $gis_types[0];
67 $geom_type = $gis_data['gis_type'];
69 // Generate parameters from value passed.
70 $gis_obj = GISFactory::factory($geom_type);
71 if (isset($_REQUEST['value'])) {
72 $gis_data = array_merge(
73 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
77 // Generate Well Known Text
78 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
79 ? htmlspecialchars($gis_data['srid']) : 0;
80 $wkt = $gis_obj->generateWkt($gis_data, 0);
81 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
82 $result = "'" . $wkt . "'," . $srid;
84 // Generate SVG based visualization
85 $visualizationSettings = array(
86 'width' => 450,
87 'height' => 300,
88 'spatialColumn' => 'wkt'
90 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
91 $visualization = GISVisualization::getByData($data, $visualizationSettings)
92 ->toImage('svg');
94 $open_layers = GISVisualization::getByData($data, $visualizationSettings)
95 ->asOl();
97 // If the call is to update the WKT and visualization make an AJAX response
98 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
99 $extra_data = array(
100 'result' => $result,
101 'visualization' => $visualization,
102 'openLayers' => $open_layers,
104 $response = PMA\libraries\Response::getInstance();
105 $response->addJSON($extra_data);
106 exit;
109 ob_start();
111 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
112 echo '<input type="hidden" id="pmaThemeImage"'
113 , ' value="' , $GLOBALS['pmaThemeImage'] , '" />';
114 echo '<div id="gis_data_editor">';
116 echo '<h3>';
117 printf(
118 __('Value for the column "%s"'),
119 htmlspecialchars($_REQUEST['field'])
121 echo '</h3>';
123 echo '<input type="hidden" name="field" value="'
124 , htmlspecialchars($_REQUEST['field']) , '" />';
125 // The input field to which the final result should be added
126 // and corresponding null checkbox
127 if (isset($_REQUEST['input_name'])) {
128 echo '<input type="hidden" name="input_name" value="'
129 , htmlspecialchars($_REQUEST['input_name']) , '" />';
131 echo URL::getHiddenInputs();
133 echo '<!-- Visualization section -->';
134 echo '<div id="placeholder" style="width:450px;height:300px;'
135 , ($srid != 0 ? 'display:none;' : '') , '">';
136 echo $visualization;
137 echo '</div>';
139 echo '<div id="openlayersmap" style="width:450px;height:300px;'
140 , ($srid == 0 ? 'display:none;' : '') , '">';
141 echo '</div>';
143 echo '<div class="choice" style="float:right;clear:right;">';
144 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
145 , ($srid != 0 ? ' checked="checked"' : '') , '/>';
146 echo '<label for="choice">' , __("Use OpenStreetMaps as Base Layer") , '</label>';
147 echo '</div>';
149 echo '<script language="javascript" type="text/javascript">';
150 echo $open_layers;
151 echo '</script>';
152 echo '<!-- End of visualization section -->';
155 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
156 echo '<div id="gis_data_header">';
157 echo '<select name="gis_data[gis_type]" class="gis_type">';
158 foreach ($gis_types as $gis_type) {
159 echo '<option value="' , $gis_type , '"';
160 if ($geom_type == $gis_type) {
161 echo ' selected="selected"';
163 echo '>' , $gis_type , '</option>';
165 echo '</select>';
166 echo '&nbsp;&nbsp;&nbsp;&nbsp;';
167 /* l10n: Spatial Reference System Identifier */
168 echo '<label for="srid">' , __('SRID:') , '</label>';
169 echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
170 echo '</div>';
171 echo '<!-- End of header section -->';
173 echo '<!-- Data section -->';
174 echo '<div id="gis_data">';
176 $geom_count = 1;
177 if ($geom_type == 'GEOMETRYCOLLECTION') {
178 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
179 ? $gis_data[$geom_type]['geom_count'] : 1;
180 if (isset($gis_data[$geom_type]['add_geom'])) {
181 $geom_count++;
183 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
184 , ' value="' , $geom_count , '" />';
187 for ($a = 0; $a < $geom_count; $a++) {
188 if (! isset($gis_data[$a])) {
189 continue;
192 if ($geom_type == 'GEOMETRYCOLLECTION') {
193 echo '<br/><br/>';
194 printf(__('Geometry %d:'), $a + 1);
195 echo '<br/>';
196 if (isset($gis_data[$a]['gis_type'])) {
197 $type = $gis_data[$a]['gis_type'];
198 } else {
199 $type = $gis_types[0];
201 echo '<select name="gis_data[' , $a , '][gis_type]" class="gis_type">';
202 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
203 echo '<option value="' , $gis_type , '"';
204 if ($type == $gis_type) {
205 echo ' selected="selected"';
207 echo '>' , $gis_type , '</option>';
209 echo '</select>';
210 } else {
211 $type = $geom_type;
214 if ($type == 'POINT') {
215 echo '<br/>';
216 echo __('Point:');
217 echo '<label for="x">' , __("X") , '</label>';
218 echo '<input name="gis_data[' , $a , '][POINT][x]" type="text"'
219 , ' value="' , escape($gis_data[$a]['POINT']['x']) , '" />';
220 echo '<label for="y">' , __("Y") , '</label>';
221 echo '<input name="gis_data[' , $a , '][POINT][y]" type="text"'
222 , ' value="' , escape($gis_data[$a]['POINT']['y']) , '" />';
224 } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
225 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
226 ? $gis_data[$a][$type]['no_of_points'] : 1;
227 if ($type == 'LINESTRING' && $no_of_points < 2) {
228 $no_of_points = 2;
230 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
231 $no_of_points = 1;
234 if (isset($gis_data[$a][$type]['add_point'])) {
235 $no_of_points++;
237 echo '<input type="hidden" value="' , $no_of_points , '"'
238 , ' name="gis_data[' , $a , '][' , $type , '][no_of_points]" />';
240 for ($i = 0; $i < $no_of_points; $i++) {
241 echo '<br/>';
242 printf(__('Point %d'), $i + 1);
243 echo ': ';
244 echo '<label for="x">' , __("X") , '</label>';
245 echo '<input type="text"'
246 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][x]"'
247 , ' value="' , escape($gis_data[$a][$type][$i]['x']) , '" />';
248 echo '<label for="y">' , __("Y") , '</label>';
249 echo '<input type="text"'
250 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][y]"'
251 , ' value="' , escape($gis_data[$a][$type][$i]['y']) , '" />';
253 echo '<input type="submit"'
254 , ' name="gis_data[' , $a , '][' , $type , '][add_point]"'
255 , ' class="add addPoint" value="' , __("Add a point") , '" />';
257 } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
258 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
259 ? $gis_data[$a][$type]['no_of_lines'] : 1;
260 if ($no_of_lines < 1) {
261 $no_of_lines = 1;
263 if (isset($gis_data[$a][$type]['add_line'])) {
264 $no_of_lines++;
266 echo '<input type="hidden" value="' , $no_of_lines , '"'
267 , ' name="gis_data[' , $a , '][' , $type , '][no_of_lines]" />';
269 for ($i = 0; $i < $no_of_lines; $i++) {
270 echo '<br/>';
271 if ($type == 'MULTILINESTRING') {
272 printf(__('Linestring %d:'), $i + 1);
273 } else {
274 if ($i == 0) {
275 echo __('Outer ring:');
276 } else {
277 printf(__('Inner ring %d:'), $i);
281 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
282 ? $gis_data[$a][$type][$i]['no_of_points'] : 2;
283 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
284 $no_of_points = 2;
286 if ($type == 'POLYGON' && $no_of_points < 4) {
287 $no_of_points = 4;
289 if (isset($gis_data[$a][$type][$i]['add_point'])) {
290 $no_of_points++;
292 echo '<input type="hidden" value="' , $no_of_points , '"'
293 , ' name="gis_data[' , $a , '][' , $type , '][' , $i
294 , '][no_of_points]" />';
296 for ($j = 0; $j < $no_of_points; $j++) {
297 echo('<br/>');
298 printf(__('Point %d'), $j + 1);
299 echo ': ';
300 echo '<label for="x">' , __("X") , '</label>';
301 echo '<input type="text" name="gis_data[' , $a , '][' , $type . ']['
302 , $i , '][' , $j , '][x]" value="'
303 , escape($gis_data[$a][$type][$i][$j]['x']) , '" />';
304 echo '<label for="y">' , __("Y") , '</label>';
305 echo '<input type="text" name="gis_data[' , $a , '][' , $type , ']['
306 , $i , '][' , $j , '][y]"' , ' value="'
307 , escape($gis_data[$a][$type][$i][$j]['y']) , '" />';
309 echo '<input type="submit" name="gis_data[' , $a , '][' , $type , ']['
310 , $i , '][add_point]"'
311 , ' class="add addPoint" value="' , __("Add a point") , '" />';
313 $caption = ($type == 'MULTILINESTRING')
314 ? __('Add a linestring')
315 : __('Add an inner ring');
316 echo '<br/>';
317 echo '<input type="submit"'
318 , ' name="gis_data[' , $a , '][' , $type , '][add_line]"'
319 , ' class="add addLine" value="' , $caption , '" />';
321 } elseif ($type == 'MULTIPOLYGON') {
322 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
323 ? $gis_data[$a][$type]['no_of_polygons'] : 1;
324 if ($no_of_polygons < 1) {
325 $no_of_polygons = 1;
327 if (isset($gis_data[$a][$type]['add_polygon'])) {
328 $no_of_polygons++;
330 echo '<input type="hidden"'
331 , ' name="gis_data[' , $a , '][' , $type , '][no_of_polygons]"'
332 , ' value="' , $no_of_polygons , '" />';
334 for ($k = 0; $k < $no_of_polygons; $k++) {
335 echo '<br/>';
336 printf(__('Polygon %d:'), $k + 1);
337 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
338 ? $gis_data[$a][$type][$k]['no_of_lines'] : 1;
339 if ($no_of_lines < 1) {
340 $no_of_lines = 1;
342 if (isset($gis_data[$a][$type][$k]['add_line'])) {
343 $no_of_lines++;
345 echo '<input type="hidden"'
346 , ' name="gis_data[' , $a , '][' , $type , '][' , $k
347 , '][no_of_lines]"' , ' value="' , $no_of_lines , '" />';
349 for ($i = 0; $i < $no_of_lines; $i++) {
350 echo '<br/><br/>';
351 if ($i == 0) {
352 echo __('Outer ring:');
353 } else {
354 printf(__('Inner ring %d:'), $i);
357 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
358 ? $gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
359 if ($no_of_points < 4) {
360 $no_of_points = 4;
362 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
363 $no_of_points++;
365 echo '<input type="hidden"'
366 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
367 , '][no_of_points]"' , ' value="' , $no_of_points , '" />';
369 for ($j = 0; $j < $no_of_points; $j++) {
370 echo '<br/>';
371 printf(__('Point %d'), $j + 1);
372 echo ': ';
373 echo '<label for="x">' , __("X") , '</label>';
374 echo '<input type="text"'
375 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
376 , $i , '][' , $j , '][x]"'
377 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['x'])
378 , '" />';
379 echo '<label for="y">' , __("Y") , '</label>';
380 echo '<input type="text"'
381 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
382 , $i , '][' , $j , '][y]"'
383 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['y'])
384 , '" />';
386 echo '<input type="submit"'
387 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
388 , '][add_point]"'
389 , ' class="add addPoint" value="' , __("Add a point") , '" />';
391 echo '<br/>';
392 echo '<input type="submit"'
393 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
394 , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
395 echo '<br/>';
397 echo '<br/>';
398 echo '<input type="submit"'
399 , ' name="gis_data[' , $a , '][' , $type , '][add_polygon]"'
400 , ' class="add addPolygon" value="' , __('Add a polygon') , '" />';
403 if ($geom_type == 'GEOMETRYCOLLECTION') {
404 echo '<br/><br/>';
405 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
406 , 'class="add addGeom" value="' , __("Add geometry") , '" />';
408 echo '</div>';
409 echo '<!-- End of data section -->';
411 echo '<br/>';
412 echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';
414 echo '<div id="gis_data_output">';
415 echo '<h3>' , __('Output') , '</h3>';
416 echo '<p>';
417 echo __(
418 'Choose "GeomFromText" from the "Function" column and paste the'
419 . ' string below into the "Value" field.'
421 echo '</p>';
422 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
423 echo $result;
424 echo '</textarea>';
425 echo '</div>';
427 echo '</div>';
428 echo '</form>';
430 PMA\libraries\Response::getInstance()->addJSON('gis_editor', ob_get_contents());
431 ob_end_clean();