adapted controllers to new layout

This commit is contained in:
Steffen Vogel 2010-09-19 20:47:29 +02:00
parent 43208c065a
commit 0d056ee51f
5 changed files with 111 additions and 127 deletions

View file

@ -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;
}
}
?>

View file

@ -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;
}
}
?>

View file

@ -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);
}
}

View file

@ -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();
}

View file

@ -32,113 +32,44 @@ use Volkszaehler\Model;
* @author Steffen Vogel <info@steffenvogel.de>
* @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;
}
}