TableGisVisualizationController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Controllers\Table\TableIndexesController
  5. *
  6. * @package PhpMyAdmin\Controllers
  7. */
  8. namespace PhpMyAdmin\Controllers\Table;
  9. use PhpMyAdmin\Controllers\TableController;
  10. use PhpMyAdmin\Core;
  11. use PhpMyAdmin\Gis\GisVisualization;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Template;
  14. use PhpMyAdmin\Url;
  15. require_once 'libraries/common.inc.php';
  16. require_once 'libraries/db_common.inc.php';
  17. /**
  18. * Class TableGisVisualizationController
  19. *
  20. * @package PhpMyAdmin\Controllers
  21. */
  22. class TableGisVisualizationController extends TableController
  23. {
  24. /**
  25. * @var array $url_params
  26. */
  27. protected $url_params;
  28. /**
  29. * @var string $sql_query
  30. */
  31. protected $sql_query;
  32. /**
  33. * @var array $visualizationSettings
  34. */
  35. protected $visualizationSettings;
  36. /**
  37. * @var \PhpMyAdmin\Gis\GisVisualization $visualization
  38. */
  39. protected $visualization;
  40. /**
  41. * Constructor
  42. *
  43. * @param string $sql_query SQL query for retrieving GIS data
  44. * @param array $url_params array of URL parameters
  45. * @param string $goto goto script
  46. * @param string $back back script
  47. * @param array $visualizationSettings visualization settings
  48. */
  49. public function __construct(
  50. $response,
  51. $dbi,
  52. $db,
  53. $table,
  54. $sql_query,
  55. array $url_params,
  56. $goto,
  57. $back,
  58. array $visualizationSettings
  59. ) {
  60. parent::__construct($response, $dbi, $db, $table);
  61. $this->sql_query = $sql_query;
  62. $this->url_params = $url_params;
  63. $this->url_params['goto'] = $goto;
  64. $this->url_params['back'] = $back;
  65. $this->visualizationSettings = $visualizationSettings;
  66. }
  67. /**
  68. * Save to file
  69. *
  70. * @return void
  71. */
  72. public function saveToFileAction()
  73. {
  74. $this->response->disable();
  75. $file_name = $this->visualizationSettings['spatialColumn'];
  76. $save_format = $_GET['fileFormat'];
  77. $this->visualization->toFile($file_name, $save_format);
  78. }
  79. /**
  80. * Index
  81. *
  82. * @return void
  83. */
  84. public function indexAction()
  85. {
  86. // Throw error if no sql query is set
  87. if (! isset($this->sql_query) || $this->sql_query == '') {
  88. $this->response->setRequestStatus(false);
  89. $this->response->addHTML(
  90. Message::error(__('No SQL query was set to fetch data.'))
  91. );
  92. return;
  93. }
  94. // Execute the query and return the result
  95. $result = $this->dbi->tryQuery($this->sql_query);
  96. // Get the meta data of results
  97. $meta = $this->dbi->getFieldsMeta($result);
  98. // Find the candidate fields for label column and spatial column
  99. $labelCandidates = array();
  100. $spatialCandidates = array();
  101. foreach ($meta as $column_meta) {
  102. if ($column_meta->type == 'geometry') {
  103. $spatialCandidates[] = $column_meta->name;
  104. } else {
  105. $labelCandidates[] = $column_meta->name;
  106. }
  107. }
  108. // Get settings if any posted
  109. if (Core::isValid($_POST['visualizationSettings'], 'array')) {
  110. $this->visualizationSettings = $_POST['visualizationSettings'];
  111. }
  112. // Check mysql version
  113. $this->visualizationSettings['mysqlVersion'] = $this->dbi->getVersion();
  114. if (!isset($this->visualizationSettings['labelColumn'])
  115. && isset($labelCandidates[0])
  116. ) {
  117. $this->visualizationSettings['labelColumn'] = '';
  118. }
  119. // If spatial column is not set, use first geometric column as spatial column
  120. if (! isset($this->visualizationSettings['spatialColumn'])) {
  121. $this->visualizationSettings['spatialColumn'] = $spatialCandidates[0];
  122. }
  123. // Convert geometric columns from bytes to text.
  124. $pos = isset($_GET['pos']) ? $_GET['pos']
  125. : $_SESSION['tmpval']['pos'];
  126. if (isset($_GET['session_max_rows'])) {
  127. $rows = $_GET['session_max_rows'];
  128. } else {
  129. if ($_SESSION['tmpval']['max_rows'] != 'all') {
  130. $rows = $_SESSION['tmpval']['max_rows'];
  131. } else {
  132. $rows = $GLOBALS['cfg']['MaxRows'];
  133. }
  134. }
  135. $this->visualization = GisVisualization::get(
  136. $this->sql_query,
  137. $this->visualizationSettings,
  138. $rows,
  139. $pos
  140. );
  141. if (isset($_GET['saveToFile'])) {
  142. $this->saveToFileAction();
  143. return;
  144. }
  145. $this->response->getHeader()->getScripts()->addFiles(
  146. array(
  147. 'vendor/openlayers/OpenLayers.js',
  148. 'vendor/jquery/jquery.svg.js',
  149. 'tbl_gis_visualization.js',
  150. )
  151. );
  152. // If all the rows contain SRID, use OpenStreetMaps on the initial loading.
  153. if (! isset($_POST['displayVisualization'])) {
  154. if ($this->visualization->hasSrid()) {
  155. $this->visualizationSettings['choice'] = 'useBaseLayer';
  156. } else {
  157. unset($this->visualizationSettings['choice']);
  158. }
  159. }
  160. $this->visualization->setUserSpecifiedSettings($this->visualizationSettings);
  161. if ($this->visualizationSettings != null) {
  162. foreach ($this->visualization->getSettings() as $setting => $val) {
  163. if (! isset($this->visualizationSettings[$setting])) {
  164. $this->visualizationSettings[$setting] = $val;
  165. }
  166. }
  167. }
  168. /**
  169. * Displays the page
  170. */
  171. $this->url_params['sql_query'] = $this->sql_query;
  172. $downloadUrl = 'tbl_gis_visualization.php' . Url::getCommon(
  173. array_merge(
  174. $this->url_params,
  175. array(
  176. 'sql_signature' => Core::signSqlQuery($this->sql_query),
  177. 'saveToFile' => true,
  178. 'session_max_rows' => $rows,
  179. 'pos' => $pos
  180. )
  181. )
  182. );
  183. $html = Template::get('table/gis_visualization/gis_visualization')->render(
  184. array(
  185. 'url_params' => $this->url_params,
  186. 'download_url' => $downloadUrl,
  187. 'label_candidates' => $labelCandidates,
  188. 'spatial_candidates' => $spatialCandidates,
  189. 'visualization_settings' => $this->visualizationSettings,
  190. 'sql_query' => $this->sql_query,
  191. 'visualization' => $this->visualization->toImage('svg'),
  192. 'draw_ol' => $this->visualization->asOl(),
  193. 'pma_theme_image' => $GLOBALS['pmaThemeImage']
  194. )
  195. );
  196. $this->response->addHTML($html);
  197. }
  198. }