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

(-)a/Koha/AuthorisedValue.pm (+180 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
=cut
92
93
=head3 add_branch_limitation
94
95
$av->del_branch_limitation( $branchcode );
96
97
=cut
98
99
sub del_branch_limitation {
100
    my ( $self, $branchcode ) = @_;
101
102
    croak("No branchcode passed in!") unless $branchcode;
103
104
    my $limitation =
105
      $self->_avb_resultset->find(
106
        { av_id => $self->id(), branchcode => $branchcode } );
107
108
    unless ($limitation) {
109
        my $id = $self->id();
110
        carp(
111
"No branch limit for branch $branchcode found for av_id $id to delete!"
112
        );
113
        return;
114
    }
115
116
    return $limitation->delete();
117
}
118
119
=head3 replace_branch_limitations
120
121
$av->replace_branch_limitations( \@branchcodes );
122
123
=cut
124
125
sub replace_branch_limitations {
126
    my ( $self, $branchcodes ) = @_;
127
128
    $self->_avb_resultset->search( { av_id => $self->id() } )->delete();
129
130
    my @return_values =
131
      map { $self->add_branch_limitation($_) } @$branchcodes;
132
133
    return \@return_values;
134
}
135
136
=head3 lib_opac
137
138
my $description = $av->lib_opac();
139
140
$av->lib_opac( $description );
141
142
=cut
143
144
sub opac_description {
145
    my ( $self, $value ) = @_;
146
147
    return $self->lib_opac() || $self->lib();
148
}
149
150
=head3 Koha::Objects->_resultset
151
152
Returns the internal resultset or creates it if undefined
153
154
=cut
155
156
sub _avb_resultset {
157
    my ($self) = @_;
158
159
    $self->{_avb_resultset} ||=
160
      Koha::Database->new()->schema()->resultset('AuthorisedValuesBranch');
161
162
    $self->{_avb_resultset};
163
}
164
165
=head3 type
166
167
=cut
168
169
sub type {
170
    return 'AuthorisedValue';
171
}
172
173
=head1 AUTHOR
174
175
Kyle M Hall <kyle@bywatersolutions.com>
176
177
=cut
178
179
1;
180
## Please see file perltidy.ERR
(-)a/Koha/Object.pm (-3 / +3 lines)
Lines 97-111 If the object is new, it will be created. Link Here
97
If the object previously existed, it will be updated.
97
If the object previously existed, it will be updated.
98
98
99
Returns:
99
Returns:
100
    1  if the store was a success
100
    $self  if the store was a success
101
    0  if the store failed
101
    undef  if the store failed
102
102
103
=cut
103
=cut
104
104
105
sub store {
105
sub store {
106
    my ($self) = @_;
106
    my ($self) = @_;
107
107
108
    return $self->_result()->update_or_insert() ? 1 : 0;
108
    return $self->_result()->update_or_insert() ? $self : undef;
109
}
109
}
110
110
111
=head3 $object->in_storage();
111
=head3 $object->in_storage();
(-)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/installer/data/mysql/updatedatabase.pl (+12 lines)
Lines 8838-8843 if ( CheckVersion($DBversion) ) { Link Here
8838
    SetVersion($DBversion);
8838
    SetVersion($DBversion);
8839
}
8839
}
8840
8840
8841
$DBversion = "3.17.00.XXX";
8842
if ( CheckVersion($DBversion) ) {
8843
    $dbh->do(q{
8844
            ALTER TABLE authorised_values_branches
8845
            CHANGE av_id av_id INT( 11 ) NOT NULL,
8846
            CHANGE branchcode branchcode VARCHAR( 10 ) NOT NULL
8847
    });
8848
    print "Upgrade to $DBversion done ( Bug 10363 - There is no package for authorised values. )\n";
8849
    SetVersion($DBversion);
8850
    SetVersion ($DBversion);
8851
}
8852
8841
=head1 FUNCTIONS
8853
=head1 FUNCTIONS
8842
8854
8843
=head2 TableExists($table)
8855
=head2 TableExists($table)
(-)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