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+'); 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; + } + +} diff --git a/lib/Alchemy/Phrasea/Kernel.php b/lib/Alchemy/Phrasea/Kernel.php index 38aed08dff..168de618d2 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() { 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') 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; + } + +} + 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(); + } + +} 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 @@ +