From d6500a77d9179b80885db8c9ebfd8e7a78b099b0 Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Thu, 5 Mar 2015 18:10:41 +0100 Subject: [PATCH] Fix Issue #35 : Complete date field when user fills only day+month --- choix_date.php | 15 ++++++++------- js/app/date_poll.js | 44 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/choix_date.php b/choix_date.php index f58b314..81fb6ca 100644 --- a/choix_date.php +++ b/choix_date.php @@ -218,7 +218,7 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) || // Step 2/4 : Select dates of the poll } else { - Utils::print_header ( _('Poll dates (2 on 3)') ); + Utils::print_header(_('Poll dates (2 on 3)')); bandeau_titre(_('Poll dates (2 on 3)')); echo ' @@ -234,7 +234,7 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) || // Fields days : 3 by default $nb_days = (isset($_SESSION['totalchoixjour'])) ? count($_SESSION['totalchoixjour']) : 3; - for ($i=0;$i<$nb_days;$i++) { + for ($i = 0; $i < $nb_days; $i++) { $day_value = isset($_SESSION['totalchoixjour'][$i]) ? strftime('%d/%m/%Y', $_SESSION['totalchoixjour'][$i]) : ''; echo '
@@ -243,13 +243,14 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) ||
- +
'. _('(dd/mm/yyyy)') .' '."\n"; // Fields hours : 3 by default - for ($j=0;$j @@ -259,8 +260,8 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) || } echo '
- - + +
'; @@ -284,7 +285,7 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) || '. _('Back') . ' - + diff --git a/js/app/date_poll.js b/js/app/date_poll.js index 10a68f4..635b610 100644 --- a/js/app/date_poll.js +++ b/js/app/date_poll.js @@ -174,17 +174,53 @@ // Title update on hours and buttons -/+ hours + var leftPad = function(text, pad) { + return text ? pad.substring(0, pad.length - text.length) + text : text; + }; + $(document).on('change', '#selected-days legend input', function () { + // Complete field if needed + var val = $(this).val(); + var capture = /([0-9]+)(?:\/([0-9]+))?/.exec(val); + + if (capture) { + var inputDay = leftPad(capture[1], "00"); // 5->05, 15->15 + var inputMonth = leftPad(capture[2], "00"); // 3->03, 11->11 + var inputDate = null; + var now = new Date(); + + if (inputMonth) { + inputDate = new Date(now.getFullYear() + '-' + inputMonth + '-' + inputDay); + + // If new date is before now, add 1 year + if (inputDate < now) { + inputDate.setFullYear(now.getFullYear() + 1); + } + } else { + inputDate = new Date(now.getFullYear() + '-' + leftPad(""+(now.getMonth() + 1), "00") + '-' + inputDay); + + // If new date is before now, add 1 month + if (inputDate < now) { + inputDate.setMonth(now.getMonth() + 1); + } + + } + + $(this).val(inputDate.toLocaleFormat("%d/%m/%Y")); + } + + // Define title on hours fields using the value of the new date $selected_days.find('.hours').each(function () { $(this).attr('title', $(this).parents('fieldset').find('legend input').val() + ' - ' + $(this).attr('placeholder')); }); + // Define title on buttons that add/remove hours using the value of the new date $('#selected-days .add-an-hour, #selected-days .remove-an-hour').each(function () { - var old_title = $(this).attr('title'); + var title = $(this).attr('title'); - if (old_title.indexOf('-') > 0) { - old_title = old_title.substring(old_title.indexOf('-') + 2, old_title.length); + if (title.indexOf('-') > 0) { + title = title.substring(title.indexOf('-') + 2, title.length); } - $(this).attr('title', $(this).parents('fieldset').find('legend input').val() + ' - ' + old_title); + $(this).attr('title', $(this).parents('fieldset').find('legend input').val() + ' - ' + title); }); });