2 /* vim: set expandtab sw=4 ts=4 sts=4: */
5 * Holds the PMA\TableChartController
10 namespace PMA\Controllers\Table
;
16 use PMA\Controllers\TableController
;
18 require_once 'libraries/Util.class.php';
19 require_once 'libraries/Message.class.php';
20 require_once 'libraries/Template.class.php';
21 require_once 'libraries/controllers/TableController.class.php';
24 * Handles table related logic
28 class TableChartController
extends TableController
32 * @var string $sql_query
37 * @var string $url_query
49 * @param string $sql_query Query
50 * @param string $url_query Query URL
51 * @param array $cfg Configuration
53 public function __construct($sql_query, $url_query, $cfg)
55 parent
::__construct();
57 $this->sql_query
= $sql_query;
58 $this->url_query
= $url_query;
63 * Execute the query and return the result
67 public function indexAction()
69 if (isset($_REQUEST['ajax_request'])
70 && isset($_REQUEST['pos'])
71 && isset($_REQUEST['session_max_rows'])
77 // Throw error if no sql query is set
78 if (!isset($this->sql_query
) ||
$this->sql_query
== '') {
79 $this->response
->isSuccess(false);
80 $this->response
->addHTML(
81 PMA_Message
::error(__('No SQL query was set to fetch data.'))
86 $this->response
->getHeader()->getScripts()->addFiles(
90 'jqplot/jquery.jqplot.js',
91 'jqplot/plugins/jqplot.barRenderer.js',
92 'jqplot/plugins/jqplot.canvasAxisLabelRenderer.js',
93 'jqplot/plugins/jqplot.canvasTextRenderer.js',
94 'jqplot/plugins/jqplot.categoryAxisRenderer.js',
95 'jqplot/plugins/jqplot.dateAxisRenderer.js',
96 'jqplot/plugins/jqplot.pointLabels.js',
97 'jqplot/plugins/jqplot.pieRenderer.js',
98 'jqplot/plugins/jqplot.highlighter.js'
103 * Extract values for common work
104 * @todo Extract common files
107 $table = &$this->table
;
112 if (/*overload*/ mb_strlen($this->table
)) {
113 $url_params['goto'] = PMA_Util
::getScriptNameForOption(
114 $this->cfg
['DefaultTabTable'], 'table'
116 $url_params['back'] = 'tbl_sql.php';
117 include 'libraries/tbl_common.inc.php';
118 include 'libraries/tbl_info.inc.php';
119 } elseif (/*overload*/ mb_strlen($this->db
)) {
120 $url_params['goto'] = PMA_Util
::getScriptNameForOption(
121 $this->cfg
['DefaultTabDatabase'], 'database'
123 $url_params['back'] = 'sql.php';
124 include 'libraries/db_common.inc.php';
132 $db_is_system_schema,
136 ) = PMA_Util
::getDbInfo($db, isset($sub_part) ?
$sub_part : '');
139 $url_params['goto'] = PMA_Util
::getScriptNameForOption(
140 $this->cfg
['DefaultTabServer'], 'server'
142 $url_params['back'] = 'sql.php';
143 include 'libraries/server_common.inc.php';
148 $result = $this->dbi
->tryQuery($this->sql_query
);
149 $fields_meta = $this->dbi
->getFieldsMeta($result);
150 while ($row = $this->dbi
->fetchAssoc($result)) {
154 $keys = array_keys($data[0]);
156 $numeric_types = array('int', 'real');
157 $numeric_column_count = 0;
158 foreach ($keys as $idx => $key) {
159 if (in_array($fields_meta[$idx]->type
, $numeric_types)) {
160 $numeric_column_count++
;
164 if ($numeric_column_count == 0) {
165 $this->response
->isSuccess(false);
166 $this->response
->addJSON(
168 __('No numeric columns present in the table to plot.')
173 $url_params['db'] = $this->db
;
174 $url_params['reload'] = 1;
179 $this->response
->addHTML(
180 Template
::get('table/chart/tbl_chart')->render(
182 'url_query' => $this->url_query
,
183 'url_params' => $url_params,
185 'fields_meta' => $fields_meta,
186 'numeric_types' => $numeric_types,
187 'numeric_column_count' => $numeric_column_count,
188 'sql_query' => $this->sql_query
195 * Handle ajax request
199 public function ajaxAction()
202 * Extract values for common work
203 * @todo Extract common files
206 $table = &$this->table
;
208 $tableLength = /*overload*/
209 mb_strlen($this->table
);
210 $dbLength = /*overload*/
211 mb_strlen($this->db
);
212 if ($tableLength && $dbLength) {
213 include './libraries/tbl_common.inc.php';
216 $sql_with_limit = sprintf(
217 'SELECT * FROM(%s) AS `temp_res` LIMIT %s, %s',
220 $_REQUEST['session_max_rows']
223 $result = $this->dbi
->tryQuery($sql_with_limit);
224 while ($row = $this->dbi
->fetchAssoc($result)) {
229 $this->response
->isSuccess(false);
230 $this->response
->addJSON('message', __('No data to display'));
233 $sanitized_data = array();
235 foreach ($data as $data_row_number => $data_row) {
237 foreach ($data_row as $data_column => $data_value) {
238 $tmp_row[htmlspecialchars($data_column)] = htmlspecialchars(
242 $sanitized_data[] = $tmp_row;
244 $this->response
->isSuccess(true);
245 $this->response
->addJSON('message', null);
246 $this->response
->addJSON('chartData', json_encode($sanitized_data));