diff --git a/composer.json b/composer.json index 05c9622..85b737a 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "OSL-3.0", "require": { "php": ">=8.4", - "forumify/forumify-platform": "^1.0", + "forumify/forumify-platform": "^1.0.7", "deschutesdesigngroupllc/perscom-php-sdk": "3.0.0", "league/commonmark": "^2.5" }, diff --git a/migrations/Version20260115160839.php b/migrations/Version20260115160839.php new file mode 100644 index 0000000..6362a3f --- /dev/null +++ b/migrations/Version20260115160839.php @@ -0,0 +1,29 @@ +addSql('INSERT INTO setting (`key`, `value`) VALUES (?, ?)', [ + 'perscom.enlistment.roleplay_names', + 'true', + ]); + } + + public function down(Schema $schema): void + { + $this->addSql('DELETE FROM setting WHERE `key` = ?', ['perscom.enlistment.roleplay_names']); + } +} diff --git a/src/Admin/Form/ConfigurationType.php b/src/Admin/Form/ConfigurationType.php index e0edf0b..3458ace 100644 --- a/src/Admin/Form/ConfigurationType.php +++ b/src/Admin/Form/ConfigurationType.php @@ -67,6 +67,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'placeholder' => 'Select a form to use for enlistments', 'required' => false, ]) + ->add('perscom__enlistment__roleplay_names', CheckboxType::class, [ + 'required' => false, + 'label' => 'Require roleplay friendly name', + 'help' => 'When enabled, the enlistment form will show "firstname" and "lastname". Otherwise, it will use the logged in user\'s forum display name.', + ]) ->add('perscom__enlistment__forum', ChoiceType::class, [ 'autocomplete' => true, 'choices' => $this->getForumChoices(), diff --git a/src/Forum/Controller/UserEnlistController.php b/src/Forum/Controller/UserEnlistController.php index c78ee7a..7b811e7 100644 --- a/src/Forum/Controller/UserEnlistController.php +++ b/src/Forum/Controller/UserEnlistController.php @@ -5,7 +5,7 @@ namespace Forumify\PerscomPlugin\Forum\Controller; use Forumify\Core\Entity\User; -use Forumify\PerscomPlugin\Forum\Form\Enlistment; +use Forumify\Core\Repository\SettingRepository; use Forumify\PerscomPlugin\Forum\Form\EnlistmentType; use Forumify\PerscomPlugin\Perscom\Entity\PerscomUser; use Forumify\PerscomPlugin\Perscom\Service\PerscomEnlistService; @@ -20,6 +20,7 @@ class UserEnlistController extends AbstractController public function __construct( private readonly PerscomEnlistService $perscomEnlistService, private readonly PerscomUserService $perscomUserService, + private readonly SettingRepository $settingRepository, ) { } @@ -59,10 +60,10 @@ public function __invoke(Request $request): Response ]); } - $enlistment = new Enlistment(); - $enlistment->email = $user->getEmail(); - - $form = $this->createForm(EnlistmentType::class, $enlistment, ['form' => $enlistmentForm]); + $form = $this->createForm(EnlistmentType::class, null, [ + 'form' => $enlistmentForm, + 'roleplay_names' => $this->settingRepository->get('perscom.enlistment.roleplay_names') ?? true, + ]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $perscomUser = $this->perscomEnlistService->enlist($form->getData()); diff --git a/src/Forum/Form/Enlistment.php b/src/Forum/Form/Enlistment.php index 4771358..1ae8a71 100644 --- a/src/Forum/Form/Enlistment.php +++ b/src/Forum/Form/Enlistment.php @@ -4,17 +4,9 @@ namespace Forumify\PerscomPlugin\Forum\Form; -use Symfony\Component\Validator\Constraints as Assert; - class Enlistment { - public string $email; - - #[Assert\NotBlank] - public string $firstName; - - #[Assert\NotBlank] - public string $lastName; - + public string $firstName = ''; + public string $lastName = ''; public array $additionalFormData = []; } diff --git a/src/Forum/Form/EnlistmentType.php b/src/Forum/Form/EnlistmentType.php index 6cdf8cf..7e81291 100644 --- a/src/Forum/Form/EnlistmentType.php +++ b/src/Forum/Form/EnlistmentType.php @@ -8,6 +8,8 @@ use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Validator\Constraints\Length; +use Symfony\Component\Validator\Constraints\NotBlank; class EnlistmentType extends AbstractType { @@ -16,18 +18,32 @@ public function configureOptions(OptionsResolver $resolver): void $resolver->setDefaults([ 'data_class' => Enlistment::class, 'form' => null, + 'roleplay_names' => false, ]); } public function buildForm(FormBuilderInterface $builder, array $options): void { - $builder - ->add('email', TextType::class, ['disabled' => true]) - ->add('firstName', TextType::class) - ->add('lastName', TextType::class) - ->add('additionalFormData', PerscomFormType::class, [ - 'label' => false, - 'perscomForm' => $options['form'], - ]); + if ($options['roleplay_names']) { + $builder + ->add('firstName', TextType::class, [ + 'constraints' => [ + new NotBlank(), + new Length(min: 2), + ], + ]) + ->add('lastName', TextType::class, [ + 'constraints' => [ + new NotBlank(), + new Length(min: 2), + ], + ]) + ; + } + + $builder->add('additionalFormData', PerscomFormType::class, [ + 'label' => false, + 'perscomForm' => $options['form'], + ]); } } diff --git a/src/Perscom/Service/PerscomEnlistService.php b/src/Perscom/Service/PerscomEnlistService.php index 6343f5a..20d365a 100644 --- a/src/Perscom/Service/PerscomEnlistService.php +++ b/src/Perscom/Service/PerscomEnlistService.php @@ -53,10 +53,8 @@ public function getEnlistmentForm(): ?Form public function enlist(Enlistment $enlistment): PerscomUser { - $perscomUser = $this->perscomUserService->getLoggedInPerscomUser() ?? $this->perscomUserService->createUser( - $enlistment->firstName, - $enlistment->lastName, - ); + $perscomUser = $this->perscomUserService->getLoggedInPerscomUser() + ?? $this->perscomUserService->createUser($enlistment); $submission = new FormSubmission(); $submission->setForm($this->getEnlistmentForm()); diff --git a/src/Perscom/Service/PerscomUserService.php b/src/Perscom/Service/PerscomUserService.php index 88c546b..39fc042 100644 --- a/src/Perscom/Service/PerscomUserService.php +++ b/src/Perscom/Service/PerscomUserService.php @@ -7,6 +7,7 @@ use Forumify\Core\Entity\SortableEntityInterface; use Forumify\Core\Entity\User; use Forumify\Core\Repository\SettingRepository; +use Forumify\PerscomPlugin\Forum\Form\Enlistment; use Forumify\PerscomPlugin\Perscom\Entity\PerscomUser; use Forumify\PerscomPlugin\Perscom\Repository\PerscomUserRepository; use Symfony\Bundle\SecurityBundle\Security; @@ -44,17 +45,20 @@ public function getPerscomUser(User $user): ?PerscomUser return $this->userIdToPerscomUser[$userId]; } - public function createUser(string $firstName, string $lastName): PerscomUser + public function createUser(Enlistment $enlistment): PerscomUser { /** @var User $user */ $user = $this->security->getUser(); - $name = ucfirst($firstName) . ' ' . ucfirst($lastName); - $perscomUser = new PerscomUser(); $perscomUser->setUser($user); - $perscomUser->setName($name); - $this->perscomUserRepository->save($perscomUser); + $perscomUser->setName($user->getDisplayName()); + if (!empty($enlistment->firstName) && !empty($enlistment->lastName)) { + $name = ucfirst($enlistment->firstName) . ' ' . ucfirst($enlistment->lastName); + $perscomUser->setName($name); + } + + $this->perscomUserRepository->save($perscomUser); return $perscomUser; } diff --git a/templates/admin/configuration.html.twig b/templates/admin/configuration.html.twig index 0776ec4..c659445 100644 --- a/templates/admin/configuration.html.twig +++ b/templates/admin/configuration.html.twig @@ -23,6 +23,7 @@
{{ successMessage|perscom_text }}
{% endif %}- {{ 'perscom.enlistment.pending'|trans }} + {% if enlistmentTopic is not null %} + {{ 'perscom.enlistment.pending_with_topic'|trans }} + {% else %} + {{ 'perscom.enlistment.pending'|trans }} + {% endif %}