From c65aa487bd1b97e53d1b0a790082a7e797f8a863 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 16 Oct 2019 14:49:42 +0200 Subject: [PATCH] Bug 23271: Use Koha::Object::Limit::Library for Koha::Patron::Categories Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall --- Koha/Patron/Category.pm | 127 +++--------------- admin/categories.pl | 23 +--- .../prog/en/modules/admin/categories.tt | 32 +++-- t/db_dependent/Koha/Patron/Categories.t | 7 +- 4 files changed, 37 insertions(+), 152 deletions(-) diff --git a/Koha/Patron/Category.pm b/Koha/Patron/Category.pm index 7a4541ebc4..713dcf9249 100644 --- a/Koha/Patron/Category.pm +++ b/Koha/Patron/Category.pm @@ -25,7 +25,7 @@ use C4::Members::Messaging; use Koha::Database; use Koha::DateUtils; -use base qw(Koha::Object); +use base qw(Koha::Object Koha::Object::Limit::Library); =head1 NAME @@ -103,117 +103,6 @@ sub default_messaging { return \@messaging; } -=head3 branch_limitations - -my $limitations = $category->branch_limitations(); - -$category->branch_limitations( \@branchcodes ); - -=cut - -sub branch_limitations { - my ( $self, $branchcodes ) = @_; - - if ($branchcodes) { - return $self->replace_branch_limitations($branchcodes); - } - else { - return $self->get_branch_limitations(); - } - -} - -=head3 get_branch_limitations - -my $limitations = $category->get_branch_limitations(); - -=cut - -sub get_branch_limitations { - my ($self) = @_; - - my @branchcodes = - $self->_catb_resultset->search( { categorycode => $self->categorycode } ) - ->get_column('branchcode')->all(); - - return \@branchcodes; -} - -=head3 add_branch_limitation - -$category->add_branch_limitation( $branchcode ); - -=cut - -sub add_branch_limitation { - my ( $self, $branchcode ) = @_; - - croak("No branchcode passed in!") unless $branchcode; - - my $limitation = $self->_catb_resultset->update_or_create( - { categorycode => $self->categorycode, branchcode => $branchcode } ); - - return $limitation ? 1 : undef; -} - -=head3 del_branch_limitation - -$category->del_branch_limitation( $branchcode ); - -=cut - -sub del_branch_limitation { - my ( $self, $branchcode ) = @_; - - croak("No branchcode passed in!") unless $branchcode; - - my $limitation = - $self->_catb_resultset->find( - { categorycode => $self->categorycode, branchcode => $branchcode } ); - - unless ($limitation) { - my $categorycode = $self->categorycode; - carp( -"No branch limit for branch $branchcode found for categorycode $categorycode to delete!" - ); - return; - } - - return $limitation->delete(); -} - -=head3 replace_branch_limitations - -$category->replace_branch_limitations( \@branchcodes ); - -=cut - -sub replace_branch_limitations { - my ( $self, $branchcodes ) = @_; - - $self->_catb_resultset->search( { categorycode => $self->categorycode } )->delete; - - my @return_values = - map { $self->add_branch_limitation($_) } @$branchcodes; - - return \@return_values; -} - -=head3 Koha::Objects->_catb_resultset - -Returns the internal resultset or creates it if undefined - -=cut - -sub _catb_resultset { - my ($self) = @_; - - $self->{_catb_resultset} ||= - Koha::Database->new->schema->resultset('CategoriesBranch'); - - return $self->{_catb_resultset}; -} - sub get_expiry_date { my ($self, $date ) = @_; if ( $self->enrolmentperiod ) { @@ -299,6 +188,20 @@ sub override_hidden_items { =head2 Internal methods +=head3 _library_limits + + configure library limits + +=cut + +sub _library_limits { + return { + class => "CategoriesBranch", + id => "categorycode", + library => "branchcode", + }; +} + =head3 type =cut diff --git a/admin/categories.pl b/admin/categories.pl index 9bf5d365a1..afc2950468 100755 --- a/admin/categories.pl +++ b/admin/categories.pl @@ -48,26 +48,9 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( ); if ( $op eq 'add_form' ) { - my ( $category, $selected_branches ); - if ($categorycode) { - $category = Koha::Patron::Categories->find($categorycode); - $selected_branches = $category->branch_limitations; - } - - my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed; - my @branches_loop; - foreach my $branch ( @$branches ) { - my $selected = ( grep { $_ eq $branch->{branchcode} } @$selected_branches ) ? 1 : 0; - push @branches_loop, - { branchcode => $branch->{branchcode}, - branchname => $branch->{branchname}, - selected => $selected, - }; - } $template->param( - category => $category, - branches_loop => \@branches_loop, + category => scalar Koha::Patron::Categories->find($categorycode), ); if ( C4::Context->preference('EnhancedMessagingPreferences') ) { @@ -138,7 +121,7 @@ elsif ( $op eq 'add_validate' ) { $category->require_strong_password($require_strong_password); eval { $category->store; - $category->replace_branch_limitations( \@branches ); + $category->replace_library_limits( \@branches ); }; if ( $@ ) { push @messages, {type => 'error', code => 'error_on_update' }; @@ -170,7 +153,7 @@ elsif ( $op eq 'add_validate' ) { }); eval { $category->store; - $category->replace_branch_limitations( \@branches ); + $category->replace_library_limits( \@branches ); }; if ( $@ ) { diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categories.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categories.tt index 54831764e2..1a5faa0b6c 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categories.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categories.tt @@ -1,6 +1,7 @@ [% USE raw %] [% USE Asset %] [% USE Koha %] +[% USE Branches %] [% USE KohaDates %] [% USE Price %] [% USE TablesSettings %] @@ -217,13 +218,7 @@
  • Select All libraries if this category type must to be displayed all the time. Otherwise select libraries you want to associate with this value. @@ -613,19 +608,22 @@ [% END %] - [% SET branch_limitations = category.branch_limitations %] - [% IF branch_limitations.size > 0 %] - [% branches_str = "" %] - [% FOREACH branch IN branch_limitations %] - [% branches_str = branches_str _ " " _ branch.branchname _ "(" _ branch.branchcode _ ")" %] + [% SET library_limits = category.library_limits %] + [% IF library_limits.count > 0 %] + [% library_str = "" %] + [% FOREACH library IN library_limits %] + [%- IF loop.first -%] + [% library_str = library.branchname _ " (" _ library.branchcode _ ")" %] + [% ELSE %] + [% library_str = library_str _ "\n" _ library.branchname _ " (" _ library.branchcode _ ")" %] + [% END %] [% END %] - - [% IF branch_limitations.size > 1 %] - [% branch_limitations.size | html %] library limitations + + [% IF library_limits.count > 1 %] + [% library_limits.count | html %] library limitations [% ELSE %] - [% branch_limitations.size | html %] library limitation + [% library_limits.count | html %] library limitation [% END %] - [% ELSE %] No limitation [% END %] diff --git a/t/db_dependent/Koha/Patron/Categories.t b/t/db_dependent/Koha/Patron/Categories.t index 58882a493e..112c699066 100755 --- a/t/db_dependent/Koha/Patron/Categories.t +++ b/t/db_dependent/Koha/Patron/Categories.t @@ -34,14 +34,15 @@ my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; my $builder = t::lib::TestBuilder->new; -my $branch = $builder->build({ source => 'Branch', }); +my $library_1 = $builder->build_object({ class => 'Koha::Libraries', }); +my $library_2 = $builder->build_object({ class => 'Koha::Libraries', }); my $nb_of_categories = Koha::Patron::Categories->search->count; my $new_category_1 = Koha::Patron::Category->new({ categorycode => 'mycatcodeX', category_type => 'A', description => 'mycatdescX', })->store; -$new_category_1->add_branch_limitation( $branch->{branchcode} ); +$new_category_1->replace_library_limits( [ $library_1->branchcode, $library_2->branchcode ] ); my $new_category_2 = Koha::Patron::Category->new({ categorycode => 'mycatcodeY', category_type => 'S', @@ -53,7 +54,7 @@ is( Koha::Patron::Categories->search->count, $nb_of_categories + 2, 'The 2 patro my $retrieved_category_1 = Koha::Patron::Categories->find( $new_category_1->categorycode ); is( $retrieved_category_1->categorycode, $new_category_1->categorycode, 'Find a patron category by categorycode should return the correct category' ); -is_deeply( $retrieved_category_1->branch_limitations, [ $branch->{branchcode} ], 'The branch limitation should have been stored and retrieved' ); +is_deeply( [ $retrieved_category_1->library_limits->get_column('branchcode') ], [ $library_1->branchcode, $library_2->branchcode ], 'The branch limitation should have been stored and retrieved' ); is_deeply( $retrieved_category_1->default_messaging, [], 'By default there is not messaging option' ); my $retrieved_category_2 = Koha::Patron::Categories->find( $new_category_2->categorycode ); -- 2.24.3 (Apple Git-128)