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);
33 my ( $self, %args ) = @_;
35 $self = $self->SUPER::new(@_);
37 foreach my $key ( keys %args ) {
38 $self->{$key} = $args{$key};
44 =head2 AddModifications
46 Koha::Patron::Modifications->AddModifications( $data );
48 Adds or updates modifications for a patron
50 Requires either the key borrowernumber, or verification_token
51 to be part of the passed in hash.
55 sub AddModifications {
56 my ( $self, $data ) = @_;
58 delete $data->{borrowernumber};
59 if( $self->{borrowernumber} ) {
60 return if( not keys %$data );
61 $data->{borrowernumber} = $self->{borrowernumber};
62 $data->{verification_token} = '';
64 elsif( $self->{verification_token} ) {
65 $data->{verification_token} = $self->{verification_token};
66 $data->{borrowernumber} = 0;
72 my $rs = Koha::Database->new()->schema->resultset('BorrowerModification');
73 return $rs->update_or_create($data, { key => 'primary' } );
78 $verified = Koha::Patron::Modifications->Verify( $verification_token );
80 Returns true if the passed in token is valid.
85 my ( $self, $verification_token ) = @_;
90 : $self->{'verification_token'};
92 my $dbh = C4::Context->dbh;
94 SELECT COUNT(*) AS count
95 FROM borrower_modifications
96 WHERE verification_token = ?
98 my $sth = $dbh->prepare($query);
99 $sth->execute($verification_token);
100 my $result = $sth->fetchrow_hashref();
102 return $result->{'count'};
105 =head2 GetPendingModificationsCount
107 $count = Koha::Patron::Modifications->GetPendingModificationsCount();
109 Returns the number of pending modifications for existing patron.
113 sub GetPendingModificationsCount {
114 my ( $self, $branchcode ) = @_;
116 my $dbh = C4::Context->dbh;
118 SELECT COUNT(*) AS count
119 FROM borrower_modifications, borrowers
120 WHERE borrower_modifications.borrowernumber > 0
121 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
126 $query .= " AND borrowers.branchcode = ? ";
127 push( @params, $branchcode );
130 my $sth = $dbh->prepare($query);
131 $sth->execute(@params);
132 my $result = $sth->fetchrow_hashref();
134 return $result->{'count'};
137 =head2 GetPendingModifications
139 $arrayref = Koha::Patron::Modifications->GetPendingModifications();
141 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
145 sub GetPendingModifications {
146 my ( $self, $branchcode ) = @_;
148 my $dbh = C4::Context->dbh;
150 SELECT borrower_modifications.*
151 FROM borrower_modifications, borrowers
152 WHERE borrower_modifications.borrowernumber > 0
153 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
158 $query .= " AND borrowers.branchcode = ? ";
159 push( @params, $branchcode );
161 $query .= " ORDER BY borrowers.surname, borrowers.firstname";
162 my $sth = $dbh->prepare($query);
163 $sth->execute(@params);
166 while ( my $row = $sth->fetchrow_hashref() ) {
167 foreach my $key ( keys %$row ) {
168 delete $row->{$key} unless defined $row->{$key};
177 =head2 ApproveModifications
179 Koha::Patron::Modifications->ApproveModifications( $borrowernumber );
181 Commits the pending modifications to the borrower record and removes
182 them from the modifications table.
186 sub ApproveModifications {
187 my ( $self, $borrowernumber ) = @_;
190 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
192 return unless $borrowernumber;
194 my $data = $self->GetModifications( { borrowernumber => $borrowernumber } );
195 delete $data->{timestamp};
196 delete $data->{verification_token};
198 my $rs = Koha::Database->new()->schema->resultset('Borrower')->search({
199 borrowernumber => $data->{borrowernumber},
201 if( $rs->update($data) ) {
202 $self->DelModifications( { borrowernumber => $borrowernumber } );
206 =head2 DenyModifications
208 Koha::Patron::Modifications->DenyModifications( $borrowernumber );
210 Removes the modifications from the table for the given patron,
211 without committing the changes to the patron record.
215 sub DenyModifications {
216 my ( $self, $borrowernumber ) = @_;
219 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
221 return unless $borrowernumber;
223 return $self->DelModifications( { borrowernumber => $borrowernumber } );
226 =head2 DelModifications
228 Koha::Patron::Modifications->DelModifications({
229 [ borrowernumber => $borrowernumber ],
230 [ verification_token => $verification_token ]
233 Deletes the modifications for the given borrowernumber or verification token.
237 sub DelModifications {
238 my ( $self, $params ) = @_;
240 my ( $field, $value );
242 if ( $params->{'borrowernumber'} ) {
243 $field = 'borrowernumber';
244 $value = $params->{'borrowernumber'};
246 elsif ( $params->{'verification_token'} ) {
247 $field = 'verification_token';
248 $value = $params->{'verification_token'};
251 return unless $value;
253 my $dbh = C4::Context->dbh;
255 $field = $dbh->quote_identifier($field);
259 FROM borrower_modifications
263 my $sth = $dbh->prepare($query);
264 return $sth->execute($value);
267 =head2 GetModifications
269 $hashref = Koha::Patron::Modifications->GetModifications({
270 [ borrowernumber => $borrowernumber ],
271 [ verification_token => $verification_token ]
274 Gets the modifications for the given borrowernumber or verification token.
278 sub GetModifications {
279 my ( $self, $params ) = @_;
281 my ( $field, $value );
283 if ( defined( $params->{'borrowernumber'} ) ) {
284 $field = 'borrowernumber';
285 $value = $params->{'borrowernumber'};
287 elsif ( defined( $params->{'verification_token'} ) ) {
288 $field = 'verification_token';
289 $value = $params->{'verification_token'};
292 return unless $value;
294 my $dbh = C4::Context->dbh;
296 $field = $dbh->quote_identifier($field);
300 FROM borrower_modifications
304 my $sth = $dbh->prepare($query);
305 $sth->execute($value);
306 my $data = $sth->fetchrow_hashref();
308 foreach my $key ( keys %$data ) {
309 delete $data->{$key} unless ( defined( $data->{$key} ) );
316 return 'BorrowerModification';
324 return 'Koha::Patron::Modification';