Bug 21156: Add plural translation capabilities to JS files
[koha.git] / Koha / Patron / Modifications.pm
blobe29079ca2e4ac9a11eaf3dc01200e1baac324b9a
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;
33 use List::Util qw /any none/;
35 use base qw(Koha::Objects);
37 =head2 pending_count
39 $count = Koha::Patron::Modifications->pending_count();
41 Returns the number of pending modifications for existing patrons.
43 =cut
45 sub pending_count {
46 my ( $self, $branchcode ) = @_;
48 my $dbh = C4::Context->dbh;
49 my $query = "
50 SELECT COUNT(*) AS count
51 FROM borrower_modifications, borrowers
52 WHERE borrower_modifications.borrowernumber > 0
53 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
56 my $userenv = C4::Context->userenv;
57 my @branchcodes;
58 if ( $userenv and $userenv->{number} ) {
59 my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
60 if ($branchcode) {
61 return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
62 @branchcodes = ( $branchcode );
64 else {
65 @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
68 my @sql_params;
69 if ( @branchcodes ) {
70 $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
71 push( @sql_params, @branchcodes );
74 my ( $count ) = $dbh->selectrow_array( $query, undef, @sql_params );
75 return $count;
78 =head2 pending
80 $arrayref = Koha::Patron::Modifications->pending();
82 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
84 =cut
86 sub pending {
87 my ( $self, $branchcode ) = @_;
89 my $dbh = C4::Context->dbh;
90 my $query = "
91 SELECT borrower_modifications.*
92 FROM borrower_modifications, borrowers
93 WHERE borrower_modifications.borrowernumber > 0
94 AND borrower_modifications.borrowernumber = borrowers.borrowernumber
97 my $userenv = C4::Context->userenv;
98 my @branchcodes;
99 if ( $userenv ) {
100 my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
101 if ($branchcode) {
102 return 0 unless $logged_in_user->can_see_patrons_from($branchcode);
103 @branchcodes = ( $branchcode );
105 else {
106 @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
109 my @sql_params;
110 if ( @branchcodes ) {
111 $query .= ' AND borrowers.branchcode IN ( ' . join( ',', ('?') x @branchcodes ) . ' )';
112 push( @sql_params, @branchcodes );
114 $query .= " ORDER BY borrowers.surname, borrowers.firstname";
115 my $sth = $dbh->prepare($query);
116 $sth->execute(@sql_params);
118 my @m;
119 while ( my $row = $sth->fetchrow_hashref() ) {
120 my @changed_keys = split /,/, $row->{changed_fields};
121 foreach my $key ( keys %$row ) {
122 if ($key eq 'changed_fields') {
123 delete $row->{$key};
124 next;
126 if ( defined $row->{$key} && $key eq 'extended_attributes' ) {
127 my $attributes = from_json( $row->{$key} );
128 my @pending_attributes;
129 foreach my $attr ( @{$attributes} ) {
130 push @pending_attributes,
131 Koha::Patron::Attribute->new(
132 { borrowernumber => $row->{borrowernumber},
133 code => $attr->{code},
134 attribute => $attr->{value}
139 $row->{$key} = \@pending_attributes;
141 if (none { $_ eq $key } @changed_keys) {
142 delete $row->{$key} unless defined $row->{$key};
146 push( @m, $row );
149 return \@m;
152 sub _type {
153 return 'BorrowerModification';
156 =head3 object_class
158 =cut
160 sub object_class {
161 return 'Koha::Patron::Modification';