From 9b2747a6c8677477b57b2cd237f2eb7ec5a25a02 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 12 Sep 2019 16:27:00 +0100 Subject: [PATCH] Bug 15497: Limit itemtypes by branch in additem.pl This patch adds a select multiple when you add/edit an itemtype, creates functions to return itemtypes by library, and filters the options of itemtype select in additem To test: 1) Apply this patch set 2) perl installer/data/mysql/updatedatabase.pl 3) In koha administration => item types, edit "Books" itemtype CHECK => there is now a select multiple whith libraries at the bottom 4) select centerville and save 5) set centerville as current libary 6) search for any biblio in the catalog 7) click on "edit items" CHECK => book item type is present 8) set any other libary as current library SUCCESS => book item type is not present 9) Sign off Sponsored-by: Northeast Kansas Library System Sponsored-by: Southeast Kansas Library System Sponsored-by: Central Kansas Library System Signed-off-by: Jessica Zairo Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize --- Koha/ItemType.pm | 16 ++++++- Koha/ItemTypes.pm | 47 ++++++++++++++++++- admin/itemtypes.pl | 25 +++++++++- cataloguing/additem.pl | 8 +++- .../prog/en/modules/admin/itemtypes.tt | 13 +++++ 5 files changed, 104 insertions(+), 5 deletions(-) diff --git a/Koha/ItemType.pm b/Koha/ItemType.pm index 63c113c83d..e33f0e66e4 100644 --- a/Koha/ItemType.pm +++ b/Koha/ItemType.pm @@ -25,7 +25,7 @@ use Koha::Database; use Koha::IssuingRules; use Koha::Localizations; -use base qw(Koha::Object); +use base qw(Koha::Object Koha::Object::Limit::Library); =head1 NAME @@ -125,6 +125,20 @@ sub may_article_request { return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{}; } +=head3 _library_limits + + configure library limits + +=cut + +sub _library_limits { + return { + class => "ItemtypesBranch", + id => "itemtype", + library => "branchcode", + }; +} + =head3 type =cut diff --git a/Koha/ItemTypes.pm b/Koha/ItemTypes.pm index a3786cbab3..e0f9ad3bcd 100644 --- a/Koha/ItemTypes.pm +++ b/Koha/ItemTypes.pm @@ -55,7 +55,48 @@ sub search_with_localization { -as => 'translated_description' } ]; - $self->SUPER::search( $params, $attributes ); + if(defined $params->{branchcode}) { + $self->search_with_library_limits( $params, $attributes ); + } else { + $self->SUPER::search( $params, $attributes ); + } +} + +=head3 search_with_library_limits + +search itemtypes by library + +my @itemtypes = Koha::ItemTypes->search_with_library_limits({branchcode => branchcode}); + +=cut + +sub search_with_library_limits { + my ( $self, $params, $attributes ) = @_; + + my $branchcode = $params->{branchcode}; + delete( $params->{branchcode} ); + + return $self->SUPER::search( $params, $attributes ) unless $branchcode; + + my $where = { + '-or' => [ + 'itemtypes_branches.branchcode' => undef, + 'itemtypes_branches.branchcode' => $branchcode + ] + }; + + $attributes //= {}; + if(exists $attributes->{join}) { + if(ref $attributes->{join} eq 'ARRAY') { + push @{$attributes->{join}}, 'itemtypes_branches'; + } else { + $attributes->{join} = [ $attributes->{join}, 'itemtypes_branches' ]; + } + } else { + $attributes->{join} = 'itemtypes_branches'; + } + + return $self->SUPER::search( { %$params, %$where, }, $attributes ); } =head3 type @@ -66,6 +107,10 @@ sub _type { return 'Itemtype'; } +=head3 object_class + +=cut + sub object_class { return 'Koha::ItemType'; } diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl index 48ddbfaef1..17ec3dc354 100755 --- a/admin/itemtypes.pl +++ b/admin/itemtypes.pl @@ -58,6 +58,19 @@ undef($sip_media_type) if defined($sip_media_type) and $sip_media_type =~ /^\s*$ if ( $op eq 'add_form' ) { my $itemtype = Koha::ItemTypes->find($itemtype_code); + + my $selected_branches = $itemtype->get_library_limits; + my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed; + my @branches_loop; + foreach my $branch ( @$branches ) { + my $selected = ($selected_branches && grep {$_->branchcode eq $branch->{branchcode}} @{ $selected_branches->as_list } ) ? 1 : 0; + push @branches_loop, { + branchcode => $branch->{branchcode}, + branchname => $branch->{branchname}, + selected => $selected, + }; + } + my $imagesets = C4::Koha::getImageSets( checked => ( $itemtype ? $itemtype->imageurl : undef ) ); my $searchcategory = GetAuthorisedValues("ITEMTYPECAT"); my $translated_languages = C4::Languages::getTranslatedLanguages( undef , C4::Context->preference('template') ); @@ -66,6 +79,7 @@ if ( $op eq 'add_form' ) { imagesets => $imagesets, searchcategory => $searchcategory, can_be_translated => ( scalar(@$translated_languages) > 1 ? 1 : 0 ), + branches_loop => \@branches_loop, ); } elsif ( $op eq 'add_validate' ) { my $is_a_modif = $input->param('is_a_modif'); @@ -77,6 +91,7 @@ if ( $op eq 'add_form' ) { my $defaultreplacecost = $input->param('defaultreplacecost'); my $processfee = $input->param('processfee'); my $image = $input->param('image') || q||; + my @branches = grep { $_ ne q{} } $input->multi_param('branches'); my $notforloan = $input->param('notforloan') ? 1 : 0; my $imageurl = @@ -107,7 +122,10 @@ if ( $op eq 'add_form' ) { $itemtype->hideinopac($hideinopac); $itemtype->searchcategory($searchcategory); - eval { $itemtype->store; }; + eval { + $itemtype->store; + $itemtype->replace_library_limits( \@branches ); + }; if ($@) { push @messages, { type => 'alert', code => 'error_on_update' }; @@ -134,7 +152,10 @@ if ( $op eq 'add_form' ) { searchcategory => $searchcategory, } ); - eval { $itemtype->store; }; + eval { + $itemtype->store; + $itemtype->replace_library_limits( \@branches ); + }; if ($@) { push @messages, { type => 'alert', code => 'error_on_insert' }; diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl index ce12b0a934..52a8ca13a9 100755 --- a/cataloguing/additem.pl +++ b/cataloguing/additem.pl @@ -183,7 +183,13 @@ sub generate_subfield_form { } elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) { push @authorised_values, ""; - my $itemtypes = Koha::ItemTypes->search_with_localization; + my $branch_limit = C4::Context->userenv && C4::Context->userenv->{"branch"}; + my $itemtypes; + if($branch_limit) { + $itemtypes = Koha::ItemTypes->search_with_localization({branchcode => $branch_limit}); + } else { + $itemtypes = Koha::ItemTypes->search_with_localization; + } while ( my $itemtype = $itemtypes->next ) { push @authorised_values, $itemtype->itemtype; $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt index 0fa08ac030..6ac7988e60 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt @@ -283,6 +283,19 @@ Item types administration [% END %] +
  • + + Select 'All libraries' if this authorized value must be displayed all the time. Otherwise select libraries you want to associate with this value. +
  • -- 2.20.1