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($_REQUEST['field'])) {
31 PhpMyAdmin\Util
::checkParameters(array('field'));
34 // Get data if any posted
36 if (Core
::isValid($_REQUEST['gis_data'], 'array')) {
37 $gis_data = $_REQUEST['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($_REQUEST['type']) && $_REQUEST['type'] != '') {
54 $gis_data['gis_type'] = mb_strtoupper($_REQUEST['type']);
56 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
57 $start = (substr($_REQUEST['value'], 0, 1) == "'") ?
1 : 0;
58 $gis_data['gis_type'] = mb_substr(
61 mb_strpos($_REQUEST['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($_REQUEST['value'])) {
75 $gis_data = array_merge(
76 $gis_data, $gis_obj->generateParams($_REQUEST['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($_REQUEST['generate']) && $_REQUEST['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($_REQUEST['field'])
126 echo '<input type="hidden" name="field" value="'
127 , htmlspecialchars($_REQUEST['field']) , '" />';
128 // The input field to which the final result should be added
129 // and corresponding null checkbox
130 if (isset($_REQUEST['input_name'])) {
131 echo '<input type="hidden" name="input_name" value="'
132 , htmlspecialchars($_REQUEST['input_name']) , '" />';
134 echo Url
::getHiddenInputs();
136 echo '<!-- Visualization section -->';
137 echo '<div id="placeholder" style="width:450px;height:300px;'
138 , ($srid != 0 ?
'display:none;' : '') , '">';
142 echo '<div id="openlayersmap" style="width:450px;height:300px;'
143 , ($srid == 0 ?
'display:none;' : '') , '">';
146 echo '<div class="choice floatright" style="clear:right;">';
147 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
148 , ($srid != 0 ?
' checked="checked"' : '') , '/>';
149 echo '<label for="choice">' , __("Use OpenStreetMaps as Base Layer") , '</label>';
152 echo '<script language="javascript" type="text/javascript">';
155 echo '<!-- End of visualization section -->';
158 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
159 echo '<div id="gis_data_header">';
160 echo '<select name="gis_data[gis_type]" class="gis_type">';
161 foreach ($gis_types as $gis_type) {
162 echo '<option value="' , $gis_type , '"';
163 if ($geom_type == $gis_type) {
164 echo ' selected="selected"';
166 echo '>' , $gis_type , '</option>';
169 echo ' ';
170 /* l10n: Spatial Reference System Identifier */
171 echo '<label for="srid">' , __('SRID:') , '</label>';
172 echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
174 echo '<!-- End of header section -->';
176 echo '<!-- Data section -->';
177 echo '<div id="gis_data">';
180 if ($geom_type == 'GEOMETRYCOLLECTION') {
181 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
182 ?
intval($gis_data[$geom_type]['geom_count']) : 1;
183 if (isset($gis_data[$geom_type]['add_geom'])) {
186 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
187 , ' value="' , $geom_count , '" />';
190 for ($a = 0; $a < $geom_count; $a++
) {
191 if (! isset($gis_data[$a])) {
195 if ($geom_type == 'GEOMETRYCOLLECTION') {
197 printf(__('Geometry %d:'), $a +
1);
199 if (isset($gis_data[$a]['gis_type'])) {
200 $type = htmlspecialchars($gis_data[$a]['gis_type']);
202 $type = $gis_types[0];
204 echo '<select name="gis_data[' , $a , '][gis_type]" class="gis_type">';
205 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
206 echo '<option value="' , $gis_type , '"';
207 if ($type == $gis_type) {
208 echo ' selected="selected"';
210 echo '>' , $gis_type , '</option>';
217 if ($type == 'POINT') {
220 echo '<label for="x">' , __("X") , '</label>';
221 echo '<input name="gis_data[' , $a , '][POINT][x]" type="text"'
222 , ' value="' , escape($gis_data[$a]['POINT']['x']) , '" />';
223 echo '<label for="y">' , __("Y") , '</label>';
224 echo '<input name="gis_data[' , $a , '][POINT][y]" type="text"'
225 , ' value="' , escape($gis_data[$a]['POINT']['y']) , '" />';
227 } elseif ($type == 'MULTIPOINT' ||
$type == 'LINESTRING') {
228 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
229 ?
intval($gis_data[$a][$type]['no_of_points']) : 1;
230 if ($type == 'LINESTRING' && $no_of_points < 2) {
233 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
237 if (isset($gis_data[$a][$type]['add_point'])) {
240 echo '<input type="hidden" value="' , $no_of_points , '"'
241 , ' name="gis_data[' , $a , '][' , $type , '][no_of_points]" />';
243 for ($i = 0; $i < $no_of_points; $i++
) {
245 printf(__('Point %d'), $i +
1);
247 echo '<label for="x">' , __("X") , '</label>';
248 echo '<input type="text"'
249 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][x]"'
250 , ' value="' , escape($gis_data[$a][$type][$i]['x']) , '" />';
251 echo '<label for="y">' , __("Y") , '</label>';
252 echo '<input type="text"'
253 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][y]"'
254 , ' value="' , escape($gis_data[$a][$type][$i]['y']) , '" />';
256 echo '<input type="submit"'
257 , ' name="gis_data[' , $a , '][' , $type , '][add_point]"'
258 , ' class="add addPoint" value="' , __("Add a point") , '" />';
260 } elseif ($type == 'MULTILINESTRING' ||
$type == 'POLYGON') {
261 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
262 ?
intval($gis_data[$a][$type]['no_of_lines']) : 1;
263 if ($no_of_lines < 1) {
266 if (isset($gis_data[$a][$type]['add_line'])) {
269 echo '<input type="hidden" value="' , $no_of_lines , '"'
270 , ' name="gis_data[' , $a , '][' , $type , '][no_of_lines]" />';
272 for ($i = 0; $i < $no_of_lines; $i++
) {
274 if ($type == 'MULTILINESTRING') {
275 printf(__('Linestring %d:'), $i +
1);
278 echo __('Outer ring:');
280 printf(__('Inner ring %d:'), $i);
284 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
285 ?
intval($gis_data[$a][$type][$i]['no_of_points']) : 2;
286 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
289 if ($type == 'POLYGON' && $no_of_points < 4) {
292 if (isset($gis_data[$a][$type][$i]['add_point'])) {
295 echo '<input type="hidden" value="' , $no_of_points , '"'
296 , ' name="gis_data[' , $a , '][' , $type , '][' , $i
297 , '][no_of_points]" />';
299 for ($j = 0; $j < $no_of_points; $j++
) {
301 printf(__('Point %d'), $j +
1);
303 echo '<label for="x">' , __("X") , '</label>';
304 echo '<input type="text" name="gis_data[' , $a , '][' , $type . ']['
305 , $i , '][' , $j , '][x]" value="'
306 , escape($gis_data[$a][$type][$i][$j]['x']) , '" />';
307 echo '<label for="y">' , __("Y") , '</label>';
308 echo '<input type="text" name="gis_data[' , $a , '][' , $type , ']['
309 , $i , '][' , $j , '][y]"' , ' value="'
310 , escape($gis_data[$a][$type][$i][$j]['y']) , '" />';
312 echo '<input type="submit" name="gis_data[' , $a , '][' , $type , ']['
313 , $i , '][add_point]"'
314 , ' class="add addPoint" value="' , __("Add a point") , '" />';
316 $caption = ($type == 'MULTILINESTRING')
317 ?
__('Add a linestring')
318 : __('Add an inner ring');
320 echo '<input type="submit"'
321 , ' name="gis_data[' , $a , '][' , $type , '][add_line]"'
322 , ' class="add addLine" value="' , $caption , '" />';
324 } elseif ($type == 'MULTIPOLYGON') {
325 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
326 ?
intval($gis_data[$a][$type]['no_of_polygons']) : 1;
327 if ($no_of_polygons < 1) {
330 if (isset($gis_data[$a][$type]['add_polygon'])) {
333 echo '<input type="hidden"'
334 , ' name="gis_data[' , $a , '][' , $type , '][no_of_polygons]"'
335 , ' value="' , $no_of_polygons , '" />';
337 for ($k = 0; $k < $no_of_polygons; $k++
) {
339 printf(__('Polygon %d:'), $k +
1);
340 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
341 ?
intval($gis_data[$a][$type][$k]['no_of_lines']) : 1;
342 if ($no_of_lines < 1) {
345 if (isset($gis_data[$a][$type][$k]['add_line'])) {
348 echo '<input type="hidden"'
349 , ' name="gis_data[' , $a , '][' , $type , '][' , $k
350 , '][no_of_lines]"' , ' value="' , $no_of_lines , '" />';
352 for ($i = 0; $i < $no_of_lines; $i++
) {
355 echo __('Outer ring:');
357 printf(__('Inner ring %d:'), $i);
360 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
361 ?
intval($gis_data[$a][$type][$k][$i]['no_of_points']) : 4;
362 if ($no_of_points < 4) {
365 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
368 echo '<input type="hidden"'
369 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
370 , '][no_of_points]"' , ' value="' , $no_of_points , '" />';
372 for ($j = 0; $j < $no_of_points; $j++
) {
374 printf(__('Point %d'), $j +
1);
376 echo '<label for="x">' , __("X") , '</label>';
377 echo '<input type="text"'
378 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
379 , $i , '][' , $j , '][x]"'
380 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['x'])
382 echo '<label for="y">' , __("Y") , '</label>';
383 echo '<input type="text"'
384 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
385 , $i , '][' , $j , '][y]"'
386 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['y'])
389 echo '<input type="submit"'
390 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
392 , ' class="add addPoint" value="' , __("Add a point") , '" />';
395 echo '<input type="submit"'
396 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
397 , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
401 echo '<input type="submit"'
402 , ' name="gis_data[' , $a , '][' , $type , '][add_polygon]"'
403 , ' class="add addPolygon" value="' , __('Add a polygon') , '" />';
406 if ($geom_type == 'GEOMETRYCOLLECTION') {
408 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
409 , 'class="add addGeom" value="' , __("Add geometry") , '" />';
412 echo '<!-- End of data section -->';
415 echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';
417 echo '<div id="gis_data_output">';
418 echo '<h3>' , __('Output') , '</h3>';
421 'Choose "GeomFromText" from the "Function" column and paste the'
422 . ' string below into the "Value" field.'
425 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
426 echo htmlspecialchars($result);
433 Response
::getInstance()->addJSON('gis_editor', ob_get_contents());