Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow participants to join by link (#481 based on 2021.01) #502

Merged
merged 19 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions assets/js/party.manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ $(document).ready(function () {
$('.link_remove_participant').attr('disabled', false);
});

$('#btn_join').click(function (e) {
$('#join-mode').show();
$('#btn_join').attr('disabled', true);
});

if (Modernizr.inputtypes.date == true) {
$("#intracto_secretsantabundle_updatepartydetailstype_eventdate").click(function (e) {
$(this).datepicker({dateFormat: 'dd-mm-yy'});
Expand Down
64 changes: 64 additions & 0 deletions src/Controller/Participant/JoinController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace App\Controller\Participant;

use App\Entity\Participant;
use App\Entity\Party;
use App\Form\Type\AddParticipantType;
use App\Repository\PartyRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class JoinController extends AbstractController
{
/**
* @Route("/join/{joinurl}", name="join_party")
* @Template("Participant/show/join.html.twig")
*/
public function joinAction(Request $request, string $joinurl, PartyRepository $partyRepository)
{
/** @var Party $party */
$party = $partyRepository->findOneBy(['joinurl' => $joinurl, 'joinmode' => 1, 'created' => 0]);

if (null !== $party) {
$addParticipantForm = $this->createForm(AddParticipantType::class, new Participant(), [
'action' => $this->generateUrl('join_party', ['joinurl' => $party->getJoinurl()]),
]);
$addParticipantForm->handleRequest($request);

if ($addParticipantForm->isSubmitted() && $addParticipantForm->isValid()) {
/** @var Participant $newParticipant */
$newParticipant = $addParticipantForm->getData();
$newParticipant->setParty($party);
$this->getDoctrine()->getManager()->persist($newParticipant);
$this->getDoctrine()->getManager()->flush();

return $this->redirectToRoute('join_party_joined', ['joinurl' => $party->getJoinurl()]);
}
}

return [
'party' => $party,
'form' => isset($addParticipantForm) ? $addParticipantForm->createView() : null,
];
}

/**
* @Route("/joined/{joinurl}", name="join_party_joined")
* @Template("Participant/show/join.html.twig")
*/
public function joinedAction(string $joinurl, PartyRepository $partyRepository)
{
/** @var Party $party */
$party = $partyRepository->findOneBy(['joinurl' => $joinurl, 'joinmode' => 1, 'created' => 0]);

return [
'party' => $party,
'form' => null,
];
}
}
31 changes: 31 additions & 0 deletions src/Controller/Party/ManagementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Entity\Party;
use App\Form\Handler\AddParticipantFormHandler;
use App\Form\Type\SetJoinModeType;
use App\Mailer\MailerService;
use App\Service\PartyService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
Expand Down Expand Up @@ -50,6 +51,9 @@ public function validAction(Party $party, Form $excludeForm = null)
$updatePartyDetailsForm = $this->createForm(UpdatePartyDetailsType::class, $party, [
'action' => $this->generateUrl('party_manage_update', ['listurl' => $party->getListurl()]),
]);
$setJoinModeForm = $this->createForm(SetJoinModeType::class, $party, [
'action' => $this->generateUrl('party_manage_joinmode', ['listurl' => $party->getListurl()]),
]);

if ($excludeForm === null) {
$excludeForm = $this->createForm(PartyExcludeParticipantType::class, $party, [
Expand All @@ -64,6 +68,7 @@ public function validAction(Party $party, Form $excludeForm = null)
'delete_party_csrf_token' => $this->get('security.csrf.token_manager')->getToken('delete_party'),
'delete_participant_csrf_token' => $this->get('security.csrf.token_manager')->getToken('delete_participant'),
'excludeForm' => $excludeForm->createView(),
'setJoinModeForm' => $setJoinModeForm->createView(),
];
}

Expand Down Expand Up @@ -144,4 +149,30 @@ public function excludeAction(Request $request, Party $party)

return $this->redirectToRoute('party_manage', ['listurl' => $party->getListurl()]);
}

/**
* @Route("/manage/joinmode/{listurl}", name="party_manage_joinmode", methods={"POST"})
*/
public function joinModeAction(Request $request, Party $party)
{
$party->setConfirmed(true);
$setJoinModeForm = $this->createForm(SetJoinModeType::class, $party, []);
$setJoinModeForm->handleRequest($request);

if ($setJoinModeForm->isSubmitted() && $setJoinModeForm->isValid()) {
if (($party->getJoinmode() == 1 && $party->getJoinurl() === null) || $request->request->get('reset', 0) == '1') {
// generate join URL
$party->setJoinurl(base_convert(sha1(uniqid((string) mt_rand(), true)), 16, 36));
}

$this->getDoctrine()->getManager()->persist($party);
$this->getDoctrine()->getManager()->flush();

$this->addFlash('success', $this->translator->trans('flashes.management.join_mode.success'));
} else {
$this->addFlash('danger', $this->translator->trans('flashes.management.join_mode.danger'));
}

return $this->redirectToRoute('party_manage', ['listurl' => $party->getListurl()]);
}
}
27 changes: 27 additions & 0 deletions src/Entity/Party.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class Party
*/
private $confirmed;

/** @var ?string */
private $joinurl;

/** @var int */
private $joinmode = 0;

public function __construct($createDefaults = true)
{
$this->participants = new ArrayCollection();
Expand All @@ -81,6 +87,7 @@ public function __construct($createDefaults = true)
$this->creationdate = new \DateTime();
$this->message = '';
$this->location = '';
$this->joinurl = base_convert(sha1(uniqid((string) mt_rand(), true)), 16, 36);
}

/**
Expand Down Expand Up @@ -292,6 +299,26 @@ public function setConfirmed(bool $confirmed)
$this->confirmed = (string) $confirmed;
}

public function getJoinurl(): ?string
{
return $this->joinurl;
}

public function setJoinurl(?string $joinurl): void
{
$this->joinurl = $joinurl;
}

public function getJoinmode(): int
{
return $this->joinmode;
}

public function setJoinmode(int $joinmode): void
{
$this->joinmode = $joinmode;
}

/**
* @return array
*/
Expand Down
50 changes: 50 additions & 0 deletions src/Form/Handler/JoinParticipantFormHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace App\Form\Handler;

use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Participant;
use App\Entity\Party;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class JoinParticipantFormHandler
{
private TranslatorInterface $translator;
private SessionInterface $session;
private EntityManagerInterface $em;

public function __construct(TranslatorInterface $translator, SessionInterface $session, EntityManagerInterface $em)
{
$this->translator = $translator;
$this->session = $session;
$this->em = $em;
}

public function handle(FormInterface $form, Request $request, Party $party): void
{
/** @var Participant $newParticipant */
$newParticipant = $form->getData();

if (!$request->isMethod('POST')) {
return;
}

if (!$form->handleRequest($request)->isValid()) {
$this->session->getFlashBag()->add('danger', $this->translator->trans('flashes.management.add_participant.danger'));

return;
}

$newParticipant->setParty($party);

$this->em->persist($newParticipant);
$this->em->flush();

$this->session->getFlashBag()->add('success', $this->translator->trans('flashes.management.add_participant.success'));
}
}
35 changes: 35 additions & 0 deletions src/Form/Type/SetJoinModeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Form\Type;

use App\Entity\Party;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SetJoinModeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'join_mode',
ChoiceType::class,
[
'attr' => ['data-hj-masked' => ''],
'choices' => ['party_manage_valid.join_mode.mode.no' => '0', 'party_manage_valid.join_mode.mode.yes' => '1'],
'expanded' => true,
'multiple' => false,
]
)
;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Party::class,
]);
}
}
3 changes: 3 additions & 0 deletions src/ORM/Mapping/Party.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<field name="created" column="created" type="boolean"/>
<field name="locale" column="locale" type="string" length="7"/>
<field name="location" column="location" type="string" length="255" nullable="true"/>

