Skip to content
Snippets Groups Projects
Commit cb63031f authored by Olivier Perez's avatar Olivier Perez
Browse files

Refactor create_date_poll.php

parent 48cd77a5
No related branches found
No related tags found
No related merge requests found
...@@ -16,13 +16,13 @@ ...@@ -16,13 +16,13 @@
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft https://git.framasoft.org/framasoft/framadate/) * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft https://git.framasoft.org/framasoft/framadate/)
*/ */
use Framadate\Choice;
use Framadate\Services\InputService; use Framadate\Services\InputService;
use Framadate\Services\LogService; use Framadate\Services\LogService;
use Framadate\Services\PollService;
use Framadate\Services\MailService; use Framadate\Services\MailService;
use Framadate\Services\PollService;
use Framadate\Services\PurgeService; use Framadate\Services\PurgeService;
use Framadate\Utils; use Framadate\Utils;
use Framadate\Choice;
include_once __DIR__ . '/app/inc/init.php'; include_once __DIR__ . '/app/inc/init.php';
...@@ -36,96 +36,75 @@ $inputService = new InputService(); ...@@ -36,96 +36,75 @@ $inputService = new InputService();
if (is_readable('bandeaux_local.php')) { if (is_readable('bandeaux_local.php')) {
include_once('bandeaux_local.php'); include_once('bandeaux_local.php');
} else {
include_once('bandeaux.php');
} }
// Step 1/4 : error if $_SESSION from info_sondage are not valid // Min/Max archive date
if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) || ($config['use_smtp'] && !isset($_SESSION['form']->admin_mail))) { $min_expiry_time = $pollService->minExpiryDate();
$max_expiry_time = $pollService->maxExpiryDate();
$smarty->assign('title', __('Error', 'Error!')); // The poll format is DATE
$smarty->assign('error', __('Error', 'You haven\'t filled the first section of the poll creation.')); if ($_SESSION['form']->format !== 'D') {
$smarty->display('error.tpl'); $_SESSION['form']->format = 'D';
$_SESSION['form']->clearChoices();
}
if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) || ($config['use_smtp'] && !isset($_SESSION['form']->admin_mail))) {
$step = 1;
} else if (!empty($_POST['confirmation'])) {
$step = 4;
} else if (empty($_POST['choixheures']) || isset($_SESSION['form']->totalchoixjour)) {
$step = 2;
} else { } else {
// Min/Max archive date $step = 3;
$min_expiry_time = $pollService->minExpiryDate(); }
$max_expiry_time = $pollService->maxExpiryDate();
// The poll format is DATE
if ($_SESSION['form']->format !== 'D') {
$_SESSION['form']->format = 'D';
$_SESSION['form']->clearChoices();
}
// Step 4 : Data prepare before insert in DB
if (!empty($_POST['confirmation'])) {
// Define expiration date switch ($step) {
$enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]); case 1:
// 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.'));
$smarty->display('error.tpl');
exit;
if (!empty($enddate)) { case 2:
$registredate = explode('/', $enddate); // Step 2/4 : Select dates of the poll
if (is_array($registredate) && count($registredate) == 3) {
$time = mktime(0, 0, 0, $registredate[1], $registredate[0], $registredate[2]);
if ($time < $min_expiry_time) { // Prefill form->choices
$_SESSION['form']->end_date = $min_expiry_time; foreach ($_SESSION['form']->getChoices() as $c) {
} elseif ($max_expiry_time < $time) { $count = 3 - count($c->getSlots());
$_SESSION['form']->end_date = $max_expiry_time; for ($i = 0; $i < $count; $i++) {
} else { $c->addSlot('');
$_SESSION['form']->end_date = $time;
}
} }
} }
if (empty($_SESSION['form']->end_date)) { $count = 3 - count($_SESSION['form']->getChoices());
// By default, expiration date is 6 months after last day for ($i = 0; $i < $count; $i++) {
$_SESSION['form']->end_date = $max_expiry_time; $c = new Choice('');
} $c->addSlot('');
$c->addSlot('');
// Insert poll in database $c->addSlot('');
$ids = $pollService->createPoll($_SESSION['form']); $_SESSION['form']->addChoice($c);
$poll_id = $ids[0];
$admin_poll_id = $ids[1];
// Send confirmation by mail if enabled
if ($config['use_smtp'] === true) {
$message = __('Mail', "This is the message you have to send to the people you want to poll. \nNow, you have to send this message to everyone you want to poll.");
$message .= '<br/><br/>';
$message .= Utils::htmlEscape($_SESSION['form']->admin_name) . ' ' . __('Mail', 'hast just created a poll called') . ' : "' . Utils::htmlEscape($_SESSION['form']->title) . '".<br/>';
$message .= __('Mail', 'Thanks for filling the poll at the link above') . ' :<br/><br/><a href="%1$s">%1$s</a>';
$message_admin = __('Mail', "This message should NOT be sent to the polled people. It is private for the poll's creator.\n\nYou can now modify it at the link above");
$message_admin .= ' :<br/><br/><a href="%1$s">%1$s</a>';
$message = sprintf($message, Utils::getUrlSondage($poll_id));
$message_admin = sprintf($message_admin, Utils::getUrlSondage($admin_poll_id, true));
if ($mailService->isValidEmail($_SESSION['form']->admin_mail)) {
$mailService->send($_SESSION['form']->admin_mail, '[' . NOMAPPLICATION . '][' . __('Mail', 'Author\'s message') . '] ' . __('Generic', 'Poll') . ': ' . Utils::htmlEscape($_SESSION['form']->title), $message_admin);
$mailService->send($_SESSION['form']->admin_mail, '[' . NOMAPPLICATION . '][' . __('Mail', 'For sending to the polled users') . '] ' . __('Generic', 'Poll') . ': ' . Utils::htmlEscape($_SESSION['form']->title), $message);
}
} }
// Clean Form data in $_SESSION // Display step 2
unset($_SESSION['form']); $smarty->assign('title', __('Step 2 date', 'Poll dates (2 on 3)'));
$smarty->assign('choices', $_SESSION['form']->getChoices());
// Delete old polls $smarty->assign('error', null);
$purgeService->purgeOldPolls();
// Redirect to poll administration $smarty->display('create_date_poll_step_2.tpl');
header('Location:' . Utils::getUrlSondage($admin_poll_id, true));
exit; exit;
} else {
case 3:
// Step 3/4 : Confirm poll creation
// Handle Step2 submission
if (!empty($_POST['days'])) { if (!empty($_POST['days'])) {
// Remove empty dates // Remove empty dates
$_POST['days'] = array_filter($_POST['days'], function($d) {return !empty($d);}); $_POST['days'] = array_filter($_POST['days'], function ($d) {
return !empty($d);
});
// Check if there are at most MAX_SLOTS_PER_POLL slots // Check if there are at most MAX_SLOTS_PER_POLL slots
if (count($_POST['days']) > MAX_SLOTS_PER_POLL) { if (count($_POST['days']) > MAX_SLOTS_PER_POLL) {
...@@ -151,8 +130,8 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) || ...@@ -151,8 +130,8 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) ||
$choice = new Choice($time); $choice = new Choice($time);
$_SESSION['form']->addChoice($choice); $_SESSION['form']->addChoice($choice);
$schedules = $inputService->filterArray($_POST['horaires'.$i], FILTER_DEFAULT); $schedules = $inputService->filterArray($_POST['horaires' . $i], FILTER_DEFAULT);
for($j = 0; $j < count($schedules); $j++) { for ($j = 0; $j < count($schedules); $j++) {
if (!empty($schedules[$j])) { if (!empty($schedules[$j])) {
$choice->addSlot(strip_tags($schedules[$j])); $choice->addSlot(strip_tags($schedules[$j]));
} }
...@@ -160,21 +139,17 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) || ...@@ -160,21 +139,17 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) ||
} }
} }
} }
}
// Step 3/4 : Confirm poll creation
if (!empty($_POST['choixheures']) && !isset($_SESSION['form']->totalchoixjour)) {
// Summary // Display step 3
$summary = '<ul>'; $summary = '<ul>';
$choices = $_SESSION['form']->getChoices(); $choices = $_SESSION['form']->getChoices();
foreach ($choices as $choice) { foreach ($choices as $choice) {
$summary .= '<li>'.strftime($date_format['txt_full'], $choice->getName()); $summary .= '<li>' . strftime($date_format['txt_full'], $choice->getName());
$first = true; $first = true;
foreach ($choice->getSlots() as $slots) { foreach ($choice->getSlots() as $slots) {
$summary .= $first ? ': ' : ', '; $summary .= $first ? ': ' : ', ';
$summary .= $slots; $summary .= $slots;
$first = false; $first = false;
} }
$summary .= '</li>'; $summary .= '</li>';
} }
...@@ -189,34 +164,69 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) || ...@@ -189,34 +164,69 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) ||
$smarty->assign('use_smtp', $config['use_smtp']); $smarty->assign('use_smtp', $config['use_smtp']);
$smarty->display('create_classic_poll_step3.tpl'); $smarty->display('create_classic_poll_step3.tpl');
exit;
// Step 2/4 : Select dates of the poll case 4:
} else { // Step 4 : Data prepare before insert in DB
// Prefill form->choices // Define expiration date
foreach ($_SESSION['form']->getChoices() as $c) { $enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]);
$count = 3 - count($c->getSlots());
for($i=0; $i< $count; $i++) {
$c->addSlot(''); if (!empty($enddate)) {
$registredate = explode('/', $enddate);
if (is_array($registredate) && count($registredate) == 3) {
$time = mktime(0, 0, 0, $registredate[1], $registredate[0], $registredate[2]);
if ($time < $min_expiry_time) {
$_SESSION['form']->end_date = $min_expiry_time;
} elseif ($max_expiry_time < $time) {
$_SESSION['form']->end_date = $max_expiry_time;
} else {
$_SESSION['form']->end_date = $time;
}
} }
} }
$count = 3 - count($_SESSION['form']->getChoices()); if (empty($_SESSION['form']->end_date)) {
for($i=0; $i< $count; $i++) { // By default, expiration date is 6 months after last day
$c = new Choice(''); $_SESSION['form']->end_date = $max_expiry_time;
$c->addSlot('');
$c->addSlot('');
$c->addSlot('');
$_SESSION['form']->addChoice($c);
} }
// Display step 2 // Insert poll in database
$smarty->assign('title', __('Step 2 date', 'Poll dates (2 on 3)')); $ids = $pollService->createPoll($_SESSION['form']);
$smarty->assign('choices', $_SESSION['form']->getChoices()); $poll_id = $ids[0];
$smarty->assign('error', null); $admin_poll_id = $ids[1];
$smarty->display('create_date_poll_step_2.tpl');
} // Send confirmation by mail if enabled
if ($config['use_smtp'] === true) {
$message = __('Mail', "This is the message you have to send to the people you want to poll. \nNow, you have to send this message to everyone you want to poll.");
$message .= '<br/><br/>';
$message .= Utils::htmlEscape($_SESSION['form']->admin_name) . ' ' . __('Mail', 'hast just created a poll called') . ' : "' . Utils::htmlEscape($_SESSION['form']->title) . '".<br/>';
$message .= __('Mail', 'Thanks for filling the poll at the link above') . ' :<br/><br/><a href="%1$s">%1$s</a>';
$message_admin = __('Mail', "This message should NOT be sent to the polled people. It is private for the poll's creator.\n\nYou can now modify it at the link above");
$message_admin .= ' :<br/><br/><a href="%1$s">%1$s</a>';
$message = sprintf($message, Utils::getUrlSondage($poll_id));
$message_admin = sprintf($message_admin, Utils::getUrlSondage($admin_poll_id, true));
if ($mailService->isValidEmail($_SESSION['form']->admin_mail)) {
$mailService->send($_SESSION['form']->admin_mail, '[' . NOMAPPLICATION . '][' . __('Mail', 'Author\'s message') . '] ' . __('Generic', 'Poll') . ': ' . Utils::htmlEscape($_SESSION['form']->title), $message_admin);
$mailService->send($_SESSION['form']->admin_mail, '[' . NOMAPPLICATION . '][' . __('Mail', 'For sending to the polled users') . '] ' . __('Generic', 'Poll') . ': ' . Utils::htmlEscape($_SESSION['form']->title), $message);
}
}
// Clean Form data in $_SESSION
unset($_SESSION['form']);
// Delete old polls
$purgeService->purgeOldPolls();
// Redirect to poll administration
header('Location:' . Utils::getUrlSondage($admin_poll_id, true));
exit;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment