create_date_poll.php 9.42 KB
Newer Older
Simon Leblanc's avatar
Simon Leblanc committed
1
<?php
2
3
4
5
6
7
/**
 * 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
8
 * Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft https://framagit.org/framasoft/framadate/)
9
 *
10
 * =============================
11
12
13
14
15
 *
 * 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
 *
16
 * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
17
 * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft https://framagit.org/framasoft/framadate/)
18
 */
Olivier Perez's avatar
Olivier Perez committed
19
use Framadate\Choice;
20
use Framadate\Services\InputService;
Olivier PEREZ's avatar
Olivier PEREZ committed
21
22
use Framadate\Services\LogService;
use Framadate\Services\MailService;
Olivier Perez's avatar
Olivier Perez committed
23
use Framadate\Services\PollService;
Olivier PEREZ's avatar
Olivier PEREZ committed
24
use Framadate\Services\PurgeService;
Olivier PEREZ's avatar
Olivier PEREZ committed
25
use Framadate\Utils;
26

Olivier PEREZ's avatar
Olivier PEREZ committed
27
include_once __DIR__ . '/app/inc/init.php';
Simon Leblanc's avatar
Simon Leblanc committed
28

Olivier PEREZ's avatar
Olivier PEREZ committed
29
30
/* Service */
/*---------*/
Olivier PEREZ's avatar
Olivier PEREZ committed
31
$logService = new LogService();
Olivier PEREZ's avatar
Olivier PEREZ committed
32
$pollService = new PollService($connect, $logService);
Simon LEBLANC's avatar
Simon LEBLANC committed
33
$mailService = new MailService($config['use_smtp'], $config['smtp_options']);
Olivier PEREZ's avatar
Olivier PEREZ committed
34
$purgeService = new PurgeService($connect, $logService);
35
$inputService = new InputService();
Simon Leblanc's avatar
Simon Leblanc committed
36

pyg77's avatar
pyg77 committed
37
if (is_readable('bandeaux_local.php')) {
38
    include_once('bandeaux_local.php');
Simon Leblanc's avatar
Simon Leblanc committed
39
40
}

Olivier Perez's avatar
Olivier Perez committed
41
42
43
// Min/Max archive date
$min_expiry_time = $pollService->minExpiryDate();
$max_expiry_time = $pollService->maxExpiryDate();
44

Olivier Perez's avatar
Olivier Perez committed
45
46
47
48
49
// The poll format is DATE
if ($_SESSION['form']->format !== 'D') {
    $_SESSION['form']->format = 'D';
    $_SESSION['form']->clearChoices();
}
50

Olivier Perez's avatar
Olivier Perez committed
51
52
53
54
55
56
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;
57
} else {
Olivier Perez's avatar
Olivier Perez committed
58
59
    $step = 3;
}
FramaJosephK's avatar
FramaJosephK committed
60

Olivier Perez's avatar
Olivier Perez committed
61
62
63
64
65
66
67
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;
68

Olivier Perez's avatar
Olivier Perez committed
69
70
    case 2:
        // Step 2/4 : Select dates of the poll
71

Olivier Perez's avatar
Olivier Perez committed
72
73
74
75
76
        // Prefill form->choices
        foreach ($_SESSION['form']->getChoices() as $c) {
            $count = 3 - count($c->getSlots());
            for ($i = 0; $i < $count; $i++) {
                $c->addSlot('');
FramaJosephK's avatar
FramaJosephK committed
77
            }
Olivier PEREZ's avatar
Olivier PEREZ committed
78
79
        }

Olivier Perez's avatar
Olivier Perez committed
80
81
82
83
84
85
86
        $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);
Olivier PEREZ's avatar
Olivier PEREZ committed
87
        }
Olivier PEREZ's avatar
Olivier PEREZ committed
88

Olivier Perez's avatar
Olivier Perez committed
89
90
91
92
        // Display step 2
        $smarty->assign('title', __('Step 2 date', 'Poll dates (2 on 3)'));
        $smarty->assign('choices', $_SESSION['form']->getChoices());
        $smarty->assign('error', null);
Olivier PEREZ's avatar
Olivier PEREZ committed
93

Olivier Perez's avatar
Olivier Perez committed
94
        $smarty->display('create_date_poll_step_2.tpl');
Olivier PEREZ's avatar
Olivier PEREZ committed
95
        exit;
pyg77's avatar
pyg77 committed
96

Olivier Perez's avatar
Olivier Perez committed
97
98
99
100
    case 3:
        // Step 3/4 : Confirm poll creation

        // Handle Step2 submission
