rewrite of View system

This commit is contained in:
Steffen Vogel 2010-12-27 14:17:08 +01:00
parent ec32a70ec2
commit bcddbfa584
4 changed files with 139 additions and 103 deletions

View file

@ -60,6 +60,32 @@ class JSON extends View {
$this->setPadding($request->getParameter('padding'));
}
/**
* Add object to output
*
* @param mixed $data
*/
public function add($data) {
if ($data instanceof Interpreter\InterpreterInterface) {
$this->addData($data);
}
elseif ($data instanceof Model\Entity) {
$this->addEntity($data);
}
elseif ($data instanceof \Exception) {
$this->addException($data);
}
elseif ($data instanceof Util\Debug) {
$this->addDebug($data);
}
elseif ($data instanceof Util\JSON || is_array($data)) {
$this->addArray($data);
}
else {
throw new \Exception('Can\'t show ' . get_class($data));
}
}
/**
* Process, encode and print output
*
@ -161,17 +187,18 @@ class JSON extends View {
* @param \Exception $exception
* @param boolean $debug
*/
protected function addException(\Exception $exception, $debug = FALSE) {
protected function addException(\Exception $exception) {
$exceptionInfo = array(
'type' => get_class($exception),
'message' => $exception->getMessage(),
'type' => get_class($exception),
'code' => $exception->getCode()
);
if ($debug) {
$debugInfo = array('file' => $exception->getFile(),
if (Util\Debug::isActivated()) {
$debugInfo = array(
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTrace()
'backtrace' => $exception->getTrace()
);
$this->json['exception'] = array_merge($exceptionInfo, $debugInfo);
@ -190,7 +217,7 @@ class JSON extends View {
$data = $interpreter->getValues($this->request->getParameter('tuples'), $this->request->getParameter('group'));
$this->json['data'] = array(
'uuid' => $interpreter->getUuid(),
'uuid' => $interpreter->getEntity()->getUuid(),
'count' => count($data),
'first' => (isset($data[0][0])) ? $data[0][0] : NULL,
'last' => (isset($data[count($data)-1][0])) ? $data[count($data)-1][0] : NULL,
@ -212,18 +239,6 @@ class JSON extends View {
}
}
/**
* Overloaded to support arrays
*/
public function add($data) {
if ($data instanceof Util\JSON || is_array($data)) {
$this->addArray($data);
}
else {
parent::add($data);
}
}
/*
* Setter & getter
*/

View file

@ -24,7 +24,6 @@
namespace Volkszaehler\View;
use Volkszaehler\Interpreter;
use Volkszaehler\Model;
use Volkszaehler\Util;
@ -45,20 +44,35 @@ require_once VZ_BACKEND_DIR . '/lib/vendor/JpGraph/jpgraph_date.php';
*/
class JpGraph extends View {
/**
* indicator => ynaxis[n] mapping
* @var indicator => ynaxis[n] mapping
*/
protected $axes = array();
protected $channels = array();
const WIDTH = 800;
const HEIGHT = 400;
/**
* @var default width
*/
protected $width = 800;
protected static $colors = array('chartreuse', 'chocolate1', 'cyan', 'blue', 'lightcyan4', 'gold');
/**
* @var default height
*/
protected $height = 400;
/**
* @var color palette for the scatter plots
* This are the same colors as in the webfronted
*/
protected static $colors = array('#83CAFF', '#7E0021', '#579D1C', '#FFD320', '#FF420E', '#004586', '#0084D1', '#C5000B', '#FF950E', '#4B1F6F', '#AECF00', '#314004');
/**
* @var JPGrpah handle
*/
protected $graph;
/**
* Constructor
*
* @param HTTP\Request $request
* @param HTTP\Response $response
@ -67,10 +81,18 @@ class JpGraph extends View {
public function __construct(HTTP\Request $request, HTTP\Response $response, $format = 'png') {
parent::__construct($request, $response);
$width = $this->request->getParameter('width') ? $this->request->getParameter('width') : self::WIDTH;
$height = $this->request->getParameter('height') ? $this->request->getParameter('height') : self::HEIGHT;
// to enabled jpgraphs graphical exception handler
restore_exception_handler();
$this->graph = new \Graph($width, $height);
if ($this->request->getParameter('width')) {
$this->width = $this->request->getParameter('width');
}
if ($this->request->getParameter('height')) {
$this->height = $this->request->getParameter('height');
}
$this->graph = new \Graph($this->width, $this->height);
$this->graph->img->SetImgFormat($format);
@ -90,7 +112,29 @@ class JpGraph extends View {
$this->graph->xaxis->SetLabelAngle(45);
$this->graph->xaxis->SetLabelFormatCallback(function($label) { return date('j.n.y G:i', $label); });
//$this->graph->img->SetAntiAliasing();
if (function_exists('imageantialias')) {
$this->graph->img->SetAntiAliasing();
}
}
/**
* Add object to output
*
* @param mixed $data
*/
public function add($data) {
if ($data instanceof Interpreter\Interpreter) {
$this->addData($data);
}
elseif($data instanceof Interpreter\AggregatorInterpreter) {
foreach ($data->getEntity()->getChildren() as $child) {
$this->add($child);
}
}
else {
// suppress other classes
//throw new \Exception('Can\'t show ' . get_class($data));
}
}
/**
@ -99,8 +143,8 @@ class JpGraph extends View {
* @param $obj
* @param $data
*/
public function addData(Interpreter\Interpreter $interpreter){
$data = $interpreter->getValues(800);
public function addData(Interpreter\InterpreterInterface $interpreter){
$data = $interpreter->getValues($this->width/4);
if (count($data) > 0) {
$count = count($this->channels);
@ -114,7 +158,7 @@ class JpGraph extends View {
// Create the scatter plot
$plot = new \ScatterPlot($yData, $xData);
$plot->setLegend($interpreter->getChannel()->getProperty('title') . ': [' . $interpreter->getChannel()->getDefinition()->getUnit() . ']');
$plot->setLegend($interpreter->getEntity()->getProperty('title') . ': [' . $interpreter->getEntity()->getDefinition()->getUnit() . ']');
$plot->SetLinkPoints(TRUE, self::$colors[$count]);
$plot->mark->SetColor(self::$colors[$count]);
@ -122,7 +166,7 @@ class JpGraph extends View {
$plot->mark->SetType(MARK_DIAMOND);
$plot->mark->SetWidth(1);
$axis = $this->getAxisIndex($interpreter->getChannel());
$axis = $this->getAxisIndex($interpreter->getEntity());
if ($axis >= 0) {
$this->graph->AddY($axis, $plot);
}
@ -130,22 +174,10 @@ class JpGraph extends View {
$this->graph->Add($plot);
}
$this->channels[] = $interpreter->getChannel();
$this->channels[] = $interpreter->getEntity();
}
}
public function addChannel(Model\Channel $channel) {
throw new \Exception(get_class($this) . ' cant show ' . get_class($channel));
}
public function addAggregator(Model\Aggregator $aggregator) {
throw new \Exception(get_class($this) . ' cant show ' . get_class($aggregator));
}
public function addDebug(Util\Debug $debug) {
throw new \Exception(get_class($this) . ' cant show ' . get_class($debug));
}
/**
* Shows exception
*
@ -197,9 +229,9 @@ class JpGraph extends View {
protected function render() {
$this->graph->SetMargin(75, (count($this->axes) - 1) * 65 + 10, 20, 90);
// Display the graph
//$this->graph->Stroke();
// display the graph
$this->graph->Stroke();
}
}
?>
?>

View file

@ -24,9 +24,6 @@
namespace Volkszaehler\View;
use Volkszaehler\Interpreter;
use Volkszaehler\Iterator;
use Volkszaehler\Model;
use Volkszaehler\View\HTTP;
use Volkszaehler\Util;
@ -36,7 +33,6 @@ use Volkszaehler\Util;
*
* @package default
* @author Steffen Vogel <info@steffenvogel.de>
*
*/
abstract class View {
/**
@ -88,7 +84,8 @@ abstract class View {
* @param \Exception $exception
*/
final public function exceptionHandler(\Exception $exception) {
$this->addException($exception, Util\Debug::isActivated());
$this->add($exception);
echo $exception;
$code = ($exception->getCode() == 0 || !HTTP\Response::getCodeDescription($exception->getCode())) ? 400 : $exception->getCode();
$this->response->setCode($code);
@ -97,40 +94,18 @@ abstract class View {
die();
}
/**
* Render response and send it to the client
*/
public function send() {
if (Util\Debug::isActivated()) {
$this->addDebug(Util\Debug::getInstance());
$this->add(Util\Debug::getInstance());
}
$this->render();
$this->response->send();
}
/**
* Add object to output
*
* @param mixed $data
*/
public function add($data) {
if (isset($data)) {
if ($data instanceof Interpreter\InterpreterInterface) {
$this->addData($data);
}
elseif ($data instanceof Model\Entity) {
$this->addEntity($data);
}
elseif ($data instanceof \Exception) {
$this->addException($data);
}
elseif ($data instanceof Util\Debug) {
$this->addDebug($data);
}
else {
throw new \Exception('Can\'t show ' . get_class($data));
}
}
}
/**
* Sets caching mode for the browser
*
@ -141,7 +116,7 @@ abstract class View {
public function setCaching($mode, $value) {
switch ($mode) {
case 'modified': // Last-modified
//$this->response->setHeader('Last-Modified', gmdate('D, d M Y H:i:s', $value) . ' GMT');
$this->response->setHeader('Last-Modified', gmdate('D, d M Y H:i:s', $value) . ' GMT');
case 'etag': // Etag
throw new Exception('This caching mode is not implemented');
@ -150,7 +125,7 @@ abstract class View {
$this->response->setHeader('Expires', gmdate('D, d M Y H:i:s', $value) . ' GMT');
break;
case 'age': // Cache-control: max-age=
case 'age': // Cache-control: max-age=
$this->response->setHeader('Cache-control', 'max-age=' . $value);
break;
@ -160,16 +135,12 @@ abstract class View {
$this->response->setHeader('Pragma', 'no-cache');
default:
throw new Exception('Unknown caching mode');
throw new Exception('Unknown caching mode: ' . $mode);
}
}
public abstract function add($object);
protected abstract function render();
protected abstract function addData(Interpreter\InterpreterInterface $data);
protected abstract function addEntity(Model\Entity $entity);
protected abstract function addException(\Exception $exception);
protected abstract function addDebug(Util\Debug $debug);
}
?>

View file

@ -62,6 +62,32 @@ class XML extends View {
$this->xmlDoc->appendChild($this->xmlRoot);
}
/**
* Add object to output
*
* @param mixed $data
*/
public function add($data) {
if ($data instanceof Interpreter\InterpreterInterface) {
$this->addData($data);
}
elseif ($data instanceof Model\Entity) {
$this->addEntity($data);
}
elseif ($data instanceof \Exception) {
//$this->addException($data);
}
elseif ($data instanceof Util\Debug) {
//$this->addDebug($data);
}
elseif (is_array($data)) {
$this->xmlRoot->appendChild($this->convertArray($data));
}
else {
throw new \Exception('Can\'t show ' . get_class($data));
}
}
/**
* Process, encode and print output
*
@ -155,12 +181,16 @@ class XML extends View {
*/
protected function addException(\Exception $exception) {
$xmlException = $this->xmlDoc->createElement('exception');
$xmlException->setAttribute('code', $exception->getCode());
$xmlException->setAttribute('type', get_class($exception));
$xmlException->appendChild($this->xmlDoc->createElement('message', $exception->getMessage()));
$xmlException->appendChild($this->xmlDoc->createElement('line', $exception->getLine()));
$xmlException->appendChild($this->xmlDoc->createElement('file', $exception->getFile()));
$xmlException->appendChild($this->convertTrace($exception->getTrace()));
if (Util\Debug::isActivated()) {
$xmlException->appendChild($this->xmlDoc->createElement('file', $exception->getFile()));
$xmlException->appendChild($this->xmlDoc->createElement('line', $exception->getLine()));
$xmlException->appendChild($this->convertTrace($exception->getTrace()));
}
$this->xmlRoot->appendChild($xmlException);
}
@ -182,7 +212,7 @@ class XML extends View {
$xmlTuples->appendChild($xmlTuple);
}
$xmlData->appendChild($this->xmlDoc->createElement('uuid', $interpreter->getUuid()));
$xmlData->appendChild($this->xmlDoc->createElement('uuid', $interpreter->getEntity()->getUuid()));
$xmlData->appendChild($this->xmlDoc->createElement('count', count($data)));
$xmlData->appendChild($this->xmlDoc->createElement('first', (isset($data[0][0])) ? $data[0][0] : NULL));
$xmlData->appendChild($this->xmlDoc->createElement('last', (isset($data[count($data)-1][0])) ? $data[count($data)-1][0] : NULL));
@ -265,18 +295,6 @@ class XML extends View {
return $xmlTraces;
}
/**
* Overloaded to support arrays
*/
public function add($data) {
if (is_array($data)) {
$this->xmlRoot->appendChild($this->convertArray($data));
}
else {
parent::add($data);
}
}
}
?>