2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Editor for Geometry data types.
8 declare(strict_types
=1);
11 use PhpMyAdmin\Gis\GisFactory
;
12 use PhpMyAdmin\Gis\GisVisualization
;
13 use PhpMyAdmin\Response
;
17 * Escapes special characters if the variable is set.
18 * Returns an empty string otherwise.
20 * @param string $variable variable to be escaped
22 * @return string escaped variable
24 function escape($variable)
26 return isset($variable) ?
htmlspecialchars($variable) : '';
29 require_once 'libraries/common.inc.php';
31 if (! isset($_REQUEST['field'])) {
32 PhpMyAdmin\Util
::checkParameters(array('field'));
35 // Get data if any posted
37 if (Core
::isValid($_REQUEST['gis_data'], 'array')) {
38 $gis_data = $_REQUEST['gis_data'];
51 // Extract type from the initial call and make sure that it's a valid one.
52 // Extract from field's values if available, if not use the column type passed.
53 if (! isset($gis_data['gis_type'])) {
54 if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
55 $gis_data['gis_type'] = mb_strtoupper($_REQUEST['type']);
57 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
58 $start = (substr($_REQUEST['value'], 0, 1) == "'") ?
1 : 0;
59 $gis_data['gis_type'] = mb_substr(
62 mb_strpos($_REQUEST['value'], "(") - $start
65 if ((! isset($gis_data['gis_type']))
66 ||
(! in_array($gis_data['gis_type'], $gis_types))
68 $gis_data['gis_type'] = $gis_types[0];
71 $geom_type = htmlspecialchars($gis_data['gis_type']);
73 // Generate parameters from value passed.
74 $gis_obj = GisFactory
::factory($geom_type);
75 if (isset($_REQUEST['value'])) {
76 $gis_data = array_merge(
77 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
81 // Generate Well Known Text
82 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
83 ?
htmlspecialchars($gis_data['srid']) : 0;
84 $wkt = $gis_obj->generateWkt($gis_data, 0);
85 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
86 $result = "'" . $wkt . "'," . $srid;
88 // Generate SVG based visualization
89 $visualizationSettings = array(
92 'spatialColumn' => 'wkt'
94 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
95 $visualization = GisVisualization
::getByData($data, $visualizationSettings)
98 $open_layers = GisVisualization
::getByData($data, $visualizationSettings)
101 // If the call is to update the WKT and visualization make an AJAX response
102 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
105 'visualization' => $visualization,
106 'openLayers' => $open_layers,
108 $response = Response
::getInstance();
109 $response->addJSON($extra_data);
115 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
116 echo '<input type="hidden" id="pmaThemeImage"'
117 , ' value="' , $GLOBALS['pmaThemeImage'] , '" />';
118 echo '<div id="gis_data_editor">';
122 __('Value for the column "%s"'),
123 htmlspecialchars($_REQUEST['field'])
127 echo '<input type="hidden" name="field" value="'
128 , htmlspecialchars($_REQUEST['field']) , '" />';
129 // The input field to which the final result should be added
130 // and corresponding null checkbox
131 if (isset($_REQUEST['input_name'])) {
132 echo '<input type="hidden" name="input_name" value="'
133 , htmlspecialchars($_REQUEST['input_name']) , '" />';
135 echo Url
::getHiddenInputs();
137 echo '<!-- Visualization section -->';
138 echo '<div id="placeholder" '
139 , ($srid != 0 ?
'class="hide' : '') , '">';
143 echo '<div id="openlayersmap" '
144 , ($srid == 0 ?
'class="hide' : '') , '">';
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>';
153 echo '<script language="javascript" type="text/javascript">';
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>';
170 echo ' ';
171 /* l10n: Spatial Reference System Identifier */
172 echo '<label for="srid">' , __('SRID:') , '</label>';
173 echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
175 echo '<!-- End of header section -->';
177 echo '<!-- Data section -->';
178 echo '<div id="gis_data">';
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'])) {
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])) {
196 if ($geom_type == 'GEOMETRYCOLLECTION') {
198 printf(__('Geometry %d:'), $a +
1);
200 if (isset($gis_data[$a]['gis_type'])) {
201 $type = htmlspecialchars($gis_data[$a]['gis_type']);
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>';
218 if ($type == '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) {
234 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
238 if (isset($gis_data[$a][$type]['add_point'])) {
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++
) {
246 printf(__('Point %d'), $i +
1);
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) {
267 if (isset($gis_data[$a][$type]['add_line'])) {
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++
) {
275 if ($type == 'MULTILINESTRING') {
276 printf(__('Linestring %d:'), $i +
1);
279 echo __('Outer ring:');
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) {
290 if ($type == 'POLYGON' && $no_of_points < 4) {
293 if (isset($gis_data[$a][$type][$i]['add_point'])) {
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++
) {
302 printf(__('Point %d'), $j +
1);
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');
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) {
331 if (isset($gis_data[$a][$type]['add_polygon'])) {
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++
) {
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) {
346 if (isset($gis_data[$a][$type][$k]['add_line'])) {
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++
) {
356 echo __('Outer ring:');
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) {
366 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
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++
) {
375 printf(__('Point %d'), $j +
1);
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'])
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'])
390 echo '<input type="submit"'
391 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
393 , ' class="add addPoint" value="' , __("Add a point") , '" />';
396 echo '<input type="submit"'
397 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
398 , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
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') {
409 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
410 , 'class="add addGeom" value="' , __("Add geometry") , '" />';
413 echo '<!-- End of data section -->';
416 echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';
418 echo '<div id="gis_data_output">';
419 echo '<h3>' , __('Output') , '</h3>';
422 'Choose "GeomFromText" from the "Function" column and paste the'
423 . ' string below into the "Value" field.'
426 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
427 echo htmlspecialchars($result);
434 Response
::getInstance()->addJSON('gis_editor', ob_get_contents());