added check for json encoder, cleaned up error handling a bit

This commit is contained in:
Martynas Mickevicius
2010-08-06 13:22:04 +03:00
parent 47f67613a2
commit 59f220eb13
5 changed files with 52 additions and 40 deletions

View File

@@ -23,6 +23,12 @@
var imageMap = { var imageMap = {
'mouseMoved': function(event, cont) { 'mouseMoved': function(event, cont) {
// return if no imageMap set
// this can happen if server has no json
if (!this.imageMap) {
return;
}
// get mouse coordinated relative to image // get mouse coordinated relative to image
var mouseX = event.pageX - cont.offsetLeft; var mouseX = event.pageX - cont.offsetLeft;
var mouseY = event.pageY - cont.offsetTop; var mouseY = event.pageY - cont.offsetTop;

View File

@@ -1,7 +1,7 @@
<?php <?php
define('ERR_NO_GD', 0); define('ERR_NO_GD', 0);
define('ERR_UNKNOWN_FORMAT', 1); define('ERR_NO_JSON', 1);
require_once './libraries/chart/pma_pchart_pie.php'; require_once './libraries/chart/pma_pchart_pie.php';
require_once './libraries/chart/pma_pchart_single_bar.php'; require_once './libraries/chart/pma_pchart_single_bar.php';
@@ -66,7 +66,7 @@ function PMA_chart_profiling($data)
/* /*
* Formats a chart for query results page. * Formats a chart for query results page.
*/ */
function PMA_chart_results($data, &$chartSettings, &$chartErrors = array()) function PMA_chart_results($data, &$chartSettings)
{ {
$chartData = array(); $chartData = array();
$chart = null; $chart = null;
@@ -103,7 +103,7 @@ function PMA_chart_results($data, &$chartSettings, &$chartErrors = array())
} }
if (count($data[0]) == 1 || count($data[0]) == 2) { if (count($data[0]) == 1 || count($data[0]) == 2) {
// Two columns in every row. // One or two columns in every row.
// This data is suitable for a simple bar chart. // This data is suitable for a simple bar chart.
if ($chartSettings['type'] == 'pie') { if ($chartSettings['type'] == 'pie') {
@@ -220,14 +220,13 @@ function PMA_chart_results($data, &$chartSettings, &$chartErrors = array())
} }
} }
else { else {
// unknown data // unknown data format
array_push($chartErrors, ERR_UNKNOWN_FORMAT);
return ''; return '';
} }
$chartCode = $chart->toString(); $chartCode = $chart->toString();
$chartSettings = $chart->getSettings(); $chartSettings = $chart->getSettings();
$chartErrors = array_merge($chartErrors, $chart->getErrors()); $chartErrors = $chart->getErrors();
PMA_handle_chart_err($chartErrors); PMA_handle_chart_err($chartErrors);
return $chartCode; return $chartCode;
@@ -241,6 +240,9 @@ function PMA_handle_chart_err($errors)
if (in_array(ERR_NO_GD, $errors)) { if (in_array(ERR_NO_GD, $errors)) {
PMA_warnMissingExtension('GD', false, 'GD extension is needed for charts.'); PMA_warnMissingExtension('GD', false, 'GD extension is needed for charts.');
} }
else if (in_array(ERR_NO_JSON, $errors)) {
PMA_warnMissingExtension('JSON', false, 'JSON encoder is needed for chart tooltips.');
}
} }
?> ?>

View File

@@ -3495,7 +3495,7 @@
/* Get the current image map */ /* Get the current image map */
function getImageMap() function getImageMap()
{ {
return json_encode($this->ImageMap); return $this->ImageMap;
} }
/* Load and cleanup the image map from disk */ /* Load and cleanup the image map from disk */
@@ -3544,7 +3544,7 @@
} }
else else
{ {
fwrite($Handle, $this->getImageMap()); fwrite($Handle, serialize($this->getImageMap()));
} }
fclose ($Handle); fclose ($Handle);
} }

View File

@@ -171,7 +171,11 @@ abstract class PMA_pChart_Chart extends PMA_Chart
public function toString() public function toString()
{ {
if (function_exists('gd_info')) { if (!function_exists('gd_info')) {
array_push($this->errors, ERR_NO_GD);
return '';
}
$this->init(); $this->init();
$this->prepareDataSet(); $this->prepareDataSet();
$this->prepareChart(); $this->prepareChart();
@@ -190,18 +194,19 @@ abstract class PMA_pChart_Chart extends PMA_Chart
$returnData .= '<img src="data:image/png;base64,'.$part.'" />'; $returnData .= '<img src="data:image/png;base64,'.$part.'" />';
} }
$returnData .= '</div>'; $returnData .= '</div>';
if (function_exists('json_encode')) {
$returnData .= ' $returnData .= '
<script type="text/javascript"> <script type="text/javascript">
imageMap.loadImageMap(\''.$this->getImageMap().'\'); imageMap.loadImageMap(\''.json_encode($this->getImageMap()).'\');
</script> </script>
'; ';
return $returnData;
} }
else { else {
array_push($this->errors, ERR_NO_GD); array_push($this->errors, ERR_NO_JSON);
return '';
} }
return $returnData;
} }
protected function getLabelHeight() protected function getLabelHeight()

View File

@@ -50,11 +50,10 @@ if (PMA_isValid($_REQUEST['chartSettings'], 'array')) {
$chartSettings = $_REQUEST['chartSettings']; $chartSettings = $_REQUEST['chartSettings'];
} }
// get the chart and settings and errors after chart generation // get the chart and settings after chart generation
$chartErrors = array(); $chart = PMA_chart_results($data, $chartSettings);
$chart = PMA_chart_results($data, $chartSettings, $chartErrors);
if (empty($chartErrors)) { if (!empty($chart)) {
$message = PMA_Message::success(__('Chart generated successfully.')); $message = PMA_Message::success(__('Chart generated successfully.'));
} }
else { else {