diff --git a/create_classic_poll.php b/create_classic_poll.php index 8f5deba..e72a8af 100644 --- a/create_classic_poll.php +++ b/create_classic_poll.php @@ -17,6 +17,7 @@ * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) */ use Framadate\Choice; +use Framadate\Services\InputService; use Framadate\Services\LogService; use Framadate\Services\MailService; use Framadate\Services\PollService; @@ -40,32 +41,112 @@ if (is_file('bandeaux_local.php')) { include_once('bandeaux.php'); } +// Min/Max archive date +$min_expiry_time = $pollService->minExpiryDate(); +$max_expiry_time = $pollService->maxExpiryDate(); + $form = unserialize($_SESSION['form']); -// Step 1/4 : error if $_SESSION from info_sondage are not valid -if (empty($form->title) || empty($form->admin_name) || (($config['use_smtp']) ? empty($form->admin_mail) : false)) { - $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; +// The poll format is AUTRE (other) if we are in this file +if (!isset($form->format)) { + $form->format = 'A'; } - // Min/Max archive date - $min_expiry_time = $pollService->minExpiryDate(); - $max_expiry_time = $pollService->maxExpiryDate(); - // The poll format is other (A) if we are in this file - if (!isset($form->format)) { - $form->format = 'A'; - } - // If we come from another format, we need to clear choices - if (isset($form->format) && $form->format !== 'A') { - $form->format = 'A'; - $form->clearChoices(); - } +// The poll format is AUTRE (other) +if ($form->format !== 'A') { + $form->format = 'A'; + $form->clearChoices(); +} - // Step 4 : Data prepare before insert in DB - if (isset($_POST['confirmation'])) { - // Define expiration date +if (!isset($form->title) || !isset($form->admin_name) || ($config['use_smtp'] && !isset($form->admin_mail))) { + $step = 1; +} elseif (isset($_POST['confirmation'])) { + $step = 4; +} elseif (empty($_POST['fin_sondage_autre']) ) { + $step = 2; +} else { + $step = 3; +} + +switch ($step) { + case 2: // Step 2/4 : Select choices of the poll + $choices = $form->getChoices(); + $nb_choices = max( 5- count($choices), 0); + while ($nb_choices-- > 0) { + $c = new Choice(''); + $form->addChoice($c); + } + + $_SESSION['form'] = serialize($form); + + // Display step 2 + $smarty->assign('title', __('Step 2 classic', 'Poll subjects (2 on 3)')); + $smarty->assign('choices', $form->getChoices()); + $smarty->assign('allowMarkdown', $config['user_can_add_img_or_link']); + $smarty->assign('error', null); + + $smarty->display('create_classic_poll_step_2.tpl'); + exit; + + case 3: // Step 3/4 : Confirm poll creation and choose a removal date + // Handle Step2 submission + if (!empty($_POST['choices'])) { + // remove empty choices + $_POST['choices'] = array_filter($_POST['choices'], function ($c) { + return !empty($c); + }); + + $form->clearChoices(); + + // store choices in $_SESSION + foreach ($_POST['choices'] as $c) { + $c = strip_tags($c); + $choice = new Choice($c); + $form->addChoice($choice); + } + } + + // Expiration date is initialised with config parameter. Value will be modified in step 4 if user has defined an other date + $form->end_date = $max_expiry_time; + + // Summary + $summary = '
    '; + foreach ($form->getChoices() as $i => $choice) { + /** @var Choice $choice */ + preg_match_all('/\[!\[(.*?)\]\((.*?)\)\]\((.*?)\)/', $choice->getName(), $md_a_img); // Markdown [![alt](src)](href) + preg_match_all('/!\[(.*?)\]\((.*?)\)/', $choice->getName(), $md_img); // Markdown ![alt](src) + 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) + $li_subject_text = (isset($md_a_img[1][0]) && $md_a_img[1][0] !== '') ? stripslashes($md_a_img[1][0]) : __('Generic', 'Choice') . ' ' . ($i + 1); + $li_subject_html = '' . $li_subject_text . ''; + } elseif (isset($md_img[2][0]) && $md_img[2][0] !== '') { // ![alt](src) + $li_subject_text = (isset($md_img[1][0]) && $md_img[1][0] !== '') ? stripslashes($md_img[1][0]) : __('Generic', 'Choice') . ' ' . ($i + 1); + $li_subject_html = '' . $li_subject_text . ''; + } elseif (isset($md_a[2][0]) && $md_a[2][0] !== '') { // [text](href) + $li_subject_text = (isset($md_a[1][0]) && $md_a[1][0] !== '') ? stripslashes($md_a[1][0]) : __('Generic', 'Choice') . ' ' . ($i + 1); + $li_subject_html = '' . $li_subject_text . ''; + } else { // text only + $li_subject_text = stripslashes($choice->getName()); + $li_subject_html = $li_subject_text; + } + + $summary .= '
  1. ' . $li_subject_html . '
  2. ' . "\n"; + } + $summary .= '
