TableChartController.php 7.2 KB

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