Bug 17501: Use Koha::Object in Koha::Upload::_delete
[koha.git] / members / nl-search.pl
blob29ec13e6cff57080051fa55766ed305b29b61217
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::Context;
41 use C4::Output;
42 use C4::Members;
43 use C4::Members::Attributes qw( SetBorrowerAttributes );
44 use C4::Utils::DataTables::Members;
45 use Koha::NorwegianPatronDB qw( NLCheckSysprefs NLSearch NLDecodePin NLGetFirstname NLGetSurname NLSync );
46 use Koha::Database;
47 use Koha::DateUtils;
48 use Koha::Patron::Categories;
50 my $cgi = CGI->new;
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",
57 query => $cgi,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => { borrowers => 1 },
61 debug => 1,
65 my $userenv = C4::Context->userenv;
67 # Check sysprefs
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;
72 exit 0;
75 if ( $op && $op eq 'search' ) {
77 # Get the string we are searching for
78 my $identifier = $cgi->param('q');
79 if ( $identifier ) {
80 # Local search
81 my $local_results = C4::Utils::DataTables::Members::search(
83 searchmember => $identifier,
84 dt_params => { iDisplayLength => -1 },
86 )->{patrons};
87 $template->param( 'local_result' => $local_results );
88 # Search NL, unless we got at least one hit and further searching is
89 # disabled
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;
96 $template->param(
97 'result' => $r,
98 'categories' => $categories,
100 } else {
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
110 my %borrower = (
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'),
136 # Add the new patron
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" );
155 } else {
156 $template->param( 'error' => 'COULD_NOT_ADD_PATRON' );
160 output_html_with_http_headers $cgi, $cookie, $template->output;
162 =head1 AUTHOR
164 Magnus Enger <digitalutvikling@gmail.com>
166 =cut