Olivier PEREZ's avatar
Olivier PEREZ committed
101
        if (!empty($_POST['days'])) {
102
            // Remove empty dates
Olivier Perez's avatar
Olivier Perez committed
103
104
105
            $_POST['days'] = array_filter($_POST['days'], function ($d) {
                return !empty($d);
            });
106
107
108
109
110
111
112
113
114
115
116

            // 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;
            }
Olivier PEREZ's avatar
Olivier PEREZ committed
117

Olivier PEREZ's avatar
Olivier PEREZ committed
118
119
120
            // Clear previous choices
            $_SESSION['form']->clearChoices();

121
            // Reorder moments to deal with suppressed dates
122
            $moments = [];
123
124
125
126
127
128
129
130
            $i = 0;
            while(count($moments) < count($_POST['days'])) {
                if (!empty($_POST['horaires' . $i])) {
                    $moments[] = $_POST['horaires' . $i];
                }
                $i++;
            }

Olivier PEREZ's avatar
Olivier PEREZ committed
131
132
            for ($i = 0; $i < count($_POST['days']); $i++) {
                $day = $_POST['days'][$i];
Olivier PEREZ's avatar
Olivier PEREZ committed
133

Olivier PEREZ's avatar
Olivier PEREZ committed
134
135
                if (!empty($day)) {
                    // Add choice to Form data
136
137
                    $date = DateTime::createFromFormat(__('Date', 'datetime_parseformat'), $_POST['days'][$i])->setTime(0, 0, 0);
                    $time = $date->getTimestamp();
Olivier PEREZ's avatar
Olivier PEREZ committed
138
139
140
                    $choice = new Choice($time);
                    $_SESSION['form']->addChoice($choice);

141
                    $schedules = $inputService->filterArray($moments[$i], FILTER_DEFAULT);
Olivier Perez's avatar
Olivier Perez committed
142
                    for ($j = 0; $j < count($schedules); $j++) {
Olivier PEREZ's avatar
Olivier PEREZ committed
143
                        if (!empty($schedules[$j])) {
144
                            $choice->addSlot(strip_tags($schedules[$j]));
145
146
147
                        }
                    }
                }
Simon Leblanc's avatar
Simon Leblanc committed
148
            }
149
            $_SESSION['form']->sortChoices();
150
        }
151

Olivier Perez's avatar
Olivier Perez committed
152
        // Display step 3
FramaJosephK's avatar
FramaJosephK committed
153
        $summary = '<ul>';
154
155
        $choices = $_SESSION['form']->getChoices();
        foreach ($choices as $choice) {
Olivier Perez's avatar
Olivier Perez committed
156
            $summary .= '<li>' . strftime($date_format['txt_full'], $choice->getName());
Olivier PEREZ's avatar
Olivier PEREZ committed
157
158
            $first = true;
            foreach ($choice->getSlots() as $slots) {
159
                $summary .= $first ? ': ' : ', ';
Olivier PEREZ's avatar
Olivier PEREZ committed
160
                $summary .= $slots;
Olivier Perez's avatar
Olivier Perez committed
161
                $first = false;
FramaJosephK's avatar
FramaJosephK committed
162
            }
Olivier PEREZ's avatar
Olivier PEREZ committed
163
            $summary .= '</li>';
FramaJosephK's avatar
FramaJosephK committed
164
165
        }
        $summary .= '</ul>';
166

167
168
169
170
171
172
173
174
175
        $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');
Olivier Perez's avatar
Olivier Perez committed
176
        exit;
177

Olivier Perez's avatar
Olivier Perez committed
178
179
    case 4:
        // Step 4 : Data prepare before insert in DB
180

Olivier Perez's avatar
Olivier Perez committed
181
182
183
184
185
186
        // 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);

187
            if (is_array($registredate) && count($registredate) === 3) {
Olivier Perez's avatar
Olivier Perez committed
188
189
190
191
192
193
194
195
196
                $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;
                }
197
198
199
            }
        }

Olivier Perez's avatar
Olivier Perez committed
200
201
202
        if (empty($_SESSION['form']->end_date)) {
            // By default, expiration date is 6 months after last day
            $_SESSION['form']->end_date = $max_expiry_time;
203
        }
Olivier PEREZ's avatar
Olivier PEREZ committed
204

Olivier Perez's avatar
Olivier Perez committed
205
206
207
208
        // Insert poll in database
        $ids = $pollService->createPoll($_SESSION['form']);
        $poll_id = $ids[0];
        $admin_poll_id = $ids[1];
209

Olivier Perez's avatar
Olivier Perez committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
        // 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;
238
}