Bug 7534: (QA follow-up) Use Koha.Preference template plugin to fetch the pref's...
[koha.git] / opac / opac-registration-verify.pl
index 20057b0..3748f5c 100755 (executable)
@@ -23,6 +23,8 @@ use C4::Auth;
 use C4::Output;
 use C4::Members;
 use C4::Form::MessagingPreferences;
+use Koha::AuthUtils;
+use Koha::Patrons;
 use Koha::Patron::Modifications;
 
 my $cgi = new CGI;
@@ -37,7 +39,15 @@ my $token = $cgi->param('token');
 my $m = Koha::Patron::Modifications->find( { verification_token => $token } );
 
 my ( $template, $borrowernumber, $cookie );
-if ( $m ) {
+
+if (
+    $m # The token exists and the email is unique if requested
+    and not(
+            C4::Context->preference('PatronSelfRegistrationEmailMustBeUnique')
+        and Koha::Patrons->search( { email => $m->email } )->count
+    )
+  )
+{
     ( $template, $borrowernumber, $cookie ) = get_template_and_user(
         {
             template_name   => "opac-registration-confirmation.tt",
@@ -50,23 +60,18 @@ if ( $m ) {
     $template->param(
         OpacPasswordChange => C4::Context->preference('OpacPasswordChange') );
 
-    my $borrower = $m->unblessed();
-    # Avoid "Column 'column' cannot be null" errors
-    # FIXME should be done at the Koha::Object level
-    for my $key ( keys %$borrower ) {
-        delete $borrower->{$key} if not defined $borrower->{$key}
-    }
+    my $patron_attrs = $m->unblessed;
+    $patron_attrs->{password} ||= Koha::AuthUtils::generate_password;
 
-    my $password;
-    ( $borrowernumber, $password ) = AddMember_Opac(%$borrower);
+    $patron_attrs->{categorycode} ||= C4::Context->preference('PatronSelfRegistrationDefaultCategory');
+    my $patron = Koha::Patron->new( $patron_attrs )->store;
 
-    if ($borrowernumber) {
+    if ($patron) {
         $m->delete();
-        C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if C4::Context->preference('EnhancedMessagingPreferences');
+        C4::Form::MessagingPreferences::handle_form_action($cgi, { borrowernumber => $patron->borrowernumber }, $template, 1, C4::Context->preference('PatronSelfRegistrationDefaultCategory') ) if C4::Context->preference('EnhancedMessagingPreferences');
 
-        $template->param( password_cleartext => $password );
-        $template->param(
-            borrower => GetMember( borrowernumber => $borrowernumber ) );
+        $template->param( password_cleartext => $patron->plain_text_password );
+        $template->param( borrower => $patron->unblessed );
         $template->param(
             PatronSelfRegistrationAdditionalInstructions =>
               C4::Context->preference(