]> Raphaël G. Git Repositories - treebundle/blobdiff - Controller/TreeController.php
Remove document route
[treebundle] / Controller / TreeController.php
index a6e2ac1a119e661c49777c610724233839b3ebdd..54bc44f08e16198200a908fbf8c9502651dd24e1 100644 (file)
 
 namespace Rapsys\TreeBundle\Controller;
 
+use Doctrine\Persistence\ManagerRegistry;
+
 use Psr\Container\ContainerInterface;
 
+use Rapsys\PackBundle\Util\FacebookUtil;
+use Rapsys\TreeBundle\Entity\Album;
+use Rapsys\TreeBundle\RapsysTreeBundle;
+
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Bundle\SecurityBundle\Security;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\RequestStack;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 use Symfony\Component\Routing\RouterInterface;
+use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
+
 use Symfony\Contracts\Translation\TranslatorInterface;
 
 use Twig\Environment;
 
-use Rapsys\TreeBundle\RapsysTreeBundle;
-
 /**
  * {@inheritdoc}
  */
@@ -39,11 +46,21 @@ class TreeController extends AbstractController {
         */
        protected array $context = [];
 
+       /**
+        * Count integer
+        */
+       protected int $count;
+
        /**
         * Locale string
         */
        protected string $locale;
 
+       /**
+        * Page integer
+        */
+       protected int $page;
+
        /**
         * Request instance
         */
@@ -62,13 +79,18 @@ class TreeController extends AbstractController {
        /**
         * Creates a new tree controller
         *
+        * @param AuthorizationCheckerInterface $checker The container instance
         * @param ContainerInterface $container The ContainerInterface instance
+        * @param ManagerRegistry $doctrine The doctrine instance
+        * @param FacebookUtil $facebook The facebook instance
         * @param RouterInterface $router The router instance
+        * @param Security $security The security instance
         * @param RequestStack $stack The stack instance
         * @param TranslatorInterface $translator The translator instance
         * @param Environment $twig The twig environment instance
+        * @param integer $limit The page limit
         */
-       function __construct(protected ContainerInterface $container, protected RouterInterface $router, protected RequestStack $stack, protected TranslatorInterface $translator, protected Environment $twig) {
+       function __construct(protected AuthorizationCheckerInterface $checker, protected ContainerInterface $container, protected ManagerRegistry $doctrine, protected FacebookUtil $facebook, protected RouterInterface $router, protected Security $security, protected RequestStack $stack, protected TranslatorInterface $translator, protected Environment $twig, protected int $limit = 5) {
                //Retrieve config
                $this->config = $container->getParameter(RapsysTreeBundle::getAlias());
 
@@ -84,6 +106,14 @@ class TreeController extends AbstractController {
                //Set alternates
                $alternates = [];
 
+               //Get current page
+               $this->page = (int) $this->request->query->get('page');
+
+               //With negative page
+               if ($this->page < 0) {
+                       $this->page = 0;
+               }
+
                //Set route
                //TODO: default to not found route ???
                //TODO: pour une url not found, cet attribut n'est pas défini, comment on fait ???
@@ -162,18 +192,90 @@ class TreeController extends AbstractController {
         * @return Response The rendered view
         */
        public function index(Request $request): Response {
-               //Get roots
-               $this->context['roots'] = $this->config['roots'];
+               //Get user
+               $user = $this->security->getUser();
 
-               //Render template
-               $response = $this->render('@RapsysTree/index.html.twig', $this->context);
+               //With not enough albums
+               if (($this->count = $this->doctrine->getRepository(Album::class)->findCountAsInt($user?->getId())) < $this->page * $this->limit) {
+                       //Throw 404
+                       throw $this->createNotFoundException($this->translator->trans('Unable to find albums'));
+               }
 
-               $response->setEtag(md5($response->getContent()));
-               $response->setPublic();
-               $response->isNotModified($request);
+               //Get albums
+               if ($this->context['albums'] = $this->doctrine->getRepository(Album::class)->findAllAsArray($user?->getId(), $this->page, $this->limit)) {
+                       //Set modified
+                       $this->modified = max(array_map(function ($v) { return $v['modified']; }, $this->context['albums']));
+               //Without albums
+               } else {
+                       //Set empty modified
+                       $this->modified = new \DateTime('-1 year');
+               }
 
-               //Return response
-               return $response;
+               /*header('Content-Type: text/plain');
+               var_dump($this->context['albums']);
+               exit;*/
+
+               //Create response
+               $response = new Response();
+
+               //With logged user
+               if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
+                       //Set last modified
+                       $response->setLastModified(new \DateTime('-1 year'));
+
+                       //Set as private
+                       $response->setPrivate();
+               //Without logged user
+               } else {
+                       //Set etag
+                       //XXX: only for public to force revalidation by last modified
+                       $response->setEtag(md5(serialize($this->context['albums'])));
+
+                       //Set last modified
+                       $response->setLastModified($this->modified);
+
+                       //Set as public
+                       $response->setPublic();
+
+                       //Without role and modification
+                       if ($response->isNotModified($request)) {
+                               //Return 304 response
+                               return $response;
+                       }
+               }
+
+               //Set keywords
+               /*$this->context['head']['keywords'] = implode(
+                       ', ',
+                       //Use closure to extract each unique article keywords sorted
+                       (function ($t) {
+                               //Return array
+                               $r = [];
+
+                               //Iterate on articles
+                               foreach($t as $a) {
+                                       //Non empty keywords
+                                       if (!empty($a['keywords'])) {
+                                               //Iterate on keywords
+                                               foreach($a['keywords'] as $k) {
+                                                       //Set keyword
+                                                       $r[$k['title']] = $k['title'];
+                                               }
+                                       }
+                               }
+
+                               //Sort array
+                               sort($r);
+
+                               //Return array
+                               return $r;
+                       })($this->context['articles'])
+               );
+               //Get albums
+               $this->context['albums'] = $this->config['albums'];*/
+
+               //Return rendered response
+               return $this->render('@RapsysTree/index.html.twig', $this->context, $response);
        }
 
        /**