Commit 29c34c1d authored by Oliver Eglseder's avatar Oliver Eglseder
Browse files

[WIP][TASK] Create invitations for an election

parent 4cb65357
......@@ -18,6 +18,7 @@ use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\Election\Domain\Model\ElectionCircular;
use TYPO3\Election\Domain\Model\Elector;
use TYPO3\Election\Service\CircularService;
use TYPO3\Election\Service\ElectionInvitationService;
use TYPO3\Election\Utility\MailUtility;
/**
......@@ -149,8 +150,14 @@ class BeElectionCircularController extends AbstractProtectedBeController
*/
public function sendAction(ElectionCircular $electionCircular)
{
$circularService = $this->objectManager->get(CircularService::class, $this->configuration);
$circularService->sendElectionCircular($electionCircular);
$electionInvitationService = $this->objectManager->get(ElectionInvitationService::class);
$electionInvitationService->inviteElectors($this->configuration, $electionCircular);
$this->redirect(
self::ACTION_SEND_PREVIEW,
self::CONTROLLER_NAME,
'election',
['electionCircular' => $electionCircular]
);
}
/**
......
......@@ -138,7 +138,14 @@ class ElectionCircular extends AbstractEntity
*/
public function getRemainingReceivers()
{
// TODO: return only those who did not receive an invitation yet
return $this->getElection()->getElectorate()->getElectors();
$election = $this->getElection();
$electors = $election->getElectorate()->getElectorsAsArray();
$receivers = [];
foreach ($electors as $elector) {
if (!$elector->hasElectionInvitationForElection($election)) {
$receivers[] = $elector;
}
}
return $receivers;
}
}
......@@ -25,20 +25,15 @@ class ElectionInvitation extends AbstractEntity
protected $secret = '';
/**
* @var \TYPO3\Election\Domain\Model\Election
* @var \TYPO3\Election\Domain\Model\ElectionCircular
*/
protected $election = null;
protected $electionCircular = null;
/**
* @var \TYPO3\Election\Domain\Model\Elector
*/
protected $elector = null;
/**
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Election\Domain\Model\ElectionToken>
*/
protected $electionTokens = [];
/**
* @return string
*/
......@@ -56,19 +51,19 @@ class ElectionInvitation extends AbstractEntity
}
/**
* @return Election
* @return ElectionCircular
*/
public function getElection()
public function getElectionCircular()
{
return $this->election;
return $this->electionCircular;
}
/**
* @param Election $election
* @param ElectionCircular $electionCircular
*/
public function setElection($election)
public function setElectionCircular($electionCircular)
{
$this->election = $election;
$this->electionCircular = $electionCircular;
}
/**
......@@ -86,20 +81,4 @@ class ElectionInvitation extends AbstractEntity
{
$this->elector = $elector;
}
/**
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage
*/
public function getElectionTokens()
{
return $this->electionTokens;
}
/**
* @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $electionTokens
*/
public function setElectionTokens($electionTokens)
{
$this->electionTokens = $electionTokens;
}
}
......@@ -26,6 +26,11 @@ class Elector extends AbstractPerson
*/
protected $electorates = '';
/**
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\Election\Domain\Model\ElectionInvitation>
*/
protected $electionInvitations = [];
/**
* Elector constructor.
*/
......@@ -60,4 +65,34 @@ class Elector extends AbstractPerson
$this->electorates->attach($electorate);
}
}
/**
* @return ElectionInvitation[]
*/
public function getElectionInvitations()
{
return $this->electionInvitations;
}
/**
* @param ObjectStorage $electionInvitations
*/
public function setElectionInvitations($electionInvitations)
{
$this->electionInvitations = $electionInvitations;
}
/**
* @param Election $election
* @return bool
*/
public function hasElectionInvitationForElection(Election $election)
{
foreach ($this->getElectionInvitations() as $electionInvitation) {
if ($electionInvitation->getElectionCircular()->getElection() === $election) {
return true;
}
}
return false;
}
}
......@@ -46,7 +46,7 @@ class Electorate extends AbstractEntity
}
/**
* @return \TYPO3\Election\Domain\Model\Elector[]
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage
*/
public function getElectors()
{
......
<?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 ElectionInvitationRepository
*/
class ElectionInvitationRepository extends Repository
{
}
......@@ -14,49 +14,42 @@ namespace TYPO3\Election\Service;
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\Election\Domain\Model\Configuration;
use TYPO3\Election\Domain\Model\ElectionCircular;
use TYPO3\Election\Domain\Model\ElectionInvitation;
use TYPO3\Election\Domain\Model\Elector;
/**
* Class CircularService
* Class ElectionInvitationService
*/
class CircularService
class ElectionInvitationService
{
/**
* @var Configuration
*/
protected $configuration = null;
/**
* @var \TYPO3\Election\Utility\MailUtility
* @var \TYPO3\Election\Domain\Repository\ElectionInvitationRepository
* @inject
*/
protected $mailUtility = null;
protected $electionInvitationRepository = null;
/**
* CircularService constructor.
*
* @param Configuration $configuration
*/
public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;
}
/**
* @param ElectionCircular $electionCircular
*/
public function sendElectionCircular(ElectionCircular $electionCircular)
public function inviteElectors(Configuration $configuration, ElectionCircular $electionCircular)
{
$mailsToSend = $this->configuration->getNumberOfMails();
$limit = $configuration->getNumberOfMails();
$receiver = [];
$electors = [];
$allElectors = $electionCircular->getElection()->getElectorate()->getElectorsAsArray();
while (count($electors) < $mailsToSend && count($allElectors) > 0) {
$electors[] = array_pop($allElectors);
}
foreach ($electors as $elector) {
$this->mailUtility->sendElectionCircularToElector($electionCircular, $elector, $this->configuration);
$possibleReceivers = $electionCircular->getRemainingReceivers();
while (count($receiver) < $limit && count($possibleReceivers) > 0) {
/** @var Elector $possibleReceiver */
$possibleReceiver = array_shift($possibleReceivers);
$receiver[] = $possibleReceiver;
$electionInvitation = new ElectionInvitation();
$electionInvitation->setElector($possibleReceiver);
$electionInvitation->setElectionCircular($electionCircular);
$electionInvitation->setSecret(GeneralUtility::getRandomHexString(mt_rand(180, 220)));
$this->electionInvitationRepository->add($electionInvitation);
}
}
}
......@@ -369,6 +369,9 @@
<trans-unit id="view.be.election_circular.send_preview.remaining_receivers">
<source>Remaining receivers</source>
</trans-unit>
<trans-unit id="view.be.election_circular.send_preview.send_invitations">
<source>Send Invitations</source>
</trans-unit>
<!-- MODEL ELECTION CIRCULARS -->
......
<div>
<h2>
<f:translate key="view.be.election_circular.send_preview.remaining_receivers">Remaining receivers</f:translate>
<f:translate key="view.be.election_circular.send_preview.remaining_receivers">Remaining receivers</f:translate> ({electionCircular.remainingReceivers -> f:count()})
</h2>
<table class="table">
......
......@@ -3,6 +3,11 @@
<f:layout name="Backend"/>
<f:section name="LeftToolBar">
<f:link.action class="btn btn-default" action="send" controller="BeElectionCircular"
arguments="{electionCircular:electionCircular}">
<core:icon identifier="actions-move-right"/>
<f:translate key="view.be.election_circular.send_preview.send_invitations">Send Invitations</f:translate>
</f:link.action>
</f:section>
<f:section name="RightToolBar">
......
......@@ -46,17 +46,18 @@ CREATE TABLE tx_election_domain_model_election (
# Table structure for table 'tx_election_domain_model_elector'
#
CREATE TABLE tx_election_domain_model_elector (
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,
first_name VARCHAR(255) DEFAULT '' NOT NULL,
middle_name VARCHAR(255) DEFAULT '' NOT NULL,
last_name VARCHAR(255) DEFAULT '' NOT NULL,
gender TINYINT(4) DEFAULT '0' NOT NULL,
email VARCHAR(255) DEFAULT '' NOT NULL,
electorates INT(11) unsigned DEFAULT '0' NOT NULL,
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,
first_name VARCHAR(255) DEFAULT '' NOT NULL,
middle_name VARCHAR(255) DEFAULT '' NOT NULL,
last_name VARCHAR(255) DEFAULT '' NOT NULL,
gender TINYINT(4) DEFAULT '0' NOT NULL,
email VARCHAR(255) DEFAULT '' NOT NULL,
electorates INT(11) unsigned DEFAULT '0' NOT NULL,
election_invitations INT(11) unsigned DEFAULT '0' NOT NULL,
PRIMARY KEY (uid),
KEY parent (pid)
......@@ -143,14 +144,14 @@ CREATE TABLE tx_election_domain_model_electioncircular (
# Table structure for table 'tx_election_domain_model_electioninvitation'
#
CREATE TABLE tx_election_domain_model_electioninvitation (
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,
secret VARCHAR(255) DEFAULT '' NOT NULL,
election INT(11) unsigned DEFAULT '0' NOT NULL,
elector INT(11) unsigned DEFAULT '0' NOT NULL,
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,
secret VARCHAR(255) DEFAULT '' NOT NULL,
election_circular INT(11) unsigned DEFAULT '0' NOT NULL,
elector INT(11) unsigned DEFAULT '0' NOT NULL,
PRIMARY KEY (uid),
KEY parent (pid)
......
......@@ -44,6 +44,14 @@ config.tx_extbase.persistence.classes {
MM_opposite_field = electors
}
}
election_invitations {
mapOnProperty = electionInvitations
config {
type = inline
foreign_table = tx_election_domain_model_electioninvitation
foreign_field = elector
}
}
}
}
}
......@@ -158,11 +166,11 @@ config.tx_extbase.persistence.classes {
tableName = tx_election_domain_model_electioninvitation
columns {
secret.mapOnProperty = secret
election {
mapOnProperty = election
election_circular {
mapOnProperty = electionCircular
config {
type = select
foreign_table = tx_election_domain_model_election
foreign_table = tx_election_domain_model_electioncircular
}
}
elector {
......
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