@@ -, +, @@ Koha::Patron::Categories --- 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(-) --- a/Koha/Patron/Category.pm +++ a/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 --- a/admin/categories.pl +++ a/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 ( $@ ) { --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categories.tt +++ a/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 @@