<?php
namespace App\Controller\Site;
use App\Entity\Article;
use App\Entity\Theme;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* @Route("/", name="site_ressources")
*/
class ArticleController extends AbstractController
{
private $doctrine;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
}
/**
* @Route("/ressources/{slugtheme}", name="", requirements={"slugtheme"="[A-Za-z0-9-]+"})
*/
public function index(Request $request, string $slugtheme = null ): Response
{
if($slugtheme) {
$theme = $this->doctrine->getRepository(Theme::class)->findOneBySlug($slugtheme);
$articlesAll = $theme->getArticles();
// A refaire, pas dans l'urgence...
$now = new \DateTime();
$articles = [];
foreach($articlesAll as $current) {
if($current->getDate() <= $now) {
$articles[] = $current;
}
}
} else {
// $articles = $this->doctrine->getRepository(Article::class)->findBy(
// [],
// ['date' => 'DESC']
// );
$articles = $this->doctrine->getRepository(Article::class)->findPublished();
}
$themes = $this->doctrine->getRepository(Theme::class)->findAll();
$defaultData = [];
$form = $this->createFormBuilder($defaultData)
->add('search', TextType::class)
->add('ok', SubmitType::class, array('label' => 'OK'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$articlesSearch = $this->doctrine->getRepository(Article::class)->search($data['search']);
}
return $this->renderForm('site/articles.html.twig', [
'articles' => $articles,
'themes' => $themes,
'form' => $form,
'search' => (isset($data) && isset($data['search']) ? $data['search'] : null),
'articlesSearch' => (isset($articlesSearch) ? $articlesSearch : null),
'theme' => (isset($theme) ? $theme : null)
]);
}
/**
* @Route("/article/{slug}", name="_article")
*/
public function article(string $slug): Response
{
$article = $this->doctrine->getRepository(Article::class)->findOneBySlug($slug);
if($this->getUser() === null &&
( $article->getDatePublication() == null || $article->getDatePublication() > new \DateTime )
) return $this->redirectToRoute('site_ressources', [], 301);
return $this->render('site/article.html.twig', [
'article' => $article,
]);
}
/**
* @Route("/article-upload-{date}", name="_cke_upload")
*/
public function ckeUpload(string $date): Response
{
// $article = $this->doctrine->getRepository(Article::class)->findOneBySlug($slug);
// return $this->render('site/article.html.twig', [
// 'article' => $article,
// ]);
$now = date('Ymd');
if(1 || sha1($now.$this->getParameter('app.salt')) == $date) {
if(isset($_FILES['upload']['name']))
{
//try {
$file = $_FILES['upload']['tmp_name'];
$file_name = $_FILES['upload']['name'];
$file_name_array = explode(".", $file_name);
$extension = end($file_name_array);
$new_image_name = rand() . '.' . $extension;
$allowed_extension = array("jpg", "gif", "png", "jpeg");
if(in_array($extension, $allowed_extension))
{
move_uploaded_file($file, $this->getParameter('folder.uploadcke') . $new_image_name);
$function_number = $_GET['CKEditorFuncNum'];
$url = '/uploadcke/' . $new_image_name;
$message = '';
echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction('$function_number', '$url', '$message');</script>";
//return new Response("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction(window.CKEDITOR.instances.ckeditor._.filebrowserFn, '$url', '$message');</script>");
// return new JsonResponse([
// [
// "uploaded" => 1,
// "fileName" => $new_image_name,
// "url" => $url
// ]
// ]);
}
// } catch (\Throwable $th) {
// dump($th);
// }
}
}
}
}