2011-05-15 01:32:47 +02:00
< ? php
2014-09-04 17:52:18 +02:00
/**
* 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
2016-11-15 00:00:29 +01:00
* Authors of Framadate / OpenSondage : Framasoft ( https :// github . com / framasoft https :// framagit . org / framasoft / framadate / )
2014-09-04 17:52:18 +02:00
*
2014-07-04 11:21:31 +02:00
* =============================
2014-09-04 17:52:18 +02:00
*
* 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
*
2014-07-04 11:21:31 +02:00
* Auteurs de STUdS ( projet initial ) : Guilhem BORGHESI ( borghesi @ unistra . fr ) et Raphaël DROZ
2016-11-15 00:00:29 +01:00
* Auteurs de Framadate / OpenSondage : Framasoft ( https :// github . com / framasoft https :// framagit . org / framasoft / framadate / )
2014-07-04 11:21:31 +02:00
*/
2016-05-04 00:56:53 +02:00
use Framadate\Choice ;
2018-05-25 18:16:22 +02:00
use Framadate\Form ;
2015-01-05 23:30:47 +01:00
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 ;
2018-04-08 11:21:11 +02:00
use Framadate\Services\SessionService ;
2014-12-25 00:55:52 +01:00
use Framadate\Utils ;
2014-09-04 17:52:18 +02:00
2014-12-03 21:08:08 +01:00
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 );
2018-05-25 15:50:35 +02:00
$mailService = new MailService ( $config [ 'use_smtp' ], $config [ 'smtp_options' ], $config [ 'use_sendmail' ]);
2014-12-29 21:54:07 +01:00
$purgeService = new PurgeService ( $connect , $logService );
2015-01-05 23:30:47 +01:00
$inputService = new InputService ();
2018-04-08 11:21:11 +02:00
$sessionService = new SessionService ();
2011-05-15 01:32:47 +02:00
2013-03-13 18:29:48 +01:00
if ( is_readable ( 'bandeaux_local.php' )) {
2014-09-04 17:52:18 +02:00
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 ();
2014-09-04 17:52:18 +02:00
2018-05-25 18:16:22 +02:00
$form = isset ( $_SESSION [ 'form' ]) ? unserialize ( $_SESSION [ 'form' ]) : null ;
if ( $form === null || ! ( $form instanceof Form )) {
$smarty -> assign ( 'title' , __ ( 'Error' , 'Error!' ));
$smarty -> assign ( 'error' , __ ( 'Error' , 'You haven\'t filled the first section of the poll creation, or your session has expired.' ));
$smarty -> display ( 'error.tpl' );
exit ;
}
2018-05-24 17:45:22 +02:00
2018-04-23 10:27:15 +02:00
// The poll format is DATE if we are in this file
2018-05-24 17:45:22 +02:00
if ( ! isset ( $form -> format )) {
$form -> format = 'D' ;
2018-04-23 10:27:15 +02:00
}
// If we come from another format, we need to clear choices
2018-05-25 10:57:46 +02:00
if ( $form -> format !== 'D' ) {
2018-05-24 17:45:22 +02:00
$form -> format = 'D' ;
$form -> clearChoices ();
2016-05-04 00:56:53 +02:00
}
2014-09-04 17:52:18 +02:00
2018-05-24 17:45:22 +02:00
if ( ! isset ( $form -> title ) || ! isset ( $form -> admin_name ) || ( $config [ 'use_smtp' ] && ! isset ( $form -> admin_mail ))) {
2016-05-04 00:56:53 +02:00
$step = 1 ;
} else if ( ! empty ( $_POST [ 'confirmation' ])) {
$step = 4 ;
2018-05-24 17:45:22 +02:00
} else if ( empty ( $_POST [ 'choixheures' ]) || isset ( $form -> totalchoixjour )) {
2016-05-04 00:56:53 +02:00
$step = 2 ;
2014-09-04 17:52:18 +02:00
} 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 2 :
// Step 2/4 : Select dates of the poll
2015-01-04 01:36:55 +01:00
2016-05-04 00:56:53 +02:00
// Prefill form->choices
2018-05-24 17:45:22 +02:00
foreach ( $form -> getChoices () as $c ) {
2018-04-23 10:27:15 +02:00
/** @var Choice $c */
2016-05-04 00:56:53 +02:00
$count = 3 - count ( $c -> getSlots ());
for ( $i = 0 ; $i < $count ; $i ++ ) {
$c -> addSlot ( '' );
2014-10-21 01:31:26 +02:00
}
2014-12-12 13:43:43 +01:00
}
2018-05-24 17:45:22 +02:00
$count = 3 - count ( $form -> getChoices ());
2016-05-04 00:56:53 +02:00
for ( $i = 0 ; $i < $count ; $i ++ ) {
$c = new Choice ( '' );
$c -> addSlot ( '' );
$c -> addSlot ( '' );
$c -> addSlot ( '' );
2018-05-24 17:45:22 +02:00
$form -> addChoice ( $c );
2014-12-25 00:55:52 +01:00
}
2014-12-12 13:43:43 +01:00
2018-05-24 17:45:22 +02:00
$_SESSION [ 'form' ] = serialize ( $form );
2016-05-04 00:56:53 +02:00
// Display step 2
2018-07-06 18:06:35 +02:00
$smarty -> assign ( 'title' , __ ( 'Step 2 date' , 'Poll dates (2 of 3)' ));
2018-05-24 17:45:22 +02:00
$smarty -> assign ( 'choices' , $form -> getChoices ());
2016-05-04 00:56:53 +02:00
$smarty -> assign ( 'error' , null );
2014-12-12 13:43:43 +01:00
2016-05-04 00:56:53 +02:00
$smarty -> display ( 'create_date_poll_step_2.tpl' );
2014-12-03 21:08:08 +01:00
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
2014-12-12 13:43:43 +01:00
if ( ! empty ( $_POST [ 'days' ])) {
2016-04-26 23:16:08 +02:00
// Remove empty dates
2016-05-04 00:56:53 +02:00
$_POST [ 'days' ] = array_filter ( $_POST [ 'days' ], function ( $d ) {
return ! empty ( $d );
});
2016-04-26 23:16:08 +02:00
// Check if there are at most MAX_SLOTS_PER_POLL slots
if ( count ( $_POST [ 'days' ]) > MAX_SLOTS_PER_POLL ) {
// Display step 2
2018-07-06 18:06:35 +02:00
$smarty -> assign ( 'title' , __ ( 'Step 2 date' , 'Poll dates (2 of 3)' ));
2018-05-24 17:45:22 +02:00
$smarty -> assign ( 'choices' , $form -> getChoices ());
2016-04-26 23:16:08 +02:00
$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 ;
}
2014-12-12 13:43:43 +01:00
2014-12-03 21:08:08 +01:00
// Clear previous choices
2018-05-24 17:45:22 +02:00
$form -> clearChoices ();
2014-12-03 21:08:08 +01:00
2016-08-08 17:15:18 +02:00
// Reorder moments to deal with suppressed dates
2018-02-19 00:18:43 +01:00
$moments = [];
2016-08-08 17:15:18 +02:00
$i = 0 ;
while ( count ( $moments ) < count ( $_POST [ 'days' ])) {
if ( ! empty ( $_POST [ 'horaires' . $i ])) {
$moments [] = $_POST [ 'horaires' . $i ];
}
$i ++ ;
}
2014-12-03 21:08:08 +01:00
for ( $i = 0 ; $i < count ( $_POST [ 'days' ]); $i ++ ) {
$day = $_POST [ 'days' ][ $i ];
2014-12-12 13:43:43 +01:00
2014-12-03 21:08:08 +01:00
if ( ! empty ( $day )) {
// Add choice to Form data
2018-07-06 18:06:35 +02:00
$date = DateTime :: createFromFormat ( __ ( 'Date' , 'Y-m-d' ), $_POST [ 'days' ][ $i ]) -> setTime ( 0 , 0 , 0 );
2018-04-23 10:27:15 +02:00
$time = ( string ) $date -> getTimestamp ();
2014-12-03 21:08:08 +01:00
$choice = new Choice ( $time );
2018-05-24 17:45:22 +02:00
$form -> addChoice ( $choice );
2014-12-03 21:08:08 +01:00
2016-08-08 17:15:18 +02:00
$schedules = $inputService -> filterArray ( $moments [ $i ], FILTER_DEFAULT );
2016-05-04 00:56:53 +02:00
for ( $j = 0 ; $j < count ( $schedules ); $j ++ ) {
2014-12-03 21:08:08 +01:00
if ( ! empty ( $schedules [ $j ])) {
2015-01-07 23:16:42 +01:00
$choice -> addSlot ( strip_tags ( $schedules [ $j ]));
2014-09-04 17:52:18 +02:00
}
}
}
2011-05-15 05:56:11 +02:00
}
2018-05-24 17:45:22 +02:00
$form -> sortChoices ();
2011-05-15 03:56:54 +02:00
}
2014-09-04 17:52:18 +02:00
2016-05-04 00:56:53 +02:00
// Display step 3
2014-10-21 01:31:26 +02:00
$summary = '<ul>' ;
2018-05-24 17:45:22 +02:00
$choices = $form -> getChoices ();
2015-04-22 00:11:13 +02:00
foreach ( $choices as $choice ) {
2018-04-23 10:27:15 +02:00
/** @var Choice $choice */
2016-05-04 00:56:53 +02:00
$summary .= '<li>' . strftime ( $date_format [ 'txt_full' ], $choice -> getName ());
2014-12-03 21:08:08 +01:00
$first = true ;
foreach ( $choice -> getSlots () as $slots ) {
2015-09-15 18:20:13 +02:00
$summary .= $first ? ': ' : ', ' ;
2014-12-03 21:08:08 +01:00
$summary .= $slots ;
2016-05-04 00:56:53 +02:00
$first = false ;
2014-10-21 01:31:26 +02:00
}
2014-12-03 21:08:08 +01:00
$summary .= '</li>' ;
2014-10-21 01:31:26 +02:00
}
$summary .= '</ul>' ;
2014-09-04 17:52:18 +02:00
2016-05-04 00:08:29 +02:00
$end_date_str = utf8_encode ( strftime ( $date_format [ 'txt_date' ], $max_expiry_time )); // textual date
2018-05-24 17:45:22 +02:00
$_SESSION [ 'form' ] = serialize ( $form );
2018-07-06 18:06:35 +02:00
$smarty -> assign ( 'title' , __ ( 'Step 3' , 'Removal date and confirmation (3 of 3)' ));
2016-05-04 00:08:29 +02:00
$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' ]);
2018-04-20 22:16:17 +02:00
$smarty -> display ( 'create_poll_step_3.tpl' );
2016-05-04 00:56:53 +02:00
exit ;
2015-01-04 01:36:55 +01:00
2016-05-04 00:56:53 +02:00
case 4 :
// Step 4 : Data prepare before insert in DB
2014-09-04 17:52:18 +02:00
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 );
2018-02-19 00:18:43 +01:00
if ( is_array ( $registredate ) && count ( $registredate ) === 3 ) {
2016-05-04 00:56:53 +02:00
$time = mktime ( 0 , 0 , 0 , $registredate [ 1 ], $registredate [ 0 ], $registredate [ 2 ]);
if ( $time < $min_expiry_time ) {
2018-05-24 17:45:22 +02:00
$form -> end_date = $min_expiry_time ;
2016-05-04 00:56:53 +02:00
} elseif ( $max_expiry_time < $time ) {
2018-05-24 17:45:22 +02:00
$form -> end_date = $max_expiry_time ;
2016-05-04 00:56:53 +02:00
} else {
2018-05-24 17:45:22 +02:00
$form -> end_date = $time ;
2016-05-04 00:56:53 +02:00
}
2015-05-31 15:57:44 +02:00
}
}
2018-05-24 17:45:22 +02:00
if ( empty ( $form -> end_date )) {
2016-05-04 00:56:53 +02:00
// By default, expiration date is 6 months after last day
2018-05-24 17:45:22 +02:00
$form -> end_date = $max_expiry_time ;
2015-04-16 13:10:28 +02:00
}
2015-03-05 14:53:42 +01:00
2016-05-04 00:56:53 +02:00
// Insert poll in database
2018-05-24 17:45:22 +02:00
$ids = $pollService -> createPoll ( $form );
2016-05-04 00:56:53 +02:00
$poll_id = $ids [ 0 ];
$admin_poll_id = $ids [ 1 ];
2014-09-04 17:52:18 +02:00
2016-05-04 00:56:53 +02:00
// Send confirmation by mail if enabled
if ( $config [ 'use_smtp' ] === true ) {
2018-07-06 18:06:35 +02:00
$message = __ ( 'Mail' , " This is the message to forward to the poll participants. " );
2016-05-04 00:56:53 +02:00
$message .= '<br/><br/>' ;
2018-07-06 18:06:35 +02:00
$message .= Utils :: htmlEscape ( $form -> admin_name ) . ' ' . __ ( 'Mail' , 'has just created a poll called' ) . ' : "' . Utils :: htmlEscape ( $form -> title ) . '".<br/>' ;
$message .= __ ( 'Mail' , 'Thank you for participating in the poll at the following link' ) . ' :<br/><br/><a href="%1$s">%1$s</a>' ;
2016-05-04 00:56:53 +02:00
2018-07-06 18:06:35 +02:00
$message_admin = __ ( 'Mail' , " This message should NOT be sent to the poll participants. You should keep it private. <br/><br/>You can modify your poll at the following link " );
2016-05-04 00:56:53 +02:00
$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 ));
2018-05-24 17:45:22 +02:00
if ( $mailService -> isValidEmail ( $form -> admin_mail )) {
2018-10-10 10:27:41 +02:00
$mailService -> send ( $form -> admin_mail , '[' . NOMAPPLICATION . '][' . __ ( 'Mail' , 'Message for the author' ) . '] ' . __ ( 'Generic' , 'Poll' ) . ': ' . $form -> title , $message_admin );
$mailService -> send ( $form -> admin_mail , '[' . NOMAPPLICATION . '][' . __ ( 'Mail' , 'Participant link' ) . '] ' . __ ( 'Generic' , 'Poll' ) . ': ' . $form -> title , $message );
2016-05-04 00:56:53 +02:00
}
}
// Clean Form data in $_SESSION
unset ( $_SESSION [ 'form' ]);
2018-04-14 08:48:24 +02:00
$purgeService -> repeatedCleanings ();
2016-05-04 00:56:53 +02:00
2018-04-07 22:24:55 +02:00
// creation message
2018-04-08 11:21:11 +02:00
$sessionService -> set ( " Framadate " , " messagePollCreated " , TRUE );
2018-04-23 10:27:15 +02:00
2016-05-04 00:56:53 +02:00
// Redirect to poll administration
header ( 'Location:' . Utils :: getUrlSondage ( $admin_poll_id , true ));
exit ;
2018-04-20 22:16:17 +02:00
case 1 :
default :
// 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, or your session has expired.' ));
$smarty -> display ( 'error.tpl' );
exit ;
2014-06-11 19:19:17 +02:00
}