Bug 7317: Handle missing email addresses gracefuly
[koha.git] / Koha / Patron / Modifications.pm
blob5e7ed9efa51869ae2c0ad70891ef385dc6364830
1 package Koha::Patron::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
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.
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.
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>.
19 =head1 NAME
21 Koha::Patron::Modifications
23 =cut
25 use Modern::Perl;
27 use C4::Context;
29 use Koha::Patron::Attribute;
30 use Koha::Patron::Modification;
32 use JSON;
34 use base qw(Koha::Objects);
36 =head2 pending_count
38 $count = Koha::Patron::Modifications->pending_count();
40 Returns the number of pending modifications for existing patrons.
42 =cut
44 sub pending_count {
45 my ( $self, $branchcode ) = @_;
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
55 my @params;
56 if ($branchcode) {
57 $query .= " AND borrowers.branchcode = ? ";
58 push( @params, $branchcode );
61 my $sth = $dbh->prepare($query);
62 $sth->execute(@params);
63 my $result = $sth->fetchrow_hashref();
65 return $result->{count};
68 =head2 pending
70 $arrayref = Koha::Patron::Modifications->pending();
72 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
74 =cut
76 sub pending {
77 my ( $self, $branchcode ) = @_;
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
87 my @params;
88 if ($branchcode) {
89 $query .= " AND borrowers.branchcode = ? ";
90 push( @params, $branchcode );
92 $query .= " ORDER BY borrowers.surname, borrowers.firstname";
93 my $sth = $dbh->prepare($query);
94 $sth->execute(@params);
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}
112 $row->{$key} = \@pending_attributes;
114 delete $row->{$key} unless defined $row->{$key};
117 push( @m, $row );
120 return \@m;
123 sub _type {
124 return 'BorrowerModification';
127 =head3 object_class
129 =cut
131 sub object_class {
132 return 'Koha::Patron::Modification';