graceful error handling

This commit is contained in:
Martynas Mickevicius
2010-07-09 17:34:28 +03:00
parent 3df1126c85
commit 979aa9a7ed
4 changed files with 32 additions and 18 deletions

View File

@@ -1,5 +1,8 @@
<?php <?php
define('ERR_NO_GD', 0);
define('ERR_UNKNOWN_FORMAT', 1);
require_once './libraries/chart/pma_ofc_pie.php'; require_once './libraries/chart/pma_ofc_pie.php';
require_once './libraries/chart/pma_pchart_pie.php'; require_once './libraries/chart/pma_pchart_pie.php';
@@ -65,7 +68,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) function PMA_chart_results($data, &$chartSettings, &$chartErrors = array())
{ {
$chartData = array(); $chartData = array();
$chart = null; $chart = null;
@@ -202,12 +205,15 @@ function PMA_chart_results($data, &$chartSettings)
} }
else { else {
// unknown data // unknown data
return __('Unknown data format.'); array_push($chartErrors, ERR_UNKNOWN_FORMAT);
return '';
} }
$chartCode = $chart->toString(); $chartCode = $chart->toString();
$chartSettings = $chart->getSettings(); $chartSettings = $chart->getSettings();
PMA_handle_chart_err($chart->getErrors()); $chartErrors = array_merge($chartErrors, $chart->getErrors());
PMA_handle_chart_err($chartErrors);
return $chartCode; return $chartCode;
} }
@@ -216,7 +222,7 @@ function PMA_chart_results($data, &$chartSettings)
*/ */
function PMA_handle_chart_err($errors) function PMA_handle_chart_err($errors)
{ {
if (!empty($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.');
} }
} }

View File

@@ -79,7 +79,7 @@ class PMA_Chart
/* /*
* Error codes will be stored here * Error codes will be stored here
*/ */
protected $errors = null; protected $errors = array();
function __construct($options = null) function __construct($options = null)
{ {

View File

@@ -135,7 +135,7 @@ abstract class PMA_pChart_Chart extends PMA_Chart
return '<img id="chart" src="data:image/png;base64,'.$this->imageEncoded.'" />'; return '<img id="chart" src="data:image/png;base64,'.$this->imageEncoded.'" />';
} }
else { else {
$this->errors = 1; // stub array_push($this->errors, ERR_NO_GD);
return ''; return '';
} }
} }

View File

