Bug 16715: Proposed followup to use Sereal for serialization
[koha.git] / Koha / Patron / Modifications.pm
1 package Koha::Patron::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
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 =head1 NAME
20
21 Koha::Patron::Modifications
22
23 =cut
24
25 use Modern::Perl;
26
27 use C4::Context;
28 use C4::Debug;
29
30 use base qw(Koha::Objects);
31
32 =head2 AddModifications
33
34 Koha::Patron::Modifications->AddModifications( $data );
35
36 Adds or updates modifications for a patron
37
38 Requires either the key borrowernumber, or verification_token
39 to be part of the passed in hash.
40
41 =cut
42
43 sub AddModifications {
44     my ( $self, $data ) = @_;
45
46     delete $data->{borrowernumber};
47     if( $self->{borrowernumber} ) {
48         return if( not keys %$data );
49         $data->{borrowernumber} = $self->{borrowernumber};
50         $data->{verification_token} = '';
51     }
52     elsif( $self->{verification_token} ) {
53         $data->{verification_token} = $self->{verification_token};
54         $data->{borrowernumber} = 0;
55     }
56     else {
57         return;
58     }
59
60     my $rs = Koha::Database->new()->schema->resultset('BorrowerModification');
61     return $rs->update_or_create($data, { key => 'primary' } );
62 }
63
64 =head2 Verify
65
66 $verified = Koha::Patron::Modifications->Verify( $verification_token );
67
68 Returns true if the passed in token is valid.
69
70 =cut
71
72 sub Verify {
73     my ( $self, $verification_token ) = @_;
74
75     $verification_token =
76       ($verification_token)
77       ? $verification_token
78       : $self->{'verification_token'};
79
80     my $dbh   = C4::Context->dbh;
81     my $query = "
82         SELECT COUNT(*) AS count
83         FROM borrower_modifications
84         WHERE verification_token = ?
85     ";
86     my $sth = $dbh->prepare($query);
87     $sth->execute($verification_token);
88     my $result = $sth->fetchrow_hashref();
89
90     return $result->{'count'};
91 }
92
93 =head2 GetPendingModificationsCount
94
95 $count = Koha::Patron::Modifications->GetPendingModificationsCount();
96
97 Returns the number of pending modifications for existing patron.
98
99 =cut
100
101 sub GetPendingModificationsCount {
102     my ( $self, $branchcode ) = @_;
103
104     my $dbh   = C4::Context->dbh;
105     my $query = "
106         SELECT COUNT(*) AS count
107         FROM borrower_modifications, borrowers
108         WHERE borrower_modifications.borrowernumber > 0
109         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
110     ";
111
112     my @params;
113     if ($branchcode) {
114         $query .= " AND borrowers.branchcode = ? ";
115         push( @params, $branchcode );
116     }
117
118     my $sth = $dbh->prepare($query);
119     $sth->execute(@params);
120     my $result = $sth->fetchrow_hashref();
121
122     return $result->{'count'};
123 }
124
125 =head2 GetPendingModifications
126
127 $arrayref = Koha::Patron::Modifications->GetPendingModifications();
128
129 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
130
131 =cut
132
133 sub GetPendingModifications {
134     my ( $self, $branchcode ) = @_;
135
136     my $dbh   = C4::Context->dbh;
137     my $query = "
138         SELECT borrower_modifications.*
139         FROM borrower_modifications, borrowers
140         WHERE borrower_modifications.borrowernumber > 0
141         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
142     ";
143
144     my @params;
145     if ($branchcode) {
146         $query .= " AND borrowers.branchcode = ? ";
147         push( @params, $branchcode );
148     }
149     $query .= " ORDER BY borrowers.surname, borrowers.firstname";
150     my $sth = $dbh->prepare($query);
151     $sth->execute(@params);
152
153     my @m;
154     while ( my $row = $sth->fetchrow_hashref() ) {
155         foreach my $key ( keys %$row ) {
156             delete $row->{$key} unless defined $row->{$key};
157         }
158
159         push( @m, $row );
160     }
161
162     return \@m;
163 }
164
165 =head2 ApproveModifications
166
167 Koha::Patron::Modifications->ApproveModifications( $borrowernumber );
168
169 Commits the pending modifications to the borrower record and removes
170 them from the modifications table.
171
172 =cut
173
174 sub ApproveModifications {
175     my ( $self, $borrowernumber ) = @_;
176
177     $borrowernumber =
178       ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
179
180     return unless $borrowernumber;
181
182     my $data = $self->GetModifications( { borrowernumber => $borrowernumber } );
183     delete $data->{timestamp};
184     delete $data->{verification_token};
185
186     my $rs = Koha::Database->new()->schema->resultset('Borrower')->search({
187         borrowernumber => $data->{borrowernumber},
188     });
189     if( $rs->update($data) ) {
190         $self->DelModifications( { borrowernumber => $borrowernumber } );
191     }
192 }
193
194 =head2 DenyModifications
195
196 Koha::Patron::Modifications->DenyModifications( $borrowernumber );
197
198 Removes the modifications from the table for the given patron,
199 without committing the changes to the patron record.
200
201 =cut
202
203 sub DenyModifications {
204     my ( $self, $borrowernumber ) = @_;
205
206     $borrowernumber =
207       ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
208
209     return unless $borrowernumber;
210
211     return $self->DelModifications( { borrowernumber => $borrowernumber } );
212 }
213
214 =head2 DelModifications
215
216 Koha::Patron::Modifications->DelModifications({
217   [ borrowernumber => $borrowernumber ],
218   [ verification_token => $verification_token ]
219 });
220
221 Deletes the modifications for the given borrowernumber or verification token.
222
223 =cut
224
225 sub DelModifications {
226     my ( $self, $params ) = @_;
227
228     my ( $field, $value );
229
230     if ( $params->{'borrowernumber'} ) {
231         $field = 'borrowernumber';
232         $value = $params->{'borrowernumber'};
233     }
234     elsif ( $params->{'verification_token'} ) {
235         $field = 'verification_token';
236         $value = $params->{'verification_token'};
237     }
238
239     return unless $value;
240
241     my $dbh = C4::Context->dbh;
242
243     $field = $dbh->quote_identifier($field);
244
245     my $query = "
246         DELETE
247         FROM borrower_modifications
248         WHERE $field = ?
249     ";
250
251     my $sth = $dbh->prepare($query);
252     return $sth->execute($value);
253 }
254
255 =head2 GetModifications
256
257 $hashref = Koha::Patron::Modifications->GetModifications({
258   [ borrowernumber => $borrowernumber ],
259   [ verification_token => $verification_token ]
260 });
261
262 Gets the modifications for the given borrowernumber or verification token.
263
264 =cut
265
266 sub GetModifications {
267     my ( $self, $params ) = @_;
268
269     my ( $field, $value );
270
271     if ( defined( $params->{'borrowernumber'} ) ) {
272         $field = 'borrowernumber';
273         $value = $params->{'borrowernumber'};
274     }
275     elsif ( defined( $params->{'verification_token'} ) ) {
276         $field = 'verification_token';
277         $value = $params->{'verification_token'};
278     }
279
280     return unless $value;
281
282     my $dbh = C4::Context->dbh;
283
284     $field = $dbh->quote_identifier($field);
285
286     my $query = "
287         SELECT *
288         FROM borrower_modifications
289         WHERE $field = ?
290     ";
291
292     my $sth = $dbh->prepare($query);
293     $sth->execute($value);
294     my $data = $sth->fetchrow_hashref();
295
296     foreach my $key ( keys %$data ) {
297         delete $data->{$key} unless ( defined( $data->{$key} ) );
298     }
299
300     return $data;
301 }
302
303 sub _type {
304     return 'BorrowerModification';
305 }
306
307 =head3 object_class
308
309 =cut
310
311 sub object_class {
312     return 'Koha::Patron::Modification';
313 }
314
315 1;