From 0d056ee51f49102e6d2512804a91dc37668ccf95 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sun, 19 Sep 2010 20:47:29 +0200 Subject: [PATCH] adapted controllers to new layout --- .../lib/Controller/AggregatorController.php | 39 +++++- backend/lib/Controller/ChannelController.php | 39 +++++- backend/lib/Controller/Controller.php | 12 +- backend/lib/Controller/DataController.php | 33 ++--- backend/lib/Controller/EntityController.php | 115 ++++-------------- 5 files changed, 111 insertions(+), 127 deletions(-) diff --git a/backend/lib/Controller/AggregatorController.php b/backend/lib/Controller/AggregatorController.php index 0138716..6d0ea4b 100644 --- a/backend/lib/Controller/AggregatorController.php +++ b/backend/lib/Controller/AggregatorController.php @@ -31,9 +31,42 @@ namespace Volkszaehler\Controller; */ use Volkszaehler\Model; -class AggregatorController extends Controller { -// TODO add/remove channels -// TODO add/remove aggregators +class AggregatorController extends EntityController { + + /** + * Get aggregator + * + * @param string $identifier + */ + public function get($identifier) { + $dql = 'SELECT a, p + FROM Volkszaehler\Model\Aggregator a + LEFT JOIN a.properties p + WHERE a.uuid = ?1'; + + $q = $this->em->createQuery($dql); + $q->setParameter(1, $identifier); + + return $q->getSingleResult(); + } + + /** + * Add aggregator + */ + public function add() { + $aggregator = new Model\Aggregator('group'); // TODO support for other aggregator types + + foreach ($this->view->request->getParameters() as $parameter => $value) { + if (Model\PropertyDefinition::exists($parameter)) { + $aggregator->setProperty($parameter, $value); + } + } + + $this->em->persist($aggregator); + $this->em->flush(); + + return $aggregator; + } } ?> \ No newline at end of file diff --git a/backend/lib/Controller/ChannelController.php b/backend/lib/Controller/ChannelController.php index 1d9fc59..7066642 100644 --- a/backend/lib/Controller/ChannelController.php +++ b/backend/lib/Controller/ChannelController.php @@ -23,9 +23,6 @@ namespace Volkszaehler\Controller; -use Volkszaehler\Util; -use Volkszaehler\Model; - /** * Channel controller * @@ -33,7 +30,41 @@ use Volkszaehler\Model; * @package default */ class ChannelController extends EntityController { -//TODO log data + + /** + * Get channel + * + * @param string $identifier + */ + public function get($identifier) { + $dql = 'SELECT c, p + FROM Volkszaehler\Model\Channel c + LEFT JOIN c.properties p + WHERE c.uuid = ?1'; + + $q = $this->em->createQuery($dql); + $q->setParameter(1, $identifier); + + return $q->getSingleResult(); + } + + /** + * Add channel + */ + public function add() { + $channel = new Model\Channel($this->view->request->getParameter('type')); + + foreach ($this->view->request->getParameters() as $parameter => $value) { + if (Model\PropertyDefinition::exists($parameter)) { + $channel->setProperty($parameter, $value); + } + } + + $this->em->persist($channel); + $this->em->flush(); + + return $channel; + } } ?> \ No newline at end of file diff --git a/backend/lib/Controller/Controller.php b/backend/lib/Controller/Controller.php index 9a2edcf..92bcda0 100644 --- a/backend/lib/Controller/Controller.php +++ b/backend/lib/Controller/Controller.php @@ -45,16 +45,16 @@ abstract class Controller { } /** - * Run controller actions + * Run operation * - * @param string $action runs the action if class method is available + * @param string $operation runs the operation if class method is available */ - public function run($action) { - if (!is_callable(array($this, $action))) { - throw new \Exception('\'' . $action . '\' is not a valid controller action'); + public function run($operation, $params = NULL) { + if (!is_callable(array($this, $operation))) { + throw new \Exception('Invalid context operation: ' . $operation); } - $this->$action(); + return $this->$operation($params); } } diff --git a/backend/lib/Controller/DataController.php b/backend/lib/Controller/DataController.php index fa38857..84ac0df 100644 --- a/backend/lib/Controller/DataController.php +++ b/backend/lib/Controller/DataController.php @@ -33,42 +33,31 @@ use Volkszaehler\Util; * @todo call via redirect from Controller\Channel * @package default */ -class DataController extends EntityController { +class DataController extends Controller { /** * Query for data by given channel or group - * - * @todo authentification/indentification - * @todo use uuids for groups or channels */ - public function get() { - if (isset($this->entity)) { - $from = $this->view->request->getParameter('from'); - $to = $this->view->request->getParameter('to'); - $groupBy = ($this->view->request->getParameter('groupBy')); // get all readings by default + public function get(Model\Entity $entity) { + $from = $this->view->request->getParameter('from'); + $to = $this->view->request->getParameter('to'); + $groupBy = $this->view->request->getParameter('groupBy'); - $data = $this->entity->getInterpreter($this->em, $from, $to)->getValues($groupBy); - - if ($this->entity instanceof Model\Aggregator) { - $this->view->addAggregator($entity, $data); - } - elseif ($this->entity instanceof Model\Channel) { - $this->view->addChannel($this->entity, $data); - } - } + return $entity->getInterpreter($this->em, $from, $to)->getValues($groupBy); } /** * Log new readings with logger interfaces * * @todo authentification/indentification + * @todo reimplement */ public function add() { - $loggerClassName = 'Volkszaehler\Logger\\' . ucfirst($this->view->request->getParameter('logger')) . 'Logger'; - if (!(Util\ClassLoader::classExists($loggerClassName)) || !is_subclass_of($loggerClassName, '\Volkszaehler\Logger\Logger')) { - throw new \Exception('\'' . $loggerClassName . '\' is not a valid controller'); + $class = 'Volkszaehler\Logger\\' . ucfirst($this->view->request->getParameter('logger')) . 'Logger'; + if (!(Util\ClassLoader::classExists($class)) || !is_subclass_of($class, '\Volkszaehler\Logger\Logger')) { + throw new \Exception('Unkown logger: ' . $class); } - $logger = new $loggerClassName($this->view->request, $this->em); + $logger = new $class($this->view->request, $this->em); $logger->log(); } diff --git a/backend/lib/Controller/EntityController.php b/backend/lib/Controller/EntityController.php index 5389ea1..af02839 100644 --- a/backend/lib/Controller/EntityController.php +++ b/backend/lib/Controller/EntityController.php @@ -32,113 +32,44 @@ use Volkszaehler\Model; * @author Steffen Vogel * @package default */ -class EntityController extends Controller { - protected $entity = NULL; - - public function __construct(\Volkszaehler\View\View $view, \Doctrine\ORM\EntityManager $em, $identifier) { - parent::__construct($view, $em); - - if ($identifier) { - $dql = 'SELECT e, p - FROM Volkszaehler\Model\Entity e - LEFT JOIN e.properties p - WHERE e.uuid LIKE \'%' . $identifier . '\''; - - $q = $this->em->createQuery($dql); - $result = $q->getResult(); - - if (count($result) == 1) { - $this->entity = reset($result); - } - elseif (count($result) > 1) { - throw new \Exception('identifier is not unique'); - } - elseif (count($result) < 1) { - throw new \Exception('no entity found'); - } - } - } - +abstract class EntityController extends Controller { /** - * Get Entities + * Get entity * - * @todo authentification/indentification - * @todo implement filters + * @param unknown_type $identifier */ - public function get() { - if (isset($this->entity)) { - // distinction between channels & aggregators - $this->view->addChannel($this->entity); - } - else { - $dql = 'SELECT e, p - FROM Volkszaehler\Model\Channel e - LEFT JOIN e.properties p'; + abstract public function get($identifier); - $q = $this->em->createQuery($dql); - $entities = $q->getResult(); + /** + * Delete entity by uuid + */ + public function delete($identifier) { + $entity = $this->get($identifier); - foreach ($entities as $entity) { - // distinction between channels & aggregators - $this->view->addChannel($entity); - } - } + $this->em->remove($entity); + $this->em->flush(); } /** - * Add channel + * Edit entity properties */ - public function add() { - if (is_null($this->entity)) { - $channel = new Model\Channel($this->view->request->getParameter('type')); + public function edit($identifier) { + $entity = $this->get($identifier); - foreach ($this->view->request->getParameters() as $parameter => $value) { - if (Model\PropertyDefinition::exists($parameter)) { - $property = new Model\Property($channel, $parameter, $value); - $channel->setProperty($property); + foreach ($this->view->request->getParameters() as $parameter => $value) { + if (Model\PropertyDefinition::exists($parameter)) { + if ($value == '') { + $entity->unsetProperty($parameter, $this->em); + } + else { + $entity->setProperty($parameter, $value); } } - - $this->em->persist($channel); - $this->em->flush(); - - $this->view->addChannel($channel); } - } - /** - * Delete channel by uuid - * - * @todo authentification/indentification - */ - public function delete() { - if (isset($this->entity)) { - $this->em->remove($this->entity); - $this->em->flush(); - } - } + $this->em->flush(); - /** - * Edit channel properties - * - * @todo authentification/indentification - * @todo to be implemented - */ - public function edit() { - if (isset($this->entity)) { - foreach ($this->view->request->getParameters() as $parameter => $value) { - if (Model\PropertyDefinition::exists($parameter)) { - $property = new Model\Property($channel, $parameter, $value); - $this->entity->setProperty($property); - } - } - - $this->em->persist($channel); - $this->em->flush(); - - // distinction between channels & aggregators - $this->view->addChannel($channel); - } + return $entity; } }