From c47c46b281dbb45133d34b932ce7d2053a81f659 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lars=20Bergl=C3=A9n?= <lars.berglen@ub.gu.se>
Date: Mon, 18 Dec 2017 13:24:24 +0100
Subject: [PATCH] Syspref for auto approval of patron profile changes

https://bugs.koha-community.org/show_bug.cgi?id=20057
---
 .../data/mysql/atomicupdate/AutoApprovePatronProfileSettings.sql | 2 ++
 .../intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref | 6 ++++++
 opac/opac-memberentry.pl                                         | 9 +++++++++
 3 files changed, 17 insertions(+)
 create mode 100644 installer/data/mysql/atomicupdate/AutoApprovePatronProfileSettings.sql

diff --git a/installer/data/mysql/atomicupdate/AutoApprovePatronProfileSettings.sql b/installer/data/mysql/atomicupdate/AutoApprovePatronProfileSettings.sql
new file mode 100644
index 0000000..6b5acc9
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/AutoApprovePatronProfileSettings.sql
@@ -0,0 +1,2 @@
+INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type`) VALUES
+('AutoApprovePatronProfileSettings', '0', '', 'Automatically approve Patron profile changes.', 'YesNo');
\ No newline at end of file
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 5591aa1..9ed1b50 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
@@ -200,6 +200,12 @@ Patrons:
                no: "Don't"
          - track last patron activity.
          - Everytime a patron will connect, the borrowers.lastseen will be updated with the current time.
+     -
+         - pref: AutoApprovePatronProfileSettings
+           choices:
+               yes: Enable
+               no: Disable
+         - "Automatically approve Patron profile changes."
     "Norwegian patron database":
      -
          - pref: NorwegianPatronDBEnable
diff --git a/opac/opac-memberentry.pl b/opac/opac-memberentry.pl
index 12eebbb..6560b63 100755
--- a/opac/opac-memberentry.pl
+++ b/opac/opac-memberentry.pl
@@ -25,6 +25,7 @@ use String::Random qw( random_string );
 
 use C4::Auth;
 use C4::Output;
+use C4::Context;
 use C4::Members;
 use C4::Members::Attributes qw( GetBorrowerAttributes );
 use C4::Form::MessagingPreferences;
@@ -281,6 +282,14 @@ elsif ( $action eq 'update' ) {
             }
 
             my $m = Koha::Patron::Modification->new( \%borrower_changes )->store();
+            #Automatically approve patron profile changes if set in syspref
+
+            if (C4::Context->preference('AutoApprovePatronProfileSettings')) {
+                # Need to get the object from database, otherwise it is not complete enough to allow deletion
+                # when approval has been performed.
+                my $tmp_m = Koha::Patron::Modifications->find({borrowernumber => $borrowernumber});
+                $tmp_m->approve() if $tmp_m;
+            }
 
             my $patron = Koha::Patrons->find( $borrowernumber );
             $template->param( borrower => $patron->unblessed );
-- 
2.7.4