<?php
namespace Felix\NewsBundle\Controller;
use Felix\IndexBundle\Controller\IndexController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Felix\NewsBundle\Entity\News;
/**
* Front controller.
*/
class FrontController extends Controller
{
public function newsAction(Request $request, $filter)
{
//action necessitant un filtre
if (!$filter) {
return null;
}
$site = IndexController::getSite();
$thumbnailer = $this->container->get('media.thumbnail');
$category = $this->getDoctrine()->getRepository('NewsBundle:Category')->find($filter);
$news = $this->getDoctrine()->getRepository('NewsBundle:News')->findByCategory($site, $category->getId());
foreach ($news as $new) {
$config = $new->getConfig();
if ($new->getMediaRelation() != null) {
$media = $new->getMediaRelation()->getMedia();
if ($media->getMediaType() == 0) {
if ($thumbnailer->isAnimated($media->getAbsolutePath())) {
$media->setThumbnail($media->getWebPath());
} else {
$media->setThumbnail($thumbnailer->thumbnail(
$media->getAbsolutePath(),
array(
$thumbnailer::RESIZE_MODE_KEY => $thumbnailer::RESIZE_MODE_RESIZE_CROP,
$thumbnailer::SIZE_WIDTH_KEY => $config['thumbs']['news']['width'],
$thumbnailer::SIZE_HEIGHT_KEY => $config['thumbs']['news']['height'],
)
));
}
}
}
}
return $this->render('NewsBundle:front:news.html.twig', array(
'news' => $news,
'category' => $category
));
}
public function viewAction(Request $request)
{
//action necessitant un code
if (!$request->get('code')) {
throw $this->createNotFoundException();
}
$site = IndexController::getSite();
$new = $this->getDoctrine()->getRepository('NewsBundle:News')->findOneByCodeAndSite($request->get('code'), $site);
if (empty($new)) {
throw $this->createNotFoundException();
}
if ($new->getMediaRelation() != null) {
$media = $new->getMediaRelation()->getMedia();
$thumbnailer = $this->container->get('media.thumbnail');
$config = $new->getConfig();
if ($media->getMediaType() == 0) {
if ($thumbnailer->isAnimated($media->getAbsolutePath())) {
$media->setThumbnail($media->getWebPath());
} else {
$media->setThumbnail($thumbnailer->thumbnail(
$media->getAbsolutePath(),
array(
$thumbnailer::RESIZE_MODE_KEY => $thumbnailer::RESIZE_MODE_RESIZE_CROP,
$thumbnailer::SIZE_WIDTH_KEY => $config['thumbs']['view']['width'],
$thumbnailer::SIZE_HEIGHT_KEY => $config['thumbs']['view']['height'],
)
));
}
}
}
return $this->render('NewsBundle:front:view.html.twig', array(
'new' => $new,
'videoUrl' => $this->videoUrl($new->getNewsVideo())
));
}
public function teaserAction(Request $request, $filter)
{
//action necessitant un filtre
if (!$filter) {
return null;
}
$site = IndexController::getSite();
$thumbnailer = $this->container->get('media.thumbnail');
$category = $this->getDoctrine()->getRepository('NewsBundle:Category')->findOneBy(array('categoryCode' => $filter));
$news = $this->getDoctrine()->getRepository('NewsBundle:News')->findByTeaser($site, $category, 1);
foreach ($news as $new) {
if ($new->getMediaRelation() != null) {
$media = $new->getMediaRelation()->getMedia();
if ($media->getMediaType() == 0) {
if ($thumbnailer->isAnimated($media->getAbsolutePath())) {
$media->setThumbnail($media->getWebPath());
} else {
$config = $new->getConfig();
$media->setThumbnail($thumbnailer->thumbnail(
$media->getAbsolutePath(),
array(
$thumbnailer::RESIZE_MODE_KEY => $thumbnailer::RESIZE_MODE_RESIZE_CROP,
$thumbnailer::SIZE_WIDTH_KEY => $config['thumbs']['teaser']['width'],
$thumbnailer::SIZE_HEIGHT_KEY => $config['thumbs']['teaser']['height'],
)
));
}
}
}
}
return $this->render('NewsBundle:front:teaser.html.twig', array(
'news' => $news,
'category' => $category
));
}
public function teaserDoubleAction(Request $request, $filter)
{
//action necessitant un filtre
if (!$filter) {
return null;
}
$site = IndexController::getSite();
$thumbnailer = $this->container->get('media.thumbnail');
$category = $this->getDoctrine()->getRepository('NewsBundle:Category')->findOneBy(array('categoryCode' => $filter));
$news = $this->getDoctrine()->getRepository('NewsBundle:News')->findByTeaser($site, $category, 2);
foreach ($news as $new) {
if ($new->getMediaRelation() != null) {
$media = $new->getMediaRelation()->getMedia();
if ($media->getMediaType() == 0) {
if ($thumbnailer->isAnimated($media->getAbsolutePath())) {
$media->setThumbnail($media->getWebPath());
} else {
$config = $new->getConfig();
$media->setThumbnail($thumbnailer->thumbnail(
$media->getAbsolutePath(),
array(
$thumbnailer::RESIZE_MODE_KEY => $thumbnailer::RESIZE_MODE_RESIZE_CROP,
$thumbnailer::SIZE_WIDTH_KEY => $config['thumbs']['teaserDouble']['width'],
$thumbnailer::SIZE_HEIGHT_KEY => $config['thumbs']['teaserDouble']['height'],
)
));
}
}
}
}
return $this->render('NewsBundle:front:teaser.html.twig', array(
'news' => $news,
'category' => $category
));
}
/**
* Get video view
*
* @param string $url
* @return string|null
*/
private function videoUrl($url = NULL)
{
if ($url) {
if (preg_match('/youtube/', $url) || preg_match('/youtu.be/', $url)) {
return 'https://www.youtube.com/embed/' . $this->getYoutube($url);
}
}
}
/**
* Get Youtube arguments
*
* @param string $url
* @return mixed|string
*/
private function getYoutube($url)
{
$videoID = NULL;
if (preg_match('/watch/', $url)) {
$matches = explode("&", $url);
foreach ($matches as $match) {
if (preg_match('/watch/', $match)) {
$explode = explode('watch?v=', $match);
return end($explode);
}
}
} elseif (preg_match('/youtu.be/', $url)) {
$matches = explode('/', $url);
return end($matches);
}
}
}