src/Controller/DefaultController.php line 114

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Board;
  4. use App\Entity\Content;
  5. use App\Entity\Feedback;
  6. use App\Entity\Files;
  7. use App\Entity\User;
  8. use App\Repository\BannerRepository;
  9. use App\Repository\BoardRepository;
  10. use App\Repository\ContentRepository;
  11. use App\Repository\FeedbackRepository;
  12. use App\Repository\FilesRepository;
  13. use App\Repository\MemberRepository;
  14. use App\Repository\UserRepository;
  15. use App\Service\FileUploader;
  16. use Doctrine\Common\Collections\Criteria;
  17. use Doctrine\Common\Collections\ExpressionBuilder;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use PHPUnit\Util\Json;
  20. use Psr\Log\LoggerInterface;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Bundle\FrameworkBundle\Translation\Translator;
  23. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  24. use Symfony\Component\HttpFoundation\File\UploadedFile;
  25. use Symfony\Component\HttpFoundation\JsonResponse;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\HttpFoundation\Response;
  28. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  29. use Symfony\Component\Routing\Annotation\Route;
  30. use Symfony\Contracts\Translation\TranslatorInterface;
  31. use function Symfony\Component\DependencyInjection\Loader\Configurator\expr;
  32. class DefaultController extends AbstractController
  33. {
  34.     #[Route('/'name'app_default')]
  35.     public function index(): Response
  36.     {
  37.         return $this->redirectToRoute('app_home', ['_locale' => 'mn']);
  38.     }
  39.     #[Route('/{_locale<%app.supported_locales%>}'name'app_home')]
  40.     public function home(BoardRepository $boardRepositoryMemberRepository $memberRepositoryContentRepository $contentRepositoryBannerRepository $bannerRepository): Response
  41.     {
  42.         $news $contentRepository->findLatest(Content::TYPE_NEWS2);
  43.         $tech $contentRepository->findLatest(Content::TYPE_TECHNOLOGY2);
  44.         return $this->render('frontend/home/index.html.twig', [
  45.             'controller_name' => 'DefaultController',
  46.             'boards' => $boardRepository->findBy(['type' => Board::BOARD_TYPE_TUZ]),
  47.             'boards2' => $boardRepository->findBy(['type' => Board::BOARD_TYPE_KHZ]),
  48.             'newsList' => $news,
  49.             'techList' => $tech,
  50.             'banners' => $bannerRepository->findAll(),
  51.             'members' => $memberRepository->findAll(),
  52.         ]);
  53.     }
  54.     #[Route('/useradd/{role}'name'useradd')]
  55.     public function useradd(UserRepository $repositoryUserPasswordHasherInterface $passwordHasherEntityManagerInterface $entityManagerstring $role): Response
  56.     {
  57.         if ($role == 'admin') {
  58.             $user = new User();
  59.             $user->setUsername("admin");
  60.             $user->setRoles(['ROLE_ADMIN']);
  61.             $hashedPassword $passwordHasher->hashPassword(
  62.                 $user,
  63.                 'admin'
  64.             );
  65.             $user->setPassword($hashedPassword);
  66.             $entityManager->persist($user);
  67.             $entityManager->flush();
  68.         }
  69.         if ($role == 'member') {
  70.             $user = new User();
  71.             $user->setUsername("member");
  72.             $user->setRoles(['ROLE_MEMBER']);
  73.             $hashedPassword $passwordHasher->hashPassword(
  74.                 $user,
  75.                 'member'
  76.             );
  77.             $user->setPassword($hashedPassword);
  78.             $entityManager->persist($user);
  79.             $entityManager->flush();
  80.         }
  81.         return new JsonResponse('ok');
  82.     }
  83.     #[Route('/content/upload'name'app_content_upload'methods: ['POST'])]
  84.     public function contentUpload(Request $requestFileUploader $uploader): Response
  85.     {
  86.         $content $request->files->get('upload');
  87.         try {
  88.             $filename $uploader->upload($content);
  89.             return new JsonResponse(['url' => $filename]);
  90.         } catch (FileException $e) {
  91.             return new JsonResponse(['error' => ['message' => $e->getMessage()]]);
  92.         }
  93.     }
  94.     #[Route('/{_locale<%app.supported_locales%>}/intro'name'app_intro')]
  95.     public function intro(FilesRepository $filesRepositoryContentRepository $contentRepository): Response
  96.     {
  97.         return $this->render('frontend/pages/intro.html.twig', [
  98.             'documents' => $filesRepository->findBy(['type' => Files::FILE_TYPE_DOCUMENT]),
  99.             'intro' => $contentRepository->findOneBy(['type' => Content::TYPE_INTRO]),
  100.         ]);
  101.     }
  102.     #[Route('/{_locale<%app.supported_locales%>}/about'name'app_about')]
  103.     public function about(FilesRepository $filesRepositoryBoardRepository $boardRepositoryContentRepository $contentRepository): Response
  104.     {
  105.         return $this->render('frontend/pages/about.html.twig', [
  106.             'images' => $filesRepository->findBy(['type' => Files::FILE_TYPE_GALLERY]),
  107.             'boards' => $boardRepository->findBy(['type' => Board::BOARD_TYPE_TUZ]),
  108.             'boards2' => $boardRepository->findBy(['type' => Board::BOARD_TYPE_KHZ]),
  109.             'about' => $contentRepository->findOneBy(['type' => Content::TYPE_ABOUT]),
  110.         ]);
  111.     }
  112.     #[Route('/{_locale<%app.supported_locales%>}/members'name'app_members')]
  113.     public function members(MemberRepository $memberRepository): Response
  114.     {
  115.         return $this->render('frontend/pages/members.html.twig', [
  116.             'members' => $memberRepository->findAll(),
  117.         ]);
  118.     }
  119.     #[Route('/{_locale<%app.supported_locales%>}/board/{id}/show'name'app_board_show')]
  120.     public function boardShow(int $idBoardRepository $boardRepository): Response
  121.     {
  122.         return $this->render('frontend/board/show.html.twig', [
  123.             'board' => $boardRepository->find($id),
  124.             'boards' => $boardRepository->matching((new Criteria())->where(Criteria::expr()->notIn('id', [$id]))),
  125.         ]);
  126.     }
  127.     #[Route('/{_locale<%app.supported_locales%>}/feedback'name'app_user_feedback')]
  128.     public function userFeedback(Request $requestFeedbackRepository $feedbackRepositoryTranslatorInterface $translatorEntityManagerInterface $entityManager): Response
  129.     {
  130.         $submittedToken $request->request->get('token');
  131.         if ($this->isCsrfTokenValid('user-feedback'$submittedToken)) {
  132.             $content $request->request->all();
  133.             $feedback = new Feedback();
  134.             $feedback->setTitle($content['email']);
  135.             $feedback->setContact($content['number']);
  136.             $feedback->setBody($content['body']);
  137.             $feedback->setCreatedAt(new \DateTimeImmutable());
  138.             $entityManager->persist($feedback);
  139.             $entityManager->flush();
  140.             $this->addFlash('feedback'$translator->trans('messages.feedback.success'));
  141.         }
  142.         return $this->redirectToRoute('app_home', ['_locale' => $request->getLocale(), '_fragment' => 'join']);
  143.     }
  144.     #[Route('/addimages'name'app_about_images')]
  145.     public function addImages(FilesRepository $repositoryEntityManagerInterface $entityManager): Response
  146.     {
  147.         for ($i 1$i 32$i++) {
  148.             $img = new Files();
  149.             $img->setTitle("Зураг " $i);
  150.             $img->setType(Files::FILE_TYPE_GALLERY);
  151.             $img->setFilename('/files/slide' $i '.jpg');
  152.             $entityManager->persist($img);
  153.             $entityManager->flush();
  154.         }
  155.         return new JsonResponse('ok');
  156.     }
  157. }