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 299-304 sub override_hidden_items { Link Here
299
188
300
=head2 Internal methods
189
=head2 Internal methods
301
190
191
=head3 _library_limits
192
193
 configure library limits
194
195
=cut
196
197
sub _library_limits {
198
    return {
199
        class => "CategoriesBranch",
200
        id => "categorycode",
201
        library => "branchcode",
202
    };
203
}
204
302
=head3 type
205
=head3 type
303
206
304
=cut
207
=cut
(-)a/admin/categories.pl (-20 / +3 lines)
Lines 48-73 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
48
);
48
);
49
49
50
if ( $op eq 'add_form' ) {
50
if ( $op eq 'add_form' ) {
51
    my ( $category, $selected_branches );
52
    if ($categorycode) {
53
        $category          = Koha::Patron::Categories->find($categorycode);
54
        $selected_branches = $category->branch_limitations;
55
    }
56
57
    my $branches = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed;
58
    my @branches_loop;
59
    foreach my $branch ( @$branches ) {
60
        my $selected = ( grep { $_ eq $branch->{branchcode} } @$selected_branches ) ? 1 : 0;
61
        push @branches_loop,
62
          { branchcode => $branch->{branchcode},
63
            branchname => $branch->{branchname},
64
            selected   => $selected,
65
          };
66
    }
67
51
68
    $template->param(
52
    $template->param(
69
        category => $category,
53
        category => scalar Koha::Patron::Categories->find($categorycode),
70
        branches_loop       => \@branches_loop,
71
    );
54
    );
72
55
73
    if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
56
    if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
Lines 138-144 elsif ( $op eq 'add_validate' ) { Link Here
138
        $category->require_strong_password($require_strong_password);
121
        $category->require_strong_password($require_strong_password);
139
        eval {
122
        eval {
140
            $category->store;
123
            $category->store;
141
            $category->replace_branch_limitations( \@branches );
124
            $category->replace_library_limits( \@branches );
142
        };
125
        };
143
        if ( $@ ) {
126
        if ( $@ ) {
144
            push @messages, {type => 'error', code => 'error_on_update' };
127
            push @messages, {type => 'error', code => 'error_on_update' };
Lines 170-176 elsif ( $op eq 'add_validate' ) { Link Here
170
        });
153
        });
171
        eval {
154
        eval {
172
            $category->store;
155
            $category->store;
173
            $category->replace_branch_limitations( \@branches );
156
            $category->replace_library_limits( \@branches );
174
        };
157
        };
175
158
176
        if ( $@ ) {
159
        if ( $@ ) {
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categories.tt (-17 / +15 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 TablesSettings %]
7
[% USE TablesSettings %]
Lines 217-229 Link Here
217
                <li><label for="branches">Library limitations: </label>
218
                <li><label for="branches">Library limitations: </label>
218
                    <select id="branches" name="branches" multiple size="10">
219
                    <select id="branches" name="branches" multiple size="10">
219
                        <option value="">All libraries</option>
220
                        <option value="">All libraries</option>
220
                        [% FOREACH branch IN branches_loop %]
221
                        [% PROCESS options_for_libraries libraries => Branches.all( selecteds => category.get_library_limits, unfiltered => 1, do_not_select_my_library => 1 ) %]
221
                          [% IF branch.selected %]
222
                            <option selected="selected" value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
223
                          [% ELSE %]
224
                            <option value="[% branch.branchcode | html %]">[% branch.branchname | html %]</option>
225
                          [% END %]
226
                        [% END %]
227
                    </select>
222
                    </select>
228
                    <span>Select <em>All libraries</em> if this category type must to be displayed all the time. Otherwise select libraries you want to associate with this value.
223
                    <span>Select <em>All libraries</em> if this category type must to be displayed all the time. Otherwise select libraries you want to associate with this value.
229
                    </span>
224
                    </span>
Lines 613-631 Link Here
613
                            </td>
608
                            </td>
614
                        [% END %]
609
                        [% END %]
615
                        <td>
610
                        <td>
616
                            [% SET branch_limitations = category.branch_limitations %]
611
                            [% SET library_limits = category.library_limits %]
617
                            [% IF branch_limitations.size > 0 %]
612
                            [% IF library_limits.count > 0 %]
618
                                [% branches_str = "" %]
613
                                [% library_str = "" %]
619
                                [% FOREACH branch IN branch_limitations %]
614
                                [% FOREACH library IN library_limits %]
620
                                    [% branches_str = branches_str _ " " _ branch.branchname _ "(" _ branch.branchcode _ ")" %]
615
                                    [%- IF loop.first -%]
616
                                        [% library_str = library.branchname _ " (" _ library.branchcode _ ")" %]
617
                                    [% ELSE %]
618
                                        [% library_str = library_str _ "\n" _ library.branchname _ " (" _ library.branchcode _ ")" %]
619
                                    [% END %]
621
                                [% END %]
620
                                [% END %]
622
                                <span title="[% branches_str | html %]">
621
                                <span class="library_limitation" title="[% library_str | html %]">
623
                                    [% IF branch_limitations.size > 1 %]
622
                                    [% IF library_limits.count > 1 %]
624
                                        [% branch_limitations.size | html %] library limitations
623
                                        [% library_limits.count | html %] library limitations
625
                                    [% ELSE %]
624
                                    [% ELSE %]
626
                                        [% branch_limitations.size | html %] library limitation
625
                                        [% library_limits.count | html %] library limitation
627
                                    [% END %]
626
                                    [% END %]
628
                                </span>
629
                            [% ELSE %]
627
                            [% ELSE %]
630
                                No limitation
628
                                No limitation
631
                            [% END %]
629
                            [% 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