Bug 14659: Allow patrons to enter card number and patron category on OPAC registratio...
authorJesse Weaver <pianohacker@gmail.com>
Mon, 31 Aug 2015 23:45:16 +0000 (17:45 -0600)
committerBrendan Gallagher <brendan@bywatersolutions.com>
Wed, 2 Mar 2016 04:29:38 +0000 (04:29 +0000)
Test plan:
  1. Open OPAC self-registration page while logged out.
  2. Note that cardnumber and categorycode are not shown.
  3. Remove cardnumber and categorycode from
     PatronSelfRegistrationBorrowerUnwantedField.
  4. Enable autoMemberNum.
  5. Reload self-registration page, note that categorycode now shows.
  6. Disable autoMemberNum.
  7. Reload self-registration page, note that cardnumber now shows.
  8. Try saving a patron with an existing cardnumber; this should fail
     and explain why.
  9. Set CardnumberLength, and verify that those length restrictions are
     enforced.
 10. Verify that patron can be created with custom categorycode and
     cardnumber.

Signed-off-by: Michael Sauers <msauers@dospace.org>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com

C4/Members.pm
Koha/Template/Plugin/Categories.pm
installer/data/mysql/atomicupdate/bug_14659-add_cardnumber_categorycode_PatronSelfRegistrationBorrowerUnwantedField_syspref.sql [new file with mode: 0644]
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-memberentry.tt
opac/opac-memberentry.pl

