From c93741f475a76b72b0d7c32c3383517b37f78564 Mon Sep 17 00:00:00 2001 From: Paul B Date: Wed, 17 Apr 2019 18:17:15 +0200 Subject: [PATCH] create_poll: don't send emails in case of DB transaction errors This commit adds an error message in the STEP 3 of the poll creation form in case there is a DB transactions error. It also makes sure to not send emails in case of those DB transaction errors. --- app/classes/Framadate/Form.php | 1 + .../Framadate/Services/PollService.php | 26 ++++++++++++------- create_classic_poll.php | 7 ++++- create_date_poll.php | 7 ++++- po/en.po | 5 +++- tpl/create_poll_step_3.tpl | 7 +++++ 6 files changed, 41 insertions(+), 12 deletions(-) diff --git a/app/classes/Framadate/Form.php b/app/classes/Framadate/Form.php index 2f62012..139a238 100644 --- a/app/classes/Framadate/Form.php +++ b/app/classes/Framadate/Form.php @@ -29,6 +29,7 @@ class Form public $end_date; public $choix_sondage; public $ValueMax; + public $errors = []; /** * Tells if users can modify their choices. diff --git a/app/classes/Framadate/Services/PollService.php b/app/classes/Framadate/Services/PollService.php index eb41f5c..4bbbdad 100644 --- a/app/classes/Framadate/Services/PollService.php +++ b/app/classes/Framadate/Services/PollService.php @@ -202,16 +202,24 @@ class PollService { 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); + if (!is_null($ids)) { + // 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(); + // Clean Form data in session + $this->sessionService->removeAll('form'); + // Creation message + $this->sessionService->set("Framadate", "messagePollCreated", TRUE); + // Delete old polls + $this->purgeService->repeatedCleanings(); + } else { + // Add an error in the form + $form->errors = [ + __('Error', 'GenericErrorPollCreation') + ]; + // TODO: change this to use sessionService function + $_SESSION['form'] = serialize($form); + } return $admin_poll_id; } diff --git a/create_classic_poll.php b/create_classic_poll.php index 00c8646..ca9f8e4 100644 --- a/create_classic_poll.php +++ b/create_classic_poll.php @@ -70,7 +70,7 @@ if (!isset($form->title) || !isset($form->admin_name) || ($config['use_smtp'] && $step = 1; } elseif (isset($_POST['confirmation'])) { $step = 4; -} elseif (empty($_POST['fin_sondage_autre']) ) { +} elseif (empty($form->errors) && empty($_POST['fin_sondage_autre']) ) { $step = 2; } else { $step = 3; @@ -151,6 +151,7 @@ switch ($step) { $smarty->assign('end_date_str', $end_date_str); $smarty->assign('default_poll_duration', $config['default_poll_duration']); $smarty->assign('use_smtp', $config['use_smtp']); + $smarty->assign('errors', $form->errors); $smarty->display('create_poll_step_3.tpl'); exit; @@ -166,6 +167,10 @@ switch ($step) { if (!is_null($admin_poll_id)) { // Redirect to poll administration header('Location:' . Utils::getUrlSondage($admin_poll_id, true)); + } else { + // Redirect to current page + $referer = $_SERVER['HTTP_REFERER']; + header("Location: $referer"); } exit; diff --git a/create_date_poll.php b/create_date_poll.php index 8ec2731..b387c1d 100644 --- a/create_date_poll.php +++ b/create_date_poll.php @@ -68,7 +68,7 @@ if (!isset($form->title) || !isset($form->admin_name) || ($config['use_smtp'] && $step = 1; } else if (!empty($_POST['confirmation'])) { $step = 4; -} else if (empty($_POST['choixheures']) || isset($form->totalchoixjour)) { +} else if (empty($form->errors) && (empty($_POST['choixheures']) || isset($form->totalchoixjour))) { $step = 2; } else { $step = 3; @@ -186,6 +186,7 @@ switch ($step) { $smarty->assign('end_date_str', $end_date_str); $smarty->assign('default_poll_duration', $config['default_poll_duration']); $smarty->assign('use_smtp', $config['use_smtp']); + $smarty->assign('errors', $form->errors); $smarty->display('create_poll_step_3.tpl'); exit; @@ -201,6 +202,10 @@ switch ($step) { if (!is_null($admin_poll_id)) { // Redirect to poll administration header('Location:' . Utils::getUrlSondage($admin_poll_id, true)); + } else { + // Redirect to current page + $referer = $_SERVER['HTTP_REFERER']; + header("Location: $referer"); } exit; diff --git a/po/en.po b/po/en.po index a2bbd33..7114163 100755 --- a/po/en.po +++ b/po/en.po @@ -548,6 +548,10 @@ msgstr "Failed to save poll" msgid "Forbidden!" msgstr "Forbidden!" +#: .Error.GenericErrorPollCreation +msgid "GenericErrorPollCreation" +msgstr "Oh no! Your poll could not be created at the moment. Sorry about that. Please try again later." + #: .Error.Identifier+is+already+used msgid "Identifier is already used" msgstr "Identifier is already used" @@ -1920,4 +1924,3 @@ msgid "" msgstr "" "Your vote has been saved, but please note: you need to keep this " "personalised link to be able to edit your vote." - diff --git a/tpl/create_poll_step_3.tpl b/tpl/create_poll_step_3.tpl index 3e650a6..02ce0bc 100644 --- a/tpl/create_poll_step_3.tpl +++ b/tpl/create_poll_step_3.tpl @@ -38,6 +38,13 @@

{__('Step 3', 'Then you will receive two emails: one containing the link of your poll for sending to the participants, the other containing the link to the poll administration page.')}

{/if} + {if !empty($errors)} +
+ {foreach $errors as $error} +

{$error}

+ {/foreach} +
+ {/if}