Bug 15497: Limit itemtypes by branch in additem.pl
authorMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 12 Sep 2019 15:27:00 +0000 (16:27 +0100)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 12 Sep 2019 15:31:06 +0000 (16:31 +0100)
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 <jzairo@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/ItemType.pm
Koha/ItemTypes.pm
admin/itemtypes.pl
cataloguing/additem.pl
koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt

index 63c113c..e33f0e6 100644 (file)
@@ -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
index a3786cb..e0f9ad3 100644 (file)
@@ -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';
 }
index 48ddbfa..17ec3dc 100755 (executable)
@@ -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' };
index ce12b0a..52a8ca1 100755 (executable)
@@ -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;
index 0fa08ac..6ac7988 100644 (file)
@@ -283,6 +283,19 @@ Item types administration
                         [% END %]
                     </select>
                 </li>
+                <li><label for="branches">Libraries limitation: </label>
+                    <select id="branches" name="branches" multiple size="10">
+                        <option value="">All libraries</option>
+                        [% FOREACH branch IN branches_loop %]
+                        [% IF ( branch.selected ) %]
+                            <option selected="selected" value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
+                        [% ELSE %]
+                            <option value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
+                        [% END %]
+                        [% END %]
+                    </select>
+                    <span>Select 'All libraries' if this authorized value must be displayed all the time. Otherwise select libraries you want to associate with this value.</span>
+                </li>
                 <li>
                     <label for="summary">Summary: </label>
                    <textarea id="summary" name="summary" cols="55" rows="5">[% itemtype.summary | html %]</textarea>