src/Controller/Site/ArticleController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Site;
  3. use App\Entity\Article;
  4. use App\Entity\Theme;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. /**
  14.  * @Route("/", name="site_ressources")
  15.  */
  16. class ArticleController extends AbstractController
  17. {
  18.     private $doctrine;
  19.     public function __construct(ManagerRegistry $doctrine)
  20.     {
  21.         $this->doctrine $doctrine;
  22.     }
  23.     /**
  24.      * @Route("/ressources/{slugtheme}", name="", requirements={"slugtheme"="[A-Za-z0-9-]+"})
  25.      */
  26.     public function index(Request $requeststring $slugtheme null ): Response
  27.     {
  28.         if($slugtheme) {
  29.             $theme $this->doctrine->getRepository(Theme::class)->findOneBySlug($slugtheme);
  30.             $articlesAll $theme->getArticles();
  31.             // A refaire, pas dans l'urgence...
  32.             $now = new \DateTime();
  33.             $articles = [];
  34.             foreach($articlesAll as $current) {
  35.                 if($current->getDate() <= $now) {
  36.                     $articles[] = $current;
  37.                 }
  38.             }
  39.             
  40.         } else {    
  41.             // $articles = $this->doctrine->getRepository(Article::class)->findBy(
  42.             //     [],
  43.             //     ['date' => 'DESC']
  44.             // );
  45.             $articles $this->doctrine->getRepository(Article::class)->findPublished();
  46.         }
  47.         $themes $this->doctrine->getRepository(Theme::class)->findAll();
  48.         $defaultData = [];
  49.         $form $this->createFormBuilder($defaultData)
  50.             ->add('search'TextType::class)
  51.             ->add('ok'SubmitType::class, array('label' => 'OK'))
  52.             ->getForm();
  53.         $form->handleRequest($request);
  54.         if ($form->isSubmitted() && $form->isValid()) {
  55.             $data $form->getData();
  56.             
  57.             $articlesSearch $this->doctrine->getRepository(Article::class)->search($data['search']);
  58.         }
  59.     
  60.         return $this->renderForm('site/articles.html.twig', [
  61.             'articles' => $articles,
  62.             'themes' => $themes,
  63.             'form' => $form,
  64.             'search' => (isset($data) && isset($data['search']) ? $data['search'] : null),
  65.             'articlesSearch' => (isset($articlesSearch) ? $articlesSearch null),
  66.             'theme' => (isset($theme) ? $theme null)
  67.         ]);
  68.     }
  69.     /**
  70.      * @Route("/article/{slug}", name="_article")
  71.      */
  72.     public function article(string $slug): Response
  73.     {
  74.         $article $this->doctrine->getRepository(Article::class)->findOneBySlug($slug);
  75.         if($this->getUser() === null &&
  76.             ( $article->getDatePublication()  == null || $article->getDatePublication() > new \DateTime )
  77.         ) return $this->redirectToRoute('site_ressources', [], 301);
  78.         return $this->render('site/article.html.twig', [
  79.             'article' => $article,
  80.         ]);
  81.     }
  82.     /**
  83.      * @Route("/article-upload-{date}", name="_cke_upload")
  84.      */
  85.     public function ckeUpload(string $date): Response
  86.     {
  87.         // $article = $this->doctrine->getRepository(Article::class)->findOneBySlug($slug);
  88.         // return $this->render('site/article.html.twig', [
  89.         //     'article' => $article,
  90.         // ]);
  91.         $now date('Ymd');
  92.         if(|| sha1($now.$this->getParameter('app.salt')) ==  $date) {
  93.             if(isset($_FILES['upload']['name']))
  94.             {
  95.                 //try {
  96.                     $file $_FILES['upload']['tmp_name'];
  97.                     $file_name $_FILES['upload']['name'];
  98.                     $file_name_array explode("."$file_name);
  99.                     $extension end($file_name_array);
  100.                     $new_image_name rand() . '.' $extension;
  101.         
  102.                     $allowed_extension = array("jpg""gif""png""jpeg");
  103.                     if(in_array($extension$allowed_extension))
  104.                     {
  105.                         move_uploaded_file($file$this->getParameter('folder.uploadcke') . $new_image_name);
  106.                         $function_number $_GET['CKEditorFuncNum'];
  107.                         $url '/uploadcke/' $new_image_name;
  108.                         $message '';
  109.                         echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction('$function_number', '$url', '$message');</script>";
  110.                         //return new Response("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction(window.CKEDITOR.instances.ckeditor._.filebrowserFn, '$url', '$message');</script>");
  111.                         // return new JsonResponse([
  112.                         //     [
  113.                         //         "uploaded" => 1,
  114.                         //         "fileName" => $new_image_name,
  115.                         //         "url" => $url
  116.                         //     ]
  117.                         // ]);
  118.                     }
  119.                 // } catch (\Throwable $th) {
  120.                 //     dump($th);
  121.                 // }
  122.             }
  123.             
  124.         }
  125.     }
  126. }