Skip to content

Commit

Permalink
Make Polls completely localisable and change form
Browse files Browse the repository at this point in the history
Adapts the `Poll`(`Option`)s to use `LocalisedText` to enhance
the experience of creating polls.
  • Loading branch information
tomudding committed Jul 2, 2023
1 parent 0d9910e commit c475f28
Show file tree
Hide file tree
Showing 21 changed files with 316 additions and 272 deletions.
11 changes: 9 additions & 2 deletions module/Application/language/en.po

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions module/Application/language/gewisweb.pot

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions module/Application/language/nl.po

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions module/Frontpage/src/Form/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(Translator $translator)
'name' => 'dutchQuestion',
'type' => Text::class,
'options' => [
'label' => $translator->translate('Dutch question'),
'label' => $translator->translate('Question'),
],
],
);
Expand All @@ -35,7 +35,7 @@ public function __construct(Translator $translator)
'name' => 'englishQuestion',
'type' => Text::class,
'options' => [
'label' => $translator->translate('English question'),
'label' => $translator->translate('Question'),
],
],
);
Expand All @@ -47,10 +47,9 @@ public function __construct(Translator $translator)
'options' => [
'count' => 2,
'should_create_template' => true,
'template_placeholder' => '__option__',
'allow_add' => true,
'target_element' => [
'type' => PollOptionFieldset::class,
],
'target_element' => new PollOptionFieldset($translator),
],
],
);
Expand Down
7 changes: 4 additions & 3 deletions module/Frontpage/src/Form/PollOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
use Laminas\Form\Fieldset;
use Laminas\Hydrator\ClassMethodsHydrator;
use Laminas\InputFilter\InputFilterProviderInterface;
use Laminas\Mvc\I18n\Translator;
use Laminas\Validator\StringLength;

class PollOption extends Fieldset implements InputFilterProviderInterface
{
public function __construct()
public function __construct(private readonly Translator $translator)
{
parent::__construct('pollOption');

Expand All @@ -26,7 +27,7 @@ public function __construct()
'name' => 'dutchText',
'type' => Text::class,
'options' => [
'label' => 'Dutch option',
'label' => $this->translator->translate('Option %s'),
],
],
);
Expand All @@ -36,7 +37,7 @@ public function __construct()
'name' => 'englishText',
'type' => Text::class,
'options' => [
'label' => 'English option',
'label' => $this->translator->translate('Option %s'),
],
],
);
Expand Down
16 changes: 16 additions & 0 deletions module/Frontpage/src/Model/FrontpageLocalisedText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Frontpage\Model;

use Application\Model\LocalisedText;
use Doctrine\ORM\Mapping\Entity;

/**
* {@link LocalisedText} for the Frontpage module.
*/
#[Entity]
class FrontpageLocalisedText extends LocalisedText
{
}
40 changes: 17 additions & 23 deletions module/Frontpage/src/Model/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OneToOne;
use Laminas\Permissions\Acl\Resource\ResourceInterface;

/**
Expand All @@ -31,16 +32,19 @@ class Poll implements ResourceInterface
protected DateTime $expiryDate;

/**
* The Dutch question for the poll.
* The localised question for the poll.
*/
#[Column(type: 'string')]
protected string $dutchQuestion;

/**
* The English question for the poll.
*/
#[Column(type: 'string')]
protected string $englishQuestion;
#[OneToOne(
targetEntity: FrontpageLocalisedText::class,
cascade: ['persist', 'remove'],
orphanRemoval: true,
)]
#[JoinColumn(
name: 'question_id',
referencedColumnName: 'id',
nullable: false,
)]
protected FrontpageLocalisedText $question;

/**
* Poll options.
Expand Down Expand Up @@ -94,14 +98,9 @@ public function getExpiryDate(): DateTime
return $this->expiryDate;
}

public function getDutchQuestion(): string
{
return $this->dutchQuestion;
}

public function getEnglishQuestion(): string
public function getQuestion(): FrontpageLocalisedText
{
return $this->englishQuestion;
return $this->question;
}

/**
Expand Down Expand Up @@ -135,14 +134,9 @@ public function setExpiryDate(DateTime $expiryDate): void
$this->expiryDate = $expiryDate;
}

public function setEnglishQuestion(string $englishQuestion): void
{
$this->englishQuestion = $englishQuestion;
}

public function setDutchQuestion(string $dutchQuestion): void
public function setQuestion(FrontpageLocalisedText $question): void
{
$this->dutchQuestion = $dutchQuestion;
$this->question = $question;
}

/**
Expand Down
40 changes: 17 additions & 23 deletions module/Frontpage/src/Model/PollOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OneToOne;
use Laminas\Permissions\Acl\Resource\ResourceInterface;

/**
Expand All @@ -37,16 +38,19 @@ class PollOption implements ResourceInterface
protected Poll $poll;

/**
* The dutch text for this option.
* The localised text for this option.
*/
#[Column(type: 'string')]
protected string $dutchText;

