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
29 use C4::SQLHelper qw(InsertInTable UpdateInTable);
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
35 # set the version for version checking
43 my $debug = C4::Context->preference("DebugLevel");
47 my ( $class, %args ) = @_;
48 my $self = bless( {}, $class );
50 $self->{'verification_token'} = $args{'verification_token'};
51 $self->{'borrowernumber'} = $args{'borrowernumber'};
56 =head AddModifications
58 Koha::Borrower::Modifications->AddModifications( %data );
60 Adds or updates modifications for a borrower.
62 Requires either the key borrowernumber, or verification_token
63 to be part of the passed in hash.
67 sub AddModifications {
68 my ( $self, %data ) = @_;
70 if ( $self->{'borrowernumber'} ) {
71 delete $data{'borrowernumber'};
74 $data{'borrowernumber'} = $self->{'borrowernumber'};
75 my $dbh = C4::Context->dbh;
78 SELECT COUNT(*) AS count
79 FROM borrower_modifications
80 WHERE borrowernumber = ?
83 my $sth = $dbh->prepare($query);
84 $sth->execute( $self->{'borrowernumber'} );
85 my $result = $sth->fetchrow_hashref();
87 if ( $result->{'count'} ) {
88 $data{'verification_token'} = q{};
89 return UpdateInTable( "borrower_modifications", \%data );
92 return InsertInTable( "borrower_modifications", \%data );
97 elsif ( $self->{'verification_token'} ) {
98 delete $data{'borrowernumber'};
99 $data{'verification_token'} = $self->{'verification_token'};
101 return InsertInTable( "borrower_modifications", \%data );
110 $verified = Koha::Borrower::Modifications->Verify( $verification_token );
112 Returns true if the passed in token is valid.
117 my ( $self, $verification_token ) = @_;
120 $verification_token =
121 ($verification_token)
122 ? $verification_token
123 : $self->{'verification_token'};
126 my $dbh = C4::Context->dbh;
128 SELECT COUNT(*) AS count
129 FROM borrower_modifications
130 WHERE verification_token = ?
132 my $sth = $dbh->prepare($query);
133 $sth->execute($verification_token);
134 my $result = $sth->fetchrow_hashref();
136 return $result->{'count'};
139 =head GetPendingModificationsCount
141 $count = Koha::Borrower::Modifications->GetPendingModificationsCount();
143 Returns the number of pending modifications for existing borrowers.
146 sub GetPendingModificationsCount {
147 my ( $self, $branchcode ) = @_;
149 my $dbh = C4::Context->dbh;
151 SELECT COUNT(*) AS count
152 FROM borrower_modifications, borrowers
153 WHERE borrower_modifications.borrowernumber > 0
154 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
159 $query .= " AND borrowers.branchcode = ? ";
160 push( @params, $branchcode );
163 my $sth = $dbh->prepare($query);
164 $sth->execute(@params);
165 my $result = $sth->fetchrow_hashref();
167 return $result->{'count'};
170 =head GetPendingModifications
172 $arrayref = Koha::Borrower::Modifications->GetPendingModifications();
174 Returns an arrayref of hashrefs for all pending modifications for existing borrowers.
178 sub GetPendingModifications {
179 my ( $self, $branchcode ) = @_;
181 my $dbh = C4::Context->dbh;
183 SELECT borrower_modifications.*
184 FROM borrower_modifications, borrowers
185 WHERE borrower_modifications.borrowernumber > 0
186 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
191 $query .= " AND borrowers.branchcode = ? ";
192 push( @params, $branchcode );
195 my $sth = $dbh->prepare($query);
196 $sth->execute(@params);
199 while ( my $row = $sth->fetchrow_hashref() ) {
200 foreach my $key ( keys %$row ) {
201 delete $row->{$key} unless defined $row->{$key};
210 =head ApproveModifications
212 Koha::Borrower::Modifications->ApproveModifications( $borrowernumber );
214 Commits the pending modifications to the borrower record and removes
215 them from the modifications table.
219 sub ApproveModifications {
220 my ( $self, $borrowernumber ) = @_;
224 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
227 return unless $borrowernumber;
229 my $data = $self->GetModifications( borrowernumber => $borrowernumber );
231 if ( UpdateInTable( "borrowers", $data ) ) {
232 $self->DelModifications( borrowernumber => $borrowernumber );
236 =head DenyModifications
238 Koha::Borrower::Modifications->DenyModifications( $borrowernumber );
240 Removes the modifications from the table for the given borrower,
241 without commiting the changes to the borrower record.
245 sub DenyModifications {
246 my ( $self, $borrowernumber ) = @_;
250 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
253 return unless $borrowernumber;
255 return $self->DelModifications( borrowernumber => $borrowernumber );
258 =head DelModifications
260 Koha::Borrower::Modifications->DelModifications(
261 [ borrowernumber => $borrowernumber ],
262 [ verification_token => $verification_token ]
265 Deletes the modifications for the given borrowernumber or verification token.
269 sub DelModifications {
270 my ( $self, %params ) = @_;
272 my ( $field, $value );
274 if ( $params{'borrowernumber'} ) {
275 $field = 'borrowernumber';
276 $value = $params{'borrowernumber'};
278 elsif ( $params{'verification_token'} ) {
279 $field = 'verification_token';
280 $value = $params{'verification_token'};
283 if ( ref($self) && !$value ) {
284 if ( $self->{'borrowernumber'} ) {
285 $field = 'borrowernumber';
286 $value = $self->{'borrowernumber'};
288 elsif ( $self->{'verification_token'} ) {
289 $field = 'verification_token';
290 $value = $self->{'verification_token'};
294 return unless $value;
296 my $dbh = C4::Context->dbh;
298 $field = $dbh->quote_identifier($field);
302 FROM borrower_modifications
306 my $sth = $dbh->prepare($query);
307 return $sth->execute($value);
310 =head GetModifications
312 $hashref = Koha::Borrower::Modifications->GetModifications(
313 [ borrowernumber => $borrowernumber ],
314 [ verification_token => $verification_token ]
317 Gets the modifications for the given borrowernumber or verification token.
321 sub GetModifications {
322 my ( $self, %params ) = @_;
324 my ( $field, $value );
326 if ( $params{'borrowernumber'} ) {
327 $field = 'borrowernumber';
328 $value = $params{'borrowernumber'};
330 elsif ( $params{'verification_token'} ) {
331 $field = 'verification_token';
332 $value = $params{'verification_token'};
335 if ( ref($self) && !$value ) {
336 if ( $self->{'borrowernumber'} ) {
337 $field = 'borrowernumber';
338 $value = $self->{'borrowernumber'};
340 elsif ( $self->{'verification_token'} ) {
341 $field = 'verification_token';
342 $value = $self->{'verification_token'};
346 return unless $value;
348 my $dbh = C4::Context->dbh;
350 $field = $dbh->quote_identifier($field);
354 FROM borrower_modifications
358 my $sth = $dbh->prepare($query);
359 $sth->execute($value);
360 my $data = $sth->fetchrow_hashref();
362 foreach my $key ( keys %$data ) {
363 delete $data->{$key} unless ( defined( $data->{$key} ) );