From 0abae7fa5b6dea2db818ec2383a4c5a03c233f73 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sun, 5 Sep 2010 22:16:41 +0200 Subject: [PATCH] reworked controllers --- .../lib/Controller/AggregatorController.php | 30 ++---------- backend/lib/Controller/Controller.php | 2 +- backend/lib/Controller/EntityController.php | 47 +++++++++++++++---- backend/lib/Dispatcher.php | 18 +++---- 4 files changed, 54 insertions(+), 43 deletions(-) diff --git a/backend/lib/Controller/AggregatorController.php b/backend/lib/Controller/AggregatorController.php index cae35fc..aed5b26 100644 --- a/backend/lib/Controller/AggregatorController.php +++ b/backend/lib/Controller/AggregatorController.php @@ -24,19 +24,19 @@ namespace Volkszaehler\Controller; /** - * Group controller + * Aggregator controller * * @author Steffen Vogel (info@steffenvogel.de) * @package default */ use Volkszaehler\Model; -class GroupController extends Controller { +class AggregatorController extends Controller { /** - * Get groups by filter + * Get aggregators by filter * - * @todo filter to root groups when using recursion + * @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'; @@ -55,7 +55,7 @@ class GroupController extends Controller { } /** - * Add new group as child of a parent group + * Add new aggregator as child of a parent aggregator * * @todo add parent validation to model? */ @@ -79,26 +79,6 @@ class GroupController extends Controller { $this->view->add($group); } - - /** - * @todo authentification/indentification - */ - public function delete() { - $ugid = $this->view->request->getParameter('ugid'); - $group = $this->em->getRepository('Volkszaehler\Model\Aggregator')->findOneBy(array('uuid' => $ugid)); - - $this->em->remove($group); - $this->em->flush(); - } - - /** - * edit group properties - * - * @todo implement Controller\Aggregator::edit() - */ - public function edit() { - - } } ?> \ No newline at end of file diff --git a/backend/lib/Controller/Controller.php b/backend/lib/Controller/Controller.php index e2ed70b..9a2edcf 100644 --- a/backend/lib/Controller/Controller.php +++ b/backend/lib/Controller/Controller.php @@ -50,7 +50,7 @@ abstract class Controller { * @param string $action runs the action if class method is available */ public function run($action) { - if (!method_exists($this, $action)) { + if (!is_callable(array($this, $action))) { throw new \Exception('\'' . $action . '\' is not a valid controller action'); } diff --git a/backend/lib/Controller/EntityController.php b/backend/lib/Controller/EntityController.php index 0a26de9..14185fb 100644 --- a/backend/lib/Controller/EntityController.php +++ b/backend/lib/Controller/EntityController.php @@ -33,25 +33,56 @@ use Volkszaehler\Model; * @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); + + if (count($q->getResult() == 1)) { + $this->entitiy = $q->getFirstResult(); + } + elseif (count($q->getResult() > 1)) { + throw new Exception('identifier is not unique'); + } + elseif (count($q->getResult() < 1)) { + throw new Exception('no entity found'); + } + } + } /** - * Get channels by filter + * Get Entities * * @todo authentification/indentification * @todo implement filters */ public function get() { - $dql = 'SELECT c, p FROM Volkszaehler\Model\Channel c LEFT JOIN c.properties p'; + Util\Debug::log('unique', print_r($this->entity, TRUE)); - if ($uuid = $this->view->request->getParameter('uuid')) { - $dql .= ' WHERE c.uuid = \'' . $uuid . '\''; + if (isset($this->entity)) { + + $this->view->addChannel($this->entity); } + else { + $dql = 'SELECT e, p + FROM Volkszaehler\Model\Channel e + LEFT JOIN e.properties p'; - $q = $this->em->createQuery($dql); - $channels = $q->getResult(); + $q = $this->em->createQuery($dql); + $entities = $q->getResult(); - foreach ($channels as $channel) { - $this->view->addChannel($channel); + foreach ($entities as $entity) { + // distinction between channels & aggregators + $this->view->addChannel($entity); + } } } diff --git a/backend/lib/Dispatcher.php b/backend/lib/Dispatcher.php index b1938ec..a5aa5f6 100644 --- a/backend/lib/Dispatcher.php +++ b/backend/lib/Dispatcher.php @@ -83,7 +83,7 @@ class Dispatcher { // initialize entity manager $this->em = Dispatcher::createEntityManager(); - // starting debugging + // initialize debugging if (($debugLevel = $request->getParameter('debug')) != NULL || $debugLevel = Util\Configuration::read('debug')) { if ($debugLevel > 0) { $this->debug = new Util\Debug($debugLevel, $this->em); @@ -108,15 +108,14 @@ class Dispatcher { $this->view = new View\JpGraph($request, $response, $format); break; - case 'json': case 'xml': case 'csv': - $viewClassName = 'Volkszaehler\View\\' . strtoupper($format); - if (!(Util\ClassLoader::classExists($viewClassName)) || !is_subclass_of($viewClassName, '\Volkszaehler\View\View')) { - throw new \Exception('\'' . $viewClassName . '\' is not a valid View'); - } + $padding = FALSE; + case 'json': + $padding = $request->getParameter('padding'); - $this->view = new $viewClassName($request, $response); + $viewClassName = 'Volkszaehler\View\\' . strtoupper($format); + $this->view = new $viewClassName($request, $response, $padding); break; case 'txt': @@ -130,9 +129,9 @@ class Dispatcher { // initialize controller if (!($controllerClassName = $this->router->getController())) { - throw new \Exception('no controller specified'); + throw new \Exception('invalid controller specified'); } - $this->controller = new $controllerClassName($this->view, $this->em); + $this->controller = new $controllerClassName($this->view, $this->em, $this->router->getIdentifier()); } /** @@ -157,6 +156,7 @@ class Dispatcher { * Factory for doctrines entitymanager * * @todo add other caching drivers (memcache, xcache) + * @todo put into static class? singleton? */ public static function createEntityManager() { $config = new \Doctrine\ORM\Configuration;