Translated using Weblate (Polish)
[phpmyadmin.git] / gis_data_editor.php
blob4eb7dd165c7fe8883c685d29513d150330efc6a7
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 escaped variable
17 function escape($variable)
19 return isset($variable) ? htmlspecialchars($variable) : '';
22 require_once 'libraries/common.inc.php';
23 require_once 'libraries/gis/pma_gis_factory.php';
24 require_once 'libraries/gis/pma_gis_visualization.php';
25 require_once 'libraries/tbl_gis_visualization.lib.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 // Extract type from the initial call and make sure that it's a valid one.
44 // Extract from field's values if availbale, if not use the column type passed.
45 if (! isset($gis_data['gis_type'])) {
46 if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
47 $gis_data['gis_type'] = strtoupper($_REQUEST['type']);
49 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
50 $start = (substr($_REQUEST['value'], 0, 1) == "'") ? 1 : 0;
51 $gis_data['gis_type'] = substr(
52 $_REQUEST['value'], $start, strpos($_REQUEST['value'], "(") - $start
55 if ((! isset($gis_data['gis_type']))
56 || (! in_array($gis_data['gis_type'], $gis_types))
57 ) {
58 $gis_data['gis_type'] = $gis_types[0];
61 $geom_type = $gis_data['gis_type'];
63 // Generate parameters from value passed.
64 $gis_obj = PMA_GIS_Factory::factory($geom_type);
65 if (isset($_REQUEST['value'])) {
66 $gis_data = array_merge(
67 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
71 // Generate Well Known Text
72 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
73 ? htmlspecialchars($gis_data['srid']) : 0;
74 $wkt = $gis_obj->generateWkt($gis_data, 0);
75 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
76 $result = "'" . $wkt . "'," . $srid;
78 // Generate PNG or SVG based visualization
79 $format = (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER <= 8)
80 ? 'png' : 'svg';
81 $visualizationSettings = array(
82 'width' => 450,
83 'height' => 300,
84 'spatialColumn' => 'wkt'
86 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
87 $visualization = PMA_GIS_visualizationResults(
88 $data, $visualizationSettings, $format
90 $open_layers = PMA_GIS_visualizationResults($data, $visualizationSettings, 'ol');
92 // If the call is to update the WKT and visualization make an AJAX response
93 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
94 $extra_data = array(
95 'result' => $result,
96 'visualization' => $visualization,
97 'openLayers' => $open_layers,
99 $response = PMA_Response::getInstance();
100 $response->addJSON($extra_data);
101 exit;
104 ob_start();
106 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
107 echo '<input type="hidden" id="pmaThemeImage"'
108 . ' value="' . $GLOBALS['pmaThemeImage'] . '" />';
109 echo '<div id="gis_data_editor">';
111 echo '<h3>';
112 printf(
113 __('Value for the column "%s"'),
114 htmlspecialchars($_REQUEST['field'])
116 echo '</h3>';
118 echo '<input type="hidden" name="field" value="'
119 . htmlspecialchars($_REQUEST['field']) . '" />';
120 // The input field to which the final result should be added
121 // and corresponding null checkbox
122 if (isset($_REQUEST['input_name'])) {
123 echo '<input type="hidden" name="input_name" value="'
124 . htmlspecialchars($_REQUEST['input_name']) . '" />';
126 echo PMA_URL_getHiddenInputs();
128 echo '<!-- Visualization section -->';
129 echo '<div id="placeholder" style="width:450px;height:300px;'
130 . ($srid != 0 ? 'display:none;' : '') . '">';
131 echo $visualization;
132 echo '</div>';
134 echo '<div id="openlayersmap" style="width:450px;height:300px;'
135 . ($srid == 0 ? 'display:none;' : '') . '">';
136 echo '</div>';
138 echo '<div class="choice" style="float:right;clear:right;">';
139 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
140 . ($srid != 0 ? ' checked="checked"' : '') . '/>';
141 echo '<label for="choice">' . __("Use OpenStreetMaps as Base Layer") . '</label>';
142 echo '</div>';
144 echo '<script language="javascript" type="text/javascript">';
145 echo $open_layers;
146 echo '</script>';
147 echo '<!-- End of visualization section -->';
150 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
151 echo '<div id="gis_data_header">';
152 echo '<select name="gis_data[gis_type]" class="gis_type">';
153 foreach ($gis_types as $gis_type) {
154 echo '<option value="' . $gis_type . '"';
155 if ($geom_type == $gis_type) {
156 echo ' selected="selected"';
158 echo '>' . $gis_type . '</option>';
160 echo '</select>';
161 echo '&nbsp;&nbsp;&nbsp;&nbsp;';
162 /* l10n: Spatial Reference System Identifier */
163 echo '<label for="srid">' . __('SRID:') . '</label>';
164 echo '<input name="gis_data[srid]" type="text" value="' . $srid . '" />';
165 echo '</div>';
166 echo '<!-- End of header section -->';
168 echo '<!-- Data section -->';
169 echo '<div id="gis_data">';
171 $geom_count = 1;
172 if ($geom_type == 'GEOMETRYCOLLECTION') {
173 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
174 ? $gis_data[$geom_type]['geom_count'] : 1;
175 if (isset($gis_data[$geom_type]['add_geom'])) {
176 $geom_count++;
178 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
179 . ' value="' . $geom_count . '" />';
182 for ($a = 0; $a < $geom_count; $a++) {
184 if ($geom_type == 'GEOMETRYCOLLECTION') {
185 echo '<br/><br/>';
186 printf(__('Geometry %d:'), $a + 1);
187 echo '<br/>';
188 if (isset($gis_data[$a]['gis_type'])) {
189 $type = $gis_data[$a]['gis_type'];
190 } else {
191 $type = $gis_types[0];
193 echo '<select name="gis_data[' . $a . '][gis_type]" class="gis_type">';
194 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
195 echo '<option value="' . $gis_type . '"';
196 if ($type == $gis_type) {
197 echo ' selected="selected"';
199 echo '>' . $gis_type . '</option>';
201 echo '</select>';
202 } else {
203 $type = $geom_type;
206 if ($type == 'POINT') {
207 echo '<br/>';
208 echo __('Point:');
209 echo '<label for="x">' . __("X") . '</label>';
210 echo '<input name="gis_data[' . $a . '][POINT][x]" type="text"'
211 . ' value="' . escape($gis_data[$a]['POINT']['x']) . '" />';
212 echo '<label for="y">' . __("Y") . '</label>';
213 echo '<input name="gis_data[' . $a . '][POINT][y]" type="text"'
214 . ' value="' . escape($gis_data[$a]['POINT']['y']) . '" />';
216 } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
217 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
218 ? $gis_data[$a][$type]['no_of_points'] : 1;
219 if ($type == 'LINESTRING' && $no_of_points < 2) {
220 $no_of_points = 2;
222 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
223 $no_of_points = 1;
226 if (isset($gis_data[$a][$type]['add_point'])) {
227 $no_of_points++;
229 echo '<input type="hidden" value="' . $no_of_points . '"'
230 . ' name="gis_data[' . $a . '][' . $type . '][no_of_points]" />';
232 for ($i = 0; $i < $no_of_points; $i++) {
233 echo '<br/>';
234 printf(__('Point %d'), $i + 1);
235 echo ': ';
236 echo '<label for="x">' . __("X") . '</label>';
237 echo '<input type="text"'
238 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][x]"'
239 . ' value="' . escape($gis_data[$a][$type][$i]['x']) . '" />';
240 echo '<label for="y">' . __("Y") . '</label>';
241 echo '<input type="text"'
242 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][y]"'
243 . ' value="' . escape($gis_data[$a][$type][$i]['y']). '" />';
245 echo '<input type="submit"'
246 . ' name="gis_data[' . $a . '][' . $type . '][add_point]"'
247 . ' class="add addPoint" value="' . __("Add a point") . '" />';
249 } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
250 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
251 ? $gis_data[$a][$type]['no_of_lines'] : 1;
252 if ($no_of_lines < 1) {
253 $no_of_lines = 1;
255 if (isset($gis_data[$a][$type]['add_line'])) {
256 $no_of_lines++;
258 echo '<input type="hidden" value="' . $no_of_lines . '"'
259 . ' name="gis_data[' . $a . '][' . $type . '][no_of_lines]" />';
261 for ($i = 0; $i < $no_of_lines; $i++) {
262 echo '<br/>';
263 if ($type == 'MULTILINESTRING') {
264 printf(__('Linestring %d:'), $i + 1);
265 } else {
266 if ($i == 0) {
267 echo __('Outer ring:');
268 } else {
269 printf(__('Inner ring %d:'), $i);
273 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
274 ? $gis_data[$a][$type][$i]['no_of_points'] : 2;
275 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
276 $no_of_points = 2;
278 if ($type == 'POLYGON' && $no_of_points < 4) {
279 $no_of_points = 4;
281 if (isset($gis_data[$a][$type][$i]['add_point'])) {
282 $no_of_points++;
284 echo '<input type="hidden" value="' . $no_of_points . '"'
285 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][no_of_points]" />';
287 for ($j = 0; $j < $no_of_points; $j++) {
288 echo('<br/>');
289 printf(__('Point %d'), $j + 1);
290 echo ': ';
291 echo '<label for="x">' . __("X") . '</label>';
292 echo '<input type="text"'
293 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][' . $j . '][x]"'
294 . ' value="' . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
295 echo '<label for="y">' . __("Y") . '</label>';
296 echo '<input type="text"'
297 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][' . $j . '][y]"'
298 . ' value="' . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
300 echo '<input type="submit"'
301 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][add_point]"'
302 . ' class="add addPoint" value="' . __("Add a point") . '" />';
304 $caption = ($type == 'MULTILINESTRING')
305 ? __('Add a linestring')
306 : __('Add an inner ring');
307 echo '<br/>';
308 echo '<input type="submit"'
309 . ' name="gis_data[' . $a . '][' . $type . '][add_line]"'
310 . ' class="add addLine" value="' . $caption . '" />';
312 } elseif ($type == 'MULTIPOLYGON') {
313 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
314 ? $gis_data[$a][$type]['no_of_polygons'] : 1;
315 if ($no_of_polygons < 1) {
316 $no_of_polygons = 1;
318 if (isset($gis_data[$a][$type]['add_polygon'])) {
319 $no_of_polygons++;
321 echo '<input type="hidden"'
322 . ' name="gis_data[' . $a . '][' . $type . '][no_of_polygons]"'
323 . ' value="' . $no_of_polygons . '" />';
325 for ($k = 0; $k < $no_of_polygons; $k++) {
326 echo '<br/>';
327 printf(__('Polygon %d:'), $k + 1);
328 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
329 ? $gis_data[$a][$type][$k]['no_of_lines'] : 1;
330 if ($no_of_lines < 1) {
331 $no_of_lines = 1;
333 if (isset($gis_data[$a][$type][$k]['add_line'])) {
334 $no_of_lines++;
336 echo '<input type="hidden"'
337 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][no_of_lines]"'
338 . ' value="' . $no_of_lines . '" />';
340 for ($i = 0; $i < $no_of_lines; $i++) {
341 echo '<br/><br/>';
342 if ($i == 0) {
343 echo __('Outer ring:');
344 } else {
345 printf(__('Inner ring %d:'), $i);
348 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
349 ? $gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
350 if ($no_of_points < 4) {
351 $no_of_points = 4;
353 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
354 $no_of_points++;
356 echo '<input type="hidden"'
357 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][no_of_points]"'
358 . ' value="' . $no_of_points . '" />';
360 for ($j = 0; $j < $no_of_points; $j++) {
361 echo '<br/>';
362 printf(__('Point %d'), $j + 1);
363 echo ': ';
364 echo '<label for="x">' . __("X") . '</label>';
365 echo '<input type="text"'
366 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][' . $j . '][x]"'
367 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['x']). '" />';
368 echo '<label for="y">' . __("Y") . '</label>';
369 echo '<input type="text"'
370 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][' . $j . '][y]"'
371 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['y']) . '" />';
373 echo '<input type="submit"'
374 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][add_point]"'
375 . ' class="add addPoint" value="' . __("Add a point") . '" />';
377 echo '<br/>';
378 echo '<input type="submit"'
379 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][add_line]"'
380 . ' class="add addLine" value="' . __('Add an inner ring'). '" />';
381 echo '<br/>';
383 echo '<br/>';
384 echo '<input type="submit"'
385 . ' name="gis_data[' . $a . '][' . $type . '][add_polygon]"'
386 . ' class="add addPolygon" value="' . __('Add a polygon') . '" />';
389 if ($geom_type == 'GEOMETRYCOLLECTION') {
390 echo '<br/><br/>';
391 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
392 . 'class="add addGeom" value="' . __("Add geometry") . '" />';
394 echo '</div>';
395 echo '<!-- End of data section -->';
397 echo '<br/>';
398 echo '<input type="submit" name="gis_data[save]" value="' . __('Go') . '" />';
400 echo '<div id="gis_data_output">';
401 echo '<h3>' . __('Output') . '</h3>';
402 echo '<p>';
403 echo __(
404 'Choose "GeomFromText" from the "Function" column and paste the'
405 . ' string below into the "Value" field'
407 echo '</p>';
408 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
409 echo $result;
410 echo '</textarea>';
411 echo '</div>';
413 echo '</div>';
414 echo '</form>';
416 PMA_Response::getInstance()->addJSON('gis_editor', ob_get_contents());
417 ob_end_clean();