1 package Koha::Borrower::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 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
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.
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.
21 C4::Borrowers::Modifications
31 my ( $class, %args ) = @_;
33 return bless( \%args, $class );
36 =head2 AddModifications
38 Koha::Borrower::Modifications->AddModifications( $data );
40 Adds or updates modifications for a borrower.
42 Requires either the key borrowernumber, or verification_token
43 to be part of the passed in hash.
47 sub AddModifications {
48 my ( $self, $data ) = @_;
50 delete $data->{borrowernumber};
51 if( $self->{borrowernumber} ) {
52 return if( not keys %$data );
53 $data->{borrowernumber} = $self->{borrowernumber};
55 elsif( $self->{verification_token} ) {
56 $data->{verification_token} = $self->{verification_token};
62 my $rs = Koha::Database->new()->schema->resultset('BorrowerModification');
63 return $rs->update_or_create($data);
68 $verified = Koha::Borrower::Modifications->Verify( $verification_token );
70 Returns true if the passed in token is valid.
75 my ( $self, $verification_token ) = @_;
80 : $self->{'verification_token'};
82 my $dbh = C4::Context->dbh;
84 SELECT COUNT(*) AS count
85 FROM borrower_modifications
86 WHERE verification_token = ?
88 my $sth = $dbh->prepare($query);
89 $sth->execute($verification_token);
90 my $result = $sth->fetchrow_hashref();
92 return $result->{'count'};
95 =head2 GetPendingModificationsCount
97 $count = Koha::Borrower::Modifications->GetPendingModificationsCount();
99 Returns the number of pending modifications for existing borrowers.
103 sub GetPendingModificationsCount {
104 my ( $self, $branchcode ) = @_;
106 my $dbh = C4::Context->dbh;
108 SELECT COUNT(*) AS count
109 FROM borrower_modifications, borrowers
110 WHERE borrower_modifications.borrowernumber > 0
111 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
116 $query .= " AND borrowers.branchcode = ? ";
117 push( @params, $branchcode );
120 my $sth = $dbh->prepare($query);
121 $sth->execute(@params);
122 my $result = $sth->fetchrow_hashref();
124 return $result->{'count'};
127 =head2 GetPendingModifications
129 $arrayref = Koha::Borrower::Modifications->GetPendingModifications();
131 Returns an arrayref of hashrefs for all pending modifications for existing borrowers.
135 sub GetPendingModifications {
136 my ( $self, $branchcode ) = @_;
138 my $dbh = C4::Context->dbh;
140 SELECT borrower_modifications.*
141 FROM borrower_modifications, borrowers
142 WHERE borrower_modifications.borrowernumber > 0
143 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
148 $query .= " AND borrowers.branchcode = ? ";
149 push( @params, $branchcode );
151 $query .= " ORDER BY borrowers.surname, borrowers.firstname";
152 my $sth = $dbh->prepare($query);
153 $sth->execute(@params);
156 while ( my $row = $sth->fetchrow_hashref() ) {
157 foreach my $key ( keys %$row ) {
158 delete $row->{$key} unless defined $row->{$key};
167 =head2 ApproveModifications
169 Koha::Borrower::Modifications->ApproveModifications( $borrowernumber );
171 Commits the pending modifications to the borrower record and removes
172 them from the modifications table.
176 sub ApproveModifications {
177 my ( $self, $borrowernumber ) = @_;
180 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
182 return unless $borrowernumber;
184 my $data = $self->GetModifications( { borrowernumber => $borrowernumber } );
185 delete $data->{timestamp};
186 delete $data->{verification_token};
188 my $rs = Koha::Database->new()->schema->resultset('Borrower')->search({
189 borrowernumber => $data->{borrowernumber},
191 if( $rs->update($data) ) {
192 $self->DelModifications( { borrowernumber => $borrowernumber } );
196 =head2 DenyModifications
198 Koha::Borrower::Modifications->DenyModifications( $borrowernumber );
200 Removes the modifications from the table for the given borrower,
201 without commiting the changes to the borrower record.
205 sub DenyModifications {
206 my ( $self, $borrowernumber ) = @_;
209 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
211 return unless $borrowernumber;
213 return $self->DelModifications( { borrowernumber => $borrowernumber } );
216 =head2 DelModifications
218 Koha::Borrower::Modifications->DelModifications({
219 [ borrowernumber => $borrowernumber ],
220 [ verification_token => $verification_token ]
223 Deletes the modifications for the given borrowernumber or verification token.
227 sub DelModifications {
228 my ( $self, $params ) = @_;
230 my ( $field, $value );
232 if ( $params->{'borrowernumber'} ) {
233 $field = 'borrowernumber';
234 $value = $params->{'borrowernumber'};
236 elsif ( $params->{'verification_token'} ) {
237 $field = 'verification_token';
238 $value = $params->{'verification_token'};
241 return unless $value;
243 my $dbh = C4::Context->dbh;
245 $field = $dbh->quote_identifier($field);
249 FROM borrower_modifications
253 my $sth = $dbh->prepare($query);
254 return $sth->execute($value);
257 =head2 GetModifications
259 $hashref = Koha::Borrower::Modifications->GetModifications({
260 [ borrowernumber => $borrowernumber ],
261 [ verification_token => $verification_token ]
264 Gets the modifications for the given borrowernumber or verification token.
268 sub GetModifications {
269 my ( $self, $params ) = @_;
271 my ( $field, $value );
273 if ( defined( $params->{'borrowernumber'} ) ) {
274 $field = 'borrowernumber';
275 $value = $params->{'borrowernumber'};
277 elsif ( defined( $params->{'verification_token'} ) ) {
278 $field = 'verification_token';
279 $value = $params->{'verification_token'};
282 return unless $value;
284 my $dbh = C4::Context->dbh;
286 $field = $dbh->quote_identifier($field);
290 FROM borrower_modifications
294 my $sth = $dbh->prepare($query);
295 $sth->execute($value);
296 my $data = $sth->fetchrow_hashref();
298 foreach my $key ( keys %$data ) {
299 delete $data->{$key} unless ( defined( $data->{$key} ) );