Translated using Weblate (French)
[phpmyadmin.git] / gis_data_editor.php
blob726872412820ce5057f5b5452263babcef455b78
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;
11 /**
12 * Escapes special characters if the variable is set.
13 * Returns an empty string otherwise.
15 * @param string $variable variable to be escaped
17 * @return string escaped variable
19 function escape($variable)
21 return isset($variable) ? htmlspecialchars($variable) : '';
24 require_once 'libraries/common.inc.php';
26 if (! isset($_REQUEST['field'])) {
27 PMA\libraries\Util::checkParameters(array('field'));
30 // Get data if any posted
31 $gis_data = array();
32 if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
33 $gis_data = $_REQUEST['gis_data'];
36 $gis_types = array(
37 'POINT',
38 'MULTIPOINT',
39 'LINESTRING',
40 'MULTILINESTRING',
41 'POLYGON',
42 'MULTIPOLYGON',
43 'GEOMETRYCOLLECTION'
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'] = 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'] = mb_substr(
55 $_REQUEST['value'],
56 $start,
57 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 SVG based visualization
84 $visualizationSettings = array(
85 'width' => 450,
86 'height' => 300,
87 'spatialColumn' => 'wkt'
89 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
90 $visualization = GISVisualization::getByData($data, $visualizationSettings)
91 ->toImage('svg');
93 $open_layers = GISVisualization::getByData($data, $visualizationSettings)
94 ->asOl();
96 // If the call is to update the WKT and visualization make an AJAX response
97 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
98 $extra_data = array(
99 'result' => $result,
100 'visualization' => $visualization,
101 'openLayers' => $open_layers,
103 $response = PMA\libraries\Response::getInstance();
104 $response->addJSON($extra_data);
105 exit;
108 ob_start();
110 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
111 echo '<input type="hidden" id="pmaThemeImage"'
112 , ' value="' , $GLOBALS['pmaThemeImage'] , '" />';
113 echo '<div id="gis_data_editor">';
115 echo '<h3>';
116 printf(
117 __('Value for the column "%s"'),
118 htmlspecialchars($_REQUEST['field'])
120 echo '</h3>';
122 echo '<input type="hidden" name="field" value="'
123 , htmlspecialchars($_REQUEST['field']) , '" />';
124 // The input field to which the final result should be added
125 // and corresponding null checkbox
126 if (isset($_REQUEST['input_name'])) {
127 echo '<input type="hidden" name="input_name" value="'
128 , htmlspecialchars($_REQUEST['input_name']) , '" />';
130 echo PMA_URL_getHiddenInputs();
132 echo '<!-- Visualization section -->';
133 echo '<div id="placeholder" style="width:450px;height:300px;'
134 , ($srid != 0 ? 'display:none;' : '') , '">';
135 echo $visualization;
136 echo '</div>';
138 echo '<div id="openlayersmap" style="width:450px;height:300px;'
139 , ($srid == 0 ? 'display:none;' : '') , '">';
140 echo '</div>';
142 echo '<div class="choice" style="float:right;clear:right;">';
143 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
144 , ($srid != 0 ? ' checked="checked"' : '') , '/>';
145 echo '<label for="choice">' , __("Use OpenStreetMaps as Base Layer") , '</label>';
146 echo '</div>';
148 echo '<script language="javascript" type="text/javascript">';
149 echo $open_layers;
150 echo '</script>';
151 echo '<!-- End of visualization section -->';
154 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
155 echo '<div id="gis_data_header">';
156 echo '<select name="gis_data[gis_type]" class="gis_type">';
157 foreach ($gis_types as $gis_type) {
158 echo '<option value="' , $gis_type , '"';
159 if ($geom_type == $gis_type) {
160 echo ' selected="selected"';
162 echo '>' , $gis_type , '</option>';
164 echo '</select>';
165 echo '&nbsp;&nbsp;&nbsp;&nbsp;';
166 /* l10n: Spatial Reference System Identifier */
167 echo '<label for="srid">' , __('SRID:') , '</label>';
168 echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
169 echo '</div>';
170 echo '<!-- End of header section -->';
172 echo '<!-- Data section -->';
173 echo '<div id="gis_data">';
175 $geom_count = 1;
176 if ($geom_type == 'GEOMETRYCOLLECTION') {
177 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
178 ? $gis_data[$geom_type]['geom_count'] : 1;
179 if (isset($gis_data[$geom_type]['add_geom'])) {
180 $geom_count++;
182 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
183 , ' value="' , $geom_count , '" />';
186 for ($a = 0; $a < $geom_count; $a++) {
187 if (! isset($gis_data[$a])) {
188 continue;
191 if ($geom_type == 'GEOMETRYCOLLECTION') {
192 echo '<br/><br/>';
193 printf(__('Geometry %d:'), $a + 1);
194 echo '<br/>';
195 if (isset($gis_data[$a]['gis_type'])) {
196 $type = $gis_data[$a]['gis_type'];
197 } else {
198 $type = $gis_types[0];
200 echo '<select name="gis_data[' , $a , '][gis_type]" class="gis_type">';
201 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
202 echo '<option value="' , $gis_type , '"';
203 if ($type == $gis_type) {
204 echo ' selected="selected"';
206 echo '>' , $gis_type , '</option>';
208 echo '</select>';
209 } else {
210 $type = $geom_type;
213 if ($type == 'POINT') {
214 echo '<br/>';
215 echo __('Point:');
216 echo '<label for="x">' , __("X") , '</label>';
217 echo '<input name="gis_data[' , $a , '][POINT][x]" type="text"'
218 , ' value="' , escape($gis_data[$a]['POINT']['x']) , '" />';
219 echo '<label for="y">' , __("Y") , '</label>';
220 echo '<input name="gis_data[' , $a , '][POINT][y]" type="text"'
221 , ' value="' , escape($gis_data[$a]['POINT']['y']) , '" />';
223 } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
224 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
225 ? $gis_data[$a][$type]['no_of_points'] : 1;
226 if ($type == 'LINESTRING' && $no_of_points < 2) {
227 $no_of_points = 2;
229 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
230 $no_of_points = 1;
233 if (isset($gis_data[$a][$type]['add_point'])) {
234 $no_of_points++;
236 echo '<input type="hidden" value="' , $no_of_points , '"'
237 , ' name="gis_data[' , $a , '][' , $type , '][no_of_points]" />';
239 for ($i = 0; $i < $no_of_points; $i++) {
240 echo '<br/>';
241 printf(__('Point %d'), $i + 1);
242 echo ': ';
243 echo '<label for="x">' , __("X") , '</label>';
244 echo '<input type="text"'
245 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][x]"'
246 , ' value="' , escape($gis_data[$a][$type][$i]['x']) , '" />';
247 echo '<label for="y">' , __("Y") , '</label>';
248 echo '<input type="text"'
249 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][y]"'
250 , ' value="' , escape($gis_data[$a][$type][$i]['y']) , '" />';
252 echo '<input type="submit"'
253 , ' name="gis_data[' , $a , '][' , $type , '][add_point]"'
254 , ' class="add addPoint" value="' , __("Add a point") , '" />';
256 } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
257 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
258 ? $gis_data[$a][$type]['no_of_lines'] : 1;
259 if ($no_of_lines < 1) {
260 $no_of_lines = 1;
262 if (isset($gis_data[$a][$type]['add_line'])) {
263 $no_of_lines++;
265 echo '<input type="hidden" value="' , $no_of_lines , '"'
266 , ' name="gis_data[' , $a , '][' , $type , '][no_of_lines]" />';
268 for ($i = 0; $i < $no_of_lines; $i++) {
269 echo '<br/>';
270 if ($type == 'MULTILINESTRING') {
271 printf(__('Linestring %d:'), $i + 1);
272 } else {
273 if ($i == 0) {
274 echo __('Outer ring:');
275 } else {
276 printf(__('Inner ring %d:'), $i);
280 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
281 ? $gis_data[$a][$type][$i]['no_of_points'] : 2;
282 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
283 $no_of_points = 2;
285 if ($type == 'POLYGON' && $no_of_points < 4) {
286 $no_of_points = 4;
288 if (isset($gis_data[$a][$type][$i]['add_point'])) {
289 $no_of_points++;
291 echo '<input type="hidden" value="' , $no_of_points , '"'
292 , ' name="gis_data[' , $a , '][' , $type , '][' , $i
293 , '][no_of_points]" />';
295 for ($j = 0; $j < $no_of_points; $j++) {
296 echo('<br/>');
297 printf(__('Point %d'), $j + 1);
298 echo ': ';
299 echo '<label for="x">' , __("X") , '</label>';
300 echo '<input type="text" name="gis_data[' , $a , '][' , $type . ']['
301 , $i , '][' , $j , '][x]" value="'
302 , escape($gis_data[$a][$type][$i][$j]['x']) , '" />';
303 echo '<label for="y">' , __("Y") , '</label>';
304 echo '<input type="text" name="gis_data[' , $a , '][' , $type , ']['
305 , $i , '][' , $j , '][y]"' , ' value="'
306 , escape($gis_data[$a][$type][$i][$j]['y']) , '" />';
308 echo '<input type="submit" name="gis_data[' , $a , '][' , $type , ']['
309 , $i , '][add_point]"'
310 , ' class="add addPoint" value="' , __("Add a point") , '" />';
312 $caption = ($type == 'MULTILINESTRING')
313 ? __('Add a linestring')
314 : __('Add an inner ring');
315 echo '<br/>';
316 echo '<input type="submit"'
317 , ' name="gis_data[' , $a , '][' , $type , '][add_line]"'
318 , ' class="add addLine" value="' , $caption , '" />';
320 } elseif ($type == 'MULTIPOLYGON') {
321 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
322 ? $gis_data[$a][$type]['no_of_polygons'] : 1;
323 if ($no_of_polygons < 1) {
324 $no_of_polygons = 1;
326 if (isset($gis_data[$a][$type]['add_polygon'])) {
327 $no_of_polygons++;
329 echo '<input type="hidden"'
330 , ' name="gis_data[' , $a , '][' , $type , '][no_of_polygons]"'
331 , ' value="' , $no_of_polygons , '" />';
333 for ($k = 0; $k < $no_of_polygons; $k++) {
334 echo '<br/>';
335 printf(__('Polygon %d:'), $k + 1);
336 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
337 ? $gis_data[$a][$type][$k]['no_of_lines'] : 1;
338 if ($no_of_lines < 1) {
339 $no_of_lines = 1;
341 if (isset($gis_data[$a][$type][$k]['add_line'])) {
342 $no_of_lines++;
344 echo '<input type="hidden"'
345 , ' name="gis_data[' , $a , '][' , $type , '][' , $k
346 , '][no_of_lines]"' , ' value="' , $no_of_lines , '" />';
348 for ($i = 0; $i < $no_of_lines; $i++) {
349 echo '<br/><br/>';
350 if ($i == 0) {
351 echo __('Outer ring:');
352 } else {
353 printf(__('Inner ring %d:'), $i);
356 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
357 ? $gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
358 if ($no_of_points < 4) {
359 $no_of_points = 4;
361 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
362 $no_of_points++;
364 echo '<input type="hidden"'
365 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
366 , '][no_of_points]"' , ' value="' , $no_of_points , '" />';
368 for ($j = 0; $j < $no_of_points; $j++) {
369 echo '<br/>';
370 printf(__('Point %d'), $j + 1);
371 echo ': ';
372 echo '<label for="x">' , __("X") , '</label>';
373 echo '<input type="text"'
374 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
375 , $i , '][' , $j , '][x]"'
376 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['x'])
377 , '" />';
378 echo '<label for="y">' , __("Y") , '</label>';
379 echo '<input type="text"'
380 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
381 , $i , '][' , $j , '][y]"'
382 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['y'])
383 , '" />';
385 echo '<input type="submit"'
386 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
387 , '][add_point]"'
388 , ' class="add addPoint" value="' , __("Add a point") , '" />';
390 echo '<br/>';
391 echo '<input type="submit"'
392 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
393 , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
394 echo '<br/>';
396 echo '<br/>';
397 echo '<input type="submit"'
398 , ' name="gis_data[' , $a , '][' , $type , '][add_polygon]"'
399 , ' class="add addPolygon" value="' , __('Add a polygon') , '" />';
402 if ($geom_type == 'GEOMETRYCOLLECTION') {
403 echo '<br/><br/>';
404 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
405 , 'class="add addGeom" value="' , __("Add geometry") , '" />';
407 echo '</div>';
408 echo '<!-- End of data section -->';
410 echo '<br/>';
411 echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';
413 echo '<div id="gis_data_output">';
414 echo '<h3>' , __('Output') , '</h3>';
415 echo '<p>';
416 echo __(
417 'Choose "GeomFromText" from the "Function" column and paste the'
418 . ' string below into the "Value" field.'
420 echo '</p>';
421 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
422 echo $result;
423 echo '</textarea>';
424 echo '</div>';
426 echo '</div>';
427 echo '</form>';
429 PMA\libraries\Response::getInstance()->addJSON('gis_editor', ob_get_contents());
430 ob_end_clean();