GisDataEditorController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Editor for Geometry data types.
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Controllers;
  7. use PhpMyAdmin\Core;
  8. use PhpMyAdmin\Gis\GisFactory;
  9. use PhpMyAdmin\Gis\GisVisualization;
  10. use function array_merge;
  11. use function in_array;
  12. use function intval;
  13. use function mb_strpos;
  14. use function mb_strtoupper;
  15. use function mb_substr;
  16. use function substr;
  17. use function trim;
  18. /**
  19. * Editor for Geometry data types.
  20. */
  21. class GisDataEditorController extends AbstractController
  22. {
  23. public function index(): void
  24. {
  25. global $gis_data, $gis_types, $start, $geom_type, $gis_obj, $srid, $wkt, $wkt_with_zero, $PMA_Theme;
  26. global $result, $visualizationSettings, $data, $visualization, $open_layers, $geom_count, $dbi;
  27. if (! isset($_POST['field'])) {
  28. return;
  29. }
  30. // Get data if any posted
  31. $gis_data = [];
  32. if (Core::isValid($_POST['gis_data'], 'array')) {
  33. $gis_data = $_POST['gis_data'];
  34. }
  35. $gis_types = [
  36. 'POINT',
  37. 'MULTIPOINT',
  38. 'LINESTRING',
  39. 'MULTILINESTRING',
  40. 'POLYGON',
  41. 'MULTIPOLYGON',
  42. 'GEOMETRYCOLLECTION',
  43. ];
  44. // Extract type from the initial call and make sure that it's a valid one.
  45. // Extract from field's values if available, if not use the column type passed.
  46. if (! isset($gis_data['gis_type'])) {
  47. if (isset($_POST['type']) && $_POST['type'] != '') {
  48. $gis_data['gis_type'] = mb_strtoupper($_POST['type']);
  49. }
  50. if (isset($_POST['value']) && trim($_POST['value']) != '') {
  51. $start = substr($_POST['value'], 0, 1) == "'" ? 1 : 0;
  52. $gis_data['gis_type'] = mb_substr(
  53. $_POST['value'],
  54. $start,
  55. mb_strpos($_POST['value'], '(') - $start
  56. );
  57. }
  58. if (! isset($gis_data['gis_type'])
  59. || (! in_array($gis_data['gis_type'], $gis_types))
  60. ) {
  61. $gis_data['gis_type'] = $gis_types[0];
  62. }
  63. }
  64. $geom_type = $gis_data['gis_type'];
  65. // Generate parameters from value passed.
  66. $gis_obj = GisFactory::factory($geom_type);
  67. if ($gis_obj === false) {
  68. return;
  69. }
  70. if (isset($_POST['value'])) {
  71. $gis_data = array_merge(
  72. $gis_data,
  73. $gis_obj->generateParams($_POST['value'])
  74. );
  75. }
  76. // Generate Well Known Text
  77. $srid = isset($gis_data['srid']) && $gis_data['srid'] != '' ? $gis_data['srid'] : 0;
  78. $wkt = $gis_obj->generateWkt($gis_data, 0);
  79. $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
  80. $result = "'" . $wkt . "'," . $srid;
  81. // Generate SVG based visualization
  82. $visualizationSettings = [
  83. 'width' => 450,
  84. 'height' => 300,
  85. 'spatialColumn' => 'wkt',
  86. 'mysqlVersion' => $dbi->getVersion(),
  87. 'isMariaDB' => $dbi->isMariaDB(),
  88. ];
  89. $data = [
  90. [
  91. 'wkt' => $wkt_with_zero,
  92. 'srid' => $srid,
  93. ],
  94. ];
  95. $visualization = GisVisualization::getByData($data, $visualizationSettings)
  96. ->toImage('svg');
  97. $open_layers = GisVisualization::getByData($data, $visualizationSettings)
  98. ->asOl();
  99. // If the call is to update the WKT and visualization make an AJAX response
  100. if (isset($_POST['generate']) && $_POST['generate'] == true) {
  101. $this->response->addJSON([
  102. 'result' => $result,
  103. 'visualization' => $visualization,
  104. 'openLayers' => $open_layers,
  105. ]);
  106. return;
  107. }
  108. $geom_count = 1;
  109. if ($geom_type === 'GEOMETRYCOLLECTION') {
  110. $geom_count = isset($gis_data[$geom_type]['geom_count'])
  111. ? intval($gis_data[$geom_type]['geom_count']) : 1;
  112. if (isset($gis_data[$geom_type]['add_geom'])) {
  113. $geom_count++;
  114. }
  115. }
  116. $templateOutput = $this->template->render('gis_data_editor_form', [
  117. 'width' => $visualizationSettings['width'],
  118. 'height' => $visualizationSettings['height'],
  119. 'theme_image_path' => $PMA_Theme->getImgPath(),
  120. 'field' => $_POST['field'],
  121. 'input_name' => $_POST['input_name'],
  122. 'srid' => $srid,
  123. 'visualization' => $visualization,
  124. 'open_layers' => $open_layers,
  125. 'gis_types' => $gis_types,
  126. 'geom_type' => $geom_type,
  127. 'geom_count' => $geom_count,
  128. 'gis_data' => $gis_data,
  129. 'result' => $result,
  130. ]);
  131. $this->response->addJSON(['gis_editor' => $templateOutput]);
  132. }
  133. }