From 1adcea231e974f7f218b9cc121225ddfbdf3b6bc Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 19 Apr 2013 09:58:02 +0200 Subject: [PATCH] Bug 10861: Add a check on cardnumber length Some libraries would like to add a check on the cardnumber length. This patch adds the ability to restrict the cardnumber to a specific length (strictly equal to XX, or length > XX or min < length < max). This restriction is checked on inserting/updating a patron or on importing patrons. This patch adds: - 1 new syspref CardnumberLength. 2 formats: a number or a range (xx,yy). - 1 new unit test file t/Members/checkcardnumber.t for the C4::Members::checkcardnumber routine. Test plan: 1/ Fill the pref CardnumberLength with '5,8' 2/ Create a new patron with an invalid cardnumber (123456789) 3/ Check that you cannot save 4/ With Firebug, replace the pattern attribute value (for the cardnumber input) with ".{5,10}" 5/ You are allowed to save but an error occurred. 6/ Try the same steps for update. 7/ Go to the import borrowers tool. 8/ Play with the import borrowers tool. We must test add/update patrons and the "record matching" field (cardnumber or a uniq patron attribute) --- C4/Members.pm | 43 ++++++++----- installer/data/mysql/updatedatabase.pl | 17 +++++ .../prog/en/modules/admin/preferences/patrons.pref | 4 ++ .../prog/en/modules/members/memberentrygen.tt | 34 +++++----- .../prog/en/modules/tools/import_borrowers.tt | 6 ++ members/memberentry.pl | 20 +++++- t/Members/checkcardnumber.t | 66 ++++++++++++++++++++ tools/import_borrowers.pl | 13 +++- 8 files changed, 173 insertions(+), 30 deletions(-) create mode 100644 t/Members/checkcardnumber.t diff --git a/C4/Members.pm b/C4/Members.pm index 7ac69df..213af9d 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -1327,25 +1327,40 @@ sub checkuniquemember { } sub checkcardnumber { - my ($cardnumber,$borrowernumber) = @_; + my ( $cardnumber, $borrowernumber ) = @_; + # If cardnumber is null, we assume they're allowed. - return 0 if !defined($cardnumber); + return 0 unless defined $cardnumber; + my $dbh = C4::Context->dbh; my $query = "SELECT * FROM borrowers WHERE cardnumber=?"; $query .= " AND borrowernumber <> ?" if ($borrowernumber); - my $sth = $dbh->prepare($query); - if ($borrowernumber) { - $sth->execute($cardnumber,$borrowernumber); - } else { - $sth->execute($cardnumber); - } - if (my $data= $sth->fetchrow_hashref()){ - return 1; - } - else { - return 0; + my $sth = $dbh->prepare($query); + $sth->execute( + $cardnumber, + ( $borrowernumber ? $borrowernumber : () ) + ); + + return 1 if $sth->fetchrow_hashref; + + if ( my $length = C4::Context->preference('CardnumberLength') ) { + # Is integer and length match + if ( + $length =~ m|^\d+$| + and length $cardnumber == $length + ) { + return 0 + } + # Else assuming it is a range + else { + my $qr = qr|^\d{$length}$|; + return 0 + if $cardnumber =~ $qr; + } + return 1 } -} + return 0; +} =head2 getzipnamecity (OUEST-PROVENCE) diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index ddcc455..efaeb4a 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -7104,6 +7104,23 @@ if ( CheckVersion($DBversion) ) { SetVersion($DBversion); } + + + + + + + +$DBversion = "3.13.00.XXX"; +if ( CheckVersion($DBversion) ) { + $dbh->do(" + INSERT INTO systempreferences (variable,value,options,explanation,type) + VALUES('CardnumberLength', '', '', 'Set a length for card numbers.', 'Free'); + "); + print "Upgrade to $DBversion done (Bug 10861: Add CardnumberLength syspref)\n"; + SetVersion ($DBversion); +} + =head1 FUNCTIONS =head2 TableExists($table) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref index 74d14af..a0c9dbf 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -139,3 +139,7 @@ Patrons: yes: Do no: "Don't" - enable the ability to upload and attach arbitrary files to a borrower record. + - + - Card numbers for patrons must be strictly equal to + - pref: CardnumberLength + - characters. The length can be a simple number or a range separated by a coma (xx,yy). 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 2cb1bb3..c92c8a7 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt @@ -152,9 +152,9 @@ [% IF ( ERROR_login_exist ) %]
  • Username/password already exists.
  • [% END %] - [% IF ( ERROR_cardnumber ) %] -
  • Cardnumber already in use.
  • - [% END %] + [% IF ( ERROR_cardnumber ) %] +
  • Cardnumber already in use or not in a good format.
  • + [% END %] [% IF ( ERROR_age_limitations ) %]
  • Patron's age is incorrect for their category. Ages allowed are [% age_low %]-[% age_high %].
  • @@ -852,17 +852,23 @@
    Library management
      - [% UNLESS nocardnumber %] -
    1. [% IF ( mandatorycardnumber ) %] -
    2. - [% END %] - [% UNLESS nobranchcode %] + [% UNLESS nocardnumber %] +
    3. + [% IF ( mandatorycardnumber ) %] +
    4. + [% END %] + [% UNLESS nobranchcode %]