diff --git a/src/User/Database/CountryLanguages.php b/src/User/Database/CountryLanguages.php index cff5efd..4a197d8 100644 --- a/src/User/Database/CountryLanguages.php +++ b/src/User/Database/CountryLanguages.php @@ -137,4 +137,13 @@ class CountryLanguages return $countryLanguages[$countryName] ?? null; } + + public static function asSelectOptions(): array + { + $result = []; + foreach (static::$languages as $language) { + $result[$language] = $language; + } + return $result; + } } diff --git a/src/User/Database/ProviderList.php b/src/User/Database/ProviderList.php index 64af104..d50da35 100644 --- a/src/User/Database/ProviderList.php +++ b/src/User/Database/ProviderList.php @@ -23,4 +23,13 @@ class ProviderList { return self::$providers; } + + public static function asSelectOptions(): array + { + $result = []; + foreach (static::$providers as $provider) { + $result[$provider] = $provider; + } + return $result; + } } diff --git a/src/User/Database/QualityList.php b/src/User/Database/QualityList.php index 4ddd653..1ef9251 100644 --- a/src/User/Database/QualityList.php +++ b/src/User/Database/QualityList.php @@ -100,6 +100,15 @@ class QualityList return array_search($key, self::$qualities) ?? null; } + public static function asSelectOptions(): array + { + $result = []; + foreach (array_keys(static::$qualities) as $quality) { + $result[$quality] = $quality; + } + return $result; + } + public static function getAsReverseMap(): array { $results = []; diff --git a/src/User/Framework/Controller/Web/PreferencesController.php b/src/User/Framework/Controller/Web/PreferencesController.php index 598323f..3dcee51 100644 --- a/src/User/Framework/Controller/Web/PreferencesController.php +++ b/src/User/Framework/Controller/Web/PreferencesController.php @@ -12,6 +12,8 @@ use App\User\Action\Input\SaveUserMediaPreferencesInput; use App\User\Database\CountryLanguages; use App\User\Database\ProviderList; use App\User\Database\QualityList; +use App\User\Dto\UserPreferencesFactory; +use App\User\Framework\Form\GettingStartedFilterForm; use App\User\Framework\Repository\PreferencesRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; @@ -41,6 +43,7 @@ class PreferencesController extends AbstractController 'qualities' => QualityList::getBaseQualities(), 'mediaPreferences' => $mediaPreferences, 'downloadPreferences' => $downloadPreferences, + 'filterForm' => $this->createForm(GettingStartedFilterForm::class, (array) UserPreferencesFactory::createFromUser($this->getUser())), ] ); } @@ -72,6 +75,7 @@ class PreferencesController extends AbstractController 'qualities' => QualityList::getBaseQualities(), 'mediaPreferences' => $mediaPreferences, 'downloadPreferences' => $downloadPreferences, + 'filterForm' => $this->createForm(GettingStartedFilterForm::class ?? null), ] ); } diff --git a/src/User/Framework/Controller/Web/RegistrationController.php b/src/User/Framework/Controller/Web/RegistrationController.php index fae7d08..3ee2883 100644 --- a/src/User/Framework/Controller/Web/RegistrationController.php +++ b/src/User/Framework/Controller/Web/RegistrationController.php @@ -5,30 +5,22 @@ namespace App\User\Framework\Controller\Web; use App\User\Action\Command\RegisterUserCommand; use App\User\Action\Handler\RegisterUserHandler; use App\User\Framework\Entity\User; +use App\User\Framework\Form\GettingStartedFilterForm; use App\User\Framework\Form\RegistrationFormType; -use App\User\Framework\Pipeline\GettingStarted\AddPreferencesToDatabase; -use App\User\Framework\Pipeline\GettingStarted\GettingStartedInput; -use App\User\Framework\Pipeline\GettingStarted\MigrateDatabase; -use App\User\Framework\Repository\PreferencesRepository; use App\User\Framework\Repository\UserRepository; use Doctrine\Common\Collections\ArrayCollection; -use League\Pipeline\Pipeline; -use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Routing\Attribute\Route; class RegistrationController extends AbstractController { public function __construct(private readonly RegisterUserHandler $registerUserHandler, - private readonly RequestStack $requestStack - ) - { - } + private readonly RequestStack $requestStack, + ) {} #[Route('/register', name: 'app_register')] public function register( @@ -57,7 +49,7 @@ class RegistrationController extends AbstractController } #[Route(path: '/getting-started', name: 'app_getting_started')] - public function gettingStarted(Request $request, Security $security, UserRepository $userRepository, PreferencesRepository $preferencesRepository, KernelInterface $kernel, LoggerInterface $logger): Response + public function gettingStarted(Request $request, Security $security, UserRepository $userRepository): Response { if ((new ArrayCollection($userRepository->findAll()))->count() !== 0) { return $this->redirectToRoute('app_index'); @@ -73,14 +65,42 @@ class RegistrationController extends AbstractController password: $form->get('plainPassword')->getData(), )); - $security->login($user->user); + $security->login($user->user, 'form_login'); $this->requestStack->getCurrentRequest()->getSession()->set('mercure_alert_topic', 'alerts_' . uniqid()); - return $this->redirectToRoute('app_index'); + return $this->redirectToRoute('app_getting_started_filter'); } - return $this->render('user/getting-started.html.twig', [ + return $this->render('user/getting_started/register-user.html.twig', [ 'registrationForm' => $form, ]); } + + #[Route(path: '/getting-started/filter', name: 'app_getting_started_filter')] + public function gettingStartedPreferences(Request $request, UserRepository $userRepository): Response + { + if ((new ArrayCollection($userRepository->findAll()))->count() !== 0) { + return $this->redirectToRoute('app_index'); + } + + $form = $this->createForm(GettingStartedFilterForm::class); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + foreach ($form->getData() as $preference => $value) { + if (null !== $value) { + $this->getUser()->updateUserPreference($preference, $value); + } + } + $userRepository->getEntityManager()->flush(); + return $this->redirectToRoute('app_index'); + } + + return $this->render( + 'user/getting_started/filter.html.twig', + [ + 'form' => $form, + ] + ); + } } diff --git a/src/User/Framework/Form/GettingStartedFilterForm.php b/src/User/Framework/Form/GettingStartedFilterForm.php new file mode 100644 index 0000000..3c01e77 --- /dev/null +++ b/src/User/Framework/Form/GettingStartedFilterForm.php @@ -0,0 +1,59 @@ +addChoiceField($builder, 'language', CountryLanguages::asSelectOptions()); + $this->addChoiceField($builder, 'quality', QualityList::asSelectOptions()); + $this->addChoiceField($builder, 'provider', ProviderList::asSelectOptions()); + $this->addChoiceField($builder, 'resolution', $this->getPreferenceChoices('resolution')); + $this->addChoiceField($builder, 'codec', $this->getPreferenceChoices('codec')); + } + + private function addChoiceField(FormBuilderInterface $builder, string $fieldName, array $choices): void + { + $question = [ + 'attr' => ['class' => 'w-full text-input mb-4'], + 'label_attr' => ['class' => 'w-full block font-semibold'], + 'choices' => $this->addDefaultChoice($choices), + ]; + $builder->add($fieldName, ChoiceType::class, $question); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([]); + } + + private function getPreferenceChoices(string $preference): array + { + $options = $this->preferenceOptionRepository->findBy(['preference' => $preference]); + $result = []; + foreach ($options as $item) { + $result[$item->getName()] = $item->getId(); + } + return $result; + } + + private function addDefaultChoice(array $choices): iterable + { + return ['n/a' => null] + $choices; + } +} diff --git a/src/User/Framework/Form/RegistrationFormType.php b/src/User/Framework/Form/RegistrationFormType.php index 8ce1a34..53ad169 100644 --- a/src/User/Framework/Form/RegistrationFormType.php +++ b/src/User/Framework/Form/RegistrationFormType.php @@ -27,7 +27,7 @@ class RegistrationFormType extends AbstractType 'message' => 'Please enter a password', ]), new Length([ - 'min' => 6, + 'min' => 8, 'minMessage' => 'Your password should be at least {{ limit }} characters', // max length allowed by Symfony for security reasons 'max' => 4096, diff --git a/templates/user/getting_started/filter.html.twig b/templates/user/getting_started/filter.html.twig new file mode 100644 index 0000000..9491b91 --- /dev/null +++ b/templates/user/getting_started/filter.html.twig @@ -0,0 +1,20 @@ +{% extends 'bare.html.twig' %} + +{% block title %}Getting Started — Torsearch{% endblock %} + +{% block body %} +
Now let's create your first Filter.
+{#Your filter will be pre-applied to your results, so you're only shown what you want to see. Don't worry, though, you can toggle each filter option afterwards, so you can see the rest of the results.
#} + + {{ form_start(form) }} + {{ form_row(form.language) }} + {{ form_row(form.quality) }} + {{ form_row(form.provider) }} + {{ form_row(form.resolution) }} + {{ form_row(form.codec) }} + + {{ form_end(form) }} +Let's get started by creating your first User.
diff --git a/templates/user/login.html.twig b/templates/user/login.html.twig index ed98cfa..9a71e77 100644 --- a/templates/user/login.html.twig +++ b/templates/user/login.html.twig @@ -3,7 +3,7 @@ {% block title %}Log in — Torsearch{% endblock %} {% block body %} -