@@ -25,7 +25,7 @@ require_once './libraries/common.inc.php';
*/ */
require './libraries/db_common.inc.php'; require './libraries/db_common.inc.php';
$url_params['goto'] = $cfg['DefaultTabDatabase']; $url_params['goto'] = $cfg['DefaultTabDatabase'];
$url_params['back'] = 'tbl_chart.php'; $url_params['back'] = 'sql.php';
/* /*
* Import chart functions * Import chart functions
@@ -48,8 +48,16 @@ if (PMA_isValid($_REQUEST['chartSettings'], 'array')) {
$chartSettings = $_REQUEST['chartSettings']; $chartSettings = $_REQUEST['chartSettings'];
} }
// get the chart and settings used to generate chart // get the chart and settings and errors after chart generation
$chart = PMA_chart_results($data, $chartSettings); $chartErrors = array();
$chart = PMA_chart_results($data, $chartSettings, $chartErrors);
if (empty($chartErrors)) {
$message = PMA_Message::success(__('Chart generated successfully.'));
}
else {
$message = PMA_Message::error(__('The result of this query can\'t be used for a chart.'));
}
/** /**
* Displays top menu links * Displays top menu links
@@ -80,33 +88,33 @@ $url_params['reload'] = 1;
<table> <table>
<tr><td><label for="width"><?php echo __("Width"); ?></label></td> <tr><td><label for="width"><?php echo __("Width"); ?></label></td>
<td><input type="text" name="chartSettings[width]" id="width" value="<?php echo $chartSettings['width']; ?>" /></td> <td><input type="text" name="chartSettings[width]" id="width" value="<?php echo (isset($chartSettings['width']) ? $chartSettings['width'] : ''); ?>" /></td>
</tr> </tr>
<tr><td><label for="height"><?php echo __("Height"); ?></label></td> <tr><td><label for="height"><?php echo __("Height"); ?></label></td>
<td><input type="text" name="chartSettings[height]" id="height" value="<?php echo $chartSettings['height']; ?>" /></td> <td><input type="text" name="chartSettings[height]" id="height" value="<?php echo (isset($chartSettings['height']) ? $chartSettings['height'] : ''); ?>" /></td>
</tr> </tr>
<tr><td><label for="titleText"><?php echo __("Title"); ?></label></td> <tr><td><label for="titleText"><?php echo __("Title"); ?></label></td>
<td><input type="text" name="chartSettings[titleText]" id="titleText" value="<?php echo $chartSettings['titleText']; ?>" /></td> <td><input type="text" name="chartSettings[titleText]" id="titleText" value="<?php echo (isset($chartSettings['titleText']) ? $chartSettings['titleText'] : ''); ?>" /></td>
</tr> </tr>
<?php if ($chartSettings['type'] != 'pie' && $chartSettings['type'] != 'radar') { ?> <?php if ($chartSettings['type'] != 'pie' && $chartSettings['type'] != 'radar') { ?>
<tr><td><label for="xLabel"><?php echo __("X Axis label"); ?></label></td> <tr><td><label for="xLabel"><?php echo __("X Axis label"); ?></label></td>
<td><input type="text" name="chartSettings[xLabel]" id="xLabel" value="<?php echo $chartSettings['xLabel']; ?>" /></td> <td><input type="text" name="chartSettings[xLabel]" id="xLabel" value="<?php echo (isset($chartSettings['xLabel']) ? $chartSettings['xLabel'] : ''); ?>" /></td>
</tr> </tr>
<tr><td><label for="yLabel"><?php echo __("Y Axis label"); ?></label></td> <tr><td><label for="yLabel"><?php echo __("Y Axis label"); ?></label></td>
<td><input type="text" name="chartSettings[yLabel]" id="yLabel" value="<?php echo $chartSettings['yLabel']; ?>" /></td> <td><input type="text" name="chartSettings[yLabel]" id="yLabel" value="<?php echo (isset($chartSettings['yLabel']) ? $chartSettings['yLabel'] : ''); ?>" /></td>
</tr> </tr>
<?php } ?> <?php } ?>
<tr><td><label for="areaMargins"><?php echo __("Area margins"); ?></label></td> <tr><td><label for="areaMargins"><?php echo __("Area margins"); ?></label></td>
<td> <td>
<input type="text" name="chartSettings[areaMargins][]" size="2" value="<?php echo $chartSettings['areaMargins'][0]; ?>" /> <input type="text" name="chartSettings[areaMargins][]" size="2" value="<?php echo (isset($chartSettings['areaMargins'][0]) ? $chartSettings['areaMargins'][0] : ''); ?>" />
<input type="text" name="chartSettings[areaMargins][]" size="2" value="<?php echo $chartSettings['areaMargins'][1]; ?>" /> <input type="text" name="chartSettings[areaMargins][]" size="2" value="<?php echo (isset($chartSettings['areaMargins'][1]) ? $chartSettings['areaMargins'][1] : ''); ?>" />
<input type="text" name="chartSettings[areaMargins][]" size="2" value="<?php echo $chartSettings['areaMargins'][2]; ?>" /> <input type="text" name="chartSettings[areaMargins][]" size="2" value="<?php echo (isset($chartSettings['areaMargins'][2]) ? $chartSettings['areaMargins'][2] : ''); ?>" />
<input type="text" name="chartSettings[areaMargins][]" size="2" value="<?php echo $chartSettings['areaMargins'][3]; ?>" /> <input type="text" name="chartSettings[areaMargins][]" size="2" value="<?php echo (isset($chartSettings['areaMargins'][3]) ? $chartSettings['areaMargins'][3] : ''); ?>" />
</td> </td>
</tr> </tr>