src/Felix/NewsBundle/Controller/FrontController.php line 56

Open in your IDE?
  1. <?php
  2. namespace Felix\NewsBundle\Controller;
  3. use Felix\IndexBundle\Controller\IndexController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Felix\NewsBundle\Entity\News;
  7. /**
  8.  * Front controller.
  9.  */
  10. class FrontController extends Controller
  11. {
  12.     public function newsAction(Request $request$filter)
  13.     {
  14.         //action necessitant un filtre
  15.         if (!$filter) {
  16.             return null;
  17.         }
  18.         $site IndexController::getSite();
  19.         $thumbnailer $this->container->get('media.thumbnail');
  20.         $category $this->getDoctrine()->getRepository('NewsBundle:Category')->find($filter);
  21.         $news $this->getDoctrine()->getRepository('NewsBundle:News')->findByCategory($site$category->getId());
  22.         foreach ($news as $new) {
  23.             $config $new->getConfig();
  24.             if ($new->getMediaRelation() != null) {
  25.                 $media $new->getMediaRelation()->getMedia();
  26.                 if ($media->getMediaType() == 0) {
  27.                     if ($thumbnailer->isAnimated($media->getAbsolutePath())) {
  28.                         $media->setThumbnail($media->getWebPath());
  29.                     } else {
  30.                         $media->setThumbnail($thumbnailer->thumbnail(
  31.                             $media->getAbsolutePath(),
  32.                             array(
  33.                                 $thumbnailer::RESIZE_MODE_KEY => $thumbnailer::RESIZE_MODE_RESIZE_CROP,
  34.                                 $thumbnailer::SIZE_WIDTH_KEY => $config['thumbs']['news']['width'],
  35.                                 $thumbnailer::SIZE_HEIGHT_KEY => $config['thumbs']['news']['height'],
  36.                             )
  37.                         ));
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.         return $this->render('NewsBundle:front:news.html.twig', array(
  43.             'news' => $news,
  44.             'category' => $category
  45.         ));
  46.     }
  47.     public function viewAction(Request $request)
  48.     {
  49.         //action necessitant un code
  50.         if (!$request->get('code')) {
  51.             throw $this->createNotFoundException();
  52.         }
  53.         $site IndexController::getSite();
  54.         $new $this->getDoctrine()->getRepository('NewsBundle:News')->findOneByCodeAndSite($request->get('code'), $site);
  55.         if (empty($new)) {
  56.             throw $this->createNotFoundException();
  57.         }
  58.         if ($new->getMediaRelation() != null) {
  59.             $media $new->getMediaRelation()->getMedia();
  60.             $thumbnailer $this->container->get('media.thumbnail');
  61.             $config $new->getConfig();
  62.             if ($media->getMediaType() == 0) {
  63.                 if ($thumbnailer->isAnimated($media->getAbsolutePath())) {
  64.                     $media->setThumbnail($media->getWebPath());
  65.                 } else {
  66.                     $media->setThumbnail($thumbnailer->thumbnail(
  67.                         $media->getAbsolutePath(),
  68.                         array(
  69.                             $thumbnailer::RESIZE_MODE_KEY => $thumbnailer::RESIZE_MODE_RESIZE_CROP,
  70.                             $thumbnailer::SIZE_WIDTH_KEY => $config['thumbs']['view']['width'],
  71.                             $thumbnailer::SIZE_HEIGHT_KEY => $config['thumbs']['view']['height'],
  72.                         )
  73.                     ));
  74.                 }
  75.             }
  76.         }
  77.         return $this->render('NewsBundle:front:view.html.twig', array(
  78.             'new' => $new,
  79.             'videoUrl' => $this->videoUrl($new->getNewsVideo())
  80.         ));
  81.     }
  82.     public function teaserAction(Request $request$filter)
  83.     {
  84.         //action necessitant un filtre
  85.         if (!$filter) {
  86.             return null;
  87.         }
  88.         $site IndexController::getSite();
  89.         $thumbnailer $this->container->get('media.thumbnail');
  90.         $category $this->getDoctrine()->getRepository('NewsBundle:Category')->findOneBy(array('categoryCode' => $filter));
  91.         $news $this->getDoctrine()->getRepository('NewsBundle:News')->findByTeaser($site$category1);
  92.         foreach ($news as $new) {
  93.             if ($new->getMediaRelation() != null) {
  94.                 $media $new->getMediaRelation()->getMedia();
  95.                 if ($media->getMediaType() == 0) {
  96.                     if ($thumbnailer->isAnimated($media->getAbsolutePath())) {
  97.                         $media->setThumbnail($media->getWebPath());
  98.                     } else {
  99.                         $config $new->getConfig();
  100.                         $media->setThumbnail($thumbnailer->thumbnail(
  101.                             $media->getAbsolutePath(),
  102.                             array(
  103.                                 $thumbnailer::RESIZE_MODE_KEY => $thumbnailer::RESIZE_MODE_RESIZE_CROP,
  104.                                 $thumbnailer::SIZE_WIDTH_KEY => $config['thumbs']['teaser']['width'],
  105.                                 $thumbnailer::SIZE_HEIGHT_KEY => $config['thumbs']['teaser']['height'],
  106.                             )
  107.                         ));
  108.                     }
  109.                 }
  110.             }
  111.         }
  112.         return $this->render('NewsBundle:front:teaser.html.twig', array(
  113.             'news' => $news,
  114.             'category' => $category
  115.         ));
  116.     }
  117.     public function teaserDoubleAction(Request $request$filter)
  118.     {
  119.         //action necessitant un filtre
  120.         if (!$filter) {
  121.             return null;
  122.         }
  123.         $site IndexController::getSite();
  124.         $thumbnailer $this->container->get('media.thumbnail');
  125.         $category $this->getDoctrine()->getRepository('NewsBundle:Category')->findOneBy(array('categoryCode' => $filter));
  126.         $news $this->getDoctrine()->getRepository('NewsBundle:News')->findByTeaser($site$category2);
  127.         foreach ($news as $new) {
  128.             if ($new->getMediaRelation() != null) {
  129.                 $media $new->getMediaRelation()->getMedia();
  130.                 if ($media->getMediaType() == 0) {
  131.                     if ($thumbnailer->isAnimated($media->getAbsolutePath())) {
  132.                         $media->setThumbnail($media->getWebPath());
  133.                     } else {
  134.                         $config $new->getConfig();
  135.                         $media->setThumbnail($thumbnailer->thumbnail(
  136.                             $media->getAbsolutePath(),
  137.                             array(
  138.                                 $thumbnailer::RESIZE_MODE_KEY => $thumbnailer::RESIZE_MODE_RESIZE_CROP,
  139.                                 $thumbnailer::SIZE_WIDTH_KEY => $config['thumbs']['teaserDouble']['width'],
  140.                                 $thumbnailer::SIZE_HEIGHT_KEY => $config['thumbs']['teaserDouble']['height'],
  141.                             )
  142.                         ));
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.         return $this->render('NewsBundle:front:teaser.html.twig', array(
  148.             'news' => $news,
  149.             'category' => $category
  150.         ));
  151.     }
  152.     /**
  153.      * Get video view
  154.      *
  155.      * @param string $url
  156.      * @return string|null
  157.      */
  158.     private function videoUrl($url NULL)
  159.     {
  160.         if ($url) {
  161.             if (preg_match('/youtube/'$url) || preg_match('/youtu.be/'$url)) {
  162.                 return 'https://www.youtube.com/embed/' $this->getYoutube($url);
  163.             }
  164.         }
  165.     }
  166.     /**
  167.      * Get Youtube arguments
  168.      *
  169.      * @param string $url
  170.      * @return mixed|string
  171.      */
  172.     private function getYoutube($url)
  173.     {
  174.         $videoID NULL;
  175.         if (preg_match('/watch/'$url)) {
  176.             $matches explode("&"$url);
  177.             foreach ($matches as $match) {
  178.                 if (preg_match('/watch/'$match)) {
  179.                     $explode explode('watch?v='$match);
  180.                     return end($explode);
  181.                 }
  182.             }
  183.         } elseif (preg_match('/youtu.be/'$url)) {
  184.             $matches explode('/'$url);
  185.             return end($matches);
  186.         }
  187.     }
  188. }