'; + + $end_date_str = utf8_encode(strftime($date_format['txt_date'], $max_expiry_time)); //textual date + + $_SESSION['form'] = serialize($form); + + $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_poll_step_3.tpl'); + exit; + case 4: // Step 4 : Data prepare before insert in DB $enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]); if (!empty($enddate)) { @@ -113,151 +194,19 @@ if (empty($form->title) || empty($form->admin_name) || (($config['use_smtp']) ? // Clean Form data in $_SESSION unset($_SESSION['form']); - $purgeService->repeatedCleanings(); + // Delete old polls + $purgeService->purgeOldPolls(); // creation message $sessionService->set("Framadate", "messagePollCreated", TRUE); - // Redirect to poll administration header('Location:' . Utils::getUrlSondage($admin_poll_id, true)); exit; - } // Step 3/4 : Confirm poll creation and choose a removal date - else if (isset($_POST['fin_sondage_autre'])) { - // Store choices in $_SESSION - if (isset($_POST['choices'])) { - $form->clearChoices(); - foreach ($_POST['choices'] as $c) { - if (!empty($c)) { - $c = strip_tags($c); - $choice = new Choice($c); - $form->addChoice($choice); - } - } - } - - // Expiration date is initialised with config parameter. Value will be modified in step 4 if user has defined an other date - $form->end_date = $max_expiry_time; - - // Summary - $summary = '
    '; - foreach ($form->getChoices() as $i=>$choice) { - preg_match_all('/\[!\[(.*?)\]\((.*?)\)\]\((.*?)\)/', $choice->getName(), $md_a_img); // Markdown [![alt](src)](href) - preg_match_all('/!\[(.*?)\]\((.*?)\)/', $choice->getName(), $md_img); // Markdown ![alt](src) - 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) - $li_subject_text = (isset($md_a_img[1][0]) && $md_a_img[1][0] !== '') ? stripslashes($md_a_img[1][0]) : __('Generic', 'Choice') . ' ' . ($i + 1); - $li_subject_html = '' . $li_subject_text . ''; - } elseif (isset($md_img[2][0]) && $md_img[2][0] !== '') { // ![alt](src) - $li_subject_text = (isset($md_img[1][0]) && $md_img[1][0] !== '') ? stripslashes($md_img[1][0]) : __('Generic', 'Choice') . ' ' . ($i + 1); - $li_subject_html = '' . $li_subject_text . ''; - } elseif (isset($md_a[2][0]) && $md_a[2][0] !== '') { // [text](href) - $li_subject_text = (isset($md_a[1][0]) && $md_a[1][0] !== '') ? stripslashes($md_a[1][0]) : __('Generic', 'Choice') . ' ' . ($i + 1); - $li_subject_html = '' . $li_subject_text . ''; - } else { // text only - $li_subject_text = stripslashes($choice->getName()); - $li_subject_html = $li_subject_text; - } - - $summary .= '
  1. ' . $li_subject_html . '
  2. ' . "\n"; - } - $summary .= '
