From 5d394c1cccf94d031abe9aa79742bd5965820b46 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 20 Jul 2010 00:31:38 +0200 Subject: [PATCH] improved view components added first draft of csv output view --- .../lib/View/{Json/Data.php => Csv/Csv.php} | 31 ++++++++++++++++--- backend/lib/View/JpGraph.php | 5 ++- backend/lib/View/Json/Channel.php | 6 ++-- backend/lib/View/Json/Group.php | 2 +- backend/lib/View/{ => Json}/Json.php | 0 backend/lib/View/View.php | 4 +++ backend/lib/View/{ => Xml}/Xml.php | 0 7 files changed, 39 insertions(+), 9 deletions(-) rename backend/lib/View/{Json/Data.php => Csv/Csv.php} (56%) rename backend/lib/View/{ => Json}/Json.php (100%) rename backend/lib/View/{ => Xml}/Xml.php (100%) diff --git a/backend/lib/View/Json/Data.php b/backend/lib/View/Csv/Csv.php similarity index 56% rename from backend/lib/View/Json/Data.php rename to backend/lib/View/Csv/Csv.php index 3c761d7..471671e 100644 --- a/backend/lib/View/Json/Data.php +++ b/backend/lib/View/Csv/Csv.php @@ -19,11 +19,34 @@ * http://www.gnu.org/copyleft/gpl.html */ -namespace Volkszaehler\View\Json; +namespace Volkszaehler\View\Csv; -class Data extends \Volkszaehler\View\Json { - public function add($data) { - $this->json['data'][] = $data; +class Csv extends \Volkszaehler\View\View { + + /* + * constructor + */ + public function __construct(Http\Request $request, Http\Response $response) { + parent::__construct($request, $response); + + $this->csv['source'] = 'volkszaehler.org'; + $this->csv['version'] = \Volkszaehler\VERSION; + + $this->response->setHeader('Content-type', 'text/csv'); + } + + public function render() { + parent::render(); + + // TODO implement + } + + public function addDebug() { + // TODO implement debug output for csv view + } + + public function addException(\Exception $exception) { + // TODO implement exception output for csv view } } diff --git a/backend/lib/View/JpGraph.php b/backend/lib/View/JpGraph.php index 1cfe132..448bf3c 100644 --- a/backend/lib/View/JpGraph.php +++ b/backend/lib/View/JpGraph.php @@ -57,7 +57,10 @@ class JpGraph extends View { //$this->graph->img->SetAntiAliasing(); } - public function add($data) { + /* + * @todo add title unit etc.. + */ + public function add(\Volkszaehler\Model\Channel $obj, $data = NULL) { $xData = $yData = array(); foreach ($data as $reading) { $xData[] = $reading['timestamp']/1000; diff --git a/backend/lib/View/Json/Channel.php b/backend/lib/View/Json/Channel.php index 9e4e1b8..024f674 100644 --- a/backend/lib/View/Json/Channel.php +++ b/backend/lib/View/Json/Channel.php @@ -21,17 +21,17 @@ namespace Volkszaehler\View\Json; -class Channel extends \Volkszaehler\View\Json { +class Channel extends Json { - public function add(\Volkszaehler\Model\Channel\Channel $obj, $data = NULL) { + public function add(\Volkszaehler\Model\Channel $obj, $data = NULL) { $channel['id'] = (int) $obj->getId(); $channel['uuid'] = (string) $obj->getUuid(); - $channel['type'] = strtolower(substr(strrchr(get_class($obj), '\\'), 1)); $channel['indicator'] = $obj->getIndicator(); $channel['unit'] = $obj->getUnit(); $channel['name'] = $obj->getName(); $channel['description'] = $obj->getDescription(); + // TODO adapt to new indicator style if (is_subclass_of($obj, '\Volkszaehler\Model\Channel\Meter')) { $channel['resolution'] = (int) $obj->getResolution(); $channel['cost'] = (float) $obj->getCost(); diff --git a/backend/lib/View/Json/Group.php b/backend/lib/View/Json/Group.php index 5cd6034..76a5909 100644 --- a/backend/lib/View/Json/Group.php +++ b/backend/lib/View/Json/Group.php @@ -21,7 +21,7 @@ namespace Volkszaehler\View\Json; -class Group extends \Volkszaehler\View\Json { +class Group extends Json { public function add(\Volkszaehler\Model\Group $obj, $recursive = false) { $group['id'] = (int) $obj->getId(); diff --git a/backend/lib/View/Json.php b/backend/lib/View/Json/Json.php similarity index 100% rename from backend/lib/View/Json.php rename to backend/lib/View/Json/Json.php diff --git a/backend/lib/View/View.php b/backend/lib/View/View.php index 9d54236..7081b47 100644 --- a/backend/lib/View/View.php +++ b/backend/lib/View/View.php @@ -46,6 +46,7 @@ abstract class View implements ViewInterface { /* * creates new view instance depending on the requested format + * @todo improve mapping */ public static function factory(Http\Request $request, Http\Response $response) { $format = strtolower($request->getParameter('format')); @@ -55,6 +56,9 @@ abstract class View implements ViewInterface { $view = new JpGraph($request, $response, $format); } else { + if ($controller == 'data' && $format == 'json') { + $controller = 'channel'; + } $viewClassName = 'Volkszaehler\View\\' . ucfirst($format) . '\\' . ucfirst($controller); if (!(\Volkszaehler\Util\ClassLoader::classExists($viewClassName)) || !is_subclass_of($viewClassName, '\Volkszaehler\View\View')) { diff --git a/backend/lib/View/Xml.php b/backend/lib/View/Xml/Xml.php similarity index 100% rename from backend/lib/View/Xml.php rename to backend/lib/View/Xml/Xml.php