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);
32 my ( $class, %args ) = @_;
34 return bless( \%args, $class );
37 =head2 AddModifications
39 Koha::Borrower::Modifications->AddModifications( $data );
41 Adds or updates modifications for a borrower.
43 Requires either the key borrowernumber, or verification_token
44 to be part of the passed in hash.
48 sub AddModifications {
49 my ( $self, $data ) = @_;
51 if ( $self->{'borrowernumber'} ) {
52 delete $data->{'borrowernumber'};
55 $data->{'borrowernumber'} = $self->{'borrowernumber'};
56 my $dbh = C4::Context->dbh;
59 SELECT COUNT(*) AS count
60 FROM borrower_modifications
61 WHERE borrowernumber = ?
64 my $sth = $dbh->prepare($query);
65 $sth->execute( $self->{'borrowernumber'} );
66 my $result = $sth->fetchrow_hashref();
68 if ( $result->{'count'} ) {
69 $data->{'verification_token'} = q{};
70 return UpdateInTable( "borrower_modifications", $data );
73 return InsertInTable( "borrower_modifications", $data );
78 elsif ( $self->{'verification_token'} ) {
79 delete $data->{'borrowernumber'};
80 $data->{'verification_token'} = $self->{'verification_token'};
82 return InsertInTable( "borrower_modifications", $data );
91 $verified = Koha::Borrower::Modifications->Verify( $verification_token );
93 Returns true if the passed in token is valid.
98 my ( $self, $verification_token ) = @_;
100 $verification_token =
101 ($verification_token)
102 ? $verification_token
103 : $self->{'verification_token'};
105 my $dbh = C4::Context->dbh;
107 SELECT COUNT(*) AS count
108 FROM borrower_modifications
109 WHERE verification_token = ?
111 my $sth = $dbh->prepare($query);
112 $sth->execute($verification_token);
113 my $result = $sth->fetchrow_hashref();
115 return $result->{'count'};
118 =head2 GetPendingModificationsCount
120 $count = Koha::Borrower::Modifications->GetPendingModificationsCount();
122 Returns the number of pending modifications for existing borrowers.
125 sub GetPendingModificationsCount {
126 my ( $self, $branchcode ) = @_;
128 my $dbh = C4::Context->dbh;
130 SELECT COUNT(*) AS count
131 FROM borrower_modifications, borrowers
132 WHERE borrower_modifications.borrowernumber > 0
133 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
138 $query .= " AND borrowers.branchcode = ? ";
139 push( @params, $branchcode );
142 my $sth = $dbh->prepare($query);
143 $sth->execute(@params);
144 my $result = $sth->fetchrow_hashref();
146 return $result->{'count'};
149 =head2 GetPendingModifications
151 $arrayref = Koha::Borrower::Modifications->GetPendingModifications();
153 Returns an arrayref of hashrefs for all pending modifications for existing borrowers.
157 sub GetPendingModifications {
158 my ( $self, $branchcode ) = @_;
160 my $dbh = C4::Context->dbh;
162 SELECT borrower_modifications.*
163 FROM borrower_modifications, borrowers
164 WHERE borrower_modifications.borrowernumber > 0
165 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
170 $query .= " AND borrowers.branchcode = ? ";
171 push( @params, $branchcode );
173 $query .= " ORDER BY borrowers.surname, borrowers.firstname";
174 my $sth = $dbh->prepare($query);
175 $sth->execute(@params);
178 while ( my $row = $sth->fetchrow_hashref() ) {
179 foreach my $key ( keys %$row ) {
180 delete $row->{$key} unless defined $row->{$key};
189 =head2 ApproveModifications
191 Koha::Borrower::Modifications->ApproveModifications( $borrowernumber );
193 Commits the pending modifications to the borrower record and removes
194 them from the modifications table.
198 sub ApproveModifications {
199 my ( $self, $borrowernumber ) = @_;
202 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
204 return unless $borrowernumber;
206 my $data = $self->GetModifications({ borrowernumber => $borrowernumber });
208 if ( UpdateInTable( "borrowers", $data ) ) {
209 $self->DelModifications({ borrowernumber => $borrowernumber });
213 =head2 DenyModifications
215 Koha::Borrower::Modifications->DenyModifications( $borrowernumber );
217 Removes the modifications from the table for the given borrower,
218 without commiting the changes to the borrower record.
222 sub DenyModifications {
223 my ( $self, $borrowernumber ) = @_;
226 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
228 return unless $borrowernumber;
230 return $self->DelModifications({ borrowernumber => $borrowernumber });
233 =head2 DelModifications
235 Koha::Borrower::Modifications->DelModifications({
236 [ borrowernumber => $borrowernumber ],
237 [ verification_token => $verification_token ]
240 Deletes the modifications for the given borrowernumber or verification token.
244 sub DelModifications {
245 my ( $self, $params ) = @_;
247 my ( $field, $value );
249 if ( $params->{'borrowernumber'} ) {
250 $field = 'borrowernumber';
251 $value = $params->{'borrowernumber'};
253 elsif ( $params->{'verification_token'} ) {
254 $field = 'verification_token';
255 $value = $params->{'verification_token'};
258 return unless $value;
260 my $dbh = C4::Context->dbh;
262 $field = $dbh->quote_identifier($field);
266 FROM borrower_modifications
270 my $sth = $dbh->prepare($query);
271 return $sth->execute($value);
274 =head2 GetModifications
276 $hashref = Koha::Borrower::Modifications->GetModifications({
277 [ borrowernumber => $borrowernumber ],
278 [ verification_token => $verification_token ]
281 Gets the modifications for the given borrowernumber or verification token.
285 sub GetModifications {
286 my ( $self, $params ) = @_;
288 my ( $field, $value );
290 if ( defined( $params->{'borrowernumber'} ) ) {
291 $field = 'borrowernumber';
292 $value = $params->{'borrowernumber'};
294 elsif ( defined( $params->{'verification_token'} ) ) {
295 $field = 'verification_token';
296 $value = $params->{'verification_token'};
299 return unless $value;
301 my $dbh = C4::Context->dbh;
303 $field = $dbh->quote_identifier($field);
307 FROM borrower_modifications
311 my $sth = $dbh->prepare($query);
312 $sth->execute($value);
313 my $data = $sth->fetchrow_hashref();
315 foreach my $key ( keys %$data ) {
316 delete $data->{$key} unless ( defined( $data->{$key} ) );