index 8635322..98e9fc5 100644 (file)
@@ -2414,7 +2414,7 @@ sub GetBorrowersWithEmail {
 sub AddMember_Opac {
     my ( %borrower ) = @_;
 
-    $borrower{'categorycode'} = C4::Context->preference('PatronSelfRegistrationDefaultCategory');
+    $borrower{'categorycode'} //= C4::Context->preference('PatronSelfRegistrationDefaultCategory');
     if (not defined $borrower{'password'}){
         my $sr = new String::Random;
         $sr->{'A'} = [ 'A'..'Z', 'a'..'z' ];
@@ -2422,7 +2422,7 @@ sub AddMember_Opac {
         $borrower{'password'} = $password;
     }
 
-    $borrower{'cardnumber'} = fixup_cardnumber();
+    $borrower{'cardnumber'} = fixup_cardnumber( $borrower{'cardnumber'} );
 
     my $borrowernumber = AddMember(%borrower);
 
index 86630ca..fe8b0d7 100644 (file)
@@ -21,6 +21,16 @@ use Template::Plugin;
 use base qw( Template::Plugin );
 
 use C4::Category;
+use Koha::Database;
+
+sub GetName {
+    my ( $self, $categorycode ) = @_;
+
+    my $schema = Koha::Database->new->schema;
+    return $schema->resultset( 'Category' )->search( {
+        categorycode => $categorycode,
+    } )->get_column( 'description' )->next // '';
+}
 
 sub all {
     my ( $self, $params ) = @_;
@@ -56,6 +66,11 @@ Koha::Template::Plugin::Categories - TT Plugin for categories
 In a template, you can get the all categories with
 the following TT code: [% Categories.all() %]
 
+=head2 GetName
+
+In a template, you can get the name of a patron category using
+[% Categories.GetName( categorycode ) %].
+
 =head1 AUTHOR
 
 Jonathan Druart <jonathan.druart@biblibre.com>
diff --git a/installer/data/mysql/atomicupdate/bug_14659-add_cardnumber_categorycode_PatronSelfRegistrationBorrowerUnwantedField_syspref.sql b/installer/data/mysql/atomicupdate/bug_14659-add_cardnumber_categorycode_PatronSelfRegistrationBorrowerUnwantedField_syspref.sql
new file mode 100644 (file)
index 0000000..47f2515
--- /dev/null
@@ -0,0 +1,2 @@
+UPDATE systempreferences SET value = CONCAT_WS('|', IF(value = '', NULL, value), 'cardnumber') WHERE variable = 'PatronSelfRegistrationBorrowerUnwantedField' AND value NOT LIKE '%cardnumber%';
+UPDATE systempreferences SET value = CONCAT_WS('|', IF(value = '', NULL, value), 'categorycode') WHERE variable = 'PatronSelfRegistrationBorrowerUnwantedField' AND value NOT LIKE '%categorycode%';
index 0ea468d..a3d152b 100644 (file)
@@ -1,3 +1,4 @@
+[% USE Categories %]
 [% USE Koha %]
 [% USE KohaDates %]
 [% SET userupdateview = 1 %]
@@ -53,7 +54,7 @@
                     <div class="alert">You have not filled out all required fields. Please fill in all missing fields and resubmit.</div>
                 [% END %]
 
-                [%  IF invalid_form_fields %]
+                [% IF invalid_form_fields %]
                     <div class="alert alert-error"><strong>The following fields contain invalid information:</strong>
                         <ul>
                             [% FOREACH field IN invalid_form_fields %]
                     </div>
                 [% END %]
 
+                [% IF cardnumber_wrong_length || cardnumber_already_exists %]
+                    <div class="alert alert-error">
+                        [% IF cardnumber_wrong_length %]
+                        <strong>The entered <a href="#borrower_cardnumber">card number</a> is the wrong length.</strong>
+                        [% ELSIF cardnumber_already_exists %]
+                        <strong>The entered <a href="#borrower_cardnumber">card number</a> is already in use.</strong>
+                        [% END %]
+                        Please correct this and resubmit.
+                    </div>
+                [% END %]
+
                 [% IF failed_captcha %]
                     <div class="alert">You typed in the wrong characters in the box before submitting. Please try again.</div>
                 [% END %]
 
                         <legend id="library_legend">Library</legend>
                             <ol>
-                                [% UNLESS hidden.defined('cardnumber') %]
+                                [% UNLESS hidden.defined('cardnumber') || Koha.Preference('autoMemberNum') %]
                                 <li>
-                                    <label>Card number:</label>
-                                    [% borrower.cardnumber %]
+                                    [% IF mandatory.defined('cardnumber') %]
+                                        <label for="borrower_cardnumber" class="required">
+                                    [% ELSE %]
+                                        <label for="borrower_cardnumber">
+                                    [% END %]
+                                    Library card number:
+                                    </label>
+                                    [% IF borrower %]
+                                        <input type="text" id="borrower_cardnumber" name="borrower_cardnumber" value="[% borrower.cardnumber %]" />
+                                        [% IF mandatory.defined('cardnumber') %]<span class="required">Required</span>[% END %]
+                                    [% ELSE %]
+                                        [% borrower.cardnumber %]
+                                    [% END %]
                                 </li>
                                 [% END %]
 
                                         </select>
                                     </li>
                                 [% END %]
+
+                                [% UNLESS hidden.defined('categorycode') %]
+                                    <li>
+                                        <label for="borrower_categorycode">
+                                        Category:</label>
+
+                                        [% IF borrower %]
+                                            <select id="borrower_categorycode" name="borrower_categorycode">
+                                                [% FOREACH c IN Categories.all() %]
+                                                    [% IF c.categorycode == Koha.Preference('PatronSelfRegistrationDefaultCategory') %]
+                                                        <option value="[% c.categorycode %]" selected="selected">[% c.description %]</option>
+                                                    [% ELSE %]
+                                                        <option value="[% c.categorycode %]">[% c.description %]</option>
+                                                    [% END %]
+                                                [% END %]
+                                            </select>
+                                        [% ELSE %]
+                                            [% Categories.GetName( borrower.categorycode ) %]
+                                        [% END %]
+                                    </li>
+                                [% END %]
                             </ol>
                         </fieldset>
                 [% END # / defined 'branchcode' %]
index 46c91f9..b7dac77 100755 (executable)
@@ -80,8 +80,20 @@ if ( $action eq 'create' ) {
     my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
     my $invalidformfields = CheckForInvalidFields(\%borrower);
     delete $borrower{'password2'};
+    my $cardnumber_error_code;
+    if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
+        # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
+        # spurious length warning.
+        $cardnumber_error_code = checkcardnumber( $borrower{cardnumber}, $borrower{borrowernumber} );
+    }
+
+    if ( @empty_mandatory_fields || @$invalidformfields || $cardnumber_error_code ) {
+        if ( $cardnumber_error_code == 1 ) {
+            $template->param( cardnumber_already_exists => 1 );
+        } elsif ( $cardnumber_error_code == 2 ) {
+            $template->param( cardnumber_wrong_length => 1 );
+        }
 
-    if (@empty_mandatory_fields || @$invalidformfields) {
         $template->param(
             empty_mandatory_fields => \@empty_mandatory_fields,
             invalid_form_fields    => $invalidformfields,