From 70bfc504ab4efc0f2d466c107c3ebd74f39b7d43 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Thu, 10 Dec 2015 16:33:53 +0100 Subject: [PATCH] Bug 15206 - CalculateAge in memberentry use C4::Members::GetAge() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tested all patches together, works as expected. Signed-off-by: Marc VĂ©ron --- .../prog/en/modules/members/memberentrygen.tt | 44 +++++++++--------- svc/members/age | 49 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 23 deletions(-) create mode 100755 svc/members/age diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt index 290bebe..6ca7570 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt @@ -145,36 +145,34 @@ return 0; } - function CalculateAge(dateofbirth) { - var today = new Date(); - var dob = new Date($("#dateofbirth").datepicker( 'getDate' )); - - var nowyear = today.getFullYear(); - var nowmonth = today.getMonth(); - var nowday = today.getDate(); + function CalculateAge() { + if (!$("#dateofbirth").datepicker( 'getDate' )) { + var elem = $("#dateofbirth").siblings(".hint").first().html(''); + return 1; + } - var birthyear = dob.getFullYear(); - var birthmonth = dob.getMonth(); - var birthday = dob.getDate(); + var dob = new Date($("#dateofbirth").datepicker( 'getDate' )); - var year = nowyear - birthyear; - var month = nowmonth - birthmonth; - var day = nowday - birthday; + var year = dob.getFullYear(); + var month = dob.getMonth(); + var day = dob.getDate(); - if(day < 0) { - month = parseInt(month) -1; - } + $.ajax({ + url : '/cgi-bin/koha/svc/members/age', + type : 'GET', + data: 'dateofbirth=' + year + '-' + month + '-' + day, + dataType: 'json', + success : function(json, statut){ + var age_string = "Age: "; + if (json.years) { age_string += json.years + " year(s) "; } + if (json.months) { age_string += json.months + " month(s) "; } + var elem = $("#dateofbirth").siblings(".hint").first().html(age_string); + }, + }); - if(month < 0) { - year = parseInt(year) -1; - month = 12 + month; - } var age_string = "Age: "; - if (year) { age_string += year + " year(s) "; } - if (month) { age_string += month + " month(s) "; } - var elem = $("#dateofbirth").siblings(".hint").first().html(age_string); } diff --git a/svc/members/age b/svc/members/age new file mode 100755 index 0000000..fb50baa --- /dev/null +++ b/svc/members/age @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +# Copyright 2012 BibLibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use CGI qw ( -utf8 ); + +use C4::Auth qw( check_cookie_auth ); +use JSON qw( to_json ); +use C4::Members qw( GetAge ); +binmode STDOUT, ":encoding(UTF-8)"; + +my $query = new CGI; + +my ( $status, $sessionID ) = check_cookie_auth( + $query->cookie('CGISESSID'), + { borrowers => '*' }, +); + +unless ($status eq "ok") { + print $query->header(-type => 'application/json', -status => '403 Forbidden'); + exit 0; +} + +my $dateofbirth = $query->param('dateofbirth'); +my ($age_year, $age_month) = GetAge($dateofbirth); + +print $query->header(-type => 'application/json'); + +print to_json({ + years => $age_year, + months => $age_month +}); + -- 1.7.10.4