diff --git a/backend/lib/Controller/AggregatorController.php b/backend/lib/Controller/AggregatorController.php index aed5b26..0138716 100644 --- a/backend/lib/Controller/AggregatorController.php +++ b/backend/lib/Controller/AggregatorController.php @@ -32,53 +32,8 @@ namespace Volkszaehler\Controller; use Volkszaehler\Model; class AggregatorController extends Controller { - - /** - * Get aggregators by filter - * - * @todo filter to root aggregators when using recursion - */ - public function get() { - $dql = 'SELECT g, c, d, p FROM Volkszaehler\Model\Aggregator g LEFT JOIN g.children c LEFT JOIN g.channels d LEFT JOIN g.properties p'; - - // TODO fix this (depending on DDC-719) - if ($recursion = $this->view->request->getParameter('recursive')) { - //$dql .= ' WHERE g.parents IS EMPTY'; - } - - $q = $this->em->createQuery($dql); - $groups = $q->getResult(); - - foreach ($groups as $group) { - $this->view->addAggregator($group, $recursion); - } - } - - /** - * Add new aggregator as child of a parent aggregator - * - * @todo add parent validation to model? - */ - public function add() { - $ugid = $this->view->request->getParameter('ugid'); - $parent = $this->em->getRepository('Volkszaehler\Model\Aggregator')->findOneBy(array('uuid' => $ugid)); - - if ($parent == FALSE) { - throw new \Exception('every group needs a parent'); - } - - $group = new Model\Aggregator(); - - $group->setName($this->view->request->getParameter('name')); - $group->setDescription($this->view->request->getParameter('description')); - - $this->em->persist($group); - $parent->addAggregator($group); - - $this->em->flush(); - - $this->view->add($group); - } +// TODO add/remove channels +// TODO add/remove aggregators } ?> \ No newline at end of file diff --git a/backend/lib/Controller/ChannelController.php b/backend/lib/Controller/ChannelController.php index 386379c..1d9fc59 100644 --- a/backend/lib/Controller/ChannelController.php +++ b/backend/lib/Controller/ChannelController.php @@ -33,24 +33,7 @@ use Volkszaehler\Model; * @package default */ class ChannelController extends EntityController { - /** - * 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)) { - $property = new Model\Property($channel, $parameter, $value); - $channel->setProperty($property); - } - } - - $this->em->persist($channel); - $this->em->flush(); - - $this->view->addChannel($channel); - } +//TODO log data } ?> \ No newline at end of file diff --git a/backend/lib/Controller/DataController.php b/backend/lib/Controller/DataController.php index f48ba9d..fa38857 100644 --- a/backend/lib/Controller/DataController.php +++ b/backend/lib/Controller/DataController.php @@ -33,7 +33,7 @@ use Volkszaehler\Util; * @todo call via redirect from Controller\Channel * @package default */ -class DataController extends Controller { +class DataController extends EntityController { /** * Query for data by given channel or group @@ -42,28 +42,19 @@ class DataController extends Controller { * @todo use uuids for groups or channels */ public function get() { - if ($uuid = $this->view->request->getParameter('uuid')) { - $entity = $this->em->getRepository('Volkszaehler\Model\Entity')->findOneBy(array('uuid' => $uuid)); - } - else { - throw new \Exception('you have to specifiy the uuid parameter'); - } + 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 - if ($entity === FALSE) { - throw new \Exception('no group/channel found'); - } + $data = $this->entity->getInterpreter($this->em, $from, $to)->getValues($groupBy); - $from = $this->view->request->getParameter('from'); - $to = $this->view->request->getParameter('to'); - $groupBy = ($this->view->request->getParameter('groupBy')); // get all readings by default - - $data = $entity->getInterpreter($this->em, $from, $to)->getValues($groupBy); - - if ($entity instanceof Model\Aggregator) { - $this->view->addAggregator($entity, $data); - } - elseif ($entity instanceof Model\Channel) { - $this->view->addChannel($entity, $data); + if ($this->entity instanceof Model\Aggregator) { + $this->view->addAggregator($entity, $data); + } + elseif ($this->entity instanceof Model\Channel) { + $this->view->addChannel($this->entity, $data); + } } } diff --git a/backend/lib/Controller/EntityController.php b/backend/lib/Controller/EntityController.php index 14185fb..5389ea1 100644 --- a/backend/lib/Controller/EntityController.php +++ b/backend/lib/Controller/EntityController.php @@ -45,15 +45,16 @@ class EntityController extends Controller { WHERE e.uuid LIKE \'%' . $identifier . '\''; $q = $this->em->createQuery($dql); + $result = $q->getResult(); - if (count($q->getResult() == 1)) { - $this->entitiy = $q->getFirstResult(); + if (count($result) == 1) { + $this->entity = reset($result); } - elseif (count($q->getResult() > 1)) { - throw new Exception('identifier is not unique'); + elseif (count($result) > 1) { + throw new \Exception('identifier is not unique'); } - elseif (count($q->getResult() < 1)) { - throw new Exception('no entity found'); + elseif (count($result) < 1) { + throw new \Exception('no entity found'); } } } @@ -65,10 +66,8 @@ class EntityController extends Controller { * @todo implement filters */ public function get() { - Util\Debug::log('unique', print_r($this->entity, TRUE)); - if (isset($this->entity)) { - + // distinction between channels & aggregators $this->view->addChannel($this->entity); } else { @@ -90,19 +89,21 @@ class EntityController extends Controller { * Add channel */ public function add() { - $channel = new Model\Channel($this->view->request->getParameter('type')); + if (is_null($this->entity)) { + $channel = new Model\Channel($this->view->request->getParameter('type')); - 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)) { + $property = new Model\Property($channel, $parameter, $value); + $channel->setProperty($property); + } } + + $this->em->persist($channel); + $this->em->flush(); + + $this->view->addChannel($channel); } - - $this->em->persist($channel); - $this->em->flush(); - - $this->view->addChannel($channel); } /** @@ -111,11 +112,10 @@ class EntityController extends Controller { * @todo authentification/indentification */ public function delete() { - $ucid = $this->view->request->getParameter('ucid'); - $channel = $this->em->getRepository('Volkszaehler\Model\Channel')->findOneBy(array('uuid' => $ucid)); - - $this->em->remove($channel); - $this->em->flush(); + if (isset($this->entity)) { + $this->em->remove($this->entity); + $this->em->flush(); + } } /** @@ -125,7 +125,20 @@ class EntityController extends Controller { * @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); + } } }