Bug 17288 - Advanced Editor - Rancor - Helpers for 006 and 007 fields
[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::Attribute;
30 use Koha::Patron::Modification;
31
32 use JSON;
33
34 use base qw(Koha::Objects);
35
36 =head2 pending_count
37
38 $count = Koha::Patron::Modifications->pending_count();
39
40 Returns the number of pending modifications for existing patrons.
41
42 =cut
43
44 sub pending_count {
45     my ( $self, $branchcode ) = @_;
46
47     my $dbh   = C4::Context->dbh;
48     my $query = "
49         SELECT COUNT(*) AS count
50         FROM borrower_modifications, borrowers
51         WHERE borrower_modifications.borrowernumber > 0
52         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
53     ";
54
55     my @params;
56     if ($branchcode) {
57         $query .= " AND borrowers.branchcode = ? ";
58         push( @params, $branchcode );
59     }
60
61     my $sth = $dbh->prepare($query);
62     $sth->execute(@params);
63     my $result = $sth->fetchrow_hashref();
64
65     return $result->{count};
66 }
67
68 =head2 pending
69
70 $arrayref = Koha::Patron::Modifications->pending();
71
72 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
73
74 =cut
75
76 sub pending {
77     my ( $self, $branchcode ) = @_;
78
79     my $dbh   = C4::Context->dbh;
80     my $query = "
81         SELECT borrower_modifications.*
82         FROM borrower_modifications, borrowers
83         WHERE borrower_modifications.borrowernumber > 0
84         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
85     ";
86
87     my @params;
88     if ($branchcode) {
89         $query .= " AND borrowers.branchcode = ? ";
90         push( @params, $branchcode );
91     }
92     $query .= " ORDER BY borrowers.surname, borrowers.firstname";
93     my $sth = $dbh->prepare($query);
94     $sth->execute(@params);
95
96     my @m;
97     while ( my $row = $sth->fetchrow_hashref() ) {
98         foreach my $key ( keys %$row ) {
99             if ( defined $row->{$key} && $key eq 'extended_attributes' ) {
100                 my $attributes = from_json( $row->{$key} );
101                 my @pending_attributes;
102                 foreach my $attr ( @{$attributes} ) {
103                     push @pending_attributes,
104                         Koha::Patron::Attribute->new(
105                         {   borrowernumber => $row->{borrowernumber},
106                             code           => $attr->{code},
107                             attribute      => $attr->{value}
108                         }
109                         );
110                 }
111
112                 $row->{$key} = \@pending_attributes;
113             }
114             delete $row->{$key} unless defined $row->{$key};
115         }
116
117         push( @m, $row );
118     }
119
120     return \@m;
121 }
122
123 sub _type {
124     return 'BorrowerModification';
125 }
126
127 =head3 object_class
128
129 =cut
130
131 sub object_class {
132     return 'Koha::Patron::Modification';
133 }
134
135 1;