Translated using Weblate (Kazakh)
[phpmyadmin.git] / gis_data_editor.php
blobab44a800d48e2284a0e8ca14e64afbb0574ae54a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Editor for Geometry data types.
6 * @package PhpMyAdmin
7 */
9 use PhpMyAdmin\Core;
10 use PhpMyAdmin\Gis\GisFactory;
11 use PhpMyAdmin\Gis\GisVisualization;
12 use PhpMyAdmin\Response;
13 use PhpMyAdmin\Url;
15 /**
16 * Escapes special characters if the variable is set.
17 * Returns an empty string otherwise.
19 * @param string $variable variable to be escaped
21 * @return string escaped variable
23 function escape($variable)
25 return isset($variable) ? htmlspecialchars($variable) : '';
28 require_once 'libraries/common.inc.php';
30 if (! isset($_POST['field'])) {
31 PhpMyAdmin\Util::checkParameters(array('field'));
34 // Get data if any posted
35 $gis_data = array();
36 if (Core::isValid($_POST['gis_data'], 'array')) {
37 $gis_data = $_POST['gis_data'];
40 $gis_types = array(
41 'POINT',
42 'MULTIPOINT',
43 'LINESTRING',
44 'MULTILINESTRING',
45 'POLYGON',
46 'MULTIPOLYGON',
47 'GEOMETRYCOLLECTION'
50 // Extract type from the initial call and make sure that it's a valid one.
51 // Extract from field's values if available, if not use the column type passed.
52 if (! isset($gis_data['gis_type'])) {
53 if (isset($_POST['type']) && $_POST['type'] != '') {
54 $gis_data['gis_type'] = mb_strtoupper($_POST['type']);
56 if (isset($_POST['value']) && trim($_POST['value']) != '') {
57 $start = (substr($_POST['value'], 0, 1) == "'") ? 1 : 0;
58 $gis_data['gis_type'] = mb_substr(
59 $_POST['value'],
60 $start,
61 mb_strpos($_POST['value'], "(") - $start
64 if ((! isset($gis_data['gis_type']))
65 || (! in_array($gis_data['gis_type'], $gis_types))
66 ) {
67 $gis_data['gis_type'] = $gis_types[0];
70 $geom_type = htmlspecialchars($gis_data['gis_type']);
72 // Generate parameters from value passed.
73 $gis_obj = GisFactory::factory($geom_type);
74 if (isset($_POST['value'])) {
75 $gis_data = array_merge(
76 $gis_data, $gis_obj->generateParams($_POST['value'])
80 // Generate Well Known Text
81 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
82 ? htmlspecialchars($gis_data['srid']) : 0;
83 $wkt = $gis_obj->generateWkt($gis_data, 0);
84 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
85 $result = "'" . $wkt . "'," . $srid;
87 // Generate SVG based visualization
88 $visualizationSettings = array(
89 'width' => 450,
90 'height' => 300,
91 'spatialColumn' => 'wkt'
93 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
94 $visualization = GisVisualization::getByData($data, $visualizationSettings)
95 ->toImage('svg');
97 $open_layers = GisVisualization::getByData($data, $visualizationSettings)
98 ->asOl();
100 // If the call is to update the WKT and visualization make an AJAX response
101 if (isset($_POST['generate']) && $_POST['generate'] == true) {
102 $extra_data = array(
103 'result' => $result,
104 'visualization' => $visualization,
105 'openLayers' => $open_layers,
107 $response = Response::getInstance();
108 $response->addJSON($extra_data);
109 exit;
112 ob_start();
114 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
115 echo '<input type="hidden" id="pmaThemeImage"'
116 , ' value="' , $GLOBALS['pmaThemeImage'] , '" />';
117 echo '<div id="gis_data_editor">';
119 echo '<h3>';
120 printf(
121 __('Value for the column "%s"'),
122 htmlspecialchars($_POST['field'])
124 echo '</h3>';
126 echo '<input type="hidden" name="field" value="'
127 , htmlspecialchars($_POST['field']) , '" />';
128 // The input field to which the final result should be added
129 // and corresponding null checkbox
130 if (isset($_POST['input_name'])) {
131 echo '<input type="hidden" name="input_name" value="'
132 , htmlspecialchars($_POST['input_name']) , '" />';
134 echo Url::getHiddenInputs();
136 echo '<!-- Visualization section -->';
137 echo '<div id="placeholder"'
138 , ($srid != 0 ? 'class="hide"' : '') , '>';
139 echo $visualization;
140 echo '</div>';
142 // No not remove inline style or it will cause "Cannot read property 'w' of null" on GIS editor map init
143 echo '<div id="openlayersmap" style="width: ' . $visualizationSettings['width'] . 'px; height: ' . $visualizationSettings['height'] . 'px;" '
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();