From 0c08355f58bf4f04d79992dbbdae7fbf2202e440 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Thu, 15 Dec 2011 19:41:59 +0100 Subject: [PATCH 1/8] Add Entities repositories --- .../Repositories/BasketRepository.php | 64 +++++++++++++++++++ .../Repositories/StoryWorkZoneRepository.php | 38 +++++++++++ .../ValidationParticipantRepository.php | 26 ++++++++ 3 files changed, 128 insertions(+) create mode 100644 lib/Doctrine/Repositories/BasketRepository.php create mode 100644 lib/Doctrine/Repositories/StoryWorkZoneRepository.php create mode 100644 lib/Doctrine/Repositories/ValidationParticipantRepository.php diff --git a/lib/Doctrine/Repositories/BasketRepository.php b/lib/Doctrine/Repositories/BasketRepository.php new file mode 100644 index 0000000000..dc78cc85f0 --- /dev/null +++ b/lib/Doctrine/Repositories/BasketRepository.php @@ -0,0 +1,64 @@ +_em->createQuery($dql); + $query->setParameters(array(':usr_id' => $user->get_id())); + + return $query->getResult(); + } + + + /** + * Returns all baskets that are in validation session not expired and + * where a specified user is participant (not owner) + * + * @param \User_Adapter $user + * @return \Doctrine\Common\Collections\ArrayCollection + */ + public function findActiveValidationByUser(\User_Adapter $user) + { + $dql = 'SELECT b FROM Entities\Basket b + JOIN b.Entities\ValidationSession s + JOIN s.Entities\ValidationParticipant + WHERE b.usr_id != ?1 AND s.usr_id = ?2 + AND s.expires > CURRENT_TIMESTAMP()'; + + $query = $this->_em->createQuery($dql); + $query->setParameters(array(1 => $user->get_id(), 2 => $user->get_id())); + + return $query->getResult(); + } + +} diff --git a/lib/Doctrine/Repositories/StoryWorkZoneRepository.php b/lib/Doctrine/Repositories/StoryWorkZoneRepository.php new file mode 100644 index 0000000000..08a36f9822 --- /dev/null +++ b/lib/Doctrine/Repositories/StoryWorkZoneRepository.php @@ -0,0 +1,38 @@ +findBy(array('usr_id'=>$user->get_id())); + } + +} + diff --git a/lib/Doctrine/Repositories/ValidationParticipantRepository.php b/lib/Doctrine/Repositories/ValidationParticipantRepository.php new file mode 100644 index 0000000000..cab02ba7d7 --- /dev/null +++ b/lib/Doctrine/Repositories/ValidationParticipantRepository.php @@ -0,0 +1,26 @@ + Date: Thu, 15 Dec 2011 19:43:05 +0100 Subject: [PATCH 2/8] Add Kernel::isAuthenticated and Kernel::getAuthenticatedUser --- lib/Alchemy/Phrasea/Kernel.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/Alchemy/Phrasea/Kernel.php b/lib/Alchemy/Phrasea/Kernel.php index def0da5021..8c36be442a 100644 --- a/lib/Alchemy/Phrasea/Kernel.php +++ b/lib/Alchemy/Phrasea/Kernel.php @@ -91,6 +91,10 @@ class Kernel extends \Pimple return $this['Request']; } + /** + * + * @return \Registry + */ public function getRegistry() { return $this['Registry']; @@ -113,6 +117,29 @@ class Kernel extends \Pimple { return $this['Version']; } + + /** + * + * @return boolean + */ + public function isAuthenticated() + { + $session = \Session_Handler::getInstance(\appbox::get_instance()); + + return $session->is_authenticated(); + } + + /** + * + * @return \User_adapter + */ + public function getAuthenticatedUser() + { + $appbox = \appbox::get_instance(); + $session = \Session_Handler::getInstance($appbox); + + return \User_Adapter::getInstance($session->get_usr_id(), $appbox); + } protected function verifyTimezone() { From 1f93a6e17f3e0cea672f34204318ad659e9508f3 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Thu, 15 Dec 2011 19:50:17 +0100 Subject: [PATCH 3/8] Route processors --- .../Phrasea/RouteProcessor/Basket/Root.php | 51 +++++ .../Phrasea/RouteProcessor/RouteAbstract.php | 205 ++++++++++++++++++ .../Phrasea/RouteProcessor/WorkZone/Root.php | 40 ++++ 3 files changed, 296 insertions(+) create mode 100644 lib/Alchemy/Phrasea/RouteProcessor/Basket/Root.php create mode 100644 lib/Alchemy/Phrasea/RouteProcessor/RouteAbstract.php create mode 100644 lib/Alchemy/Phrasea/RouteProcessor/WorkZone/Root.php diff --git a/lib/Alchemy/Phrasea/RouteProcessor/Basket/Root.php b/lib/Alchemy/Phrasea/RouteProcessor/Basket/Root.php new file mode 100644 index 0000000000..e4c41c22aa --- /dev/null +++ b/lib/Alchemy/Phrasea/RouteProcessor/Basket/Root.php @@ -0,0 +1,51 @@ +getEntityManager(); + + $Basket = new \Entities\Basket(); + $Basket->setName($this->getRequest()->get('name')); + $Basket->setUser($this->getRequest()->get('desc')); + $Basket->setDescription($this->getRequest()->get('desc')); + + $em->persist($Basket); + $em->flush(); + + return new RedirectResponse(sprintf('/%d/', $Basket->getId())); + } + +} diff --git a/lib/Alchemy/Phrasea/RouteProcessor/RouteAbstract.php b/lib/Alchemy/Phrasea/RouteProcessor/RouteAbstract.php new file mode 100644 index 0000000000..ddba9885b5 --- /dev/null +++ b/lib/Alchemy/Phrasea/RouteProcessor/RouteAbstract.php @@ -0,0 +1,205 @@ +requestHandler = $requestHandler; + } + + /** + * + * @return RequestHandlerAbstract + */ + public function getRequestHandler() + { + return $this->requestHandler; + } + + /** + * Getter + * @return Phrasea\Kernel + */ + public function getKernel() + { + return $this->requestHandler->getKernel(); + } + + /** + * Getter + * @return Request + */ + public function getRequest() + { + return $this->getKernel()->getRequest(); + } + + /** + * Getter + * @return EntityManager + */ + public function getEntityManager() + { + return $this->getKernel()->getEntityManager(); + } + + /** + * Getter + * @return \registryInterface + */ + public function getRegistry() + { + return $this->getKernel()->getRegistry(); + } + + /** + * Getter + * @return Response + */ + public function getResponse() + { + if (null === $this->response) + { + $this->process(); + } + + return $this->response; + } + + /** + * + * @return ControllerProcessorAbstract + */ + public function process() + { + $response = null; + + switch (strtoupper($this->getRequest()->getMethod())) + { + case 'POST' : + $response = $this->post(); + break; + case 'PUT' : + $response = $this->put(); + break; + case 'DELETE' : + $response = $this->delete(); + break; + case 'GET' : + $response = $this->get(); + break; + case 'OPTIONS' : + $response = $this->options(); + break; + case 'HEAD' : + $response = $this->head(); + break; + default : + throw new Http\NotImplemented(); + break; + } + + $this->response = $response; + + return $this; + } + + /** + * Handle post action + */ + protected function post() + { + throw new Http\MethodNotAllowed($this->getAllowedMethods()); + } + + /** + * Handle delete action + */ + protected function delete() + { + throw new Http\MethodNotAllowed($this->getAllowedMethods()); + } + + /** + * Handle get action + */ + protected function get() + { + throw new Http\MethodNotAllowed($this->getAllowedMethods()); + } + + /** + * Handle put action + */ + protected function put() + { + throw new Http\MethodNotAllowed($this->getAllowedMethods()); + } + + /** + * Handle options action + */ + protected function options() + { + throw new Http\MethodNotAllowed($this->getAllowedMethods()); + } + + /** + * Handle head action + */ + protected function head() + { + throw new Http\MethodNotAllowed($this->getAllowedMethods()); + } + +} \ No newline at end of file diff --git a/lib/Alchemy/Phrasea/RouteProcessor/WorkZone/Root.php b/lib/Alchemy/Phrasea/RouteProcessor/WorkZone/Root.php new file mode 100644 index 0000000000..40d7936801 --- /dev/null +++ b/lib/Alchemy/Phrasea/RouteProcessor/WorkZone/Root.php @@ -0,0 +1,40 @@ +getRequestHandler()->getContent(); + } + +} From 05f42b05e3893ac43651963f99e7ed92cc18d22e Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Thu, 15 Dec 2011 19:50:57 +0100 Subject: [PATCH 4/8] RequestHandlers --- lib/Alchemy/Phrasea/RequestHandler/Basket.php | 25 +++++++++ .../RequestHandler/RequestHandlerAbstract.php | 40 ++++++++++++++ .../Phrasea/RequestHandler/WorkZone.php | 53 +++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 lib/Alchemy/Phrasea/RequestHandler/Basket.php create mode 100644 lib/Alchemy/Phrasea/RequestHandler/RequestHandlerAbstract.php create mode 100644 lib/Alchemy/Phrasea/RequestHandler/WorkZone.php diff --git a/lib/Alchemy/Phrasea/RequestHandler/Basket.php b/lib/Alchemy/Phrasea/RequestHandler/Basket.php new file mode 100644 index 0000000000..a493c59f52 --- /dev/null +++ b/lib/Alchemy/Phrasea/RequestHandler/Basket.php @@ -0,0 +1,25 @@ +kernel = $kernel; + + return $this; + } + + public function getKernel() + { + return $this->kernel; + } + + +} \ No newline at end of file diff --git a/lib/Alchemy/Phrasea/RequestHandler/WorkZone.php b/lib/Alchemy/Phrasea/RequestHandler/WorkZone.php new file mode 100644 index 0000000000..95ef0b9ccc --- /dev/null +++ b/lib/Alchemy/Phrasea/RequestHandler/WorkZone.php @@ -0,0 +1,53 @@ +kernel->getEntityManager(); + $current_user = $this->kernel->getAuthenticatedUser(); + + /* @var $repo_baskets \Repositories\BasketRepository */ + $repo_baskets = $em->getRepository('Entities\Baskets'); + + /* @var $repo_stories \Repositories\StoryWorkzoneRepository */ + $repo_stories = $em->getRepository('Entities\StoryWorkZone'); + + $ret = new \Doctrine\Common\Collections\ArrayCollection(); + + $baskets = $repo_baskets->findActiveByUser($current_user); + $validations = $repo_baskets->findActiveValidationByUser($current_user); + + $ret->set(self::BASKETS, $baskets); + $ret->set(self::VALIDATIONS, $validations); + $ret->set(self::STORIES, $repo_stories->findByUser($current_user)); + + return $ret; + } + +} + From cc6c0aa5cdb9e6225518aa9bb5fdcfb2ed42f6e2 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Thu, 15 Dec 2011 19:53:13 +0100 Subject: [PATCH 5/8] Basket controller --- .../Phrasea/Controller/Prod/Basket.php | 57 ++++++++----------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Basket.php b/lib/Alchemy/Phrasea/Controller/Prod/Basket.php index f93012611d..7416ccac2e 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Basket.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Basket.php @@ -11,15 +11,16 @@ namespace Alchemy\Phrasea\Controller\Prod; -use Silex\Application; -use Silex\ControllerProviderInterface; -use Silex\ControllerCollection; - -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\HttpKernel\Exception\HttpException; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Silex\Application, + Silex\ControllerProviderInterface, + Silex\ControllerCollection; +use Symfony\Component\HttpFoundation\Request, + Symfony\Component\HttpFoundation\Response, + Symfony\Component\HttpFoundation\RedirectResponse, + Symfony\Component\HttpKernel\Exception\HttpException, + Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Alchemy\Phrasea\RouteProcessor\Basket as BasketRoute, + Alchemy\Phrasea\RequestHandler; /** * @@ -34,40 +35,30 @@ class Basket implements ControllerProviderInterface { $controllers = new ControllerCollection(); - $kernel = $app['Kernel'] ; - - $controllers->post('/create/', function() use ($app) + $controllers->match('/', function(Application $app) { - - $em = $app['Kernel']->getEntityManager(); - - $Basket = new \Entities\Basket; - $Basket->setName($app['request']->get('name')); - $Basket->setUser($app['request']->get('desc')); - $Basket->setDescription($app['request']->get('desc')); - - $em->persist($Basket); - $em->flush(); - - return new RedirectResponse(sprintf('/%d/', $Basket->getId())); + $requestHandler = new RequestHandler\Basket($app["kernel"]); + $processor = new BasketRoute\Root($requestHandler); + + return $processor->getResponse(); }); - + + $controllers->get('/{basket_id}/', function($basket_id) use ($app) { - $em = $app['Kernel']->getEntityManager(); - + /* @var $entityManager \Doctrine\ORM\EntityManager */ - + $repo = $em->getRepository('Entities\Basket'); - + /* @todo implement ord */ $Basket = $repo->find($basket_id); - + $twig = new \supertwig(); - - $html = $twig->render('prod/basket.twig', array('basket' => $Basket));//, 'ordre' => $order)); - + + $html = $twig->render('prod/basket.twig', array('basket' => $Basket)); //, 'ordre' => $order)); + return new Response($html); })->assert('basket_id', '\d+'); From 0ec8ce3421eacc17bf797511d81e708f55ca8dff Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Thu, 15 Dec 2011 19:53:27 +0100 Subject: [PATCH 6/8] Add autoload for doctrine repositories --- lib/Alchemy/Phrasea/Kernel/Service/Doctrine.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/Alchemy/Phrasea/Kernel/Service/Doctrine.php b/lib/Alchemy/Phrasea/Kernel/Service/Doctrine.php index b5c985cd92..037477c6bd 100644 --- a/lib/Alchemy/Phrasea/Kernel/Service/Doctrine.php +++ b/lib/Alchemy/Phrasea/Kernel/Service/Doctrine.php @@ -110,6 +110,12 @@ class Doctrine ); $classLoader->register(); + $classLoader = new \Doctrine\Common\ClassLoader( + 'Repositories' + , realpath(__DIR__ . '/../../../../Doctrine') + ); + $classLoader->register(); + $classLoader = new \Doctrine\Common\ClassLoader( 'Proxies' , realpath(__DIR__ . '/../../../../Doctrine') From 24ad9b5d4bb1d63539c1cb649a730c905644809d Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Thu, 15 Dec 2011 19:55:06 +0100 Subject: [PATCH 7/8] Workzone Controller --- .../Phrasea/Controller/Prod/WorkZone.php | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 lib/Alchemy/Phrasea/Controller/Prod/WorkZone.php diff --git a/lib/Alchemy/Phrasea/Controller/Prod/WorkZone.php b/lib/Alchemy/Phrasea/Controller/Prod/WorkZone.php new file mode 100644 index 0000000000..350a19df72 --- /dev/null +++ b/lib/Alchemy/Phrasea/Controller/Prod/WorkZone.php @@ -0,0 +1,49 @@ +match('/', function(Application $app) + { + $requestHandler = new RequestHandler\WorkZone($app["kernel"]); + $processor = new RouteWorkZone\Root($requestHandler); + + return $processor->getResponse(); + }); + + return $controllers; + } + +} From 69402d4ef718fa201aa01d58e56860b6c7ee86dd Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Thu, 15 Dec 2011 19:56:53 +0100 Subject: [PATCH 8/8] Add Repositories to YAML --- lib/conf.d/Doctrine/Entities.Basket.dcm.yml | 1 + lib/conf.d/Doctrine/Entities.ValidationParticipant.dcm.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/conf.d/Doctrine/Entities.Basket.dcm.yml b/lib/conf.d/Doctrine/Entities.Basket.dcm.yml index dd6d2e0539..beac4de8c3 100644 --- a/lib/conf.d/Doctrine/Entities.Basket.dcm.yml +++ b/lib/conf.d/Doctrine/Entities.Basket.dcm.yml @@ -1,5 +1,6 @@ Entities\Basket: type: entity + repositoryClass: Repositories\BasketRepository table: Baskets id: id: diff --git a/lib/conf.d/Doctrine/Entities.ValidationParticipant.dcm.yml b/lib/conf.d/Doctrine/Entities.ValidationParticipant.dcm.yml index b0408273ef..240f54ad23 100644 --- a/lib/conf.d/Doctrine/Entities.ValidationParticipant.dcm.yml +++ b/lib/conf.d/Doctrine/Entities.ValidationParticipant.dcm.yml @@ -1,5 +1,6 @@ Entities\ValidationParticipant: type: entity + repositoryClass: Repositories\ValidationParticipantRepository table: ValidationParticipants id: id: