reworked controllers
This commit is contained in:
parent
5a916e5d46
commit
0abae7fa5b
4 changed files with 54 additions and 43 deletions
|
@ -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() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue