adapted controllers to new mvc implementation
added some operations
This commit is contained in:
parent
c3cc6987ca
commit
208f8ad12b
5 changed files with 121 additions and 39 deletions
|
@ -32,41 +32,89 @@ namespace Volkszaehler\Controller;
|
|||
use Volkszaehler\Model;
|
||||
|
||||
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';
|
||||
$aggregator = parent::get($identifier);
|
||||
|
||||
$q = $this->em->createQuery($dql);
|
||||
$q->setParameter(1, $identifier);
|
||||
|
||||
return $q->getSingleResult();
|
||||
if ($aggregator instanceof Model\Aggregator) {
|
||||
return $aggregator;
|
||||
}
|
||||
else {
|
||||
throw new \Exception($identifier . ' is not a group uuid');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add aggregator
|
||||
* Create new aggregator or add entity to aggregator
|
||||
*/
|
||||
public function add() {
|
||||
$aggregator = new Model\Aggregator('group'); // TODO support for other aggregator types
|
||||
public function add($identifier = NULL) {
|
||||
if (isset($identifier)) { // add entity to aggregator
|
||||
$aggregator = $this->get($identifier);
|
||||
|
||||
foreach ($this->view->request->getParameters() as $parameter => $value) {
|
||||
if (Model\PropertyDefinition::exists($parameter)) {
|
||||
$aggregator->setProperty($parameter, $value);
|
||||
if ($uuid = $this->view->request->getParameter('uuid')) {
|
||||
$ec = new EntityController($this->view, $this->em);
|
||||
$entity = $ec->get($uuid);
|
||||
|
||||
if ($entity instanceof Model\Channel) {
|
||||
$aggregator->addChannel($entity);
|
||||
}
|
||||
elseif ($entity instanceof Model\Aggregator) {
|
||||
$aggregator->addAggregator($entity);
|
||||
}
|
||||
}
|
||||
else { // create new aggregator
|
||||
throw new \Exception('You have to specifiy a uuid to add');
|
||||
}
|
||||
}
|
||||
else {
|
||||
$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->persist($aggregator);
|
||||
$this->em->flush();
|
||||
|
||||
return $aggregator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete Aggregator or remove entity from aggregator
|
||||
*/
|
||||
public function delete($identifier) {
|
||||
if (isset($identifier) && $uuid = $this->view->request->getParameter('uuid')) { // remove entity from aggregator
|
||||
$aggregator = $this->get($identifier);
|
||||
|
||||
if ($uuid) {
|
||||
$ec = new EntityController($this->view, $this->em);
|
||||
$entity = $ec->get($uuid);
|
||||
|
||||
if ($entity instanceof Model\Channel) {
|
||||
$aggregator->removeChannel($entity);
|
||||
}
|
||||
elseif ($entity instanceof Model\Aggregator) {
|
||||
$aggregator->removeAggregator($entity);
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
else { // remove aggregator
|
||||
throw new \Exception('You have to specifiy a uuid to remove');
|
||||
}
|
||||
}
|
||||
else {
|
||||
parent::delete($identifier);
|
||||
}
|
||||
|
||||
return $aggregator;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -23,6 +23,8 @@
|
|||
|
||||
namespace Volkszaehler\Controller;
|
||||
|
||||
use Volkszaehler\Model;
|
||||
|
||||
/**
|
||||
* Channel controller
|
||||
*
|
||||
|
@ -30,22 +32,18 @@ namespace Volkszaehler\Controller;
|
|||
* @package default
|
||||
*/
|
||||
class ChannelController extends EntityController {
|
||||
|
||||
/**
|
||||
* 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';
|
||||
$channel = parent::get($identifier);
|
||||
|
||||
$q = $this->em->createQuery($dql);
|
||||
$q->setParameter(1, $identifier);
|
||||
|
||||
return $q->getSingleResult();
|
||||
if ($channel instanceof Model\Channel) {
|
||||
return $channel;
|
||||
}
|
||||
else {
|
||||
throw new \Exception($identifier . ' is not a channel uuid');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -49,12 +49,12 @@ abstract class Controller {
|
|||
*
|
||||
* @param string $operation runs the operation if class method is available
|
||||
*/
|
||||
public function run($operation, $params = NULL) {
|
||||
public function run($operation, array $params = array()) {
|
||||
if (!is_callable(array($this, $operation))) {
|
||||
throw new \Exception('Invalid context operation: ' . $operation);
|
||||
}
|
||||
|
||||
return $this->$operation($params);
|
||||
return call_user_func_array(array($this, $operation), $params);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,6 @@ use Volkszaehler\Util;
|
|||
* Data controller
|
||||
*
|
||||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
* @todo call via redirect from Controller\Channel
|
||||
* @package default
|
||||
*/
|
||||
class DataController extends Controller {
|
||||
|
@ -41,16 +40,39 @@ class DataController extends Controller {
|
|||
public function get(Model\Entity $entity) {
|
||||
$from = $this->view->request->getParameter('from');
|
||||
$to = $this->view->request->getParameter('to');
|
||||
$groupBy = $this->view->request->getParameter('groupBy');
|
||||
|
||||
return $entity->getInterpreter($this->em, $from, $to)->getValues($groupBy);
|
||||
return $entity->getInterpreter($this->em, $from, $to);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log new readings with logger interfaces
|
||||
* Sporadic test/demo implemenation
|
||||
*
|
||||
* @todo replace by pluggable api parser
|
||||
*/
|
||||
public function add() {
|
||||
public function add(Model\Channel $channel) {
|
||||
$timestamp = $this->view->request->getParameter('ts');
|
||||
$value = $this->view->request->getParameter('value');
|
||||
|
||||
if (!$timestamp) {
|
||||
$timestamp = round(microtime(TRUE) * 1000);
|
||||
}
|
||||
|
||||
if (!$value) {
|
||||
$value = 1;
|
||||
}
|
||||
|
||||
$data = new Model\Data($channel, $timestamp, $value);
|
||||
|
||||
$channel->addData($data);
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
public function run($operation, array $params = array()) {
|
||||
$ec = new EntityController($this->view, $this->em);
|
||||
$params[0] = $ec->get($params[0]);
|
||||
|
||||
return parent::run($operation, $params);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,13 +32,27 @@ use Volkszaehler\Model;
|
|||
* @author Steffen Vogel <info@steffenvogel.de>
|
||||
* @package default
|
||||
*/
|
||||
abstract class EntityController extends Controller {
|
||||
class EntityController extends Controller {
|
||||
/**
|
||||
* Get entity
|
||||
*
|
||||
* @param unknown_type $identifier
|
||||
* @param string $identifier
|
||||
*/
|
||||
abstract public function get($identifier);
|
||||
public function get($identifier) {
|
||||
$dql = 'SELECT a, p
|
||||
FROM Volkszaehler\Model\Entity a
|
||||
LEFT JOIN a.properties p
|
||||
WHERE a.uuid = ?1';
|
||||
|
||||
$q = $this->em->createQuery($dql);
|
||||
$q->setParameter(1, $identifier);
|
||||
|
||||
try {
|
||||
return $q->getSingleResult();
|
||||
} catch (\Doctrine\ORM\NoResultException $e) {
|
||||
throw new \Exception('No entity found with uuid: ' . $identifier);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete entity by uuid
|
||||
|
|
Loading…
Add table
Reference in a new issue