Translated using Weblate (Russian)
[phpmyadmin.git] / gis_data_editor.php
blob16dd46763afdeb9f7df196739d9c26d8be02ce28
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)
92 ->toImage($format);
94 $open_layers = PMA_GIS_Visualization::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_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 PMA_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++) {
189 if ($geom_type == 'GEOMETRYCOLLECTION') {
190 echo '<br/><br/>';
191 printf(__('Geometry %d:'), $a + 1);
192 echo '<br/>';
193 if (isset($gis_data[$a]['gis_type'])) {
194 $type = $gis_data[$a]['gis_type'];
195 } else {
196 $type = $gis_types[0];
198 echo '<select name="gis_data[' . $a . '][gis_type]" class="gis_type">';
199 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
200 echo '<option value="' . $gis_type . '"';
201 if ($type == $gis_type) {
202 echo ' selected="selected"';
204 echo '>' . $gis_type . '</option>';
206 echo '</select>';
207 } else {
208 $type = $geom_type;
211 if ($type == 'POINT') {
212 echo '<br/>';
213 echo __('Point:');
214 echo '<label for="x">' . __("X") . '</label>';
215 echo '<input name="gis_data[' . $a . '][POINT][x]" type="text"'
216 . ' value="' . escape($gis_data[$a]['POINT']['x']) . '" />';
217 echo '<label for="y">' . __("Y") . '</label>';
218 echo '<input name="gis_data[' . $a . '][POINT][y]" type="text"'
219 . ' value="' . escape($gis_data[$a]['POINT']['y']) . '" />';
221 } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
222 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
223 ? $gis_data[$a][$type]['no_of_points'] : 1;
224 if ($type == 'LINESTRING' && $no_of_points < 2) {
225 $no_of_points = 2;
227 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
228 $no_of_points = 1;
231 if (isset($gis_data[$a][$type]['add_point'])) {
232 $no_of_points++;
234 echo '<input type="hidden" value="' . $no_of_points . '"'
235 . ' name="gis_data[' . $a . '][' . $type . '][no_of_points]" />';
237 for ($i = 0; $i < $no_of_points; $i++) {
238 echo '<br/>';
239 printf(__('Point %d'), $i + 1);
240 echo ': ';
241 echo '<label for="x">' . __("X") . '</label>';
242 echo '<input type="text"'
243 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][x]"'
244 . ' value="' . escape($gis_data[$a][$type][$i]['x']) . '" />';
245 echo '<label for="y">' . __("Y") . '</label>';
246 echo '<input type="text"'
247 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][y]"'
248 . ' value="' . escape($gis_data[$a][$type][$i]['y']) . '" />';
250 echo '<input type="submit"'
251 . ' name="gis_data[' . $a . '][' . $type . '][add_point]"'
252 . ' class="add addPoint" value="' . __("Add a point") . '" />';
254 } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
255 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
256 ? $gis_data[$a][$type]['no_of_lines'] : 1;
257 if ($no_of_lines < 1) {
258 $no_of_lines = 1;
260 if (isset($gis_data[$a][$type]['add_line'])) {
261 $no_of_lines++;
263 echo '<input type="hidden" value="' . $no_of_lines . '"'
264 . ' name="gis_data[' . $a . '][' . $type . '][no_of_lines]" />';
266 for ($i = 0; $i < $no_of_lines; $i++) {
267 echo '<br/>';
268 if ($type == 'MULTILINESTRING') {
269 printf(__('Linestring %d:'), $i + 1);
270 } else {
271 if ($i == 0) {
272 echo __('Outer ring:');
273 } else {
274 printf(__('Inner ring %d:'), $i);
278 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
279 ? $gis_data[$a][$type][$i]['no_of_points'] : 2;
280 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
281 $no_of_points = 2;
283 if ($type == 'POLYGON' && $no_of_points < 4) {
284 $no_of_points = 4;
286 if (isset($gis_data[$a][$type][$i]['add_point'])) {
287 $no_of_points++;
289 echo '<input type="hidden" value="' . $no_of_points . '"'
290 . ' name="gis_data[' . $a . '][' . $type . '][' . $i
291 . '][no_of_points]" />';
293 for ($j = 0; $j < $no_of_points; $j++) {
294 echo('<br/>');
295 printf(__('Point %d'), $j + 1);
296 echo ': ';
297 echo '<label for="x">' . __("X") . '</label>';
298 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
299 . $i . '][' . $j . '][x]" value="'
300 . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
301 echo '<label for="y">' . __("Y") . '</label>';
302 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
303 . $i . '][' . $j . '][y]"' . ' value="'
304 . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
306 echo '<input type="submit" name="gis_data[' . $a . '][' . $type . ']['
307 . $i . '][add_point]"'
308 . ' class="add addPoint" value="' . __("Add a point") . '" />';
310 $caption = ($type == 'MULTILINESTRING')
311 ? __('Add a linestring')
312 : __('Add an inner ring');
313 echo '<br/>';
314 echo '<input type="submit"'
315 . ' name="gis_data[' . $a . '][' . $type . '][add_line]"'
316 . ' class="add addLine" value="' . $caption . '" />';
318 } elseif ($type == 'MULTIPOLYGON') {
319 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
320 ? $gis_data[$a][$type]['no_of_polygons'] : 1;
321 if ($no_of_polygons < 1) {
322 $no_of_polygons = 1;
324 if (isset($gis_data[$a][$type]['add_polygon'])) {
325 $no_of_polygons++;
327 echo '<input type="hidden"'
328 . ' name="gis_data[' . $a . '][' . $type . '][no_of_polygons]"'
329 . ' value="' . $no_of_polygons . '" />';
331 for ($k = 0; $k < $no_of_polygons; $k++) {
332 echo '<br/>';
333 printf(__('Polygon %d:'), $k + 1);
334 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
335 ? $gis_data[$a][$type][$k]['no_of_lines'] : 1;
336 if ($no_of_lines < 1) {
337 $no_of_lines = 1;
339 if (isset($gis_data[$a][$type][$k]['add_line'])) {
340 $no_of_lines++;
342 echo '<input type="hidden"'
343 . ' name="gis_data[' . $a . '][' . $type . '][' . $k
344 . '][no_of_lines]"' . ' value="' . $no_of_lines . '" />';
346 for ($i = 0; $i < $no_of_lines; $i++) {
347 echo '<br/><br/>';
348 if ($i == 0) {
349 echo __('Outer ring:');
350 } else {
351 printf(__('Inner ring %d:'), $i);
354 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
355 ? $gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
356 if ($no_of_points < 4) {
357 $no_of_points = 4;
359 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
360 $no_of_points++;
362 echo '<input type="hidden"'
363 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
364 . '][no_of_points]"' . ' value="' . $no_of_points . '" />';
366 for ($j = 0; $j < $no_of_points; $j++) {
367 echo '<br/>';
368 printf(__('Point %d'), $j + 1);
369 echo ': ';
370 echo '<label for="x">' . __("X") . '</label>';
371 echo '<input type="text"'
372 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
373 . $i . '][' . $j . '][x]"'
374 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['x'])
375 . '" />';
376 echo '<label for="y">' . __("Y") . '</label>';
377 echo '<input type="text"'
378 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
379 . $i . '][' . $j . '][y]"'
380 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['y'])
381 . '" />';
383 echo '<input type="submit"'
384 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
385 . '][add_point]"'
386 . ' class="add addPoint" value="' . __("Add a point") . '" />';
388 echo '<br/>';
389 echo '<input type="submit"'
390 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][add_line]"'
391 . ' class="add addLine" value="' . __('Add an inner ring') . '" />';
392 echo '<br/>';
394 echo '<br/>';
395 echo '<input type="submit"'
396 . ' name="gis_data[' . $a . '][' . $type . '][add_polygon]"'
397 . ' class="add addPolygon" value="' . __('Add a polygon') . '" />';
400 if ($geom_type == 'GEOMETRYCOLLECTION') {
401 echo '<br/><br/>';
402 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
403 . 'class="add addGeom" value="' . __("Add geometry") . '" />';
405 echo '</div>';
406 echo '<!-- End of data section -->';
408 echo '<br/>';
409 echo '<input type="submit" name="gis_data[save]" value="' . __('Go') . '" />';
411 echo '<div id="gis_data_output">';
412 echo '<h3>' . __('Output') . '</h3>';
413 echo '<p>';
414 echo __(
415 'Choose "GeomFromText" from the "Function" column and paste the'
416 . ' string below into the "Value" field.'
418 echo '</p>';
419 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
420 echo $result;
421 echo '</textarea>';
422 echo '</div>';
424 echo '</div>';
425 echo '</form>';
427 PMA_Response::getInstance()->addJSON('gis_editor', ob_get_contents());
428 ob_end_clean();