Symfony/Controller/ContentController.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  6. use App\Sanity\Query;
  7. use App\DOT\DreamCaster;
  8. class ContentController extends AbstractSanityController
  9. {
  10.     /**
  11.     * @Route("/", name="index")
  12.     * @Template
  13.     */
  14.      
  15.     public function indexAction()
  16.     {
  17.         return [];
  18.     }
  19.     
  20.     /**
  21.     * @Route("/articles", name="articles_landing")
  22.     * @Template
  23.     */
  24.     
  25.     public function ArticlesLandingAction()
  26.     {
  27.         return $this->getControllerResult(
  28.             'document',
  29.             Query::getDocumentBySlug('page''articles')
  30.         );             
  31.     }
  32.     /**
  33.     * @Route("/articles/{slug}", name="single_article")
  34.     * @Template
  35.     */
  36.     
  37.     public function SingleArticleAction$slug )
  38.     {
  39.         return $this->getControllerResult(
  40.             'document',
  41.             Query::getArticleBySlug($slug)
  42.         );             
  43.     }
  44.     /**
  45.     * @Route("/docs/{slug?}", name="docs")
  46.     * @Template
  47.     */
  48.     
  49.     public function DocsAction$slug=null )
  50.     {
  51.         return $this->getControllerResult(
  52.             'document',
  53.             Query::getDocumentBySlug('page''docs'.($slug "/$slug''))
  54.         );
  55.     }
  56.  
  57.      /**
  58.     * @Route("/{slug}", name="page", requirements={"slug"=".+"}, priority=-100)
  59.     * @Template("content/page.html.twig")
  60.     */
  61.     
  62.     public function PageAction$slug )
  63.     {
  64.         return $this->getControllerResult(
  65.             'document',
  66.             Query::getDocumentBySlug('page'$slug)
  67.         );             
  68.     }
  69.  
  70.      /**
  71.     * @Route(name="esi_menu")
  72.     * @Template("elements/esi-menu.html.twig")
  73.     */    
  74.     
  75.     public function esiMenuAction$slug )
  76.     {
  77.         return $this->getControllerResult(
  78.             'menu',
  79.             Query::getDocumentBySlug('menu'$slug)
  80.         );
  81.     }
  82.      /**
  83.     * @Route(name="esi_latest_artciles_menu")
  84.     * @Template("elements/esi-latest-articles-menu.html.twig")
  85.     */    
  86.     
  87.     public function esiLatestArticlesMenuAction$num=)
  88.     {
  89.         return $this->getControllerResult(
  90.             'articles',
  91.             Query::getLatestArticleTeasers($num
  92.         );
  93.     }
  94.     
  95.     /**
  96.     * @Route(name="esi_latest_articles_home")
  97.     * @Template("elements/esi-latest-articles-home.html.twig")
  98.     */    
  99.     
  100.     public function esiLatestArticlesHomeAction$num=)
  101.     {
  102.         return $this->getControllerResult(
  103.             'articles',
  104.             Query::getLatestArticleTeasers($num
  105.         );
  106.     }
  107.     /**
  108.     * @Route(name="esi_articles_listing")
  109.     * @Template
  110.     */
  111.     
  112.     public function esiArticlesListingAction$num=10$offset=)
  113.     {
  114.         return $this->getControllerResult(
  115.             'articles',
  116.             Query::getArticles($num$offset
  117.         );            
  118.     }
  119.     /**
  120.     * @Route(name="esi_references")
  121.     * @Template("elements/esi-references.html.twig")
  122.     */    
  123.     public function esiReferencesAction$references )
  124.     {        
  125.         if(!$ids array_filter(explode(','$references)))
  126.             return ['sorted' => []];
  127.         
  128.         return $this->getControllerResult(
  129.             'result'
  130.             new Query('*[_id in $refs]',
  131.             [
  132.                 'refs' => $ids
  133.             ])
  134.         )->addAuxiliaryData('sorted', function( $result ) use ($ids) {
  135.             $sorted = [];
  136.             foreach($ids as $id)
  137.                 foreach($result as $object)
  138.                     if($object->get('_id') == $id)
  139.                         $sorted[] = $object;
  140.             
  141.             return $sorted;
  142.         });        
  143.     }
  144.     /**
  145.     * @Route("/graphic/{size}/{slug}.{xtn}", name="graphic")
  146.     */
  147.     public function graphicAction$size$slug$xtn )
  148.     {
  149.         // die($size);
  150.         
  151.         if( !$graphic $this->sanity->query(Query::getGraphic($slug)) )
  152.             return $this->createNotFoundException("No such graphic")
  153.         ;
  154.         
  155.         $options = [];
  156.         
  157.         if( $size != 'original' ){
  158.             
  159.             @list($x$y) = @explode('x'$size);
  160.             
  161.             if(!is_numeric($x) && !is_numeric($y))
  162.                 throw new \Exception("Bad size instruction: $size");
  163.             
  164.             $options += [
  165.                 'w' => $x,
  166.                 'h' => $y ?: $x,
  167.                 'fit' => 'fill' 
  168.             ];
  169.         }    
  170.         
  171.         
  172.        // die($size);
  173.         
  174.         if( !$url $this->sanity->getImageUrl($graphic['image'], ['imageOptions' => $options]) )
  175.             throw new \Exception("That didn't work")
  176.         ;
  177.         
  178.         //$_xtn = @array_pop(explode('.', basename($url))); all sorts of query crap in there
  179.         $data file_get_contents($url);
  180.         
  181.         $response = new Response;
  182.         $disposition $response->headers
  183.             ->makeDisposition(ResponseHeaderBag::DISPOSITION_INLINE"$slug.$xtn");
  184.         $response->headers->set('Content-Disposition'$disposition);
  185.         $response->headers->set('Content-Type'"image/$xtn");
  186.         $response->setContent($data);
  187.         
  188.         return $response;
  189.     }
  190.     /**
  191.     * @Route(name="esi_route")
  192.     * @Template
  193.     */
  194.    
  195.     public function esiRouteAction($aircraft$from$dest$airline=null
  196.     {
  197.         return $this->getControllerResult(
  198.             'route',
  199.             Query::getFlightObjects([
  200.                 'aircraft' => $aircraft,
  201.                 'from' => $from,
  202.                 'dest' => $dest
  203.             ] + ($airline ? ['airline' => $airline] : []))
  204.         );
  205.     }
  206. }