<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use \Exception;
use App\Form\QuickSearchType;
use App\Entity\Post;
use App\Form\EnqueueType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use App\Services\Crawlers\CrawlerSelect;
use App\Queue\VideoProcess\Producer as VideoProcessProducer;
use App\Form\ManagerType;
use App\Helpers\FileSystem;
use Symfony\Component\HttpKernel\KernelInterface;
use Cake\Filesystem\File;
use \Datetime;
use \DateInterval;
/**
* Route("/post", host="www.factoriax.com")
* @Route(host="www.factoriax.com")
*/
class PostController extends AbstractController
{
/**
* @Route("/post/enqueue", name="post_enqueue_form", options={"expose"=true})
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function enqueueForm(Request $request, EntityManagerInterface $em, CrawlerSelect $crawlerSelect, VideoProcessProducer $producer)
{
$post = new Post();
$form = $this->createForm(EnqueueType::class, $post);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
try {
$crawler = $crawlerSelect->getCrawler($post->getUrl());
if($crawler)
{
$existing_post = $em->getRepository(Post::class)->findOneBy(['url' => $post->getUrl()]);
if(!$existing_post)
{
$post->setStatus(Post::STATUS_CREATING);
$em->persist($post);
$em->flush();
$producer->process([
'post_id' => $post->getId()
]);
$this->addFlash('success_toast', 'URL successfully added.');
} else {
$this->addFlash('error', 'This URL already exists.');
}
} else {
$this->addFlash('error', 'Unexisting crawler.');
}
} catch(Exception $e) {
$this->addFlash('error', $e->getMessage());
}
return $this->redirectToRoute('post_enqueue_form');
}
return $this->render('admin/post/enqueue_form.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("/post/manager_form", name="post_manager_form", options={"expose"=true})
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function managerForm(Request $request, KernelInterface $kernel, EntityManagerInterface $em, CrawlerSelect $crawlerSelect, VideoProcessProducer $producer)
{
$post = $em->getRepository(Post::class)->findOneBy([
'status' => Post::STATUS_WAITING
], ['createdAt' => 'ASC']);
$post = !$post ? new Post() : $post;
$form = $this->createForm(ManagerType::class, $post);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
// Move cover to final destination
$public_folder = $kernel->getProjectDir().'/public/';
$post_folder = $public_folder.$this->getParameter('APP_MEDIA_DIR');
//FileSystem::createFolder($post_folder, 0777);
$t = new File($public_folder.$post->getCover(), false);
$new_filename = $post_folder.$post->getUuid().'.jpg';
$t->copy($new_filename, true) && $t->delete();
@chmod($new_filename, 0777);
$post->setCover(str_replace($public_folder, '', $new_filename));
$post->setThumbnails(null);
$post->setStatus(Post::STATUS_READY);
$post->setPublishDate(new Datetime());
$em->persist($post);
$em->flush();
FileSystem::deleteFolder($public_folder.$this->getParameter('APP_TMP_DIR').$post->getUuid());
return $this->redirectToRoute('post_manager_form');
}
return $this->render('admin/post/manager_form.html.twig', [
'form' => $form->createView(),
'post' => $post
]);
}
/**
* @Route("/post/leave_for_later/{post_id}", name="post_leave_for_later", options={"expose"=true})
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function leaveForLater(Request $request, KernelInterface $kernel, EntityManagerInterface $em, $post_id)
{
$qb = $em->createQueryBuilder();
$lastCreatedAt = $qb
->select('MAX(p.createdAt)')
->from(Post::class, 'p')
->getQuery()
->getSingleScalarResult()
;
$lastCreatedAt = $lastCreatedAt ? (new DateTime($lastCreatedAt))->add(new DateInterval('P1D')) : (new DateTime());
$post = $em->getRepository(Post::class)->find($post_id);
if($post)
{
$post->setCreatedAt($lastCreatedAt);
$em->persist($post);
$em->flush();
}
return $this->redirectToRoute('post_manager_form');
}
/**
* @Route("/post/delete/{post_id}", name="post_delete", options={"expose"=true})
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function delete(Request $request, KernelInterface $kernel, EntityManagerInterface $em, $post_id)
{
$post = $em->getRepository(Post::class)->find($post_id);
if($post)
{
$em->remove($post);
$em->flush();
}
$referer = $request->headers->get('referer');
return $this->redirect($referer ?? $this->generateUrl('post_manager_form'));
//return $this->redirectToRoute('post_manager_form');
}
/**
* @Route("/view/{slug}", name="post_details", options={"expose"=true})
*/
public function details(Request $request, KernelInterface $kernel, EntityManagerInterface $em, $slug)
{
$post = $em->getRepository(Post::class)->findOneBy(['slug' => $slug]);
return $this->render('post_details.html.twig', [
'post' => $post
]);
}
}