/**
* The english translation of the option if available.
*/
#[Column(type: 'string')]
protected string $englishText;
#[OneToOne(
targetEntity: FrontpageLocalisedText::class,
cascade: ['persist', 'remove'],
orphanRemoval: true,
)]
#[JoinColumn(
name: 'text_id',
referencedColumnName: 'id',
nullable: false,
)]
protected FrontpageLocalisedText $text;

/**
* Votes for this option.
Expand All @@ -72,16 +76,6 @@ public function getPoll(): Poll
return $this->poll;
}

public function getDutchText(): string
{
return $this->dutchText;
}

public function getEnglishText(): string
{
return $this->englishText;
}

/**
* Adds a new vote for this poll option.
*/
Expand All @@ -96,14 +90,14 @@ public function setPoll(Poll $poll): void
$this->poll = $poll;
}

public function setDutchText(string $dutchText): void
public function getText(): FrontpageLocalisedText
{
$this->dutchText = $dutchText;
return $this->text;
}

public function setEnglishText(string $englishText): void
public function setText(FrontpageLocalisedText $text): void
{
$this->englishText = $englishText;
$this->text = $text;
}

/**
Expand Down
8 changes: 3 additions & 5 deletions module/Frontpage/src/Service/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Frontpage\Mapper\Poll as PollMapper;
use Frontpage\Mapper\PollComment as PollCommentMapper;
use Frontpage\Mapper\PollOption as PollOptionMapper;
use Frontpage\Model\FrontpageLocalisedText;
use Frontpage\Model\Poll as PollModel;
use Frontpage\Model\PollComment as PollCommentModel;
use Frontpage\Model\PollOption;
Expand Down Expand Up @@ -289,9 +290,7 @@ public function savePollData(
MemberModel $user,
): PollModel {
$poll = new PollModel();
$poll->setDutchQuestion($data['dutchQuestion']);
$poll->setEnglishQuestion($data['englishQuestion']);

$poll->setQuestion(new FrontpageLocalisedText($data['englishQuestion'], $data['dutchQuestion']));
$poll->setExpiryDate(new DateTime());
$poll->setCreator($user);

Expand All @@ -313,8 +312,7 @@ public function createPollOption(
PollModel $poll,
): PollOption {
$pollOption = new PollOptionModel();
$pollOption->setDutchText($data['dutchText']);
$pollOption->setEnglishText($data['englishText']);
$pollOption->setText(new FrontpageLocalisedText($data['englishText'], $data['dutchText']));
$pollOption->setPoll($poll);

return $pollOption;
Expand Down
2 changes: 1 addition & 1 deletion module/Frontpage/view/email/poll.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ New&nbsp;poll&nbsp;requested
<p style="margin: 0; font-size: 18px;">
<p>Dear board member,</p>
<p><i><?= $this->escapeHtml($poll->getCreator()->getFullName()) ?></i> has created a new poll. It requires your review before it will appear on the website.</p>
<p><a href="<?= $this->serverUrl($this->url('admin_poll')) ?>" style="color: #C40000; text-decoration: none;" target="_blank" rel="noopener nofollow">Click here</a> to view this poll. The question of this poll is <i>"<?= $this->escapeHtml($poll->getEnglishQuestion()) ?>"</i>.</p>
<p><a href="<?= $this->serverUrl($this->url('admin_poll')) ?>" style="color: #C40000; text-decoration: none;" target="_blank" rel="noopener nofollow">Click here</a> to view this poll. The question of this poll is <i>"<?= $this->escapeHtml($poll->getQuestion()->getValueEN()) ?>"</i>.</p>
With kind regards,<br> The ApplicatieBeheerCommissie
</p>
</div>
Expand Down
Loading

0 comments on commit c475f28

Please sign in to comment.