3 # Copyright 2013 Oslo Public Library
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 nl-search.pl - Script for searching the Norwegian national patron database
26 This script will search the Norwegian national patron database, and let staff
27 import patrons from the natial database into the local database.
29 In order to use this, a username/password from the Norwegian national database
30 of libraries ("Base Bibliotek") is needed. A special key is also needed, in
31 order to decrypt and encrypt PIN-codes/passwords.
33 See http://www.lanekortet.no/ for more information (in Norwegian).
43 use C4::Members::Attributes qw( SetBorrowerAttributes );
44 use C4::Utils::DataTables::Members;
45 use Koha::NorwegianPatronDB qw( NLCheckSysprefs NLSearch NLDecodePin NLGetFirstname NLGetSurname NLSync );
48 use Koha::Patron::Categories;
51 my $dbh = C4::Context->dbh;
52 my $op = $cgi->param('op');
54 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
56 template_name => "members/nl-search.tt",
60 flagsrequired => { borrowers => 1 },
65 my $userenv = C4::Context->userenv;
68 my $check_result = NLCheckSysprefs();
69 if ( $check_result->{'error'} == 1 ) {
70 $template->param( 'error' => $check_result );
71 output_html_with_http_headers $cgi, $cookie, $template->output;
75 if ( $op && $op eq 'search' ) {
77 # Get the string we are searching for
78 my $identifier = $cgi->param('q');
81 my $local_results = C4::Utils::DataTables::Members::search(
83 searchmember => $identifier,
84 dt_params => { iDisplayLength => -1 },
87 $template->param( 'local_result' => $local_results );
88 # Search NL, unless we got at least one hit and further searching is
90 if ( scalar @{ $local_results } == 0 || C4::Context->preference("NorwegianPatronDBSearchNLAfterLocalHit") == 1 ) {
91 # TODO Check the format of the identifier before searching NL
92 my $result = NLSearch( $identifier );
93 unless ($result->fault) {
94 my $r = $result->result();
95 my $categories = Koha::Patron::Categories->search_limited;
98 'categories' => $categories,
101 $template->param( 'error' => join ', ', $result->faultcode, $result->faultstring, $result->faultdetail );
104 $template->param( 'q' => $identifier );
107 } elsif ( $op && $op eq 'save' ) {
109 # This is where we map from fields in NL to fields in Koha
111 'surname' => NLGetSurname( $cgi->param('navn') ),
112 'firstname' => NLGetFirstname( $cgi->param('navn') ),
113 'sex' => scalar $cgi->param('kjonn'),
114 'dateofbirth' => scalar $cgi->param('fdato'),
115 'cardnumber' => scalar $cgi->param('lnr'),
116 'userid' => scalar $cgi->param('lnr'),
117 'address' => scalar $cgi->param('p_adresse1'),
118 'address2' => scalar $cgi->param('p_adresse2'),
119 'zipcode' => scalar $cgi->param('p_postnr'),
120 'city' => scalar $cgi->param('p_sted'),
121 'country' => scalar $cgi->param('p_land'),
122 'B_address' => scalar $cgi->param('m_adresse1'),
123 'B_address2' => scalar $cgi->param('m_adresse2'),
124 'B_zipcode' => scalar $cgi->param('m_postnr'),
125 'B_city' => scalar $cgi->param('m_sted'),
126 'B_country' => scalar $cgi->param('m_land'),
127 'password' => NLDecodePin( $cgi->param('pin') ),
128 'dateexpiry' => scalar $cgi->param('gyldig_til'),
129 'email' => scalar $cgi->param('epost'),
130 'mobile' => scalar $cgi->param('tlf_mobil'),
131 'phone' => scalar $cgi->param('tlf_hjemme'),
132 'phonepro' => scalar $cgi->param('tlf_jobb'),
133 'branchcode' => $userenv->{'branch'},
134 'categorycode' => scalar $cgi->param('categorycode'),
137 my $borrowernumber = &AddMember(%borrower);
138 if ( $borrowernumber ) {
139 # Add extended patron attributes
140 SetBorrowerAttributes($borrowernumber, [
141 { code => 'fnr', value => scalar $cgi->param('fnr_hash') },
142 ], 'no_branch_limit' );
143 # Override the default sync data created by AddMember
144 my $borrowersync = Koha::Database->new->schema->resultset('BorrowerSync')->find({
145 'synctype' => 'norwegianpatrondb',
146 'borrowernumber' => $borrowernumber,
148 $borrowersync->update({ 'syncstatus', 'synced' });
149 $borrowersync->update({ 'lastsync', $cgi->param('sist_endret') });
150 $borrowersync->update({ 'hashed_pin', $cgi->param('pin') });
151 # Try to sync in real time. If this fails it will be picked up by the cronjob
152 NLSync({ 'borrowernumber' => $borrowernumber });
153 # Redirect to the edit screen
154 print $cgi->redirect( "/cgi-bin/koha/members/memberentry.pl?op=modify&destination=circ&borrowernumber=$borrowernumber" );
156 $template->param( 'error' => 'COULD_NOT_ADD_PATRON' );
160 output_html_with_http_headers $cgi, $cookie, $template->output;
164 Magnus Enger <digitalutvikling@gmail.com>