From 141e9105beece8315efb86ce9b3cef89fff66778 Mon Sep 17 00:00:00 2001 From: chrosey Date: Fri, 20 Apr 2018 22:05:13 +0200 Subject: [PATCH] refactored create_classic_poll and put step_2 view into own tpl file --- create_classic_poll.php | 268 ++++++++++++----------------- tpl/create_classic_poll_step_2.tpl | 77 +++++++++ 2 files changed, 183 insertions(+), 162 deletions(-) create mode 100644 tpl/create_classic_poll_step_2.tpl diff --git a/create_classic_poll.php b/create_classic_poll.php index 8f5deba..3830305 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,107 @@ 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; +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; } - // 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 +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 +189,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/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} +