Commit 02120452 authored by Oliver Eglseder's avatar Oliver Eglseder
Browse files

[TASK] Persist election votes

parent 55977a28
......@@ -19,6 +19,7 @@ use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\Election\Domain\Model\Dto\ElectionVoting;
use TYPO3\Election\Domain\Model\ElectionInvitation;
use TYPO3\Election\Domain\Model\ElectionVote;
/**
* Class FeElectionController
......@@ -35,6 +36,18 @@ class FeElectionController extends ActionController
*/
protected $hashService = null;
/**
* @var \TYPO3\Election\Domain\Repository\ElectionVoteRepository
* @inject
*/
protected $electionVoteRepository = null;
/**
* @var \TYPO3\Election\Domain\Repository\ElectionInvitationRepository
* @inject
*/
protected $electionInvitationRepository = null;
/**
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentNameException
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
......@@ -61,8 +74,17 @@ class FeElectionController extends ActionController
if (null !== $electionInvitation && '' !== $hmac) {
$saltedEmail = $electionInvitation->getSecret() . $electionInvitation->getElector()->getEmail();
if ($this->hashService->validateHmac($saltedEmail, $hmac)) {
$this->view->assign('electionInvitation', $electionInvitation);
$this->view->assign('electionVoting', new ElectionVoting());
if ($electionInvitation->isVoted()) {
$this->addFlashMessage(
LocalizationUtility::translate('controller.fe.election.vote.already_voted', 'election'),
LocalizationUtility::translate('controller.fe.election.vote.request_failed', 'election'),
AbstractMessage::ERROR
);
$this->redirect(FeDashboardController::ACTION_INDEX, FeDashboardController::CONTROLLER_NAME);
} else {
$this->view->assign('electionInvitation', $electionInvitation);
$this->view->assign('electionVoting', new ElectionVoting());
}
} else {
$this->addFlashMessage(
LocalizationUtility::translate('controller.fe.election.vote.hmac_invalid', 'election'),
......@@ -98,19 +120,36 @@ class FeElectionController extends ActionController
/**
* @param ElectionVoting $electionVoting
* @validate $electionVoting \TYPO3\Election\Domain\Validator\ElectionVotingValidator
*/
public function submitVoteAction(ElectionVoting $electionVoting)
{
if (count($electionVoting->getNominees()) > $electionVoting->getElectionInvitation()
->getElectionCircular()
->getElection()
->getNumberOfVotes()
) {
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump('too many votes', __CLASS__ . '@' . __LINE__, 20);
} else {
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump('accepted', __CLASS__ . '@' . __LINE__, 20);
$electionInvitation = $electionVoting->getElectionInvitation();
if ($electionInvitation->isVoted()) {
$this->addFlashMessage(
LocalizationUtility::translate('controller.fe.election.vote.already_voted', 'election'),
LocalizationUtility::translate('controller.fe.election.vote.request_failed', 'election'),
AbstractMessage::ERROR
);
$this->redirect(FeDashboardController::ACTION_INDEX, FeDashboardController::CONTROLLER_NAME);
}
$electionInvitation->setVoted(true);
$this->electionInvitationRepository->update($electionInvitation);
foreach ($electionVoting->getNominees() as $electedNominee) {
$electionVote = $this->objectManager->get(ElectionVote::class);
$electionVote->setElection($electionInvitation->getElectionCircular()->getElection());
$electionVote->setElector($electionInvitation->getElector());
$electionVote->setNominee($electedNominee);
$this->electionVoteRepository->add($electionVote);
}
die;
$this->addFlashMessage(
LocalizationUtility::translate('controller.fe.election.vote.thank_you', 'election'),
LocalizationUtility::translate('controller.fe.election.vote.success', 'election'),
AbstractMessage::ERROR
);
$this->redirect(FeDashboardController::ACTION_INDEX, FeDashboardController::CONTROLLER_NAME);
}
/**
......
......@@ -19,7 +19,7 @@ use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
/**
* Class AbstractEntity
*/
class AbstractEntity extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
abstract class AbstractEntity extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* @var int
......
......@@ -39,6 +39,11 @@ class ElectionInvitation extends AbstractEntity
*/
protected $success = false;
/**
* @var bool
*/
protected $voted = false;
/**
* @return string
*/
......@@ -102,4 +107,20 @@ class ElectionInvitation extends AbstractEntity
{
$this->success = $success;
}
/**
* @return boolean
*/
public function isVoted()
{
return $this->voted;
}
/**
* @param boolean $voted
*/
public function setVoted($voted)
{
$this->voted = $voted;
}
}
<?php
namespace TYPO3\Election\Domain\Model;
/**
* Class ElectionVote
*/
class ElectionVote extends AbstractEntity
{
/**
* @var \TYPO3\Election\Domain\Model\Elector
*/
protected $elector = null;
/**
* @var \TYPO3\Election\Domain\Model\Election
*/
protected $election = null;
/**
* @var \TYPO3\Election\Domain\Model\Nominee
*/
protected $nominee = null;
/**
* @return Elector
*/
public function getElector()
{
return $this->elector;
}
/**
* @param Elector $elector
*/
public function setElector($elector)
{
$this->elector = $elector;
}
/**
* @return Election
*/
public function getElection()
{
return $this->election;
}
/**
* @param Election $election
*/
public function setElection($election)
{
$this->election = $election;
}
/**
* @return Nominee
*/
public function getNominee()
{
return $this->nominee;
}
/**
* @param Nominee $nominee
*/
public function setNominee($nominee)
{
$this->nominee = $nominee;
}
}
<?php
namespace TYPO3\Election\Domain\Repository;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Extbase\Persistence\Repository;
/**
* Class ElectionVoteRepository
*/
class ElectionVoteRepository extends Repository
{
}
<?php
namespace TYPO3\Election\Domain\Validator;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
use TYPO3\Election\Domain\Model\Dto\ElectionVoting;
/**
* Class ElectionVotingValidator
*/
class ElectionVotingValidator extends AbstractValidator
{
/**
* @param mixed $value
*/
protected function isValid($value)
{
if ($value instanceof ElectionVoting) {
if (count($value->getNominees()) > $value->getElectionInvitation()
->getElectionCircular()
->getElection()
->getNumberOfVotes()
) {
$this->addError(
LocalizationUtility::translate(
'validator.fe.election_voting.election_voting_object.to_many_nominees',
'election'
),
1451137701
);
$value->setNominees(new ObjectStorage());
}
} else {
$this->addError(
LocalizationUtility::translate(
'validator.fe.election_voting.election_voting_object.not_an_object',
'election'
),
1451142398
);
}
}
}
......@@ -396,6 +396,15 @@
<trans-unit id="controller.fe.election.vote.no_election_or_hmac">
<source>The link you clicked is not valid anymore.</source>
</trans-unit>
<trans-unit id="controller.fe.election.vote.already_voted">
<source>You voted already.</source>
</trans-unit>
<trans-unit id="controller.fe.election.vote.thank_you">
<source>Thank you for voting.</source>
</trans-unit>
<trans-unit id="controller.fe.election.vote.success">
<source>Success</source>
</trans-unit>
<!-- FE ELECTION -->
......@@ -412,6 +421,15 @@
<source>Vote</source>
</trans-unit>
<!-- FE ELECTION VOTING VALIDATION-->
<trans-unit id="validator.fe.election_voting.election_voting_object.to_many_nominees">
<source>To many votes</source>
</trans-unit>
<trans-unit id="validator.fe.election_voting.election_voting_object.not_an_object">
<source>The received votes are not in a valiid format</source>
</trans-unit>
</body>
</file>
</xliff>
......@@ -31,7 +31,7 @@
<f:link.page pageUid="{nominee.url}">{nominee.url}</f:link.page>
</td>
<td>
<f:form.checkbox property="nominees." value="{nominee.uid}"/>
<f:form.checkbox property="nominees.{nominee.uid}" value="{nominee.uid}"/>
</td>
</tr>
</f:if>
......
......@@ -153,6 +153,24 @@ CREATE TABLE tx_election_domain_model_electioninvitation (
election_circular INT(11) unsigned DEFAULT '0' NOT NULL,
elector INT(11) unsigned DEFAULT '0' NOT NULL,
success TINYINT(4) unsigned DEFAULT '0' NOT NULL,
voted TINYINT(4) unsigned DEFAULT '0' NOT NULL,
PRIMARY KEY (uid),
KEY parent (pid)
);
#
# Table structure for table 'tx_election_domain_model_electionvote'
#
CREATE TABLE tx_election_domain_model_electionvote (
uid INT(11) unsigned NOT NULL auto_increment,
pid INT(11) unsigned DEFAULT '0' NOT NULL,
creation_date INT(11) unsigned DEFAULT '0' NOT NULL,
creation_user INT(11) unsigned DEFAULT '0' NOT NULL,
elector INT(11) unsigned DEFAULT '0' NOT NULL,
election INT(11) unsigned DEFAULT '0' NOT NULL,
nominee INT(11) unsigned DEFAULT '0' NOT NULL,
PRIMARY KEY (uid),
KEY parent (pid)
......
......@@ -167,6 +167,7 @@ config.tx_extbase.persistence.classes {
columns {
secret.mapOnProperty = secret
success.mapOnProperty = success
voted.mapOnProperty = voted
election_circular {
mapOnProperty = electionCircular
config {
......@@ -184,4 +185,34 @@ config.tx_extbase.persistence.classes {
}
}
}
TYPO3\Election\Domain\Model\ElectionVote < temp.abstractEntity
TYPO3\Election\Domain\Model\ElectionVote {
mapping {
tableName = tx_election_domain_model_electionvote
columns {
elector {
mapOnProperty = elector
config {
type = select
foreign_table = tx_election_domain_model_elector
}
}
election {
mapOnProperty = election
config {
type = select
foreign_table = tx_election_domain_model_election
}
}
nominee {
mapOnProperty = nominee
config {
type = select
foreign_table = tx_election_domain_model_nominee
}
}
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment