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

(-)a/Koha/AuthorisedValue.pm (+178 lines)
Line 0 Link Here
1
package Koha::AuthorisedValue;
2
3
# Copyright ByWater Solutions 2014
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Carp;
23
24
use Koha::Database;
25
26
use base qw(Koha::Object);
27
28
=head1 NAME
29
30
Koha::AuthorisedValue - Koha Authorised Value Object class
31
32
=head1 API
33
34
=head2 Class Methods
35
36
=cut
37
38
=head3 branch_limitations
39
40
my $limitations = $av->branch_limitations();
41
42
$av->branch_limitations( \@branchcodes );
43
44
=cut
45
46
sub branch_limitations {
47
    my ( $self, $branchcodes ) = @_;
48
49
    if ($branchcodes) {
50
        return $self->replace_branch_limitations($branchcodes);
51
    }
52
    else {
53
        return $self->get_branch_limitations();
54
    }
55
56
}
57
58
=head3 get_branch_limitations
59
60
my $limitations = $av->get_branch_limitations();
61
62
=cut
63
64
sub get_branch_limitations {
65
    my ($self) = @_;
66
67
    my @branchcodes =
68
      $self->_avb_resultset->search( { av_id => $self->id() } )
69
      ->get_column('branchcode')->all();
70
71
    return \@branchcodes;
72
}
73
74
=head3 add_branch_limitation
75
76
$av->add_branch_limitation( $branchcode );
77
78
=cut
79
80
sub add_branch_limitation {
81
    my ( $self, $branchcode ) = @_;
82
83
    croak("No branchcode passed in!") unless $branchcode;
84
85
    my $limitation = $self->_avb_resultset->update_or_create(
86
        { av_id => $self->id(), branchcode => $branchcode } );
87
88
    return $limitation ? 1 : undef;
89
}
90
91
=head3 add_branch_limitation
92
93
$av->del_branch_limitation( $branchcode );
94
95
=cut
96
97
sub del_branch_limitation {
98
    my ( $self, $branchcode ) = @_;
99
100
    croak("No branchcode passed in!") unless $branchcode;
101
102
    my $limitation =
103
      $self->_avb_resultset->find(
104
        { av_id => $self->id(), branchcode => $branchcode } );
105
106
    unless ($limitation) {
107
        my $id = $self->id();
108
        carp(
109
"No branch limit for branch $branchcode found for av_id $id to delete!"
110
        );
111
        return;
112
    }
113
114
    return $limitation->delete();
115
}
116
117
=head3 replace_branch_limitations
118
119
$av->replace_branch_limitations( \@branchcodes );
120
121
=cut
122
123
sub replace_branch_limitations {
124
    my ( $self, $branchcodes ) = @_;
125
126
    $self->_avb_resultset->search( { av_id => $self->id() } )->delete();
127
128
    my @return_values =
129
      map { $self->add_branch_limitation($_) } @$branchcodes;
130
131
    return \@return_values;
132
}
133
134
=head3 lib_opac
135
136
my $description = $av->lib_opac();
137
138
$av->lib_opac( $description );
139
140
=cut
141
142
sub opac_description {
143
    my ( $self, $value ) = @_;
144
145
    return $self->lib_opac() || $self->lib();
146
}
147
148
=head3 Koha::Objects->_resultset
149
150
Returns the internal resultset or creates it if undefined
151
152
=cut
153
154
sub _avb_resultset {
155
    my ($self) = @_;
156
157
    $self->{_avb_resultset} ||=
158
      Koha::Database->new()->schema()->resultset('AuthorisedValuesBranch');
159
160
    $self->{_avb_resultset};
161
}
162
163
=head3 type
164
165
=cut
166
167
sub type {
168
    return 'AuthorisedValue';
169
}
170
171
=head1 AUTHOR
172
173
Kyle M Hall <kyle@bywatersolutions.com>
174
175
=cut
176
177
1;
178
## Please see file perltidy.ERR
(-)a/Koha/AuthorisedValues.pm (+88 lines)
Line 0 Link Here
1
package Koha::AuthorisedValues;
2
3
# Copyright ByWater Solutions 2014
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Carp;
23
24
use Koha::Database;
25
26
use Koha::Borrower;
27
28
use base qw(Koha::Objects);
29
30
=head1 NAME
31
32
Koha::Borrower - Koha Borrower Object class
33
34
=head1 API
35
36
=head2 Class Methods
37
38
=cut
39
40
=head3 Koha::AuthorisedValues->search();
41
42
my @objects = Koha::AuthorisedValues->search($params);
43
44
=cut
45
46
sub search {
47
    my ( $self, $params ) = @_;
48
49
    carp("No branchcode passed in for authorised values search!")
50
      unless $params->{branchcode};
51
52
    my $branchcode = $params->{branchcode};
53
    delete( $params->{branchcode} );
54
55
    my $rs = $self->_resultset()->search(
56
        {
57
            %$params,
58
            -or => [
59
                'authorised_values_branches.branchcode' => undef,
60
                'authorised_values_branches.branchcode' => $branchcode,
61
            ],
62
        },
63
        { join => 'authorised_values_branches' }
64
    );
65
66
    my $class = ref($self);
67
    return wantarray ? $self->_wrap( $rs->all() ) : $class->new_from_dbic($rs);
68
}
69
70
=head3 type
71
72
=cut
73
74
sub type {
75
    return 'AuthorisedValue';
76
}
77
78
sub object_class {
79
    return 'Koha::AuthorisedValue';
80
}
81
82
=head1 AUTHOR
83
84
Kyle M Hall <kyle@bywatersolutions.com>
85
86
=cut
87
88
1;
(-)a/Koha/Object.pm (-2 / +2 lines)
Lines 98-105 If the object is new, it will be created. Link Here
98
If the object previously existed, it will be updated.
98
If the object previously existed, it will be updated.
99
99
100
Returns:
100
Returns:
101
    1  if the store was a success
101
    $self  if the store was a success
102
    0  if the store failed
102
    undef  if the store failed
103
103
104
=cut
104
=cut
105
105
(-)a/Koha/Schema/Result/AuthorisedValuesBranch.pm (-19 / +9 lines)
Lines 27-48 __PACKAGE__->table("authorised_values_branches"); Link Here
27
27
28
  data_type: 'integer'
28
  data_type: 'integer'
29
  is_foreign_key: 1
29
  is_foreign_key: 1
30
  is_nullable: 1
30
  is_nullable: 0
31
31
32
=head2 branchcode
32
=head2 branchcode
33
33
34
  data_type: 'varchar'
34
  data_type: 'varchar'
35
  is_foreign_key: 1
35
  is_foreign_key: 1
36
  is_nullable: 1
36
  is_nullable: 0
37
  size: 10
37
  size: 10
38
38
39
=cut
39
=cut
40
40
41
__PACKAGE__->add_columns(
41
__PACKAGE__->add_columns(
42
  "av_id",
42
  "av_id",
43
  { data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
43
  { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
44
  "branchcode",
44
  "branchcode",
45
  { data_type => "varchar", is_foreign_key => 1, is_nullable => 1, size => 10 },
45
  { data_type => "varchar", is_foreign_key => 1, is_nullable => 0, size => 10 },
46
);
46
);
47
47
48
=head1 RELATIONS
48
=head1 RELATIONS
Lines 59-70 __PACKAGE__->belongs_to( Link Here
59
  "av",
59
  "av",
60
  "Koha::Schema::Result::AuthorisedValue",
60
  "Koha::Schema::Result::AuthorisedValue",
61
  { id => "av_id" },
61
  { id => "av_id" },
62
  {
62
  { is_deferrable => 1, on_delete => "CASCADE", on_update => "RESTRICT" },
63
    is_deferrable => 1,
64
    join_type     => "LEFT",
65
    on_delete     => "CASCADE",
66
    on_update     => "RESTRICT",
67
  },
68
);
63
);
69
64
70
=head2 branchcode
65
=head2 branchcode
Lines 79-96 __PACKAGE__->belongs_to( Link Here
79
  "branchcode",
74
  "branchcode",
80
  "Koha::Schema::Result::Branch",
75
  "Koha::Schema::Result::Branch",
81
  { branchcode => "branchcode" },
76
  { branchcode => "branchcode" },
82
  {
77
  { is_deferrable => 1, on_delete => "CASCADE", on_update => "RESTRICT" },
83
    is_deferrable => 1,
84
    join_type     => "LEFT",
85
    on_delete     => "CASCADE",
86
    on_update     => "RESTRICT",
87
  },
88
);
78
);
89
79
90
80
91
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-07-11 09:26:55
81
# Created by DBIx::Class::Schema::Loader v0.07040 @ 2014-10-23 10:48:27
92
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:FlVw5Eu4bXF2ygD0QkwwCg
82
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:cjPAyQSK7F7sEkiWjCUE7Q
93
83
84
__PACKAGE__->set_primary_key(__PACKAGE__->columns);
94
85
95
# You can replace this text with custom content, and it will be preserved on regeneration
96
1;
86
1;
(-)a/t/db_dependent/AuthorisedValues.t (-1 / +83 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use Test::More;    # tests => 25;
5
6
use C4::Context;
7
use Koha::AuthorisedValue;
8
use Koha::AuthorisedValues;
9
10
my $dbh = C4::Context->dbh;
11
$dbh->{AutoCommit} = 0;
12
$dbh->{RaiseError} = 1;
13
14
$dbh->do("DELETE FROM authorised_values");
15
16
# insert
17
my $av1 = Koha::AuthorisedValue->new(
18
    {
19
        category         => 'av_for_testing',
20
        authorised_value => 'value 1',
21
        lib              => 'display value 1',
22
        lib_opac         => 'opac display value 1',
23
        imageurl         => 'image1.png',
24
    }
25
)->store();
26
27
my $av2 = Koha::AuthorisedValue->new(
28
    {
29
        category         => 'av_for_testing',
30
        authorised_value => 'value 2',
31
        lib              => 'display value 2',
32
        lib_opac         => 'opac display value 2',
33
        imageurl         => 'image2.png',
34
    }
35
)->store();
36
37
my $av3 = Koha::AuthorisedValue->new(
38
    {
39
        category         => 'av_for_testing',
40
        authorised_value => 'value 3',
41
        lib              => 'display value 3',
42
        lib_opac         => 'opac display value 3',
43
        imageurl         => 'image2.png',
44
    }
45
)->store();
46
47
ok( $av1->id(), 'AV 1 is inserted' );
48
ok( $av2->id(), 'AV 2 is inserted' );
49
ok( $av3->id(), 'AV 3 is inserted' );
50
51
is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
52
$av3->lib_opac('');
53
is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
54
55
my @authorised_values =
56
  Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
57
is( @authorised_values, 3, "Get correct number of values" );
58
59
my $branches_rs = Koha::Database->new()->schema()->resultset('Branch')->search();
60
my $branch1 = $branches_rs->next();
61
my $branchcode1 = $branch1->branchcode();
62
my $branch2 = $branches_rs->next();
63
my $branchcode2 = $branch2->branchcode();
64
65
$av1->add_branch_limitation( $branchcode1 );
66
67
@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode1 } );
68
is( @authorised_values, 3, "Search including value with a branch limit ( branch can use the limited value ) gives correct number of results" );
69
70
@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
71
is( @authorised_values, 2, "Search including value with a branch limit ( branch *cannot* use the limited value ) gives correct number of results" );
72
73
$av1->del_branch_limitation( $branchcode1 );
74
@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
75
is( @authorised_values, 3, "Branch limitation deleted successfully" );
76
77
$av1->add_branch_limitation( $branchcode1 );
78
$av1->branch_limitations( [ $branchcode1, $branchcode2 ] );
79
80
my $limits = $av1->branch_limitations;
81
is( @$limits, 2, 'branch_limitations functions correctly both as setter and getter' );
82
83
done_testing;

Return to bug 10363