View | Details | Raw Unified | Return to bug 23271
Collapse All | Expand All

(-)a/Koha/Patron/Category.pm (-112 / +15 lines)
Lines 25-31 use C4::Members::Messaging; Link Here
25
use Koha::Database;
25
use Koha::Database;
26
use Koha::DateUtils;
26
use Koha::DateUtils;
27
27
28
use base qw(Koha::Object);
28
use base qw(Koha::Object Koha::Object::Limit::Library);
29
29
30
=head1 NAME
30
=head1 NAME
31
31
Lines 103-219 sub default_messaging { Link Here
103
    return \@messaging;
103
    return \@messaging;
104
}
104
}
105
105
106
=head3 branch_limitations
107
108
my $limitations = $category->branch_limitations();
109
110
$category->branch_limitations( \@branchcodes );
111
112
=cut
113
114
sub branch_limitations {
115
    my ( $self, $branchcodes ) = @_;
116
117
    if ($branchcodes) {
118
        return $self->replace_branch_limitations($branchcodes);
119
    }
120
    else {
121
        return $self->get_branch_limitations();
122
    }
123
124
}
125
126
=head3 get_branch_limitations
127
128
my $limitations = $category->get_branch_limitations();
129
130
=cut
131
132
sub get_branch_limitations {
133
    my ($self) = @_;
134
135
    my @branchcodes =
136
      $self->_catb_resultset->search( { categorycode => $self->categorycode } )
137
      ->get_column('branchcode')->all();
138
139
    return \@branchcodes;
140
}
141
142
=head3 add_branch_limitation
143
144
$category->add_branch_limitation( $branchcode );
145
146
=cut
147
148
sub add_branch_limitation {
149
    my ( $self, $branchcode ) = @_;
150
151
    croak("No branchcode passed in!") unless $branchcode;
152
153
    my $limitation = $self->_catb_resultset->update_or_create(
154
        { categorycode => $self->categorycode, branchcode => $branchcode } );
155
156
    return $limitation ? 1 : undef;
157
}
158
159
=head3 del_branch_limitation
160
161
$category->del_branch_limitation( $branchcode );
162
163
=cut
164
165
sub del_branch_limitation {
166
    my ( $self, $branchcode ) = @_;
167
168
    croak("No branchcode passed in!") unless $branchcode;
169
170
    my $limitation =
171
      $self->_catb_resultset->find(
172
        { categorycode => $self->categorycode, branchcode => $branchcode } );
173
174
    unless ($limitation) {
175
        my $categorycode = $self->categorycode;
176
        carp(
177
"No branch limit for branch $branchcode found for categorycode $categorycode to delete!"
178
        );
179
        return;
180
    }
181
182
    return $limitation->delete();
183
}
184
185
=head3 replace_branch_limitations
186
187
$category->replace_branch_limitations( \@branchcodes );
188
189
=cut
190
191
sub replace_branch_limitations {
192
    my ( $self, $branchcodes ) = @_;
193
194
    $self->_catb_resultset->search( { categorycode => $self->categorycode } )->delete;
195
196
    my @return_values =
197
      map { $self->add_branch_limitation($_) } @$branchcodes;
198
199
    return \@return_values;
200
}
201
202
=head3 Koha::Objects->_catb_resultset
203
204
Returns the internal resultset or creates it if undefined
205
206
=cut
207
208
sub _catb_resultset {
209
    my ($self) = @_;
210
211
    $self->{_catb_resultset} ||=
212
      Koha::Database->new->schema->resultset('CategoriesBranch');
213
214
    return $self->{_catb_resultset};
215
}
216
217
sub get_expiry_date {
106
sub get_expiry_date {
218
    my ($self, $date ) = @_;
107
    my ($self, $date ) = @_;
219
    if ( $self->enrolmentperiod ) {
108
    if ( $self->enrolmentperiod ) {
Lines 275-280 sub override_hidden_items { Link Here
275
164
276
=head2 Internal methods
165
=head2 Internal methods
277
166
167
=head3 _library_limits
168
169
 configure library limits
170
171
=cut
172
173
sub _library_limits {
174
    return {
175
        class => "CategoriesBranch",
176
        id => "categorycode",
177
        library => "branchcode",
178
    };
179
}
180
278
=head3 type
181
=head3 type
279
182
280
=cut
183
=cut
(-)a/admin/categories.pl (-20 / +3 lines)
Lines 49-74 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
49
);
49
);
50
50
51
if ( $op eq 'add_form' ) {
51
if ( $op eq 'add_form' ) {
52
    my ( $category, $selected_branches );
53
    if ($categorycode) {
54
        $category          = Koha::Patron::Categories->find($categorycode);
55
        $selected_branches = $category->branch_limitations;
56
    }
57
58
    my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
59
    my @branches_loop;
60
    foreach my $branch ( @$branches ) {
61
        my $selected = ( grep { $_ eq $branch->{branchcode} } @$selected_branches ) ? 1 : 0;
62
        push @branches_loop,
63
          { branchcode => $branch->{branchcode},
64
            branchname => $branch->{branchname},
65
            selected   => $selected,
66
          };
67
    }
68
52
69
    $template->param(
53
    $template->param(
70
        category => $category,
54
        category => scalar Koha::Patron::Categories->find($categorycode),
71
        branches_loop       => \@branches_loop,
72
    );
55
    );
73
56
74
    if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
57
    if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
Lines 131-137 elsif ( $op eq 'add_validate' ) { Link Here
131
        $category->change_password($change_password);
114
        $category->change_password($change_password);
132
        eval {
115
        eval {
133
            $category->store;
116
            $category->store;
134
            $category->replace_branch_limitations( \@branches );
117
            $category->replace_library_limits( \@branches );
135
        };
118
        };
136
        if ( $@ ) {
119
        if ( $@ ) {
137
            push @messages, {type => 'error', code => 'error_on_update' };
120
            push @messages, {type => 'error', code => 'error_on_update' };
Lines 160-166 elsif ( $op eq 'add_validate' ) { Link Here
160
        });
143
        });
161
        eval {
144
        eval {
162
            $category->store;
145
            $category->store;
163
            $category->replace_branch_limitations( \@branches );
146
            $category->replace_library_limits( \@branches );
164
        };
147
        };
165
148
166
        if ( $@ ) {
149
        if ( $@ ) {
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categories.tt (-18 / +16 lines)
Lines 1-6 Link Here
1
[% USE raw %]
1
[% USE raw %]
2
[% USE Asset %]
2
[% USE Asset %]
3
[% USE Koha %]
3
[% USE Koha %]
4
[% USE Branches %]
4
[% USE KohaDates %]
5
[% USE KohaDates %]
5
[% USE Price %]
6
[% USE Price %]
6
[% USE ColumnsSettings %]
7
[% USE ColumnsSettings %]
Lines 151-166 Link Here
151
                    </select>
152
                    </select>
152
                    <span class="required">Required</span>
153
                    <span class="required">Required</span>
153
                </li>
154
                </li>
154
                <li><label for="branches">Library limitations: </label>
155
                <li><label for="branches">Library limitation: </label>
155
                    <select id="branches" name="branches" multiple size="10">
156
                    <select id="branches" name="branches" multiple size="10">
156
                        <option value="">All libraries</option>
157
                        <option value="">All libraries</option>
157
                        [% FOREACH branch IN branches_loop %]
158
                        [% PROCESS options_for_libraries libraries => Branches.all( selected => category.get_library_limits, unfiltered => 1, do_not_select_my_library => 1 ) %]
158
                          [% IF branch.selected %]
159
                            <option selected="selected" value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
160
                          [% ELSE %]
161
                            <option value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
162
                          [% END %]
163
                        [% END %]
164
                    </select>
159
                    </select>
165
                    <span>Select <i>All libraries</i> if this category type must to be displayed all the time. Otherwise select libraries you want to associate with this value.
160
                    <span>Select <i>All libraries</i> if this category type must to be displayed all the time. Otherwise select libraries you want to associate with this value.
166
                    </span>
161
                    </span>
Lines 497-515 Link Here
497
                            </td>
492
                            </td>
498
                        [% END %]
493
                        [% END %]
499
                        <td>
494
                        <td>
500
                            [% SET branch_limitations = category.branch_limitations %]
495
                            [% SET library_limits = category.library_limits %]
501
                            [% IF branch_limitations.size > 0 %]
496
                            [% IF library_limits.count > 0 %]
502
                                [% branches_str = "" %]
497
                                [% library_str = "" %]
503
                                [% FOREACH branch IN branch_limitations %]
498
                                [% FOREACH library IN library_limits %]
504
                                    [% branches_str = branches_str _ " " _ branch.branchname _ "(" _ branch.branchcode _ ")" %]
499
                                    [%- IF loop.first -%]
500
                                        [% library_str = library.branchname _ " (" _ library.branchcode _ ")" %]
501
                                    [% ELSE %]
502
                                        [% library_str = library_str _ "\n" _ library.branchname _ " (" _ library.branchcode _ ")" %]
503
                                    [% END %]
505
                                [% END %]
504
                                [% END %]
506
                                <span title="[% branches_str | html %]">
505
                                <span class="library_limitation" title="[% library_str | html %]">
507
                                    [% IF branch_limitations.size > 1 %]
506
                                    [% IF library_limits.count > 1 %]
508
                                        [% branch_limitations.size | html %] library limitations
507
                                        [% library_limits.count | html %] library limitations
509
                                    [% ELSE %]
508
                                    [% ELSE %]
510
                                        [% branch_limitations.size | html %] library limitation
509
                                        [% library_limits.count | html %] library limitation
511
                                    [% END %]
510
                                    [% END %]
512
                                </span>
513
                            [% ELSE %]
511
                            [% ELSE %]
514
                                No limitation
512
                                No limitation
515
                            [% END %]
513
                            [% END %]
(-)a/t/db_dependent/Koha/Patron/Categories.t (-4 / +4 lines)
Lines 34-47 my $schema = Koha::Database->new->schema; Link Here
34
$schema->storage->txn_begin;
34
$schema->storage->txn_begin;
35
35
36
my $builder = t::lib::TestBuilder->new;
36
my $builder = t::lib::TestBuilder->new;
37
my $branch = $builder->build({ source => 'Branch', });
37
my $library_1 = $builder->build_object({ class => 'Koha::Libraries', });
38
my $library_2 = $builder->build_object({ class => 'Koha::Libraries', });
38
my $nb_of_categories = Koha::Patron::Categories->search->count;
39
my $nb_of_categories = Koha::Patron::Categories->search->count;
39
my $new_category_1 = Koha::Patron::Category->new({
40
my $new_category_1 = Koha::Patron::Category->new({
40
    categorycode => 'mycatcodeX',
41
    categorycode => 'mycatcodeX',
41
    category_type => 'A',
42
    category_type => 'A',
42
    description  => 'mycatdescX',
43
    description  => 'mycatdescX',
43
})->store;
44
})->store;
44
$new_category_1->add_branch_limitation( $branch->{branchcode} );
45
$new_category_1->replace_library_limits( [ $library_1->branchcode, $library_2->branchcode ] );
45
my $new_category_2 = Koha::Patron::Category->new({
46
my $new_category_2 = Koha::Patron::Category->new({
46
    categorycode => 'mycatcodeY',
47
    categorycode => 'mycatcodeY',
47
    category_type => 'S',
48
    category_type => 'S',
Lines 53-59 is( Koha::Patron::Categories->search->count, $nb_of_categories + 2, 'The 2 patro Link Here
53
54
54
my $retrieved_category_1 = Koha::Patron::Categories->find( $new_category_1->categorycode );
55
my $retrieved_category_1 = Koha::Patron::Categories->find( $new_category_1->categorycode );
55
is( $retrieved_category_1->categorycode, $new_category_1->categorycode, 'Find a patron category by categorycode should return the correct category' );
56
is( $retrieved_category_1->categorycode, $new_category_1->categorycode, 'Find a patron category by categorycode should return the correct category' );
56
is_deeply( $retrieved_category_1->branch_limitations, [ $branch->{branchcode} ], 'The branch limitation should have been stored and retrieved' );
57
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' );
57
is_deeply( $retrieved_category_1->default_messaging, [], 'By default there is not messaging option' );
58
is_deeply( $retrieved_category_1->default_messaging, [], 'By default there is not messaging option' );
58
59
59
my $retrieved_category_2 = Koha::Patron::Categories->find( $new_category_2->categorycode );
60
my $retrieved_category_2 = Koha::Patron::Categories->find( $new_category_2->categorycode );
60
- 

Return to bug 23271