From 731624d886b5adaf37100081348cd08abf246609 Mon Sep 17 00:00:00 2001 From: ghost Date: Thu, 5 Oct 2023 00:15:00 +0300 Subject: [PATCH] draft submit form #14 --- .env | 8 + config/services.yaml | 8 + src/Controller/PageController.php | 185 ++++++++++++++++++++- src/Service/PageService.php | 37 +++++ templates/default/page/submit.html.twig | 208 +++++++++++------------- 5 files changed, 328 insertions(+), 118 deletions(-) create mode 100644 src/Service/PageService.php diff --git a/.env b/.env index 34c0871..de0c6aa 100644 --- a/.env +++ b/.env @@ -51,3 +51,11 @@ APP_NAME=YGGtracker APP_LOCALE=en APP_LOCALES=de|fr|en|uk + +APP_PAGE_TITLE_LENGTH_MIN=10 +APP_PAGE_TITLE_LENGTH_MAX=255 +APP_PAGE_DESCRIPTION_LENGTH_MIN=0 +APP_PAGE_DESCRIPTION_LENGTH_MAX=10000 +APP_PAGE_TORRENT_QUANTITY_MIN=1 +APP_PAGE_TORRENT_QUANTITY_MAX=100 +APP_PAGE_TORRENT_SIZE_MAX=1024000 \ No newline at end of file diff --git a/config/services.yaml b/config/services.yaml index df414d4..1bccbb7 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -8,6 +8,14 @@ parameters: app.name: '%env(APP_NAME)%' app.locale: '%env(APP_LOCALE)%' app.locales: '%env(APP_LOCALES)%' + app.page.title.length.min: '%env(APP_PAGE_TITLE_LENGTH_MIN)%' + app.page.title.length.max: '%env(APP_PAGE_TITLE_LENGTH_MAX)%' + app.page.description.length.min: '%env(APP_PAGE_DESCRIPTION_LENGTH_MIN)%' + app.page.description.length.max: '%env(APP_PAGE_DESCRIPTION_LENGTH_MAX)%' + app.page.torrent.quantity.min: '%env(APP_PAGE_TORRENT_QUANTITY_MIN)%' + app.page.torrent.quantity.max: '%env(APP_PAGE_TORRENT_QUANTITY_MAX)%' + app.page.torrent.size.min: '%env(APP_PAGE_TORRENT_SIZE_MIN)%' + app.page.torrent.size.max: '%env(APP_PAGE_TORRENT_SIZE_MAX)%' services: # default configuration for services in *this* file diff --git a/src/Controller/PageController.php b/src/Controller/PageController.php index fe48db3..1a580b2 100644 --- a/src/Controller/PageController.php +++ b/src/Controller/PageController.php @@ -3,25 +3,196 @@ namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Contracts\Translation\TranslatorInterface; + use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; +use App\Service\UserService; +use App\Service\PageService; +use App\Service\TimeService; + class PageController extends AbstractController { #[Route( '/{_locale}/page/submit', name: 'page_submit' )] - public function submit(): Response + public function submit( + Request $request, + TranslatorInterface $translator, + UserService $userService, + PageService $pageService, + ): Response { - /* - return $this->redirectToRoute('page', [ - 'id' => $page->getId() - ]); - */ - return $this->render('default/page/submit.html.twig', [ + // Init user + $user = $userService->init( + $request->getClientIp() + ); + + if (!$user->isStatus()) + { // @TODO + throw new \Exception( + $translator->trans('Access denied') + ); + } + + // Init form + $form = + [ + 'locale' => + [ + 'error' => [], + 'value' => $request->get('_locale'), + 'placeholder' => $translator->trans('Content language'), + ], + 'title' => + [ + 'error' => [], + 'attribute' => + [ + 'value' => $request->get('title'), + 'minlength' => $this->getParameter('app.page.title.length.min'), + 'maxlength' => $this->getParameter('app.page.title.length.max'), + 'placeholder' => sprintf( + $translator->trans('Page title text (%s-%s chars)'), + number_format($this->getParameter('app.page.title.length.min')), + number_format($this->getParameter('app.page.title.length.max')) + ), + ] + ], + 'description' => + [ + 'error' => [], + 'attribute' => + [ + 'value' => $request->get('description'), + 'minlength' => $this->getParameter('app.page.description.length.min'), + 'maxlength' => $this->getParameter('app.page.description.length.max'), + 'placeholder' => sprintf( + $translator->trans('Page description text (%s-%s chars)'), + number_format($this->getParameter('app.page.description.length.min')), + number_format($this->getParameter('app.page.description.length.max')) + ), + ] + ], + 'torrents' => + [ + 'error' => [], + 'attribute' => + [ + 'placeholder' => $translator->trans('Select torrent files'), + ] + ], + 'sensitive' => + [ + 'error' => [], + 'attribute' => + [ + 'value' => $request->get('sensitive'), + 'placeholder' => $translator->trans('Apply sensitive filters for this publication'), + ] + ] + ]; + + // Process request + if ($request->isMethod('post')) + { + // Init new + $page = $pageService->new(); + + /// Locale + if (!in_array($request->get('locale'), explode('|', $this->getParameter('app.locales')))) + { + $form['locale']['error'][] = $translator->trans('Requested locale not supported'); + } + + else + { + // $request->get('locale') + } + + /// Title + if (mb_strlen($request->get('title')) < $this->getParameter('app.page.title.length.min') || + mb_strlen($request->get('title')) > $this->getParameter('app.page.title.length.max')) + { + $form['title']['error'][] = sprintf( + $translator->trans('Page title out of %s-%s chars'), + number_format($this->getParameter('app.page.title.length.min')), + number_format($this->getParameter('app.page.title.length.max')) + ); + } + + else + { + // $request->get('title') + } + + /// Description + if (mb_strlen($request->get('description')) < $this->getParameter('app.page.description.length.min') || + mb_strlen($request->get('description')) > $this->getParameter('app.page.description.length.max')) + { + $form['description']['error'][] = sprintf( + $translator->trans('Page description out of %s-%s chars'), + number_format($this->getParameter('app.page.description.length.min')), + number_format($this->getParameter('app.page.description.length.max')) + ); + } + + else + { + // $request->get('description') + } + + /// Torrents + $total = 0; + + if ($files = $request->files->get('torrents')) + { + foreach ($files as $file) + { + //// Quantity + $total++; + + //// File size + if (filesize($file->getPathName()) > $this->getParameter('app.page.torrent.size.max')) + { + $form['torrents']['error'][] = $translator->trans('Torrent file out of size limit'); + } + + //// Content + $decoder = new \BitTorrent\Decoder(); + $decodedFile = $decoder->decodeFile( + $file->getPathName() + ); + + // var_dump($decodedFile['info']['name']); + } + } + + if ($total < $this->getParameter('app.page.torrent.quantity.min') || + $total > $this->getParameter('app.page.torrent.quantity.max')) + { + $form['torrents']['error'][] = sprintf( + $translator->trans('Torrents quantity out of %s-%s range'), + number_format($this->getParameter('app.page.torrent.quantity.min')), + number_format($this->getParameter('app.page.torrent.quantity.max')) + ); + } + + + if (empty($error)) + { + // isset($request->get('sensitive')) + // $pageService->save($page); + } + } + + return $this->render('default/page/submit.html.twig', [ + 'locales' => explode('|', $this->getParameter('app.locales')), + 'form' => $form, ]); } } \ No newline at end of file diff --git a/src/Service/PageService.php b/src/Service/PageService.php new file mode 100644 index 0000000..ae50c44 --- /dev/null +++ b/src/Service/PageService.php @@ -0,0 +1,37 @@ +entityManager = $entityManager; + $this->pageRepository = $entityManager->getRepository(Page::class); + $this->parameterBagInterface = $parameterBagInterface; + } + + public function new(): ?Page + { + return new Page(); + } + + public function save(Page $page) : void + { + $this->entityManager->persist($page); + $this->entityManager->flush(); + } +} \ No newline at end of file diff --git a/templates/default/page/submit.html.twig b/templates/default/page/submit.html.twig index 736ec81..19d4cb6 100644 --- a/templates/default/page/submit.html.twig +++ b/templates/default/page/submit.html.twig @@ -1,129 +1,115 @@ {% extends 'default/layout.html.twig' %} {% block title %}{{'Submit'|trans }} - {{ name }}{% endblock %} - {% block main_content %} -
+
+

{{'Submit'|trans }}

-
-
- -
- - - - - - -
-
- - - - - - - {% for errors in form.title.error %} - {% for error in errors %} + +
+ + + + + + + +
+
+ + + + + + + {% for error in form.title.error %}
{{ error }}
{% endfor %} - {% endfor %} - -
-
- - - - - - - {% for errors in form.description.error %} - {% for error in errors %} + +
+
+ + + + + + + {% for error in form.description.error %}
{{ error }}
{% endfor %} - {% endfor %} - -
-
- - - - - - - {% for errors in form.keywords.error %} - {% for error in errors %} + +
+
+ + + + + + + {% for error in form.torrents.error %}
{{ error }}
{% endfor %} - {% endfor %} - -
-
- - - - - - - -
-
- -
- + +
+
+ + + + + + + +
+
+ +
+ +
{% endblock %}