@@ -, +, @@ --- 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(-) --- a/Koha/ItemType.pm +++ a/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 --- a/Koha/ItemTypes.pm +++ a/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'; } --- a/admin/itemtypes.pl +++ a/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' }; --- a/cataloguing/additem.pl +++ a/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; --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt +++ a/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. +
  • --