From 69e9f656ce527d9e5f14961a321bbb4e70b8875a Mon Sep 17 00:00:00 2001
From: Antonin <antonin.murtin@gmail.com>
Date: Sun, 1 May 2016 14:21:08 +0200
Subject: [PATCH] Some refactoring

---
 studs.php | 58 +++++++++++++++++++++++--------------------------------
 1 file changed, 24 insertions(+), 34 deletions(-)

diff --git a/studs.php b/studs.php
index 4b69f656..cbe1b560 100644
--- a/studs.php
+++ b/studs.php
@@ -140,23 +140,7 @@ if ($accessGranted) {
                 if ($result) {
                     if ($poll->editable == Editable::EDITABLE_BY_OWN) {
                         $editedVoteUniqueId = filter_input(INPUT_POST, 'edited_vote', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => POLL_REGEX]]);
-                        $sessionService->set(USER_REMEMBER_VOTES_KEY, $poll_id, $editedVoteUniqueId);
-                        $urlEditVote = Utils::getUrlSondage($poll_id, false, $editedVoteUniqueId);
-                        $message = new Message(
-                            'success',
-                            __('studs', 'Your vote has been registered successfully, but be careful: regarding this poll options, you need to keep this personal link to edit your own vote:'),
-                            $urlEditVote,
-                            __('Poll results', 'Edit the line:').' '.$name,
-                            'glyphicon-pencil');
-                        if ($config['use_smtp']) {
-                            $token = new Token();
-                            $sessionService->set("Common", SESSION_EDIT_LINK_TOKEN, $token);
-                            $smarty->assign('editedVoteUniqueId', $editedVoteUniqueId);
-                            $smarty->assign('token', $token->getValue());
-                            $smarty->assign('poll_id', $poll_id);
-                            $message->includeTemplate = $smarty->fetch('part/form_remember_edit_link.tpl');
-                            $smarty->clearAssign('token');
-                        }
+                        $message = getMessageForOwnVoteEditableVote($sessionService, $smarty, $editedVoteUniqueId, $config['use_smtp'], $poll_id, $name);
                     } else {
                         $message = new Message('success', __('studs', 'Update vote succeeded'));
                     }
@@ -187,23 +171,7 @@ if ($accessGranted) {
                 if ($result) {
                     if ($poll->editable == Editable::EDITABLE_BY_OWN) {
                         $editedVoteUniqueId = $result->uniqId;
-                        $sessionService->set(USER_REMEMBER_VOTES_KEY, $poll_id, $editedVoteUniqueId);
-                        $urlEditVote = Utils::getUrlSondage($poll_id, false, $editedVoteUniqueId);
-                        $message = new Message(
-                            'success',
-                            __('studs', 'Your vote has been registered successfully, but be careful: regarding this poll options, you need to keep this personal link to edit your own vote:'),
-                            $urlEditVote,
-                            __('Poll results', 'Edit the line:').' '.$name,
-                            'glyphicon-pencil');
-                        if ($config['use_smtp']) {
-                            $token = new Token();
-                            $sessionService->set("Common", SESSION_EDIT_LINK_TOKEN, $token);
-                            $smarty->assign('editedVoteUniqueId', $editedVoteUniqueId);
-                            $smarty->assign('token', $token->getValue());
-                            $smarty->assign('poll_id', $poll_id);
-                            $message->includeTemplate = $smarty->fetch('part/form_remember_edit_link.tpl');
-                            $smarty->clearAssign('token');
-                        }
+                        $message = getMessageForOwnVoteEditableVote($sessionService, $smarty, $editedVoteUniqueId, $config['use_smtp'], $poll_id, $name);
                     } else {
                         $message = new Message('success', __('studs', 'Adding the vote succeeded'));
                     }
@@ -220,6 +188,28 @@ if ($accessGranted) {
     }
 }
 
+// Functions
+function getMessageForOwnVoteEditableVote(SessionService &$sessionService, Smarty &$smarty, $editedVoteUniqueId, $canUseSMTP, $poll_id, $name) {
+    $sessionService->set(USER_REMEMBER_VOTES_KEY, $poll_id, $editedVoteUniqueId);
+    $urlEditVote = Utils::getUrlSondage($poll_id, false, $editedVoteUniqueId);
+    $message = new Message(
+        'success',
+        __('studs', 'Your vote has been registered successfully, but be careful: regarding this poll options, you need to keep this personal link to edit your own vote:'),
+        $urlEditVote,
+        __('Poll results', 'Edit the line:').' '.$name,
+        'glyphicon-pencil');
+    if ($canUseSMTP) {
+        $token = new Token();
+        $sessionService->set("Common", SESSION_EDIT_LINK_TOKEN, $token);
+        $smarty->assign('editedVoteUniqueId', $editedVoteUniqueId);
+        $smarty->assign('token', $token->getValue());
+        $smarty->assign('poll_id', $poll_id);
+        $message->includeTemplate = $smarty->fetch('part/form_remember_edit_link.tpl');
+        $smarty->clearAssign('token');
+    }
+    return $message;
+}
+
 // Retrieve data
 if ($resultPubliclyVisible || $accessGranted) {
     $slots = $pollService->allSlotsByPoll($poll);
-- 
GitLab