Fix Issue #35 : Complete date field when user fills only day+month

This commit is contained in:
Olivier PEREZ 2015-03-05 18:10:41 +01:00
parent 6bbe03758c
commit d6500a77d9
2 changed files with 48 additions and 11 deletions

View File

@ -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 '
<fieldset>
@ -243,13 +243,14 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) ||
<label class="sr-only" for="day'.$i.'">'. _('Day') .' '. ($i+1) .'</label>
<div class="input-group date col-xs-7">
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar text-info"></i></span>
<input type="text" class="form-control" id="day'.$i.'" title="'. _("Day") .' '. ($i+1) .'" data-date-format="'. _('dd/mm/yyyy') .'" aria-describedby="dateformat'.$i.'" name="days[]" value="'.$day_value.'" size="10" maxlength="10" placeholder="'. _("dd/mm/yyyy") .'" />
<input type="text" class="form-control" id="day'.$i.'" title="'. _('Day') .' '. ($i+1) .'" data-date-format="'. _('dd/mm/yyyy') .'" aria-describedby="dateformat'.$i.'" name="days[]" value="'.$day_value.'" size="10" maxlength="10" placeholder="'. _('dd/mm/yyyy') .'" />
</div>
<span id="dateformat'.$i.'" class="sr-only">'. _('(dd/mm/yyyy)') .'</span>
</legend>'."\n";
// Fields hours : 3 by default
for ($j=0;$j<max(count(isset($_SESSION['horaires'.$i]) ? $_SESSION['horaires'.$i] : 0),3);$j++) {
$moments = isset($_SESSION['horaires' . $i]) ? $_SESSION['horaires' . $i] : [];
for ($j = 0; $j < max(count($moments), 3); $j++) {
$hour_value = isset($_SESSION['horaires'.$i][$j]) ? $_SESSION['horaires'.$i][$j] : '';
echo '
<div class="col-sm-2">
@ -259,8 +260,8 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) ||
}
echo '
<div class="col-sm-2"><div class="btn-group btn-group-xs" style="margin-top: 5px;">
<button type="button" title="'. _("Remove an hour") .'" class="remove-an-hour btn btn-default"><span class="glyphicon glyphicon-minus text-info"></span><span class="sr-only">'. _("Remove an hour") .'</span></button>
<button type="button" title="'. _("Add an hour") .'" class="add-an-hour btn btn-default"><span class="glyphicon glyphicon-plus text-success"></span><span class="sr-only">'. _("Add an hour") .'</span></button>
<button type="button" title="'. _('Remove an hour') .'" class="remove-an-hour btn btn-default"><span class="glyphicon glyphicon-minus text-info"></span><span class="sr-only">'. _("Remove an hour") .'</span></button>
<button type="button" title="'. _('Add an hour') .'" class="add-an-hour btn btn-default"><span class="glyphicon glyphicon-plus text-success"></span><span class="sr-only">'. _("Add an hour") .'</span></button>
</div></div>
</div>
</fieldset>';
@ -284,7 +285,7 @@ if (!isset($_SESSION['form']->title) || !isset($_SESSION['form']->admin_name) ||
</ul>
</div>
<a class="btn btn-default" href="'.Utils::get_server_name().'infos_sondage.php?choix_sondage=date" title="'. _('Back to step 1') . '">'. _('Back') . '</a>
<button name="choixheures" value="'. _('Next') .'" type="submit" class="btn btn-success disabled" title="'. _('Go to step 3') . '">'. _("Next") .'</button>
<button name="choixheures" value="'. _('Next') .'" type="submit" class="btn btn-success disabled" title="'. _('Go to step 3') . '">'. _('Next') .'</button>
</div>
</div>
</div>

View File

@ -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);
});
});