Update edih_x12file_class.php (#295)
[openemr.git] / phpmyadmin / libraries / controllers / TableChartController.class.php
blob807a5da0dfb5b9a2dbaf7356122084ed3033c9b7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 /**
5 * Holds the PMA\TableChartController
7 * @package PMA
8 */
10 namespace PMA\Controllers\Table;
12 use PMA\DI\Container;
13 use PMA_Util;
14 use PMA_Message;
15 use PMA\Template;
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';
23 /**
24 * Handles table related logic
26 * @package PhpMyAdmin
28 class TableChartController extends TableController
31 /**
32 * @var string $sql_query
34 protected $sql_query;
36 /**
37 * @var string $url_query
39 protected $url_query;
41 /**
42 * @var array $cfg
44 protected $cfg;
46 /**
47 * Constructor
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;
59 $this->cfg = $cfg;
62 /**
63 * Execute the query and return the result
65 * @return void
67 public function indexAction()
69 if (isset($_REQUEST['ajax_request'])
70 && isset($_REQUEST['pos'])
71 && isset($_REQUEST['session_max_rows'])
72 ) {
73 $this->ajaxAction();
74 return;
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.'))
83 return;
86 $this->response->getHeader()->getScripts()->addFiles(
87 array(
88 'chart.js',
89 'tbl_chart.js',
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
106 $db = &$this->db;
107 $table = &$this->table;
110 * Runs common work
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';
126 list(
127 $tables,
128 $num_tables,
129 $total_num_tables,
130 $sub_part,
131 $is_show_stats,
132 $db_is_system_schema,
133 $tooltip_truename,
134 $tooltip_aliasname,
135 $pos
136 ) = PMA_Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
138 } else {
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';
146 $data = array();
148 $result = $this->dbi->tryQuery($this->sql_query);
149 $fields_meta = $this->dbi->getFieldsMeta($result);
150 while ($row = $this->dbi->fetchAssoc($result)) {
151 $data[] = $row;
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(
167 'message',
168 __('No numeric columns present in the table to plot.')
170 return;
173 $url_params['db'] = $this->db;
174 $url_params['reload'] = 1;
177 * Displays the page
179 $this->response->addHTML(
180 Template::get('table/chart/tbl_chart')->render(
181 array(
182 'url_query' => $this->url_query,
183 'url_params' => $url_params,
184 'keys' => $keys,
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
197 * @return void
199 public function ajaxAction()
202 * Extract values for common work
203 * @todo Extract common files
205 $db = &$this->db;
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',
218 $this->sql_query,
219 $_REQUEST['pos'],
220 $_REQUEST['session_max_rows']
222 $data = array();
223 $result = $this->dbi->tryQuery($sql_with_limit);
224 while ($row = $this->dbi->fetchAssoc($result)) {
225 $data[] = $row;
228 if (empty($data)) {
229 $this->response->isSuccess(false);
230 $this->response->addJSON('message', __('No data to display'));
231 return;
233 $sanitized_data = array();
235 foreach ($data as $data_row_number => $data_row) {
236 $tmp_row = array();
237 foreach ($data_row as $data_column => $data_value) {
238 $tmp_row[htmlspecialchars($data_column)] = htmlspecialchars(
239 $data_value
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));