Bug 7067 [Follow-up: templates] allow patron self registration via the opac
[koha.git] / Koha / Borrower / Modifications.pm
1 package Koha::Borrower::Modifications;
2
3 # Copyright 2012 ByWater Solutions
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 =head1 NAME
20
21 C4::Borrowers::Modifications
22
23 =cut
24
25 use Modern::Perl;
26
27 use C4::Context;
28 use C4::Debug;
29 use C4::SQLHelper qw(InsertInTable UpdateInTable);
30
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32
33 BEGIN {
34
35     # set the version for version checking
36     $VERSION = 0.01;
37     require Exporter;
38     @ISA    = qw(Exporter);
39     @EXPORT = qw(
40
41     );
42
43     my $debug = C4::Context->preference("DebugLevel");
44 }
45
46 sub new {
47     my ( $class, %args ) = @_;
48     my $self = bless( {}, $class );
49
50     $self->{'verification_token'} = $args{'verification_token'};
51     $self->{'borrowernumber'}     = $args{'borrowernumber'};
52
53     return $self;
54 }
55
56 =head AddModifications
57
58 Koha::Borrower::Modifications->AddModifications( %data );
59
60 Adds or updates modifications for a borrower.
61
62 Requires either the key borrowernumber, or verification_token
63 to be part of the passed in hash.
64
65 =cut
66
67 sub AddModifications {
68     my ( $self, %data ) = @_;
69
70     if ( $self->{'borrowernumber'} ) {
71         delete $data{'borrowernumber'};
72
73         if ( keys %data ) {
74             $data{'borrowernumber'} = $self->{'borrowernumber'};
75             my $dbh = C4::Context->dbh;
76
77             my $query = "
78                 SELECT COUNT(*) AS count
79                 FROM borrower_modifications
80                 WHERE borrowernumber = ?
81             ";
82
83             my $sth = $dbh->prepare($query);
84             $sth->execute( $self->{'borrowernumber'} );
85             my $result = $sth->fetchrow_hashref();
86
87             if ( $result->{'count'} ) {
88                 $data{'verification_token'} = q{};
89                 return UpdateInTable( "borrower_modifications", \%data );
90             }
91             else {
92                 return InsertInTable( "borrower_modifications", \%data );
93             }
94         }
95
96     }
97     elsif ( $self->{'verification_token'} ) {
98         delete $data{'borrowernumber'};
99         $data{'verification_token'} = $self->{'verification_token'};
100
101         return InsertInTable( "borrower_modifications", \%data );
102     }
103     else {
104         return;
105     }
106 }
107
108 =head Verify
109
110 $verified = Koha::Borrower::Modifications->Verify( $verification_token );
111
112 Returns true if the passed in token is valid.
113
114 =cut
115
116 sub Verify {
117     my ( $self, $verification_token ) = @_;
118
119     if ( ref($self) ) {
120         $verification_token =
121           ($verification_token)
122           ? $verification_token
123           : $self->{'verification_token'};
124     }
125
126     my $dbh   = C4::Context->dbh;
127     my $query = "
128         SELECT COUNT(*) AS count
129         FROM borrower_modifications
130         WHERE verification_token = ?
131     ";
132     my $sth = $dbh->prepare($query);
133     $sth->execute($verification_token);
134     my $result = $sth->fetchrow_hashref();
135
136     return $result->{'count'};
137 }
138
139 =head GetPendingModificationsCount
140
141 $count = Koha::Borrower::Modifications->GetPendingModificationsCount();
142
143 Returns the number of pending modifications for existing borrowers.
144 =cut
145
146 sub GetPendingModificationsCount {
147     my ( $self, $branchcode ) = @_;
148
149     my $dbh   = C4::Context->dbh;
150     my $query = "
151         SELECT COUNT(*) AS count
152         FROM borrower_modifications, borrowers
153         WHERE borrower_modifications.borrowernumber > 0
154         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
155     ";
156
157     my @params;
158     if ($branchcode) {
159         $query .= " AND borrowers.branchcode = ? ";
160         push( @params, $branchcode );
161     }
162
163     my $sth = $dbh->prepare($query);
164     $sth->execute(@params);
165     my $result = $sth->fetchrow_hashref();
166
167     return $result->{'count'};
168 }
169
170 =head GetPendingModifications
171
172 $arrayref = Koha::Borrower::Modifications->GetPendingModifications();
173
174 Returns an arrayref of hashrefs for all pending modifications for existing borrowers.
175
176 =cut
177
178 sub GetPendingModifications {
179     my ( $self, $branchcode ) = @_;
180
181     my $dbh   = C4::Context->dbh;
182     my $query = "
183         SELECT borrower_modifications.*
184         FROM borrower_modifications, borrowers
185         WHERE borrower_modifications.borrowernumber > 0
186         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
187     ";
188
189     my @params;
190     if ($branchcode) {
191         $query .= " AND borrowers.branchcode = ? ";
192         push( @params, $branchcode );
193     }
194     $query .= " ORDER BY borrowers.surname, borrowers.firstname";
195     my $sth = $dbh->prepare($query);
196     $sth->execute(@params);
197
198     my @m;
199     while ( my $row = $sth->fetchrow_hashref() ) {
200         foreach my $key ( keys %$row ) {
201             delete $row->{$key} unless defined $row->{$key};
202         }
203
204         push( @m, $row );
205     }
206
207     return \@m;
208 }
209
210 =head ApproveModifications
211
212 Koha::Borrower::Modifications->ApproveModifications( $borrowernumber );
213
214 Commits the pending modifications to the borrower record and removes
215 them from the modifications table.
216
217 =cut
218
219 sub ApproveModifications {
220     my ( $self, $borrowernumber ) = @_;
221
222     if ( ref($self) ) {
223         $borrowernumber =
224           ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
225     }
226
227     return unless $borrowernumber;
228
229     my $data = $self->GetModifications( borrowernumber => $borrowernumber );
230
231     if ( UpdateInTable( "borrowers", $data ) ) {
232         $self->DelModifications( borrowernumber => $borrowernumber );
233     }
234 }
235
236 =head DenyModifications
237
238 Koha::Borrower::Modifications->DenyModifications( $borrowernumber );
239
240 Removes the modifications from the table for the given borrower,
241 without commiting the changes to the borrower record.
242
243 =cut
244
245 sub DenyModifications {
246     my ( $self, $borrowernumber ) = @_;
247
248     if ( ref($self) ) {
249         $borrowernumber =
250           ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
251     }
252
253     return unless $borrowernumber;
254
255     return $self->DelModifications( borrowernumber => $borrowernumber );
256 }
257
258 =head DelModifications
259
260 Koha::Borrower::Modifications->DelModifications(
261   [ borrowernumber => $borrowernumber ],
262   [ verification_token => $verification_token ]
263 );
264
265 Deletes the modifications for the given borrowernumber or verification token.
266
267 =cut
268
269 sub DelModifications {
270     my ( $self, %params ) = @_;
271
272     my ( $field, $value );
273
274     if ( $params{'borrowernumber'} ) {
275         $field = 'borrowernumber';
276         $value = $params{'borrowernumber'};
277     }
278     elsif ( $params{'verification_token'} ) {
279         $field = 'verification_token';
280         $value = $params{'verification_token'};
281     }
282
283     if ( ref($self) && !$value ) {
284         if ( $self->{'borrowernumber'} ) {
285             $field = 'borrowernumber';
286             $value = $self->{'borrowernumber'};
287         }
288         elsif ( $self->{'verification_token'} ) {
289             $field = 'verification_token';
290             $value = $self->{'verification_token'};
291         }
292     }
293
294     return unless $value;
295
296     my $dbh = C4::Context->dbh;
297
298     $field = $dbh->quote_identifier($field);
299
300     my $query = "
301         DELETE
302         FROM borrower_modifications
303         WHERE $field = ?
304     ";
305
306     my $sth = $dbh->prepare($query);
307     return $sth->execute($value);
308 }
309
310 =head GetModifications
311
312 $hashref = Koha::Borrower::Modifications->GetModifications(
313   [ borrowernumber => $borrowernumber ],
314   [ verification_token => $verification_token ]
315 );
316
317 Gets the modifications for the given borrowernumber or verification token.
318
319 =cut
320
321 sub GetModifications {
322     my ( $self, %params ) = @_;
323
324     my ( $field, $value );
325
326     if ( $params{'borrowernumber'} ) {
327         $field = 'borrowernumber';
328         $value = $params{'borrowernumber'};
329     }
330     elsif ( $params{'verification_token'} ) {
331         $field = 'verification_token';
332         $value = $params{'verification_token'};
333     }
334
335     if ( ref($self) && !$value ) {
336         if ( $self->{'borrowernumber'} ) {
337             $field = 'borrowernumber';
338             $value = $self->{'borrowernumber'};
339         }
340         elsif ( $self->{'verification_token'} ) {
341             $field = 'verification_token';
342             $value = $self->{'verification_token'};
343         }
344     }
345
346     return unless $value;
347
348     my $dbh = C4::Context->dbh;
349
350     $field = $dbh->quote_identifier($field);
351
352     my $query = "
353         SELECT *
354         FROM borrower_modifications
355         WHERE $field = ?
356     ";
357
358     my $sth = $dbh->prepare($query);
359     $sth->execute($value);
360     my $data = $sth->fetchrow_hashref();
361
362     foreach my $key ( keys %$data ) {
363         delete $data->{$key} unless ( defined( $data->{$key} ) );
364     }
365
366     return $data;
367 }
368
369 1;