Merge branch 'refacto-creation-poll-service' into 'develop'

Refactorisation of the creation poll service

See merge request framasoft/framadate/framadate!383
This commit is contained in:
Thomas Citharel 2019-04-19 16:40:47 +02:00
commit 13d1103698
14 changed files with 126 additions and 104 deletions

View File

@ -40,10 +40,10 @@ $is_admin = false;
/*----------*/
$logService = new LogService();
$pollService = new PollService($connect, $logService);
$inputService = new InputService();
$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']);
$notificationService = new NotificationService($mailService, $smarty);
$pollService = new PollService($connect, $logService, $notificationService);
$securityService = new SecurityService();
/* PAGE */

View File

@ -31,7 +31,7 @@ $logService = new LogService();
$sessionService = new SessionService();
$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']);
$notificationService = new NotificationService($mailService, $smarty);
$pollService = new PollService($connect, $logService);
$pollService = new PollService($connect, $logService, $notificationService);
$result = false;
$message = null;

View File

@ -19,6 +19,7 @@
use Framadate\Services\AdminPollService;
use Framadate\Services\LogService;
use Framadate\Services\MailService;
use Framadate\Services\PollService;
use Framadate\Services\SecurityService;
use Framadate\Services\SuperAdminService;
@ -50,7 +51,9 @@ $poll_to_delete = null;
/*----------*/
$logService = new LogService();
$pollService = new PollService($connect, $logService);
$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']);
$notificationService = new NotificationService($mailService, $smarty);
$pollService = new PollService($connect, $logService, $notificationService);
$adminPollService = new AdminPollService($connect, $pollService, $logService);
$superAdminService = new SuperAdminService();
$securityService = new SecurityService();

View File

@ -47,11 +47,11 @@ $editingVoteId = 0;
/*----------*/
$logService = new LogService();
$pollService = new PollService($connect, $logService);
$adminPollService = new AdminPollService($connect, $pollService, $logService);
$inputService = new InputService();
$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']);
$notificationService = new NotificationService($mailService, $smarty);
$pollService = new PollService($connect, $logService, $notificationService);
$adminPollService = new AdminPollService($connect, $pollService, $logService);
$inputService = new InputService();
$sessionService = new SessionService();
/* PAGE */

View File

