Jonathan Druart
9ce0dd6b92
merge_with were returning Koha::Patron::Attribute for existing attribute and hashref for new attribute. Signed-off-by: Nick Clemens <nick@bywatersolutions.com> Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
138 lines
3.5 KiB
Perl
138 lines
3.5 KiB
Perl
package Koha::Patron::Attributes;
|
|
|
|
# This file is part of Koha.
|
|
#
|
|
# Koha is free software; you can redistribute it and/or modify it
|
|
# under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Koha is distributed in the hope that it will be useful, but
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with Koha; if not, see <http://www.gnu.org/licenses>.
|
|
|
|
use Modern::Perl;
|
|
|
|
use Koha::Patron::Attribute;
|
|
use Koha::Patron::Attribute::Types;
|
|
|
|
use base qw(Koha::Objects);
|
|
|
|
=head1 NAME
|
|
|
|
Koha::Patron::Attributes - Koha Patron Attributes Object set class
|
|
|
|
=head1 API
|
|
|
|
=head2 Class Methods
|
|
|
|
=cut
|
|
|
|
=head3 search
|
|
|
|
my $attributes = Koha::Patron::Attributes->search( $params );
|
|
|
|
=cut
|
|
|
|
sub search {
|
|
my ( $self, $params, $attributes ) = @_;
|
|
|
|
unless ( exists $attributes->{order_by} ) { $attributes->{order_by} = ['me.code', 'attribute'] }
|
|
|
|
return $self->SUPER::search( $params, $attributes );
|
|
}
|
|
|
|
=head3 filter_by_branch_limitations
|
|
|
|
my $attributes = Koha::Patron::Attributes->filter_by_branch_limitations([$branchcode]);
|
|
|
|
Search patron attributes filtered by a library
|
|
|
|
If $branchcode exists it will be used to filter the result set.
|
|
|
|
Otherwise it will be the library of the logged in user.
|
|
|
|
=cut
|
|
|
|
sub filter_by_branch_limitations {
|
|
my ( $self, $branchcode ) = @_;
|
|
|
|
# Maybe we should not limit if logged in user is superlibrarian?
|
|
my $branch_limit =
|
|
$branchcode ? $branchcode
|
|
# Do we raise an exception if no userenv defined?
|
|
: C4::Context->userenv ? C4::Context->userenv->{"branch"}
|
|
: undef;
|
|
|
|
my $or = $branch_limit
|
|
? {
|
|
'-or' => [
|
|
'borrower_attribute_types_branches.b_branchcode' => undef,
|
|
'borrower_attribute_types_branches.b_branchcode' => $branch_limit,
|
|
]
|
|
}
|
|
: {};
|
|
|
|
my $join = $branch_limit
|
|
? {
|
|
join => {
|
|
code => 'borrower_attribute_types_branches'
|
|
},
|
|
}
|
|
: {};
|
|
return $self->search( $or, $join );
|
|
}
|
|
|
|
=head3 merge_with
|
|
|
|
$new_attributes is an arrayref of hashrefs
|
|
|
|
=cut
|
|
|
|
sub merge_with {
|
|
my ( $self, $new_attributes ) = @_;
|
|
|
|
my @merged = @{$self->unblessed};
|
|
my $attribute_types = { map { $_->code => $_->unblessed } Koha::Patron::Attribute::Types->search };
|
|
for my $attr ( @$new_attributes ) {
|
|
unless ( $attr->{code} ) {
|
|
warn "Cannot merge element: no 'code' defined";
|
|
next;
|
|
}
|
|
|
|
my $attribute_type = $attribute_types->{$attr->{code}};
|
|
# FIXME Do we need that here or let ->store do the job?
|
|
unless ( $attribute_type ) {
|
|
warn "Cannot merge element: unrecognized code = '$attr->{code}'";
|
|
next;
|
|
}
|
|
unless ( $attribute_type->{repeatable} ) {
|
|
# filter out any existing attributes of the same code
|
|
@merged = grep {$attr->{code} ne $_->{code}} @merged;
|
|
}
|
|
|
|
push @merged, $attr;
|
|
}
|
|
|
|
# WARNING - we would like to return a set, but $new_attributes is not in storage yet
|
|
# Maybe there is something obvious I (JD) am missing
|
|
return [ sort { $a->{code} cmp $b->{code} || $a->{attribute} cmp $b->{attribute} } @merged ];
|
|
}
|
|
|
|
=head3 _type
|
|
|
|
=cut
|
|
|
|
sub _type {
|
|
return 'BorrowerAttribute';
|
|
}
|
|
|
|
sub object_class {
|
|
return 'Koha::Patron::Attribute';
|
|
}
|
|
|
|
1;
|