src/Controller/DefaultController.php line 45
<?php
namespace App\Controller;
use App\Entity\Board;
use App\Entity\Content;
use App\Entity\Feedback;
use App\Entity\Files;
use App\Entity\User;
use App\Repository\BannerRepository;
use App\Repository\BoardRepository;
use App\Repository\ContentRepository;
use App\Repository\FeedbackRepository;
use App\Repository\FilesRepository;
use App\Repository\MemberRepository;
use App\Repository\UserRepository;
use App\Service\FileUploader;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\ExpressionBuilder;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Util\Json;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use function Symfony\Component\DependencyInjection\Loader\Configurator\expr;
class DefaultController extends AbstractController
{
#[Route('/', name: 'app_default')]
public function index(): Response
{
return $this->redirectToRoute('app_home', ['_locale' => 'mn']);
}
#[Route('/{_locale<%app.supported_locales%>}', name: 'app_home')]
public function home(BoardRepository $boardRepository, MemberRepository $memberRepository, ContentRepository $contentRepository, BannerRepository $bannerRepository): Response
{
$news = $contentRepository->findLatest(Content::TYPE_NEWS, 2);
$tech = $contentRepository->findLatest(Content::TYPE_TECHNOLOGY, 2);
return $this->render('frontend/home/index.html.twig', [
'controller_name' => 'DefaultController',
'boards' => $boardRepository->findBy(['type' => Board::BOARD_TYPE_TUZ]),
'boards2' => $boardRepository->findBy(['type' => Board::BOARD_TYPE_KHZ]),
'newsList' => $news,
'techList' => $tech,
'banners' => $bannerRepository->findAll(),
'members' => $memberRepository->findAll(),
]);
}
#[Route('/useradd/{role}', name: 'useradd')]
public function useradd(UserRepository $repository, UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $entityManager, string $role): Response
{
if ($role == 'admin') {
$user = new User();
$user->setUsername("admin");
$user->setRoles(['ROLE_ADMIN']);
$hashedPassword = $passwordHasher->hashPassword(
$user,
'admin'
);
$user->setPassword($hashedPassword);
$entityManager->persist($user);
$entityManager->flush();
}
if ($role == 'member') {
$user = new User();
$user->setUsername("member");
$user->setRoles(['ROLE_MEMBER']);
$hashedPassword = $passwordHasher->hashPassword(
$user,
'member'
);
$user->setPassword($hashedPassword);
$entityManager->persist($user);
$entityManager->flush();
}
return new JsonResponse('ok');
}
#[Route('/content/upload', name: 'app_content_upload', methods: ['POST'])]
public function contentUpload(Request $request, FileUploader $uploader): Response
{
$content = $request->files->get('upload');
try {
$filename = $uploader->upload($content);
return new JsonResponse(['url' => $filename]);
} catch (FileException $e) {
return new JsonResponse(['error' => ['message' => $e->getMessage()]]);
}
}
#[Route('/{_locale<%app.supported_locales%>}/intro', name: 'app_intro')]
public function intro(FilesRepository $filesRepository, ContentRepository $contentRepository): Response
{
return $this->render('frontend/pages/intro.html.twig', [
'documents' => $filesRepository->findBy(['type' => Files::FILE_TYPE_DOCUMENT]),
'intro' => $contentRepository->findOneBy(['type' => Content::TYPE_INTRO]),
]);
}
#[Route('/{_locale<%app.supported_locales%>}/about', name: 'app_about')]
public function about(FilesRepository $filesRepository, BoardRepository $boardRepository, ContentRepository $contentRepository): Response
{
return $this->render('frontend/pages/about.html.twig', [
'images' => $filesRepository->findBy(['type' => Files::FILE_TYPE_GALLERY]),
'boards' => $boardRepository->findBy(['type' => Board::BOARD_TYPE_TUZ]),
'boards2' => $boardRepository->findBy(['type' => Board::BOARD_TYPE_KHZ]),
'about' => $contentRepository->findOneBy(['type' => Content::TYPE_ABOUT]),
]);
}
#[Route('/{_locale<%app.supported_locales%>}/members', name: 'app_members')]
public function members(MemberRepository $memberRepository): Response
{
return $this->render('frontend/pages/members.html.twig', [
'members' => $memberRepository->findAll(),
]);
}
#[Route('/{_locale<%app.supported_locales%>}/board/{id}/show', name: 'app_board_show')]
public function boardShow(int $id, BoardRepository $boardRepository): Response
{
return $this->render('frontend/board/show.html.twig', [
'board' => $boardRepository->find($id),
'boards' => $boardRepository->matching((new Criteria())->where(Criteria::expr()->notIn('id', [$id]))),
]);
}
#[Route('/{_locale<%app.supported_locales%>}/feedback', name: 'app_user_feedback')]
public function userFeedback(Request $request, FeedbackRepository $feedbackRepository, TranslatorInterface $translator, EntityManagerInterface $entityManager): Response
{
$submittedToken = $request->request->get('token');
if ($this->isCsrfTokenValid('user-feedback', $submittedToken)) {
$content = $request->request->all();
$feedback = new Feedback();
$feedback->setTitle($content['email']);
$feedback->setContact($content['number']);
$feedback->setBody($content['body']);
$feedback->setCreatedAt(new \DateTimeImmutable());
$entityManager->persist($feedback);
$entityManager->flush();
$this->addFlash('feedback', $translator->trans('messages.feedback.success'));
}
return $this->redirectToRoute('app_home', ['_locale' => $request->getLocale(), '_fragment' => 'join']);
}
#[Route('/addimages', name: 'app_about_images')]
public function addImages(FilesRepository $repository, EntityManagerInterface $entityManager): Response
{
for ($i = 1; $i < 32; $i++) {
$img = new Files();
$img->setTitle("Зураг " . $i);
$img->setType(Files::FILE_TYPE_GALLERY);
$img->setFilename('/files/slide' . $i . '.jpg');
$entityManager->persist($img);
$entityManager->flush();
}
return new JsonResponse('ok');
}
}