Bug 17767: Make Koha::Patron::Modification handle extended attributes
[koha.git] / Koha / Patron / Modifications.pm
1 package Koha::Patron::Modifications;
2
3 # Copyright 2012 ByWater Solutions
4 # This file is part of Koha.
5 #
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.
10 #
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.
15 #
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>.
18
19 =head1 NAME
20
21 Koha::Patron::Modifications
22
23 =cut
24
25 use Modern::Perl;
26
27 use C4::Context;
28
29 use Koha::Patron::Modification;
30
31 use JSON;
32
33 use base qw(Koha::Objects);
34
35 =head2 pending_count
36
37 $count = Koha::Patron::Modifications->pending_count();
38
39 Returns the number of pending modifications for existing patrons.
40
41 =cut
42
43 sub pending_count {
44     my ( $self, $branchcode ) = @_;
45
46     my $dbh   = C4::Context->dbh;
47     my $query = "
48         SELECT COUNT(*) AS count
49         FROM borrower_modifications, borrowers
50         WHERE borrower_modifications.borrowernumber > 0
51         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
52     ";
53
54     my @params;
55     if ($branchcode) {
56         $query .= " AND borrowers.branchcode = ? ";
57         push( @params, $branchcode );
58     }
59
60     my $sth = $dbh->prepare($query);
61     $sth->execute(@params);
62     my $result = $sth->fetchrow_hashref();
63
64     return $result->{count};
65 }
66
67 =head2 pending
68
69 $arrayref = Koha::Patron::Modifications->pending();
70
71 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
72
73 =cut
74
75 sub pending {
76     my ( $self, $branchcode ) = @_;
77
78     my $dbh   = C4::Context->dbh;
79     my $query = "
80         SELECT borrower_modifications.*
81         FROM borrower_modifications, borrowers
82         WHERE borrower_modifications.borrowernumber > 0
83         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
84     ";
85
86     my @params;
87     if ($branchcode) {
88         $query .= " AND borrowers.branchcode = ? ";
89         push( @params, $branchcode );
90     }
91     $query .= " ORDER BY borrowers.surname, borrowers.firstname";
92     my $sth = $dbh->prepare($query);
93     $sth->execute(@params);
94
95     my @m;
96     while ( my $row = $sth->fetchrow_hashref() ) {
97         foreach my $key ( keys %$row ) {
98             if ( defined $row->{$key} && $key eq 'extended_attributes' ) {
99                 $row->{$key} = from_json($row->{$key});
100             }
101             delete $row->{$key} unless defined $row->{$key};
102         }
103
104         push( @m, $row );
105     }
106
107     return \@m;
108 }
109
110 sub _type {
111     return 'BorrowerModification';
112 }
113
114 =head3 object_class
115
116 =cut
117
118 sub object_class {
119     return 'Koha::Patron::Modification';
120 }
121
122 1;