Commit 098b00b9 authored by Oliver Eglseder's avatar Oliver Eglseder
Browse files

[TASK] Add debug and election circular mail send

parent 078e4d9f
......@@ -17,6 +17,7 @@ use TYPO3\CMS\Backend\Utility\BackendUtility;
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\Utility\MailUtility;
/**
......@@ -33,6 +34,7 @@ class BeElectionCircularController extends AbstractProtectedBeController
const ACTION_DELETE = 'delete';
const ACTION_PREVIEW = 'preview';
const ACTION_TEST_SEND = 'testSend';
const ACTION_SEND = 'send';
/**
* @var \TYPO3\Election\Domain\Repository\ElectionCircularRepository
......@@ -133,6 +135,15 @@ class BeElectionCircularController extends AbstractProtectedBeController
);
}
/**
* @param ElectionCircular $electionCircular
*/
public function sendAction(ElectionCircular $electionCircular)
{
$circularService = $this->objectManager->get(CircularService::class, $this->configuration);
$circularService->sendElectionCircular($electionCircular);
}
/**
* @return bool
*/
......@@ -163,6 +174,7 @@ class BeElectionCircularController extends AbstractProtectedBeController
self::ACTION_DELETE,
self::ACTION_PREVIEW,
self::ACTION_TEST_SEND,
self::ACTION_SEND,
]
);
}
......
......@@ -13,6 +13,7 @@ namespace TYPO3\Election\Controller;
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\Election\Domain\Model\Election;
/**
......
......@@ -14,10 +14,6 @@ namespace TYPO3\Election\Domain\Model;
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
use TYPO3\Election\Domain\Repository\ConfigurationRepository;
class Configuration extends AbstractEntity
{
const AUTH_NONE = 0;
......@@ -56,12 +52,22 @@ class Configuration extends AbstractEntity
/**
* @var string
*/
protected $fromEmail = 'eab@typo3.org';
protected $fromEmail = '';
/**
* @var string
*/
protected $fromName = '';
/**
* @var bool
*/
protected $debug = false;
/**
* @var string
*/
protected $fromName = 'TYPO3 Association Board';
protected $testEmail = '';
/**
* @return int
......@@ -175,6 +181,38 @@ class Configuration extends AbstractEntity
$this->fromName = $fromName;
}
/**
* @return boolean
*/
public function isDebug()
{
return $this->debug;
}
/**
* @param boolean $debug
*/
public function setDebug($debug)
{
$this->debug = $debug;
}
/**
* @return string
*/
public function getTestEmail()
{
return $this->testEmail;
}
/**
* @param string $testEmail
*/
public function setTestEmail($testEmail)
{
$this->testEmail = $testEmail;
}
/**
* @return Configuration
*/
......@@ -186,6 +224,10 @@ class Configuration extends AbstractEntity
$clone->setPollManagerGroup($this->getPollManagerGroup());
$clone->setPluginPid($this->getPluginPid());
$clone->setNumberOfMails($this->getNumberOfMails());
$clone->setFromEmail($this->getFromEmail());
$clone->setFromName($this->getFromName());
$clone->setDebug($this->isDebug());
$clone->setTestEmail($this->getTestEmail());
return $clone;
}
}
......@@ -53,6 +53,14 @@ class Electorate extends AbstractEntity
return $this->electors;
}
/**
* @return \TYPO3\Election\Domain\Model\Elector[]
*/
public function getElectorsAsArray()
{
return $this->electors->toArray();
}
/**
* @param \TYPO3\Election\Domain\Model\Elector[] $electors
*/
......
<?php
namespace TYPO3\Election\Service;
/*
* 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\Election\Domain\Model\Configuration;
use TYPO3\Election\Domain\Model\ElectionCircular;
/**
* Class CircularService
*/
class CircularService
{
/**
* @var Configuration
*/
protected $configuration = null;
/**
* @var \TYPO3\Election\Utility\MailUtility
* @inject
*/
protected $mailUtility = null;
/**
* CircularService constructor.
*
* @param Configuration $configuration
*/
public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;
}
/**
* @param ElectionCircular $electionCircular
*/
public function sendElectionCircular(ElectionCircular $electionCircular)
{
$mailsToSend = $this->configuration->getNumberOfMails();
$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);
}
}
}
......@@ -29,17 +29,24 @@ class MailUtility
* @param ElectionCircular $electionCircular
* @param Elector $elector
* @param Configuration $configuration
* @return bool
*/
public function sendElectionCircularToElector(
ElectionCircular $electionCircular,
Elector $elector,
Configuration $configuration
) {
GeneralUtility::makeInstance(MailMessage::class)
->setSubject($electionCircular->getSubject())
->setFrom(array($configuration->getFromEmail() => $configuration->getFromName()))
->setTo(array($elector->getEmail() => $elector->getFullName()))
->setBody($electionCircular->getRenderedBody($elector))
->send();
$mail = GeneralUtility::makeInstance(MailMessage::class);
$mail->setSubject($electionCircular->getSubject());
$mail->setFrom([$configuration->getFromEmail() => $configuration->getFromName()]);
if ($configuration->isDebug()) {
$mail->setTo([$configuration->getTestEmail() => $elector->getFullName()]);
} else {
$mail->setTo([$elector->getEmail() => $elector->getFullName()]);
}
$mail->setBody($electionCircular->getRenderedBody($elector));
return $mail->send() === 1;
}
}
......@@ -121,6 +121,18 @@
<trans-unit id="model.configuration.field.number_of_mails">
<source>Number of mails to send at once</source>
</trans-unit>
<trans-unit id="model.configuration.field.from_email">
<source>Email address of the sender</source>
</trans-unit>
<trans-unit id="model.configuration.field.from_name">
<source>Name of the sender</source>
</trans-unit>
<trans-unit id="model.configuration.field.debug">
<source>Debug mode (will send all invitations to the configured test mail, but creates only dummy tokens)</source>
</trans-unit>
<trans-unit id="model.configuration.field.test_email">
<source>Debug mode (will send all invitations to the configured test mail, but creates only dummy tokens)</source>
</trans-unit>
<!-- MODEL ABSTRACT PERSON -->
......
<fieldset class="form-section">
<div class="form-group">
<label for="tx_election_configuration_debug">
<f:translate key="model.configuration.field.debug">Debug mode (will send all invitations to the configured
test mail, but creates only dummy tokens)
</f:translate>
</label>
<div class="form-control-wrap" style="max-width: 480px">
<f:form.checkbox id="tx_election_configuration_debug" property="debug" value="true"/>
</div>
</div>
</fieldset>
<fieldset class="form-section">
<div class="form-group">
<label>
<f:translate key="model.configuration.field.from_email">Email of the sender</f:translate>
</label>
<f:form.textfield class="form-control" property="fromEmail"/>
</div>
</fieldset>
<fieldset class="form-section">
<div class="form-group">
<label>
<f:translate key="model.configuration.field.from_name">Name of the sender</f:translate>
</label>
<f:form.textfield class="form-control" property="fromName"/>
</div>
</fieldset>
<fieldset class="form-section">
<div class="form-group">
<label>
<f:translate key="model.configuration.field.test_email">Receiver email address for debug mode</f:translate>
</label>
<f:form.textfield class="form-control" property="testEmail"/>
</div>
</fieldset>
......@@ -6,3 +6,7 @@
arguments="{configuration:configuration,beUserGroups:beUserGroups}"/>
<f:render partial="Model/Configuration/FormFields/PluginPid" arguments="{configuration:configuration}"/>
<f:render partial="Model/Configuration/FormFields/NumberOfMails" arguments="{configuration:configuration}"/>
<f:render partial="Model/Configuration/FormFields/FromEmail" arguments="{configuration:configuration}"/>
<f:render partial="Model/Configuration/FormFields/FromName" arguments="{configuration:configuration}"/>
<f:render partial="Model/Configuration/FormFields/Debug" arguments="{configuration:configuration}"/>
<f:render partial="Model/Configuration/FormFields/TestEmail" arguments="{configuration:configuration}"/>
......@@ -12,6 +12,10 @@ CREATE TABLE tx_election_domain_model_configuration (
poll_manager_group INT(11) unsigned DEFAULT '0' NOT NULL,
plugin_pid INT(11) unsigned DEFAULT '0' NOT NULL,
number_of_mails INT(11) unsigned DEFAULT '10' NOT NULL,
from_email VARCHAR(255) DEFAULT '' NOT NULL,
from_name VARCHAR(255) DEFAULT '' NOT NULL,
debug TINYINT(4) DEFAULT '0' NOT NULL,
test_email VARCHAR(255) DEFAULT '' NOT NULL,
PRIMARY KEY (uid),
KEY parent (pid)
......
......@@ -22,6 +22,10 @@ config.tx_extbase.persistence.classes {
poll_manager_group.mapOnProperty = pollManagerGroup
plugin_pid.mapOnProperty = pluginPid
number_of_mails.mapOnProperty = numberOfMails
from_email.mapOnProperty = fromEmail
from_name.mapOnProperty = fromName
debug.mapOnProperty = debug
test_email.mapOnProperty = testEmail
}
}
}
......
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