2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Editor for Geometry data types.
10 use PhpMyAdmin\Gis\GisFactory
;
11 use PhpMyAdmin\Gis\GisVisualization
;
12 use PhpMyAdmin\Response
;
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
36 if (Core
::isValid($_POST['gis_data'], 'array')) {
37 $gis_data = $_POST['gis_data'];
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(
61 mb_strpos($_POST['value'], "(") - $start
64 if ((! isset($gis_data['gis_type']))
65 ||
(! in_array($gis_data['gis_type'], $gis_types))
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(
91 'spatialColumn' => 'wkt'
93 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
94 $visualization = GisVisualization
::getByData($data, $visualizationSettings)
97 $open_layers = GisVisualization
::getByData($data, $visualizationSettings)
100 // If the call is to update the WKT and visualization make an AJAX response
101 if (isset($_POST['generate']) && $_POST['generate'] == true) {
104 'visualization' => $visualization,
105 'openLayers' => $open_layers,
107 $response = Response
::getInstance();
108 $response->addJSON($extra_data);
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">';
121 __('Value for the column "%s"'),
122 htmlspecialchars($_POST['field'])
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"' : '') , '>';
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"' : '') , '>';
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());