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',
92 'mysqlVersion' => $GLOBALS['dbi']->getVersion()
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($_POST['generate']) && $_POST['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($_POST['field'])
127 echo '<input type="hidden" name="field" value="'
128 , htmlspecialchars($_POST['field']) , '" />';
129 // The input field to which the final result should be added
130 // and corresponding null checkbox
131 if (isset($_POST['input_name'])) {
132 echo '<input type="hidden" name="input_name" value="'
133 , htmlspecialchars($_POST['input_name']) , '" />';
135 echo Url
::getHiddenInputs();
137 echo '<!-- Visualization section -->';
138 echo '<div id="placeholder"'
139 , ($srid != 0 ?
'class="hide"' : '') , '>';
143 // No not remove inline style or it will cause "Cannot read property 'w' of null" on GIS editor map init
144 echo '<div id="openlayersmap" style="width: ' . $visualizationSettings['width'] . 'px; height: ' . $visualizationSettings['height'] . 'px;" '
145 , ($srid == 0 ?
'class="hide"' : '') , '>';
148 echo '<div class="choice floatright">';
149 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
150 , ($srid != 0 ?
' checked="checked"' : '') , '/>';
151 echo '<label for="choice">' , __("Use OpenStreetMaps as Base Layer") , '</label>';
154 echo '<script language="javascript" type="text/javascript">';
157 echo '<!-- End of visualization section -->';
160 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
161 echo '<div id="gis_data_header">';
162 echo '<select name="gis_data[gis_type]" class="gis_type">';
163 foreach ($gis_types as $gis_type) {
164 echo '<option value="' , $gis_type , '"';
165 if ($geom_type == $gis_type) {
166 echo ' selected="selected"';
168 echo '>' , $gis_type , '</option>';
171 echo ' ';
172 /* l10n: Spatial Reference System Identifier */
173 echo '<label for="srid">' , __('SRID:') , '</label>';
174 echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
176 echo '<!-- End of header section -->';
178 echo '<!-- Data section -->';
179 echo '<div id="gis_data">';
182 if ($geom_type == 'GEOMETRYCOLLECTION') {
183 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
184 ?
intval($gis_data[$geom_type]['geom_count']) : 1;
185 if (isset($gis_data[$geom_type]['add_geom'])) {
188 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
189 , ' value="' , $geom_count , '" />';
192 for ($a = 0; $a < $geom_count; $a++
) {
193 if (! isset($gis_data[$a])) {
197 if ($geom_type == 'GEOMETRYCOLLECTION') {
199 printf(__('Geometry %d:'), $a +
1);
201 if (isset($gis_data[$a]['gis_type'])) {
202 $type = htmlspecialchars($gis_data[$a]['gis_type']);
204 $type = $gis_types[0];
206 echo '<select name="gis_data[' , $a , '][gis_type]" class="gis_type">';
207 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
208 echo '<option value="' , $gis_type , '"';
209 if ($type == $gis_type) {
210 echo ' selected="selected"';
212 echo '>' , $gis_type , '</option>';
219 if ($type == 'POINT') {
222 echo '<label for="x">' , __("X") , '</label>';
223 echo '<input name="gis_data[' , $a , '][POINT][x]" type="text"'
224 , ' value="' , escape($gis_data[$a]['POINT']['x']) , '" />';
225 echo '<label for="y">' , __("Y") , '</label>';
226 echo '<input name="gis_data[' , $a , '][POINT][y]" type="text"'
227 , ' value="' , escape($gis_data[$a]['POINT']['y']) , '" />';
229 } elseif ($type == 'MULTIPOINT' ||
$type == 'LINESTRING') {
230 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
231 ?
intval($gis_data[$a][$type]['no_of_points']) : 1;
232 if ($type == 'LINESTRING' && $no_of_points < 2) {
235 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
239 if (isset($gis_data[$a][$type]['add_point'])) {
242 echo '<input type="hidden" value="' , $no_of_points , '"'
243 , ' name="gis_data[' , $a , '][' , $type , '][no_of_points]" />';
245 for ($i = 0; $i < $no_of_points; $i++
) {
247 printf(__('Point %d'), $i +
1);
249 echo '<label for="x">' , __("X") , '</label>';
250 echo '<input type="text"'
251 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][x]"'
252 , ' value="' , escape($gis_data[$a][$type][$i]['x']) , '" />';
253 echo '<label for="y">' , __("Y") , '</label>';
254 echo '<input type="text"'
255 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][y]"'
256 , ' value="' , escape($gis_data[$a][$type][$i]['y']) , '" />';
258 echo '<input type="submit"'
259 , ' name="gis_data[' , $a , '][' , $type , '][add_point]"'
260 , ' class="add addPoint" value="' , __("Add a point") , '" />';
262 } elseif ($type == 'MULTILINESTRING' ||
$type == 'POLYGON') {
263 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
264 ?
intval($gis_data[$a][$type]['no_of_lines']) : 1;
265 if ($no_of_lines < 1) {
268 if (isset($gis_data[$a][$type]['add_line'])) {
271 echo '<input type="hidden" value="' , $no_of_lines , '"'
272 , ' name="gis_data[' , $a , '][' , $type , '][no_of_lines]" />';
274 for ($i = 0; $i < $no_of_lines; $i++
) {
276 if ($type == 'MULTILINESTRING') {
277 printf(__('Linestring %d:'), $i +
1);
280 echo __('Outer ring:');
282 printf(__('Inner ring %d:'), $i);
286 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
287 ?
intval($gis_data[$a][$type][$i]['no_of_points']) : 2;
288 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
291 if ($type == 'POLYGON' && $no_of_points < 4) {
294 if (isset($gis_data[$a][$type][$i]['add_point'])) {
297 echo '<input type="hidden" value="' , $no_of_points , '"'
298 , ' name="gis_data[' , $a , '][' , $type , '][' , $i
299 , '][no_of_points]" />';
301 for ($j = 0; $j < $no_of_points; $j++
) {
303 printf(__('Point %d'), $j +
1);
305 echo '<label for="x">' , __("X") , '</label>';
306 echo '<input type="text" name="gis_data[' , $a , '][' , $type . ']['
307 , $i , '][' , $j , '][x]" value="'
308 , escape($gis_data[$a][$type][$i][$j]['x']) , '" />';
309 echo '<label for="y">' , __("Y") , '</label>';
310 echo '<input type="text" name="gis_data[' , $a , '][' , $type , ']['
311 , $i , '][' , $j , '][y]"' , ' value="'
312 , escape($gis_data[$a][$type][$i][$j]['y']) , '" />';
314 echo '<input type="submit" name="gis_data[' , $a , '][' , $type , ']['
315 , $i , '][add_point]"'
316 , ' class="add addPoint" value="' , __("Add a point") , '" />';
318 $caption = ($type == 'MULTILINESTRING')
319 ?
__('Add a linestring')
320 : __('Add an inner ring');
322 echo '<input type="submit"'
323 , ' name="gis_data[' , $a , '][' , $type , '][add_line]"'
324 , ' class="add addLine" value="' , $caption , '" />';
326 } elseif ($type == 'MULTIPOLYGON') {
327 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
328 ?
intval($gis_data[$a][$type]['no_of_polygons']) : 1;
329 if ($no_of_polygons < 1) {
332 if (isset($gis_data[$a][$type]['add_polygon'])) {
335 echo '<input type="hidden"'
336 , ' name="gis_data[' , $a , '][' , $type , '][no_of_polygons]"'
337 , ' value="' , $no_of_polygons , '" />';
339 for ($k = 0; $k < $no_of_polygons; $k++
) {
341 printf(__('Polygon %d:'), $k +
1);
342 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
343 ?
intval($gis_data[$a][$type][$k]['no_of_lines']) : 1;
344 if ($no_of_lines < 1) {
347 if (isset($gis_data[$a][$type][$k]['add_line'])) {
350 echo '<input type="hidden"'
351 , ' name="gis_data[' , $a , '][' , $type , '][' , $k
352 , '][no_of_lines]"' , ' value="' , $no_of_lines , '" />';
354 for ($i = 0; $i < $no_of_lines; $i++
) {
357 echo __('Outer ring:');
359 printf(__('Inner ring %d:'), $i);
362 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
363 ?
intval($gis_data[$a][$type][$k][$i]['no_of_points']) : 4;
364 if ($no_of_points < 4) {
367 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
370 echo '<input type="hidden"'
371 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
372 , '][no_of_points]"' , ' value="' , $no_of_points , '" />';
374 for ($j = 0; $j < $no_of_points; $j++
) {
376 printf(__('Point %d'), $j +
1);
378 echo '<label for="x">' , __("X") , '</label>';
379 echo '<input type="text"'
380 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
381 , $i , '][' , $j , '][x]"'
382 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['x'])
384 echo '<label for="y">' , __("Y") , '</label>';
385 echo '<input type="text"'
386 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
387 , $i , '][' , $j , '][y]"'
388 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['y'])
391 echo '<input type="submit"'
392 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
394 , ' class="add addPoint" value="' , __("Add a point") , '" />';
397 echo '<input type="submit"'
398 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
399 , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
403 echo '<input type="submit"'
404 , ' name="gis_data[' , $a , '][' , $type , '][add_polygon]"'
405 , ' class="add addPolygon" value="' , __('Add a polygon') , '" />';
408 if ($geom_type == 'GEOMETRYCOLLECTION') {
410 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
411 , 'class="add addGeom" value="' , __("Add geometry") , '" />';
414 echo '<!-- End of data section -->';
417 echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';
419 echo '<div id="gis_data_output">';
420 echo '<h3>' , __('Output') , '</h3>';
423 'Choose "ST_GeomFromText" from the "Function" column and paste the'
424 . ' string below into the "Value" field.'
427 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
428 echo htmlspecialchars($result);
435 Response
::getInstance()->addJSON('gis_editor', ob_get_contents());