Bug 13417: (qa follow-up) Rename the permission to delete_public_lists
[koha.git] / members / nl-search.pl
blob384b1fefa7b9f9167721456d104859fae1e5b79c
1 #!/usr/bin/perl
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>.
20 =head1 NAME
22 nl-search.pl - Script for searching the Norwegian national patron database
24 =head1 DESCRIPTION
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).
35 =cut
37 use Modern::Perl;
38 use CGI;
39 use C4::Auth;
40 use C4::Category;
41 use C4::Context;
42 use C4::Output;
43 use C4::Members;
44 use C4::Members::Attributes qw( SetBorrowerAttributes );
45 use Koha::NorwegianPatronDB qw( NLCheckSysprefs NLSearch NLDecodePin NLGetFirstname NLGetSurname NLSync );
46 use Koha::Database;
47 use Koha::DateUtils;
49 my $cgi = CGI->new;
50 my $dbh = C4::Context->dbh;
51 my $op = $cgi->param('op');
53 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
55 template_name => "members/nl-search.tt",
56 query => $cgi,
57 type => "intranet",
58 authnotrequired => 0,
59 flagsrequired => { borrowers => 1 },
60 debug => 1,
64 my $userenv = C4::Context->userenv;
66 # Check sysprefs
67 my $check_result = NLCheckSysprefs();
68 if ( $check_result->{'error'} == 1 ) {
69 $template->param( 'error' => $check_result );
70 output_html_with_http_headers $cgi, $cookie, $template->output;
71 exit 0;
74 if ( $op && $op eq 'search' ) {
76 # Get the string we are searching for
77 my $identifier = $cgi->param('q');
78 if ( $identifier ) {
79 # Local search
80 my $local_results = Search( $identifier );
81 $template->param( 'local_result' => $local_results );
82 # Search NL, unless we got at least one hit and further searching is
83 # disabled
84 if ( scalar @{ $local_results } == 0 || C4::Context->preference("NorwegianPatronDBSearchNLAfterLocalHit") == 1 ) {
85 # TODO Check the format of the identifier before searching NL
86 my $result = NLSearch( $identifier );
87 unless ($result->fault) {
88 my $r = $result->result();
89 # Send the data to the template
90 my @categories = C4::Category->all;
91 $template->param(
92 'result' => $r,
93 'categories' => \@categories,
95 } else {
96 $template->param( 'error' => join ', ', $result->faultcode, $result->faultstring, $result->faultdetail );
99 $template->param( 'q' => $identifier );
102 } elsif ( $op && $op eq 'save' ) {
104 # This is where we map from fields in NL to fields in Koha
105 my %borrower = (
106 'surname' => NLGetSurname( $cgi->param('navn') ),
107 'firstname' => NLGetFirstname( $cgi->param('navn') ),
108 'sex' => $cgi->param('kjonn'),
109 'dateofbirth' => $cgi->param('fdato'),
110 'cardnumber' => $cgi->param('lnr'),
111 'userid' => $cgi->param('lnr'),
112 'address' => $cgi->param('p_adresse1'),
113 'address2' => $cgi->param('p_adresse2'),
114 'zipcode' => $cgi->param('p_postnr'),
115 'city' => $cgi->param('p_sted'),
116 'country' => $cgi->param('p_land'),
117 'B_address' => $cgi->param('m_adresse1'),
118 'B_address2' => $cgi->param('m_adresse2'),
119 'B_zipcode' => $cgi->param('m_postnr'),
120 'B_city' => $cgi->param('m_sted'),
121 'B_country' => $cgi->param('m_land'),
122 'password' => NLDecodePin( $cgi->param('pin') ),
123 'dateexpiry' => $cgi->param('gyldig_til'),
124 'email' => $cgi->param('epost'),
125 'mobile' => $cgi->param('tlf_mobil'),
126 'phone' => $cgi->param('tlf_hjemme'),
127 'phonepro' => $cgi->param('tlf_jobb'),
128 'branchcode' => $userenv->{'branch'},
129 'categorycode' => $cgi->param('categorycode'),
131 # Add the new patron
132 my $borrowernumber = &AddMember(%borrower);
133 if ( $borrowernumber ) {
134 # Add extended patron attributes
135 SetBorrowerAttributes($borrowernumber, [
136 { code => 'fnr', value => $cgi->param('fnr_hash') },
137 ] );
138 # Override the default sync data created by AddMember
139 my $borrowersync = Koha::Database->new->schema->resultset('BorrowerSync')->find({
140 'synctype' => 'norwegianpatrondb',
141 'borrowernumber' => $borrowernumber,
143 $borrowersync->update({ 'syncstatus', 'synced' });
144 $borrowersync->update({ 'lastsync', $cgi->param('sist_endret') });
145 $borrowersync->update({ 'hashed_pin', $cgi->param('pin') });
146 # Try to sync in real time. If this fails it will be picked up by the cronjob
147 NLSync({ 'borrowernumber' => $borrowernumber });
148 # Redirect to the edit screen
149 print $cgi->redirect( "/cgi-bin/koha/members/memberentry.pl?op=modify&destination=circ&borrowernumber=$borrowernumber" );
150 } else {
151 $template->param( 'error' => 'COULD_NOT_ADD_PATRON' );
155 output_html_with_http_headers $cgi, $cookie, $template->output;
157 =head1 AUTHOR
159 Magnus Enger <digitalutvikling@gmail.com>
161 =cut