ChartController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Controllers\Table\ChartController
  5. *
  6. * @package PhpMyAdmin\Controllers
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin\Controllers\Table;
  10. use PhpMyAdmin\DatabaseInterface;
  11. use PhpMyAdmin\Message;
  12. use PhpMyAdmin\Response;
  13. use PhpMyAdmin\SqlParser\Components\Limit;
  14. use PhpMyAdmin\SqlParser\Parser;
  15. use PhpMyAdmin\SqlParser\Statements\SelectStatement;
  16. use PhpMyAdmin\Template;
  17. use PhpMyAdmin\Util;
  18. /**
  19. * Handles table related logic
  20. *
  21. * @package PhpMyAdmin\Controllers
  22. */
  23. class ChartController extends AbstractController
  24. {
  25. /**
  26. * @var string
  27. */
  28. protected $sql_query;
  29. /**
  30. * @var string
  31. */
  32. protected $url_query;
  33. /**
  34. * @var array
  35. */
  36. protected $cfg;
  37. /**
  38. * Constructor
  39. *
  40. * @param Response $response Response object
  41. * @param DatabaseInterface $dbi DatabaseInterface object
  42. * @param Template $template Template object
  43. * @param string $db Database name
  44. * @param string $table Table name
  45. * @param string $sql_query Query
  46. * @param string $url_query Query URL
  47. * @param array $cfg Configuration
  48. */
  49. public function __construct(
  50. $response,
  51. $dbi,
  52. Template $template,
  53. $db,
  54. $table,
  55. $sql_query,
  56. $url_query,
  57. array $cfg
  58. ) {
  59. parent::__construct($response, $dbi, $template, $db, $table);
  60. $this->sql_query = $sql_query;
  61. $this->url_query = $url_query;
  62. $this->cfg = $cfg;
  63. }
  64. /**
  65. * Execute the query and return the result
  66. *
  67. * @return void
  68. */
  69. public function indexAction()
  70. {
  71. $response = Response::getInstance();
  72. if ($response->isAjax()
  73. && isset($_REQUEST['pos'])
  74. && isset($_REQUEST['session_max_rows'])
  75. ) {
  76. $this->ajaxAction();
  77. return;
  78. }
  79. // Throw error if no sql query is set
  80. if (! isset($this->sql_query) || $this->sql_query == '') {
  81. $this->response->setRequestStatus(false);
  82. $this->response->addHTML(
  83. Message::error(__('No SQL query was set to fetch data.'))
  84. );
  85. return;
  86. }
  87. $this->response->getHeader()->getScripts()->addFiles(
  88. [
  89. 'chart.js',
  90. 'table/chart.js',
  91. 'vendor/jqplot/jquery.jqplot.js',
  92. 'vendor/jqplot/plugins/jqplot.barRenderer.js',
  93. 'vendor/jqplot/plugins/jqplot.canvasAxisLabelRenderer.js',
  94. 'vendor/jqplot/plugins/jqplot.canvasTextRenderer.js',
  95. 'vendor/jqplot/plugins/jqplot.categoryAxisRenderer.js',
  96. 'vendor/jqplot/plugins/jqplot.dateAxisRenderer.js',
  97. 'vendor/jqplot/plugins/jqplot.pointLabels.js',
  98. 'vendor/jqplot/plugins/jqplot.pieRenderer.js',
  99. 'vendor/jqplot/plugins/jqplot.enhancedPieLegendRenderer.js',
  100. 'vendor/jqplot/plugins/jqplot.highlighter.js',
  101. ]
  102. );
  103. /**
  104. * Extract values for common work
  105. * @todo Extract common files
  106. */
  107. $db = &$this->db;
  108. $table = &$this->table;
  109. $url_params = [];
  110. /**
  111. * Runs common work
  112. */
  113. if (strlen($this->table) > 0) {
  114. $url_params['goto'] = Util::getScriptNameForOption(
  115. $this->cfg['DefaultTabTable'],
  116. 'table'
  117. );
  118. $url_params['back'] = 'tbl_sql.php';
  119. include ROOT_PATH . 'libraries/tbl_common.inc.php';
  120. $this->dbi->selectDb($GLOBALS['db']);
  121. } elseif (strlen($this->db) > 0) {
  122. $url_params['goto'] = Util::getScriptNameForOption(
  123. $this->cfg['DefaultTabDatabase'],
  124. 'database'
  125. );
  126. $url_params['back'] = 'sql.php';
  127. include ROOT_PATH . 'libraries/db_common.inc.php';
  128. } else {
  129. $url_params['goto'] = Util::getScriptNameForOption(
  130. $this->cfg['DefaultTabServer'],
  131. 'server'
  132. );
  133. $url_params['back'] = 'sql.php';
  134. include ROOT_PATH . 'libraries/server_common.inc.php';
  135. }
  136. $data = [];
  137. $result = $this->dbi->tryQuery($this->sql_query);
  138. $fields_meta = $this->dbi->getFieldsMeta($result);
  139. while ($row = $this->dbi->fetchAssoc($result)) {
  140. $data[] = $row;
  141. }
  142. $keys = array_keys($data[0]);
  143. $numeric_types = [
  144. 'int',
  145. 'real',
  146. ];
  147. $numeric_column_count = 0;
  148. foreach ($keys as $idx => $key) {
  149. if (in_array($fields_meta[$idx]->type, $numeric_types)) {
  150. $numeric_column_count++;
  151. }
  152. }
  153. if ($numeric_column_count == 0) {
  154. $this->response->setRequestStatus(false);
  155. $this->response->addJSON(
  156. 'message',
  157. __('No numeric columns present in the table to plot.')
  158. );
  159. return;
  160. }
  161. $url_params['db'] = $this->db;
  162. $url_params['reload'] = 1;
  163. /**
  164. * Displays the page
  165. */
  166. $this->response->addHTML(
  167. $this->template->render('table/chart/tbl_chart', [
  168. 'url_query' => $this->url_query,
  169. 'url_params' => $url_params,
  170. 'keys' => $keys,
  171. 'fields_meta' => $fields_meta,
  172. 'numeric_types' => $numeric_types,
  173. 'numeric_column_count' => $numeric_column_count,
  174. 'sql_query' => $this->sql_query,
  175. ])
  176. );
  177. }
  178. /**
  179. * Handle ajax request
  180. *
  181. * @return void
  182. */
  183. public function ajaxAction()
  184. {
  185. /**
  186. * Extract values for common work
  187. * @todo Extract common files
  188. */
  189. $db = &$this->db;
  190. $table = &$this->table;
  191. if (strlen($this->table) > 0 && strlen($this->db) > 0) {
  192. include ROOT_PATH . 'libraries/tbl_common.inc.php';
  193. }
  194. $parser = new Parser($this->sql_query);
  195. /**
  196. * @var SelectStatement $statement
  197. */
  198. $statement = $parser->statements[0];
  199. if (empty($statement->limit)) {
  200. $statement->limit = new Limit(
  201. $_REQUEST['session_max_rows'],
  202. $_REQUEST['pos']
  203. );
  204. } else {
  205. $start = $statement->limit->offset + $_REQUEST['pos'];
  206. $rows = min(
  207. $_REQUEST['session_max_rows'],
  208. $statement->limit->rowCount - $_REQUEST['pos']
  209. );
  210. $statement->limit = new Limit($rows, $start);
  211. }
  212. $sql_with_limit = $statement->build();
  213. $data = [];
  214. $result = $this->dbi->tryQuery($sql_with_limit);
  215. while ($row = $this->dbi->fetchAssoc($result)) {
  216. $data[] = $row;
  217. }
  218. if (empty($data)) {
  219. $this->response->setRequestStatus(false);
  220. $this->response->addJSON('message', __('No data to display'));
  221. return;
  222. }
  223. $sanitized_data = [];
  224. foreach ($data as $data_row_number => $data_row) {
  225. $tmp_row = [];
  226. foreach ($data_row as $data_column => $data_value) {
  227. $escaped_value = $data_value === null ? null : htmlspecialchars($data_value);
  228. $tmp_row[htmlspecialchars($data_column)] = $escaped_value;
  229. }
  230. $sanitized_data[] = $tmp_row;
  231. }
  232. $this->response->setRequestStatus(true);
  233. $this->response->addJSON('message', null);
  234. $this->response->addJSON('chartData', json_encode($sanitized_data));
  235. }
  236. }