'; - - $end_date_str = utf8_encode(strftime($date_format['txt_date'], $max_expiry_time)); //textual date - - $_SESSION['form'] = serialize($form); - - $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'); - - // Step 2/4 : Select choices of the poll - } else { - Utils::print_header(__('Step 2 classic', 'Poll subjects (2 on 3)')); - bandeau_titre(__('Step 2 classic', 'Poll subjects (2 on 3)')); - - echo ' -
-
-
'; - echo ' -
-

' . __('Step 2 classic', 'To make a generic poll you need to propose at least two choices between differents subjects.') . '

-

' . __('Step 2 classic', 'You can add or remove additional choices with the buttons') . ' ' . __('Generic', 'Remove') . ' ' . __('Generic', 'Add') . '

'; - if ($config['user_can_add_img_or_link']) { - echo '

' . __('Step 2 classic', 'It\'s possible to propose links or images by using') . ' ' . __('Step 2 classic', 'the Markdown syntax') . '.

'; - } - echo '
' . "\n"; - - // Fields choices : 5 by default - $choices = $form->getChoices(); - $nb_choices = max(count($choices), 5); - for ($i = 0; $i < $nb_choices; $i++) { - $choice = isset($choices[$i]) ? $choices[$i] : new Choice(); - echo ' -
- -
- '; - if ($config['user_can_add_img_or_link']) { - echo ' '; - } - echo ' -
-
' . "\n"; - } - - echo ' -
-
- - -
-
-
- ' . __('Generic', 'Back') . ' - -
-
-
- -
- - - - ' . "\n"; - - bandeau_pied(); - } + case 1: // Step 1/4 : error if $_SESSION from info_sondage are not valid + default: + $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; +} diff --git a/create_date_poll.php b/create_date_poll.php index b27f7a0..42fef6c 100644 --- a/create_date_poll.php +++ b/create_date_poll.php @@ -51,7 +51,7 @@ if (!isset($form->format)) { $form->format = 'D'; } // If we come from another format, we need to clear choices -if (isset($form->format) && $form->format !== 'D') { +if ($form->format !== 'D') { $form->format = 'D'; $form->clearChoices(); } @@ -67,13 +67,6 @@ if (!isset($form->title) || !isset($form->admin_name) || ($config['use_smtp'] && } 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, or your session has expired.')); - $smarty->display('error.tpl'); - exit; - case 2: // Step 2/4 : Select dates of the poll @@ -186,7 +179,7 @@ switch ($step) { $smarty->assign('default_poll_duration', $config['default_poll_duration']); $smarty->assign('use_smtp', $config['use_smtp']); - $smarty->display('create_classic_poll_step3.tpl'); + $smarty->display('create_poll_step_3.tpl'); exit; case 4: @@ -251,4 +244,12 @@ switch ($step) { // Redirect to poll administration header('Location:' . Utils::getUrlSondage($admin_poll_id, true)); exit; + + 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; } diff --git a/js/app/classic_poll.js b/js/app/classic_poll.js index a091f4a..7a22066 100644 --- a/js/app/classic_poll.js +++ b/js/app/classic_poll.js @@ -15,75 +15,74 @@ * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) */ -(function () { - // 2 choices filled and you can submit - var submitChoicesAvalaible = function () { +$(document).ready(function () { + $(document.formulaire).on('submit', function (e) { + if (!isSubmitChoicesAvalaible()) { + e.preventDefault(); + e.stopPropagation(); + } + }); + + var $next = $('button[name="fin_sondage_autre"]'); + var $removeAChoice = $('#remove-a-choice'); + var $addAChoice = $('#add-a-choice'); + + var updateButtonState = function() { + var $choiceFields = $('.choice-field'); + $removeAChoice.prop('disabled', function() { return $choiceFields.length <= 2; }); + $next.prop('disabled', !isSubmitChoicesAvalaible()); + } + + var isSubmitChoicesAvalaible = function () { + return (countFilledChoices() >= 2); + }; + + var countFilledChoices = function() { var nb_filled_choices = 0; $('.choice-field input').each(function () { if ($(this).val() != '') { nb_filled_choices++; } }); - if (nb_filled_choices >= 1) { - $('button[name="fin_sondage_autre"]').removeClass('disabled'); - return true; - } else { - $('button[name="fin_sondage_autre"]').addClass('disabled'); - return false; - } + return nb_filled_choices; }; - // Handle form submission - $(document.formulaire).on('submit', function (e) { - if (!submitChoicesAvalaible()) { - e.preventDefault(); - e.stopPropagation(); - } + $removeAChoice.on('click', function () { + $('.choice-field:last').remove(); + updateButtonState(); }); - // Button "Add a choice" - - $('#add-a-choice').on('click', function () { - var nb_choices = $('.choice-field').length; - var last_choice = $('.choice-field:last'); - + $addAChoice.on('click', function () { + var $choiceFields = $('.choice-field'); + var nb_choices = $choiceFields.length; + var last_choice = $choiceFields.last(); + var new_choice = last_choice.html(); - + // label var last_choice_label = last_choice.children('label').text(); var choice_text = last_choice_label.substring(0, last_choice_label.indexOf(' ')); - + // for and id var re_id_choice = new RegExp('"choice' + (nb_choices - 1) + '"', 'g'); - + var new_choice_html = new_choice.replace(re_id_choice, '"choice' + nb_choices + '"') .replace(last_choice_label, choice_text + ' ' + (nb_choices + 1)) .replace(/value="(.*?)"/g, 'value=""'); - - last_choice.after('
' + new_choice_html + '
'); + + last_choice.after('
' + new_choice_html + '
'); $('#choice' + nb_choices).focus(); - $('#remove-a-choice').removeClass('disabled'); - - }); - - // Button "Remove a choice" - - $('#remove-a-choice').on('click', function () { - $('.choice-field:last').remove(); - var nb_choices = $('.choice-field').length; - $('#choice' + (nb_choices - 1)).focus(); - if (nb_choices == 1) { - $('#remove-a-choice').addClass('disabled'); - } - submitChoicesAvalaible(); + updateButtonState() + }); $(document).on('keyup, change', '.choice-field input', function () { - submitChoicesAvalaible(); + updateButtonState(); }); - submitChoicesAvalaible(); + + updateButtonState(); // Button to build markdown from: link + image-url + text @@ -92,7 +91,7 @@ var md_img = $('#md-img'); var md_val = $('#md-a'); - $(document).on('click', '.md-a-img', function () { + $('.md-a-img').on('click' , function () { md_a_imgModal.modal('show'); md_a_imgModal.find('.btn-primary').attr('value', $(this).prev().attr('id')); $('#md-a-imgModalLabel').text($(this).attr('title')); @@ -116,6 +115,6 @@ md_img.val(''); md_val.val(''); md_text.val(''); - submitChoicesAvalaible(); + updateButtonState(); }); -})(); \ No newline at end of file +}); diff --git a/tpl/create_classic_poll_step_2.tpl b/tpl/create_classic_poll_step_2.tpl new file mode 100644 index 0000000..bdb5142 --- /dev/null +++ b/tpl/create_classic_poll_step_2.tpl @@ -0,0 +1,77 @@ +{extends file='page.tpl'} +{block name="header"} + + + + +{/block} + +{block name=main} + +
+
+
+
+

{__('Step 2 classic', 'To make a generic poll you need to propose at least two choices between differents subjects.')}

+

{__('Step 2 classic', 'You can add or remove additional choices with the buttons')} {__('Generic', 'Remove')} {__('Generic', 'Add')}

+ {if ($allowMarkdown)} +

{__('Step 2 classic', 'It\'s possible to propose links or images by using')}{__('Step 2 classic', 'the Markdown syntax')}.

+ {/if} +
+ {foreach $choices as $i=>$choice} +
+ +
+ + {if ($allowMarkdown) } +   + {/if} +
+
+ {/foreach} +
+
+ + +
+
+
+ {__('Generic', 'Back')} + +
+
+
+ + +
+ +{/block} + diff --git a/tpl/create_classic_poll_step3.tpl b/tpl/create_poll_step_3.tpl similarity index 100% rename from tpl/create_classic_poll_step3.tpl rename to tpl/create_poll_step_3.tpl