<?php 
 
namespace Whater\UI\WebBundle\Controller\Blog; 
 
use Whater\Application\UseCase\Blog\CommandRequest\GetBlogArticleCategoryForBlogCommand; 
use Whater\Application\UseCase\Blog\CommandRequest\GetBlogArticleForBlogCommand; 
use Whater\Application\UseCase\Blog\CommandRequest\PublicListBlogArticlesCommand; 
use Whater\Domain\User\Model\Role; 
use Whater\UI\WebBundle\Controller\AbstractBusController; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\Routing\Annotation\Route; 
 
/** 
 * @Route("public/blob-article") 
 */ 
class PublicBlogArticleController extends AbstractBusController 
{ 
 
    /** 
     * @Route("/{_locale}/list",  
     * requirements={"_locale" = "en|es"}, 
     * name="web_blog_list_articles",  
     * defaults={"_format" = "html", "_locale" = "es"}) 
     */ 
    public function publicBlogArticlesAction(Request $request, string $_locale) 
    { 
        $pageNumber = $request->query->get('page'); 
        if (!$pageNumber) { 
            $pageNumber = 1; 
        } 
 
        try { 
            $publicListBlogArticlesCommand = new PublicListBlogArticlesCommand($pageNumber, 10, 'publish_at', 'desc','show_in','["BLOG"]'); 
            $page = $this->handle($publicListBlogArticlesCommand); 
        } catch (\Exception $e) { 
            return new RedirectResponse($this->router()->generate('web_blog_list_articles', array(), true)); 
        } 
        return $this->render('Blog/articles_list.html.twig', array( 
            'articlePage' => $page, 
            'locale' => $_locale 
        )); 
    } 
 
    // public function publicBlogRecentArticlesAction(Request $request, string $_locale) 
    // { 
    //     return new RedirectResponse($this->router()->generate('web_init', array(), true)); 
    //     try { 
    //         $publicListBlogArticlesCommand = new PublicListBlogArticlesCommand(1, 4, 'publish_at', 'desc'); 
    //         $page = $this->handle($publicListBlogArticlesCommand); 
    //     } catch (\Exception $e) { 
    //         return ''; 
    //     } 
    //     return $this->render('Blog/articles_recent.html.twig', array( 
    //         'articlePage' => $page, 
    //         'locale' => $_locale 
    //     )); 
    // } 
 
    /** 
     * @Route("/{_locale}/list/{categorySlug}",  
     * requirements={"_locale" = "en|es"}, 
     * name="web_blog_list_articles_category",  
     * defaults={"_format" = "html", "_locale" = "es"}) 
     */ 
    public function publicBlogArticlesCategoryAction(Request $request, string $_locale, string $categorySlug) 
    { 
        $articlePage = null; 
        $articleCategory = null; 
        $pageNumber = $request->query->get('page'); 
        if (!$pageNumber) { 
            $pageNumber = 1; 
        } 
        try { 
            $getBlogArticleCategoryForBlogCommand = new GetBlogArticleCategoryForBlogCommand($categorySlug); 
            $articleCategory = $this->handle($getBlogArticleCategoryForBlogCommand); 
 
            $publicListBlogArticlesCommand = new PublicListBlogArticlesCommand($pageNumber, 10, 'publish_date', '[asc]', 'category', '["' . $articleCategory->id() . '"]'); 
            $articlePage = $this->handle($publicListBlogArticlesCommand); 
        } catch (\Exception $e) { 
            return new RedirectResponse($this->router()->generate('web_blog_list_articles', array(), true)); 
        } 
 
        return $this->render('Blog/articles_list.html.twig', array( 
            'articlePage' => $articlePage, 
            'articleCategory' => $articleCategory, 
            'locale' => $_locale 
        )); 
    } 
 
    /** 
     * @Route("/{_locale}/{articleSlug}",  
     * requirements={"_locale" = "en|es"}, 
     * name="web_blog_show_article",  
     * defaults={"_format" = "html", "_locale" = "es"}) 
     */ 
    public function publicBlogArticleAction(Request $request, string $_locale, string $articleSlug) 
    { 
        $article = null; 
        $preview = false; 
        try { 
 
            $grantUser = $this->getGrantedUser(); 
            if ($grantUser != null && $grantUser->hasRole(Role::ROLE_ADMIN)) { 
                $preview = true; 
            } 
            $getBlogArticleForBlogCommand = new GetBlogArticleForBlogCommand($articleSlug, $preview); 
            $article = $this->handle($getBlogArticleForBlogCommand); 
        } catch (\Exception $e) { 
            $this->setFlash('error', $this->translator()->trans('blog.show_article.not_found')); 
            return new RedirectResponse($this->router()->generate('web_blog_list_articles', array(), true)); 
        } 
 
        return $this->render('Blog/article_show.html.twig', array( 
            'article' => $article, 
            'locale' => $_locale 
        )); 
    } 
}