@ -47,8 +47,12 @@ class MailService {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
public function isEnabled() {
return $this->smtp_allowed === true;
}
public function send($to, $subject, $body, $msgKey = null) {
if ($this->smtp_allowed === true && $this->canSendMsg($msgKey)) {
if ($this->isEnabled() && $this->canSendMsg($msgKey)) {
$mail = new PHPMailer(true);
$this->configureMailer($mail);

View File

@ -87,6 +87,10 @@ class NotificationService {
}
function sendPollCreationMails($creator_mail, $creator_name, $poll_name, $poll_id, $admin_poll_id) {
if (!$this->mailService->isEnabled() || !$this->mailService->isValidEmail($creator_mail)) {
return null;
}
$this->smarty->assign('poll_creator_name', Utils::htmlMailEscape($creator_name));
$this->smarty->assign('poll_name', Utils::htmlMailEscape($poll_name));
$this->smarty->assign('poll_url', Utils::getUrlSondage($poll_id));

View File

@ -17,7 +17,6 @@
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
namespace Framadate\Services;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\DBALException;
@ -27,6 +26,11 @@ use Framadate\Exception\ConcurrentVoteException;
use Framadate\Form;
use Framadate\Repositories\RepositoryFactory;
use Framadate\Security\Token;
use Framadate\Services\LogService;
use Framadate\Services\NotificationService;
use Framadate\Services\PurgeService;
use Framadate\Services\SessionService;
use Framadate\Utils;
class PollService {
private $connect;
@ -37,9 +41,12 @@ class PollService {
private $voteRepository;
private $commentRepository;
function __construct(Connection $connect, LogService $logService) {
function __construct(Connection $connect, LogService $logService, NotificationService $notificationService) {
$this->connect = $connect;
$this->logService = $logService;
$this->notificationService = $notificationService;
$this->sessionService = new SessionService();
$this->purgeService = new PurgeService($connect, $logService);
$this->pollRepository = RepositoryFactory::pollRepository();
$this->slotRepository = RepositoryFactory::slotRepository();
$this->voteRepository = RepositoryFactory::voteRepository();
@ -154,6 +161,61 @@ class PollService {
return $this->commentRepository->insert($poll_id, $name, $comment);
}
/**
* Main entry point for poll creation. This action does all the following:
* - Validate input data
* - Create the poll in database
* - Send emails
* - Cleanups post creation (session and purges)
*
* @param Form $form
* @param string $end_date
* @return string|null returns the admin_id if the poll was created
*/
function doPollCreation(Form $form, $end_date) {
// Min/Max archive date
$min_expiry_time = $this->minExpiryDate();
$max_expiry_time = $this->maxExpiryDate();
if (!empty($end_date)) {
$registredate = explode('/', $end_date);
if (is_array($registredate) && count($registredate) === 3) {
$time = mktime(0, 0, 0, $registredate[1], $registredate[0], $registredate[2]);
if ($time < $min_expiry_time) {
$form->end_date = $min_expiry_time;
} elseif ($max_expiry_time < $time) {
$form->end_date = $max_expiry_time;
} else {
$form->end_date = $time;
}
}
}
if (empty($form->end_date)) {
// By default, expiration date is 6 months after last day
$form->end_date = $max_expiry_time;
}
// Insert poll in database
list($poll_id, $admin_poll_id) = $this->createPoll($form);
// Send confirmation by mail if enabled
// Everything went well
$this->notificationService->sendPollCreationMails($form->admin_mail, $form->admin_name, $form->title, $poll_id, $admin_poll_id);
// Clean Form data in session
$this->sessionService->removeAll('form');
// Creation message
$this->sessionService->set("Framadate", "messagePollCreated", TRUE);
// Delete old polls
$this->purgeService->repeatedCleanings();
return $admin_poll_id;
}
/**
* @param Form $form
* @return array

View File

@ -55,9 +55,20 @@ class SessionService {
unset($_SESSION[$section][$key]);
}
/**
* Remove a session section
*
* @param $section
*/
public function removeAll($section) {
assert(!empty($section));
unset($_SESSION[$section]);
}
private function initSectionIfNeeded($section) {
if (!isset($_SESSION[$section])) {
$_SESSION[$section] = [];
}
}
}
}

View File

@ -32,9 +32,9 @@ include_once __DIR__ . '/app/inc/init.php';
/* Service */
/*---------*/
$logService = new LogService();
$pollService = new PollService($connect, $logService);
$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']);
$notificationService = new NotificationService($mailService, $smarty);
$pollService = new PollService($connect, $logService, $notificationService);
$purgeService = new PurgeService($connect, $logService);
$sessionService = new SessionService();
@ -44,8 +44,6 @@ if (is_file('bandeaux_local.php')) {
include_once('bandeaux.php');
}
// Min/Max archive date
$min_expiry_time = $pollService->minExpiryDate();
$max_expiry_time = $pollService->maxExpiryDate();
$form = isset($_SESSION['form']) ? unserialize($_SESSION['form']) : null;
@ -156,54 +154,24 @@ switch ($step) {
$smarty->display('create_poll_step_3.tpl');
exit;
case 4: // Step 4 : Data prepare before insert in DB
$enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]);
if (!empty($enddate)) {
$registredate = explode('/', $enddate);
case 4:
// Step 4 : Do the poll creation
if (is_array($registredate) && count($registredate) === 3) {
$time = mktime(0, 0, 0, $registredate[1], $registredate[0], $registredate[2]);
// Read expiration date passed in POST parameters
$end_date = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]);
if ($time < $min_expiry_time) {
$form->end_date = $min_expiry_time;
} elseif ($max_expiry_time < $time) {
$form->end_date = $max_expiry_time;
} else {
$form->end_date = $time;
}
}
$admin_poll_id = $pollService->doPollCreation($form, $end_date);
if (!is_null($admin_poll_id)) {
// Redirect to poll administration
header('Location:' . Utils::getUrlSondage($admin_poll_id, true));
}
if (empty($form->end_date)) {
// By default, expiration date is 6 months after last day
$form->end_date = $max_expiry_time;
}
// Insert poll in database
$ids = $pollService->createPoll($form);
$poll_id = $ids[0];
$admin_poll_id = $ids[1];
// Send confirmation by mail if enabled
if ($config['use_smtp'] === true && $mailService->isValidEmail($form->admin_mail)) {
$notificationService->sendPollCreationMails($form->admin_mail, $form->admin_name, $form->title, $poll_id, $admin_poll_id);
}
// Clean Form data in $_SESSION
unset($_SESSION['form']);
// Delete old polls
$purgeService->purgeOldPolls();
// creation message
$sessionService->set("Framadate", "messagePollCreated", TRUE);
// Redirect to poll administration
header('Location:' . Utils::getUrlSondage($admin_poll_id, true));
exit;
case 1: // Step 1/4 : error if $_SESSION from info_sondage are not valid
case 1:
default:
// Step 1/4 : error if $_SESSION from info_sondage are not valid
$smarty->assign('title', __('Error', 'Error!'));
$smarty->assign('error', __('Error', 'You haven\'t filled the first section of the poll creation, or your session has expired.'));
$smarty->display('error.tpl');

View File

@ -32,19 +32,17 @@ include_once __DIR__ . '/app/inc/init.php';
/* Service */
/*---------*/
$logService = new LogService();
$pollService = new PollService($connect, $logService);
$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']);
$notificationService = new NotificationService($mailService, $smarty);
$purgeService = new PurgeService($connect, $logService);
$inputService = new InputService();
$sessionService = new SessionService();
$pollService = new PollService($connect, $logService, $notificationService);
if (is_readable('bandeaux_local.php')) {
include_once('bandeaux_local.php');
}
// Min/Max archive date
$min_expiry_time = $pollService->minExpiryDate();
$max_expiry_time = $pollService->maxExpiryDate();
$form = isset($_SESSION['form']) ? unserialize($_SESSION['form']) : null;
@ -193,52 +191,17 @@ switch ($step) {
exit;
case 4:
// Step 4 : Data prepare before insert in DB
// Step 4 : Do the poll creation
// Define expiration date
$enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]);
// Read expiration date passed in POST parameters
$end_date = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]);
if (!empty($enddate)) {
$registredate = explode('/', $enddate);
$admin_poll_id = $pollService->doPollCreation($form, $end_date);
if (is_array($registredate) && count($registredate) === 3) {
$time = mktime(0, 0, 0, $registredate[1], $registredate[0], $registredate[2]);
if ($time < $min_expiry_time) {
$form->end_date = $min_expiry_time;
} elseif ($max_expiry_time < $time) {
$form->end_date = $max_expiry_time;
} else {
$form->end_date = $time;
}
}
if (!is_null($admin_poll_id)) {
// Redirect to poll administration
header('Location:' . Utils::getUrlSondage($admin_poll_id, true));
}
if (empty($form->end_date)) {
// By default, expiration date is 6 months after last day
$form->end_date = $max_expiry_time;
}
// Insert poll in database
$ids = $pollService->createPoll($form);
$poll_id = $ids[0];
$admin_poll_id = $ids[1];
// Send confirmation by mail if enabled
if ($config['use_smtp'] === true && $mailService->isValidEmail($form->admin_mail)) {
$notificationService->sendPollCreationMails($form->admin_mail, $form->admin_name, $form->title, $poll_id, $admin_poll_id);
}
// Clean Form data in $_SESSION
unset($_SESSION['form']);
$purgeService->repeatedCleanings();
// creation message
$sessionService->set("Framadate", "messagePollCreated", TRUE);
// Redirect to poll administration
header('Location:' . Utils::getUrlSondage($admin_poll_id, true));
exit;
case 1:

View File

@ -35,7 +35,9 @@ $poll = null;
/*----------*/
$logService = new LogService();
$pollService = new PollService($connect, $logService);
$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']);
$notificationService = new NotificationService($mailService, $smarty);
$pollService = new PollService($connect, $logService, $notificationService);
$securityService = new SecurityService();
/* PAGE */

View File

@ -28,9 +28,9 @@ include_once __DIR__ . '/app/inc/init.php';
/* SERVICES */
/* -------- */
$logService = new LogService();
$pollService = new PollService($connect, $logService);
$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']);
$notificationService = new NotificationService($mailService, $smarty);
$pollService = new PollService($connect, $logService, $notificationService);
/* PAGE */
/* ---- */

View File

@ -17,6 +17,9 @@
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
use Framadate\Services\LogService;
use Framadate\Services\MailService;
use Framadate\Services\NotificationService;
use Framadate\Services\PollService;
use Framadate\Utils;
@ -29,8 +32,10 @@ if (!is_file(CONF_FILENAME)) {
/* SERVICES */
/* -------- */
$logService = '\Framadate\Services\LogService';
$pollService = new PollService($connect, new $logService());
$logService = new LogService();
$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']);
$notificationService = new NotificationService($mailService, $smarty);
$pollService = new PollService($connect, $logService, $notificationService);
/* PAGE */
/* ---- */

View File

@ -56,10 +56,10 @@ $selectedNewVotes = [];
/*----------*/
$logService = new LogService();
$pollService = new PollService($connect, $logService);
$inputService = new InputService();
$mailService = new MailService($config['use_smtp'], $config['smtp_options'], $config['use_sendmail']);
$notificationService = new NotificationService($mailService, $smarty);
$pollService = new PollService($connect, $logService, $notifierService);
$securityService = new SecurityService();
$sessionService = new SessionService();