Refactor on choix_autre.php

* Utils::issetAndNoEmpty is now removed
* Use Form object to store value from page to page
This commit is contained in:
Olivier PEREZ 2014-12-06 19:42:01 +01:00
parent de04d47434
commit 46ea697cb6
5 changed files with 94 additions and 72 deletions

View File

@ -13,7 +13,7 @@ class Choice
*/ */
private $slots; private $slots;
public function __construct($name) public function __construct($name='')
{ {
$this->name = $name; $this->name = $name;
$this->slots = array(); $this->slots = array();

View File

@ -185,18 +185,19 @@ class Utils
/** /**
* Fonction vérifiant l'existance et la valeur non vide d'une clé d'un tableau * Fonction vérifiant l'existance et la valeur non vide d'une clé d'un tableau
* @deprecated
* @param string $name La clé à tester * @param string $name La clé à tester
* @param array $tableau Le tableau rechercher la clé ($_POST par défaut) * @param array $tableau Le tableau rechercher la clé ($_POST par défaut)
* @return bool Vrai si la clé existe et renvoie une valeur non vide * @return bool Vrai si la clé existe et renvoie une valeur non vide
*/ */
public static function issetAndNoEmpty($name, $tableau = null) /*public static function issetAndNoEmpty($name, $tableau = null)
{ {
if (is_null($tableau)) { if (is_null($tableau)) {
$tableau = $_POST; $tableau = $_POST;
} }
return isset($tableau[$name]) && ! empty($tableau[$name]); return isset($tableau[$name]) && ! empty($tableau[$name]);
} }*/
/** /**
* Fonction permettant de générer les URL pour les sondage * Fonction permettant de générer les URL pour les sondage
@ -258,4 +259,11 @@ class Utils
} }
$connect->commit(); $connect->commit();
} }
public static function debug($object)
{
echo '<pre>';
print_r($object);
echo '</pre>';
}
} }

View File

@ -18,7 +18,8 @@
*/ */
namespace Framadate; namespace Framadate;
session_start(); include_once __DIR__ . '/app/inc/init.php';
include_once('creation_sondage.php'); include_once('creation_sondage.php');
if (file_exists('bandeaux_local.php')) { if (file_exists('bandeaux_local.php')) {
@ -27,82 +28,90 @@ if (file_exists('bandeaux_local.php')) {
include_once('bandeaux.php'); include_once('bandeaux.php');
} }
// Step 1/3 : error if $_SESSION from info_sondage are not valid // Step 1/4 : error if $_SESSION from info_sondage are not valid
if (Utils::issetAndNoEmpty('titre', $_SESSION) === false || Utils::issetAndNoEmpty('nom', $_SESSION) === false || (($config['use_smtp']) ? Utils::issetAndNoEmpty('adresse', $_SESSION) === false : false)) { if (empty($_SESSION['form']->titre) || empty($_SESSION['form']->nom) || (($config['use_smtp']) ? empty($_SESSION['form']->adresse) : false)) {
Utils::print_header ( _("Error!") ); Utils::print_header ( _("Error!") );
bandeau_titre(_("Error!")); bandeau_titre(_("Error!"));
echo ' echo '
<div class="alert alert-danger"> <div class="alert alert-danger">
<h3>' . _("You haven't filled the first section of the poll creation.") . ' !</h3> <h3>' . _('You haven\'t filled the first section of the poll creation.') . ' !</h3>
<p>' . _("Back to the homepage of ") . ' <a href="' . Utils::get_server_name() . '"> ' . NOMAPPLICATION . '</a></p> <p>' . _('Back to the homepage of') . ' <a href="' . Utils::get_server_name() . '"> ' . NOMAPPLICATION . '</a></p>
</div>'."\n"; </div>'."\n";
bandeau_pied(); bandeau_pied();
} else { } else {
// Step 4 : Data prepare before insert in DB // Step 4 : Data prepare before insert in DB
if (isset($_POST["confirmecreation"])) { if (isset($_POST['confirmecreation'])) {
//recuperation des données de champs textes
$temp_results = ''; $registredate = explode('/', $_POST['champdatefin']);
if (isset($_SESSION['choices'])) { if (is_array($registredate) == true && count($registredate) == 3) {
for ($i = 0; $i < count($_SESSION['choices']); $i++) { $time = mktime(0,0,0,$registredate[1],$registredate[0],$registredate[2]);
if ($_SESSION['choices'][$i]!="") { if ($time > time() + (24*60*60)) {
$temp_results.=','.str_replace(",", " ", htmlentities(html_entity_decode($_SESSION['choices'][$i], ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8')); $_SESSION['form']->champdatefin = $time;
}
} }
} }
$temp_results=substr($temp_results,1); // format du sondage AUTRE
$_SESSION["toutchoix"]=$temp_results; $_SESSION['form']->formatsondage = 'A';
// Insert poll in database
$admin_poll_id = ajouter_sondage(
$_SESSION['form']->titre,
$_SESSION['form']->commentaires,
$_SESSION['form']->nom,
$_SESSION['form']->adresse,
$_SESSION['form']->formatsondage,
$_SESSION['form']->editable,
$_SESSION['form']->champdatefin,
$_SESSION['form']->receiveNewVotes,
$_SESSION['form']->getChoices()
);
// Clean Form data in $_SESSION
unset($_SESSION['form']);
// Delete old polls
if (Utils::issetAndNoEmpty('champdatefin')) { Utils::cleaningOldPolls($connect, 'admin/logs_studs.txt');
$registredate = explode("/",$_POST["champdatefin"]);
if (is_array($registredate) == true && count($registredate) == 3) { // Redirect to poll administration
$time = mktime(0,0,0,$registredate[1],$registredate[0],$registredate[2]); header('Location:' . Utils::getUrlSondage($admin_poll_id, true));
if ($time > time() + (24*60*60)) { exit;
$_SESSION["champdatefin"]=$time;
}
}
}
//format du sondage AUTRE
$_SESSION["formatsondage"]="A".$_SESSION["studsplus"];
ajouter_sondage();
} }
// recuperation des sujets pour sondage AUTRE // Step 3/4 : Confirm poll creation and choose a removal date
if (isset($_POST['choices'])) { else if (isset($_POST['fin_sondage_autre'])) {
$k = 0; Utils::print_header ( _('Removal date and confirmation (3 on 3)') );
for ($i = 0; $i < count($_POST['choices']); $i++) { bandeau_titre(_('Removal date and confirmation (3 on 3)'));
if (Utils::issetAndNoEmpty($i, $_POST['choices'])) {
$_SESSION['choices'][$k]=htmlentities(html_entity_decode($_POST['choices'][$i], ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
$k++; // Store choices in $_SESSION
if (isset($_POST['choices'])) {
$_SESSION['form']->clearChoices();
foreach ($_POST['choices'] as $c)
{
if (!empty($c))
{
$choice = new Choice(htmlentities(html_entity_decode($c, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8'));
$_SESSION['form']->addChoice($choice);
}
} }
} }
}
// Step 3/3 : Confirm poll creation and choose a removal date
if (isset($_POST["fin_sondage_autre"])) {
Utils::print_header ( _("Removal date and confirmation (3 on 3)") );
bandeau_titre(_("Removal date and confirmation (3 on 3)"));
// Expiration date is initialised with config parameter. Value will be modified in step 4 if user has defined an other date // Expiration date is initialised with config parameter. Value will be modified in step 4 if user has defined an other date
$_SESSION["champdatefin"]= time()+ (86400 * $config['default_poll_duration']); //60 sec * 60 min * 24 hours * config $_SESSION['form']->champdatefin = time() + (86400 * $config['default_poll_duration']); //60 sec * 60 min * 24 hours * config
$removal_date= utf8_encode(strftime($date_format['txt_full'], ($_SESSION["champdatefin"])));//textual date
// Summary // Summary
$summary = '<ol>'; $summary = '<ol>';
for ($i=0;$i<count($_SESSION['choices']);$i++) { foreach ($_SESSION['form']->getChoices() as $choice) {
preg_match_all('/\[!\[(.*?)\]\((.*?)\)\]\((.*?)\)/',$_SESSION['choices'][$i],$md_a_img); // Markdown [![alt](src)](href) preg_match_all('/\[!\[(.*?)\]\((.*?)\)\]\((.*?)\)/', $choice->getName(), $md_a_img); // Markdown [![alt](src)](href)
preg_match_all('/!\[(.*?)\]\((.*?)\)/',$_SESSION['choices'][$i],$md_img); // Markdown ![alt](src) preg_match_all('/!\[(.*?)\]\((.*?)\)/', $choice->getName(), $md_img); // Markdown ![alt](src)
preg_match_all('/\[(.*?)\]\((.*?)\)/',$_SESSION['choices'][$i],$md_a); // Markdown [text](href) preg_match_all('/\[(.*?)\]\((.*?)\)/', $choice->getName(), $md_a); // Markdown [text](href)
if (isset($md_a_img[2][0]) && $md_a_img[2][0]!='' && isset($md_a_img[3][0]) && $md_a_img[3][0]!='') { // [![alt](src)](href) if (isset($md_a_img[2][0]) && $md_a_img[2][0]!='' && isset($md_a_img[3][0]) && $md_a_img[3][0]!='') { // [![alt](src)](href)
$li_subject_text = (isset($md_a_img[1][0]) && $md_a_img[1][0]!='') ? stripslashes($md_a_img[1][0]) : _("Choice") .' '.($i+1); $li_subject_text = (isset($md_a_img[1][0]) && $md_a_img[1][0]!='') ? stripslashes($md_a_img[1][0]) : _("Choice") .' '.($i+1);
@ -120,7 +129,7 @@ if (Utils::issetAndNoEmpty('titre', $_SESSION) === false || Utils::issetAndNoEmp
} else { // text only } else { // text only
$li_subject_text = stripslashes($_SESSION['choices'][$i]); $li_subject_text = stripslashes($choice->getName());
$li_subject_html = $li_subject_text; $li_subject_html = $li_subject_text;
} }
@ -129,6 +138,8 @@ if (Utils::issetAndNoEmpty('titre', $_SESSION) === false || Utils::issetAndNoEmp
} }
$summary .= '</ol>'; $summary .= '</ol>';
$end_date_str = utf8_encode(strftime('%d/%M/%Y', $_SESSION['form']->champdatefin));//textual date
echo ' echo '
<form name="formulaire" action="' . Utils::get_server_name() . 'choix_autre.php" method="POST" class="form-horizontal" role="form"> <form name="formulaire" action="' . Utils::get_server_name() . 'choix_autre.php" method="POST" class="form-horizontal" role="form">
<div class="row"> <div class="row">
@ -138,13 +149,13 @@ if (Utils::issetAndNoEmpty('titre', $_SESSION) === false || Utils::issetAndNoEmp
'. $summary .' '. $summary .'
</div> </div>
<div class="alert alert-info"> <div class="alert alert-info">
<p>' . _("Your poll will be automatically removed after"). " " . $config['default_poll_duration'] . " " . _("days") . ': <strong>'.$removal_date.'</strong>.<br />' . _("You can fix another removal date for it.") .'</p> <p>' . _('Your poll will be automatically removed after'). ' ' . $config['default_poll_duration'] . ' ' . _('days') . '.<br />' . _("You can fix another removal date for it.") .'</p>
<div class="form-group"> <div class="form-group">
<label for="champdatefin" class="col-sm-5 control-label">'. _("Removal date (optional)") .'</label> <label for="champdatefin" class="col-sm-5 control-label">'. _("Removal date (optional)") .'</label>
<div class="col-sm-6"> <div class="col-sm-6">
<div class="input-group date"> <div class="input-group date">
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar text-info"></i></span> <span class="input-group-addon"><i class="glyphicon glyphicon-calendar text-info"></i></span>
<input type="text" class="form-control" id="champdatefin" data-date-format="'. _("dd/mm/yyyy") .'" aria-describedby="dateformat" name="champdatefin" value="" size="10" maxlength="10" placeholder="'. _("dd/mm/yyyy") .'" /> <input type="text" class="form-control" id="champdatefin" data-date-format="'. _("dd/mm/yyyy") .'" aria-describedby="dateformat" name="champdatefin" value="'. $end_date_str .'" size="10" maxlength="10" placeholder="'. _("dd/mm/yyyy") .'" />
</div> </div>
</div> </div>
<span id="dateformat" class="sr-only">'. _("(dd/mm/yyyy)") .'</span> <span id="dateformat" class="sr-only">'. _("(dd/mm/yyyy)") .'</span>
@ -168,10 +179,10 @@ if (Utils::issetAndNoEmpty('titre', $_SESSION) === false || Utils::issetAndNoEmp
bandeau_pied(); bandeau_pied();
// Step 2/3 : Select choices of the poll // Step 2/4 : Select choices of the poll
} else { } else {
Utils::print_header( _("Poll subjects (2 on 3)")); Utils::print_header( _('Poll subjects (2 on 3)'));
bandeau_titre(_("Poll subjects (2 on 3)")); bandeau_titre(_('Poll subjects (2 on 3)'));
echo ' echo '
<form name="formulaire" action="' . Utils::get_server_name() . 'choix_autre.php" method="POST" class="form-horizontal" role="form"> <form name="formulaire" action="' . Utils::get_server_name() . 'choix_autre.php" method="POST" class="form-horizontal" role="form">
@ -187,16 +198,17 @@ if (Utils::issetAndNoEmpty('titre', $_SESSION) === false || Utils::issetAndNoEmp
echo ' </div>'."\n"; echo ' </div>'."\n";
// Fields choices : 5 by default // Fields choices : 5 by default
$nb_choices = (isset($_SESSION['choices'])) ? max(count($_SESSION['choices']), 5) : 5; $choices = $_SESSION['form']->getChoices();
$nb_choices = max(count($choices), 5);
for ($i = 0; $i < $nb_choices; $i++) { for ($i = 0; $i < $nb_choices; $i++) {
$choice_value = (isset($_SESSION['choices'][$i])) ? str_replace("\\","",$_SESSION['choices'][$i]) : ''; $choice = isset($choices[$i]) ? $choices[$i] : new Choice();
echo ' echo '
<div class="form-group choice-field"> <div class="form-group choice-field">
<label for="choice'.$i.'" class="col-sm-2 control-label">'. _("Choice") .' '.($i+1).'</label> <label for="choice'.$i.'" class="col-sm-2 control-label">'. _('Choice') .' '.($i+1).'</label>
<div class="col-sm-10 input-group"> <div class="col-sm-10 input-group">
<input type="text" class="form-control" name="choices[]" size="40" value="'.$choice_value.'" id="choice'.$i.'" />'; <input type="text" class="form-control" name="choices[]" size="40" value="'.$choice->getName().'" id="choice'.$i.'" />';
if($config['user_can_add_img_or_link']){ if($config['user_can_add_img_or_link']){
echo '<span class="input-group-addon btn-link md-a-img" title="'. _("Add a link or an image") .' - '. _("Choice") .' '.($i+1).'" ><span class="glyphicon glyphicon-picture"></span> <span class="glyphicon glyphicon-link"></span></span>'; echo '<span class="input-group-addon btn-link md-a-img" title="'. _('Add a link or an image') .' - '. _('Choice') .' '.($i+1).'" ><span class="glyphicon glyphicon-picture"></span> <span class="glyphicon glyphicon-link"></span></span>';
} }
echo ' echo '
</div> </div>
@ -206,8 +218,8 @@ if (Utils::issetAndNoEmpty('titre', $_SESSION) === false || Utils::issetAndNoEmp
echo ' echo '
<div class="col-md-4"> <div class="col-md-4">
<div class="btn-group btn-group"> <div class="btn-group btn-group">
<button type="button" id="remove-a-choice" class="btn btn-default" title="'. _("Remove a choice") .'"><span class="glyphicon glyphicon-minus text-info"></span><span class="sr-only">'. _("Remove") .'</span></button> <button type="button" id="remove-a-choice" class="btn btn-default" title="'. _('Remove a choice') .'"><span class="glyphicon glyphicon-minus text-info"></span><span class="sr-only">'. _('Remove') .'</span></button>
<button type="button" id="add-a-choice" class="btn btn-default" title="'. _("Add a choice") .'"><span class="glyphicon glyphicon-plus text-success"></span><span class="sr-only">'. _("Add") .'</span></button> <button type="button" id="add-a-choice" class="btn btn-default" title="'. _('Add a choice') .'"><span class="glyphicon glyphicon-plus text-success"></span><span class="sr-only">'. _('Add') .'</span></button>
</div> </div>
</div> </div>
<div class="col-md-8 text-right"> <div class="col-md-8 text-right">

View File

@ -28,7 +28,7 @@ if (is_readable('bandeaux_local.php')) {
include_once('bandeaux.php'); include_once('bandeaux.php');
} }
// Step 1/3 : error if $_SESSION from info_sondage are not valid // Step 1/4 : error if $_SESSION from info_sondage are not valid
if (!isset($_SESSION['form']->titre) || !isset($_SESSION['form']->nom) || (($config['use_smtp']) ? !isset($_SESSION['form']->adresse) : false)) { if (!isset($_SESSION['form']->titre) || !isset($_SESSION['form']->nom) || (($config['use_smtp']) ? !isset($_SESSION['form']->adresse) : false)) {
Utils::print_header ( _("Error!") ); Utils::print_header ( _("Error!") );
@ -39,6 +39,7 @@ if (!isset($_SESSION['form']->titre) || !isset($_SESSION['form']->nom) || (($con
<h3>' . _("You haven't filled the first section of the poll creation.") . ' !</h3> <h3>' . _("You haven't filled the first section of the poll creation.") . ' !</h3>
<p>' . _("Back to the homepage of ") . ' ' . '<a href="' . Utils::get_server_name() . '">' . NOMAPPLICATION . '</a>.</p> <p>' . _("Back to the homepage of ") . ' ' . '<a href="' . Utils::get_server_name() . '">' . NOMAPPLICATION . '</a>.</p>
</div>'; </div>';
bandeau_pied(); bandeau_pied();
@ -66,6 +67,7 @@ if (!isset($_SESSION['form']->titre) || !isset($_SESSION['form']->nom) || (($con
$_SESSION['form']->champdatefin=end($temp_results)+(86400 * $config['default_poll_duration']); $_SESSION['form']->champdatefin=end($temp_results)+(86400 * $config['default_poll_duration']);
} }
// Insert poll in database
$admin_poll_id = ajouter_sondage( $admin_poll_id = ajouter_sondage(
$_SESSION['form']->titre, $_SESSION['form']->titre,
$_SESSION['form']->commentaires, $_SESSION['form']->commentaires,

View File

@ -57,7 +57,7 @@ $erreur_injection_nom = false;
$erreur_injection_commentaires = false; $erreur_injection_commentaires = false;
#tests #tests
if (Utils::issetAndNoEmpty("poursuivre")){ if (!empty($_POST['poursuivre'])){
$_SESSION['form']->titre = $titre; $_SESSION['form']->titre = $titre;
$_SESSION['form']->nom = $nom; $_SESSION['form']->nom = $nom;
$_SESSION['form']->adresse = $adresse; $_SESSION['form']->adresse = $adresse;
@ -140,7 +140,7 @@ $errors = array(
) )
); );
if (!$_SESSION['form']->titre && Utils::issetAndNoEmpty("poursuivre") ) { if (!$_SESSION['form']->titre && !empty($_POST['poursuivre'])) {
$errors['title']['aria'] = 'aria-describeby="poll_title_error" '; $errors['title']['class'] = ' has-error'; $errors['title']['aria'] = 'aria-describeby="poll_title_error" '; $errors['title']['class'] = ' has-error';
$errors['title']['msg'] = '<div class="alert alert-danger" ><p id="poll_title_error">' . _("Enter a title") . '</p></div>'; $errors['title']['msg'] = '<div class="alert alert-danger" ><p id="poll_title_error">' . _("Enter a title") . '</p></div>';
} elseif ($erreur_injection_titre) { } elseif ($erreur_injection_titre) {
@ -153,7 +153,7 @@ if ($erreur_injection_commentaires) {
$errors['description']['msg'] = '<div class="alert alert-danger"><p id="poll_comment_error">' . _("Characters < > and \" are not permitted") . '</p></div>'; $errors['description']['msg'] = '<div class="alert alert-danger"><p id="poll_comment_error">' . _("Characters < > and \" are not permitted") . '</p></div>';
} }
if (!$_SESSION['form']->nom && Utils::issetAndNoEmpty("poursuivre")) { if (!$_SESSION['form']->nom && !empty($_POST['poursuivre'])) {
$errors['name']['aria'] = 'aria-describeby="poll_name_error" '; $errors['name']['class'] = ' has-error'; $errors['name']['aria'] = 'aria-describeby="poll_name_error" '; $errors['name']['class'] = ' has-error';
$errors['name']['msg'] = '<div class="alert alert-danger"><p id="poll_name_error">' . _("Enter a name") . '</p></div>'; $errors['name']['msg'] = '<div class="alert alert-danger"><p id="poll_name_error">' . _("Enter a name") . '</p></div>';
} elseif ($erreur_injection_nom) { } elseif ($erreur_injection_nom) {
@ -161,10 +161,10 @@ if (!$_SESSION['form']->nom && Utils::issetAndNoEmpty("poursuivre")) {
$errors['name']['msg'] = '<div class="alert alert-danger"><p id="poll_name_error">' . _("Characters < > and \" are not permitted") . '</p></div>'; $errors['name']['msg'] = '<div class="alert alert-danger"><p id="poll_name_error">' . _("Characters < > and \" are not permitted") . '</p></div>';
} }
if (!$_SESSION['form']->adresse && Utils::issetAndNoEmpty("poursuivre")) { if (!$_SESSION['form']->adresse && !empty($_POST['poursuivre'])) {
$errors['email']['aria'] = 'aria-describeby="poll_name_error" '; $errors['email']['class'] = ' has-error'; $errors['email']['aria'] = 'aria-describeby="poll_name_error" '; $errors['email']['class'] = ' has-error';
$errors['email']['msg'] = '<div class="alert alert-danger"><p id="poll_email_error">' . _("Enter an email address") . '</p></div>'; $errors['email']['msg'] = '<div class="alert alert-danger"><p id="poll_email_error">' . _("Enter an email address") . '</p></div>';
} elseif ($erreur_adresse && Utils::issetAndNoEmpty("poursuivre")) { } elseif ($erreur_adresse && !empty($_POST['poursuivre'])) {
$errors['email']['aria'] = 'aria-describeby="poll_email_error" '; $errors['email']['class'] = ' has-error'; $errors['email']['aria'] = 'aria-describeby="poll_email_error" '; $errors['email']['class'] = ' has-error';
$errors['email']['msg'] = '<div class="alert alert-danger"><p id="poll_email_error">' . _("The address is not correct! You should enter a valid email address (like r.stallman@outlock.com) in order to receive the link to your poll.") . '</p></div>'; $errors['email']['msg'] = '<div class="alert alert-danger"><p id="poll_email_error">' . _("The address is not correct! You should enter a valid email address (like r.stallman@outlock.com) in order to receive the link to your poll.") . '</p></div>';
} }