<field name="joinurl" column="join_url" type="string" length="50" nullable="true" unique="true"/>
<field name="joinmode" column="join_mode" type="integer"/>
</entity>

</doctrine-mapping>
66 changes: 66 additions & 0 deletions templates/Participant/show/join.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{% extends "Participant/show/base.html.twig" %}

{% block main %}
<div class="box">
<div class="row participant-title-row">
<div class="col-xs-12 col-sm-12 col-md-12">
<h1>{{ 'party_join.title'|trans }}</h1>

{% if party is not null %}

{{ 'party_join.description'|trans|raw }}

{% if form is null %}
<div class="alert alert-success">
{{ 'party_join.joined'|trans|raw }}
</div>
{% endif %}

<div class="party-info">
<h2>{{ 'participant_show_base.headers.title'|trans|raw }}</h2>
<div id="partyDetails">
<ul class="liststyle1">
<li><strong>{{ 'participant_show_base.headers.date'|trans }}: </strong> {{ party.eventdate|format_datetime('medium', 'none') }}</li>
<li><strong>{{ 'participant_show_base.headers.location'|trans }}: </strong> {{ party.location }}</li>
<li><strong>{{ 'participant_show_base.headers.amount'|trans }}: </strong> {{ party.amount }}</li>
<li><strong>{{ 'participant_show_base.headers.person_created_list'|trans }}: </strong> <span data-hj-masked>{{ party.participants|first.name }} ({{ party.participants|first.email }})</span>
</ul>
</div>
</div>

{% if form is not null %}
{{ form_start(form) }}
{{ form_row(form._token) }}
<div class="form-group {% if form_errors(form.name) %}error{% endif %}">
<strong>{{ 'party_manage_valid.label.name'|trans }}</strong> {{ form_widget(form.name ,{'attr':{'class':'form-control'}}) }}
{% if form_errors(form.name) %}
{% for error in form.name.vars.errors %}
<strong>{{ error.message }}</strong><br/>
{% endfor %}
{% endif %}
</div>
<div class="form-group {% if form_errors(form.email) %}error{% endif %}">
<strong>{{ 'party_manage_valid.label.email'|trans }}</strong> {{ form_widget(form.email ,{'attr':{'class':'form-control'}}) }}
{% if form_errors(form.email) %}
{% for error in form.email.vars.errors %}
<strong>{{ error.message }}</strong><br/>
{% endfor %}
{% endif %}
</div>
<button type="submit" class="btn btn-success"
id="btn_add_confirmation">{{ 'party_join.btn.join_confirm'|trans|raw }}</button>
<button type="reset" class="btn btn-success" id="btn_add_cancel">{{ 'party_manage_valid.btn.cancel'|trans }}</button>
{{ form_end(form) }}
{% endif %}
{% else %}
<div class="alert alert-danger">
{{ 'party_join.invalid'|trans|raw }}
</div>
{% endif %}

</div>
</div>

</div>

{% endblock %}
Loading