tbl_chart.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * handles creation of the chart
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. require_once 'libraries/common.inc.php';
  9. require_once 'libraries/tbl_chart.lib.php';
  10. /*
  11. * Execute the query and return the result
  12. */
  13. if (isset($_REQUEST['ajax_request'])
  14. && isset($_REQUEST['pos'])
  15. && isset($_REQUEST['session_max_rows'])
  16. ) {
  17. $response = PMA_Response::getInstance();
  18. if (strlen($GLOBALS['table']) && strlen($GLOBALS['db'])) {
  19. include './libraries/tbl_common.inc.php';
  20. }
  21. $sql_with_limit = 'SELECT * FROM( ' . $sql_query . ' ) AS `temp_res` LIMIT '
  22. . $_REQUEST['pos'] . ', ' . $_REQUEST['session_max_rows'];
  23. $data = array();
  24. $result = $GLOBALS['dbi']->tryQuery($sql_with_limit);
  25. while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
  26. $data[] = $row;
  27. }
  28. if (empty($data)) {
  29. $response->isSuccess(false);
  30. $response->addJSON('message', __('No data to display'));
  31. exit;
  32. }
  33. $sanitized_data = array();
  34. foreach ($data as $data_row_number => $data_row) {
  35. $tmp_row = array();
  36. foreach ($data_row as $data_column => $data_value) {
  37. $tmp_row[htmlspecialchars($data_column)] = htmlspecialchars($data_value);
  38. }
  39. $sanitized_data[] = $tmp_row;
  40. }
  41. $response->isSuccess(true);
  42. $response->addJSON('message', null);
  43. $response->addJSON('chartData', json_encode($sanitized_data));
  44. unset($sanitized_data);
  45. exit;
  46. }
  47. $response = PMA_Response::getInstance();
  48. // Throw error if no sql query is set
  49. if (! isset($sql_query) || $sql_query == '') {
  50. $response->isSuccess(false);
  51. $response->addHTML(
  52. PMA_Message::error(__('No SQL query was set to fetch data.'))
  53. );
  54. exit;
  55. }
  56. $header = $response->getHeader();
  57. $scripts = $header->getScripts();
  58. $scripts->addFile('chart.js');
  59. $scripts->addFile('tbl_chart.js');
  60. $scripts->addFile('jqplot/jquery.jqplot.js');
  61. $scripts->addFile('jqplot/plugins/jqplot.barRenderer.js');
  62. $scripts->addFile('jqplot/plugins/jqplot.canvasAxisLabelRenderer.js');
  63. $scripts->addFile('jqplot/plugins/jqplot.canvasTextRenderer.js');
  64. $scripts->addFile('jqplot/plugins/jqplot.categoryAxisRenderer.js');
  65. $scripts->addFile('jqplot/plugins/jqplot.dateAxisRenderer.js');
  66. $scripts->addFile('jqplot/plugins/jqplot.pointLabels.js');
  67. $scripts->addFile('jqplot/plugins/jqplot.pieRenderer.js');
  68. $scripts->addFile('jqplot/plugins/jqplot.highlighter.js');
  69. /**
  70. * Runs common work
  71. */
  72. if (strlen($GLOBALS['table'])) {
  73. $url_params['goto'] = $cfg['DefaultTabTable'];
  74. $url_params['back'] = 'tbl_sql.php';
  75. include 'libraries/tbl_common.inc.php';
  76. include 'libraries/tbl_info.inc.php';
  77. } elseif (strlen($GLOBALS['db'])) {
  78. $url_params['goto'] = $cfg['DefaultTabDatabase'];
  79. $url_params['back'] = 'sql.php';
  80. include 'libraries/db_common.inc.php';
  81. include 'libraries/db_info.inc.php';
  82. } else {
  83. $url_params['goto'] = $cfg['DefaultTabServer'];
  84. $url_params['back'] = 'sql.php';
  85. include 'libraries/server_common.inc.php';
  86. }
  87. $data = array();
  88. $result = $GLOBALS['dbi']->tryQuery($sql_query);
  89. $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result);
  90. while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
  91. $data[] = $row;
  92. }
  93. $keys = array_keys($data[0]);
  94. $numeric_types = array('int', 'real');
  95. $numeric_column_count = 0;
  96. foreach ($keys as $idx => $key) {
  97. if (in_array($fields_meta[$idx]->type, $numeric_types)) {
  98. $numeric_column_count++;
  99. }
  100. }
  101. if ($numeric_column_count == 0) {
  102. $response->isSuccess(false);
  103. $response->addJSON(
  104. 'message',
  105. __('No numeric columns present in the table to plot.')
  106. );
  107. exit;
  108. }
  109. // get settings if any posted
  110. $chartSettings = array();
  111. if (PMA_isValid($_REQUEST['chartSettings'], 'array')) {
  112. $chartSettings = $_REQUEST['chartSettings'];
  113. }
  114. $url_params['db'] = $GLOBALS['db'];
  115. $url_params['reload'] = 1;
  116. /**
  117. * Displays the page
  118. */
  119. $htmlString = PMA_getHtmlForTableChartDisplay(
  120. $url_query, $url_params, $keys, $fields_meta, $numeric_types,
  121. $numeric_column_count, $sql_query
  122. );
  123. $response->addHTML($htmlString);
  124. ?>