date.chapril.org-framadate/create_date_poll.php

233 lines
9.0 KiB
PHP
Raw Normal View History

2011-05-15 01:32:47 +02:00
<?php
/**
* This software is governed by the CeCILL-B license. If a copy of this license
* is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
*
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
* Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft https://git.framasoft.org/framasoft/framadate/)
*
* =============================
*
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
*
* 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/)
*/
2016-05-04 00:56:53 +02:00
use Framadate\Choice;
use Framadate\Services\InputService;
2014-12-25 00:55:52 +01:00
use Framadate\Services\LogService;
use Framadate\Services\MailService;
2016-05-04 00:56:53 +02:00
use Framadate\Services\PollService;
2014-12-29 21:54:07 +01:00
use Framadate\Services\PurgeService;
2014-12-25 00:55:52 +01:00
use Framadate\Utils;
include_once __DIR__ . '/app/inc/init.php';
2011-05-15 01:32:47 +02:00
2014-12-25 00:55:52 +01:00
/* Service */
/*---------*/
2015-01-06 23:52:52 +01:00
$logService = new LogService();
2014-12-25 00:55:52 +01:00
$pollService = new PollService($connect, $logService);
$mailService = new MailService($config['use_smtp']);
2014-12-29 21:54:07 +01:00
$purgeService = new PurgeService($connect, $logService);
$inputService = new InputService();
2011-05-15 01:32:47 +02:00
2013-03-13 18:29:48 +01:00
if (is_readable('bandeaux_local.php')) {
include_once('bandeaux_local.php');
2011-05-15 01:32:47 +02:00
}
2016-05-04 00:56:53 +02:00
// Min/Max archive date
$min_expiry_time = $pollService->minExpiryDate();
$max_expiry_time = $pollService->maxExpiryDate();
2016-05-04 00:56:53 +02:00
// The poll format is DATE
if ($_SESSION['form']->format !== 'D') {
$_SESSION['form']->format = 'D';
$_SESSION['form']->clearChoices();
}
2016-05-04 00:56:53 +02:00
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 {
2016-05-04 00:56:53 +02:00
$step = 3;
}
2014-10-21 01:31:26 +02:00
2016-05-04 00:56:53 +02:00
switch ($step) {
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;
2016-05-04 00:56:53 +02:00
case 2:
// Step 2/4 : Select dates of the poll
2016-05-04 00:56:53 +02:00
// Prefill form->choices
foreach ($_SESSION['form']->getChoices() as $c) {
$count = 3 - count($c->getSlots());
for ($i = 0; $i < $count; $i++) {
$c->addSlot('');
2014-10-21 01:31:26 +02:00
}
}
2016-05-04 00:56:53 +02:00
$count = 3 - count($_SESSION['form']->getChoices());
for ($i = 0; $i < $count; $i++) {
$c = new Choice('');
$c->addSlot('');
$c->addSlot('');
$c->addSlot('');
$_SESSION['form']->addChoice($c);
2014-12-25 00:55:52 +01:00
}
2016-05-04 00:56:53 +02:00
// Display step 2
$smarty->assign('title', __('Step 2 date', 'Poll dates (2 on 3)'));
$smarty->assign('choices', $_SESSION['form']->getChoices());
$smarty->assign('error', null);
2016-05-04 00:56:53 +02:00
$smarty->display('create_date_poll_step_2.tpl');
exit;
2013-03-13 18:29:48 +01:00
2016-05-04 00:56:53 +02:00
case 3:
// Step 3/4 : Confirm poll creation
// Handle Step2 submission
if (!empty($_POST['days'])) {
// Remove empty dates
2016-05-04 00:56:53 +02:00
$_POST['days'] = array_filter($_POST['days'], function ($d) {
return !empty($d);
});
// Check if there are at most MAX_SLOTS_PER_POLL slots
if (count($_POST['days']) > MAX_SLOTS_PER_POLL) {
// Display step 2
$smarty->assign('title', __('Step 2 date', 'Poll dates (2 on 3)'));
$smarty->assign('choices', $_SESSION['form']->getChoices());
$smarty->assign('error', __f('Error', 'You can\'t select more than %d dates', MAX_SLOTS_PER_POLL));
$smarty->display('create_date_poll_step_2.tpl');
exit;
}
// Clear previous choices
$_SESSION['form']->clearChoices();
for ($i = 0; $i < count($_POST['days']); $i++) {
$day = $_POST['days'][$i];
if (!empty($day)) {
// Add choice to Form data
$date = DateTime::createFromFormat(__('Date', 'datetime_parseformat'), $_POST['days'][$i])->setTime(0, 0, 0);
$time = $date->getTimestamp();
$choice = new Choice($time);
$_SESSION['form']->addChoice($choice);
2016-05-04 00:56:53 +02:00
$schedules = $inputService->filterArray($_POST['horaires' . $i], FILTER_DEFAULT);
for ($j = 0; $j < count($schedules); $j++) {
if (!empty($schedules[$j])) {
$choice->addSlot(strip_tags($schedules[$j]));
}
}
}
2011-05-15 05:56:11 +02:00
}
2011-05-15 03:56:54 +02:00
}
2016-05-04 00:56:53 +02:00
// Display step 3
2014-10-21 01:31:26 +02:00
$summary = '<ul>';
$choices = $_SESSION['form']->getChoices();
foreach ($choices as $choice) {
2016-05-04 00:56:53 +02:00
$summary .= '<li>' . strftime($date_format['txt_full'], $choice->getName());
$first = true;
foreach ($choice->getSlots() as $slots) {
$summary .= $first ? ': ' : ', ';
$summary .= $slots;
2016-05-04 00:56:53 +02:00
$first = false;
2014-10-21 01:31:26 +02:00
}
$summary .= '</li>';
2014-10-21 01:31:26 +02:00
}
$summary .= '</ul>';
$end_date_str = utf8_encode(strftime($date_format['txt_date'], $max_expiry_time)); // textual date
$smarty->assign('title', __('Step 3', 'Removal date and confirmation (3 on 3)'));
$smarty->assign('summary', $summary);
$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->display('create_classic_poll_step3.tpl');
2016-05-04 00:56:53 +02:00
exit;
2016-05-04 00:56:53 +02:00
case 4:
// Step 4 : Data prepare before insert in DB
2016-05-04 00:56:53 +02:00
// Define expiration date
$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);
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;
}
}
}
2016-05-04 00:56:53 +02:00
if (empty($_SESSION['form']->end_date)) {
// By default, expiration date is 6 months after last day
$_SESSION['form']->end_date = $max_expiry_time;
}
2016-05-04 00:56:53 +02:00
// Insert poll in database
$ids = $pollService->createPoll($_SESSION['form']);
$poll_id = $ids[0];
$admin_poll_id = $ids[1];
2016-05-04 00:56:53 +02:00
// 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;
}