1 package Koha::Patron::Modifications;
3 # Copyright 2012 ByWater Solutions
4 # This file is part of Koha.
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.
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.
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>.
21 Koha::Patron::Modifications
30 use base qw(Koha::Objects);
32 =head2 AddModifications
34 Koha::Patron::Modifications->AddModifications( $data );
36 Adds or updates modifications for a patron
38 Requires either the key borrowernumber, or verification_token
39 to be part of the passed in hash.
43 sub AddModifications {
44 my ( $self, $data ) = @_;
46 delete $data->{borrowernumber};
47 if( $self->{borrowernumber} ) {
48 return if( not keys %$data );
49 $data->{borrowernumber} = $self->{borrowernumber};
50 $data->{verification_token} = '';
52 elsif( $self->{verification_token} ) {
53 $data->{verification_token} = $self->{verification_token};
54 $data->{borrowernumber} = 0;
60 my $rs = Koha::Database->new()->schema->resultset('BorrowerModification');
61 return $rs->update_or_create($data, { key => 'primary' } );
66 $verified = Koha::Patron::Modifications->Verify( $verification_token );
68 Returns true if the passed in token is valid.
73 my ( $self, $verification_token ) = @_;
78 : $self->{'verification_token'};
80 my $dbh = C4::Context->dbh;
82 SELECT COUNT(*) AS count
83 FROM borrower_modifications
84 WHERE verification_token = ?
86 my $sth = $dbh->prepare($query);
87 $sth->execute($verification_token);
88 my $result = $sth->fetchrow_hashref();
90 return $result->{'count'};
93 =head2 GetPendingModificationsCount
95 $count = Koha::Patron::Modifications->GetPendingModificationsCount();
97 Returns the number of pending modifications for existing patron.
101 sub GetPendingModificationsCount {
102 my ( $self, $branchcode ) = @_;
104 my $dbh = C4::Context->dbh;
106 SELECT COUNT(*) AS count
107 FROM borrower_modifications, borrowers
108 WHERE borrower_modifications.borrowernumber > 0
109 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
114 $query .= " AND borrowers.branchcode = ? ";
115 push( @params, $branchcode );
118 my $sth = $dbh->prepare($query);
119 $sth->execute(@params);
120 my $result = $sth->fetchrow_hashref();
122 return $result->{'count'};
125 =head2 GetPendingModifications
127 $arrayref = Koha::Patron::Modifications->GetPendingModifications();
129 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
133 sub GetPendingModifications {
134 my ( $self, $branchcode ) = @_;
136 my $dbh = C4::Context->dbh;
138 SELECT borrower_modifications.*
139 FROM borrower_modifications, borrowers
140 WHERE borrower_modifications.borrowernumber > 0
141 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
146 $query .= " AND borrowers.branchcode = ? ";
147 push( @params, $branchcode );
149 $query .= " ORDER BY borrowers.surname, borrowers.firstname";
150 my $sth = $dbh->prepare($query);
151 $sth->execute(@params);
154 while ( my $row = $sth->fetchrow_hashref() ) {
155 foreach my $key ( keys %$row ) {
156 delete $row->{$key} unless defined $row->{$key};
165 =head2 ApproveModifications
167 Koha::Patron::Modifications->ApproveModifications( $borrowernumber );
169 Commits the pending modifications to the borrower record and removes
170 them from the modifications table.
174 sub ApproveModifications {
175 my ( $self, $borrowernumber ) = @_;
178 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
180 return unless $borrowernumber;
182 my $data = $self->GetModifications( { borrowernumber => $borrowernumber } );
183 delete $data->{timestamp};
184 delete $data->{verification_token};
186 my $rs = Koha::Database->new()->schema->resultset('Borrower')->search({
187 borrowernumber => $data->{borrowernumber},
189 if( $rs->update($data) ) {
190 $self->DelModifications( { borrowernumber => $borrowernumber } );
194 =head2 DenyModifications
196 Koha::Patron::Modifications->DenyModifications( $borrowernumber );
198 Removes the modifications from the table for the given patron,
199 without committing the changes to the patron record.
203 sub DenyModifications {
204 my ( $self, $borrowernumber ) = @_;
207 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
209 return unless $borrowernumber;
211 return $self->DelModifications( { borrowernumber => $borrowernumber } );
214 =head2 DelModifications
216 Koha::Patron::Modifications->DelModifications({
217 [ borrowernumber => $borrowernumber ],
218 [ verification_token => $verification_token ]
221 Deletes the modifications for the given borrowernumber or verification token.
225 sub DelModifications {
226 my ( $self, $params ) = @_;
228 my ( $field, $value );
230 if ( $params->{'borrowernumber'} ) {
231 $field = 'borrowernumber';
232 $value = $params->{'borrowernumber'};
234 elsif ( $params->{'verification_token'} ) {
235 $field = 'verification_token';
236 $value = $params->{'verification_token'};
239 return unless $value;
241 my $dbh = C4::Context->dbh;
243 $field = $dbh->quote_identifier($field);
247 FROM borrower_modifications
251 my $sth = $dbh->prepare($query);
252 return $sth->execute($value);
255 =head2 GetModifications
257 $hashref = Koha::Patron::Modifications->GetModifications({
258 [ borrowernumber => $borrowernumber ],
259 [ verification_token => $verification_token ]
262 Gets the modifications for the given borrowernumber or verification token.
266 sub GetModifications {
267 my ( $self, $params ) = @_;
269 my ( $field, $value );
271 if ( defined( $params->{'borrowernumber'} ) ) {
272 $field = 'borrowernumber';
273 $value = $params->{'borrowernumber'};
275 elsif ( defined( $params->{'verification_token'} ) ) {
276 $field = 'verification_token';
277 $value = $params->{'verification_token'};
280 return unless $value;
282 my $dbh = C4::Context->dbh;
284 $field = $dbh->quote_identifier($field);
288 FROM borrower_modifications
292 my $sth = $dbh->prepare($query);
293 $sth->execute($value);
294 my $data = $sth->fetchrow_hashref();
296 foreach my $key ( keys %$data ) {
297 delete $data->{$key} unless ( defined( $data->{$key} ) );
304 return 'BorrowerModification';
312 return 'Koha::Patron::Modification';