Bug 17642: Add find_by_koha_field
[koha.git] / Koha / AuthorisedValues.pm
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::AuthorisedValue;
27 use Koha::MarcSubfieldStructures;
28
29 use base qw(Koha::Objects);
30
31 =head1 NAME
32
33 Koha::AuthorisedValues - Koha Authorised value Object set class
34
35 =head1 API
36
37 =head2 Class Methods
38
39 =cut
40
41 =head3 Koha::AuthorisedValues->search();
42
43 my @objects = Koha::AuthorisedValues->search($params);
44
45 =cut
46
47 sub search {
48     my ( $self, $params, $attributes ) = @_;
49
50     my $branchcode = $params->{branchcode};
51     delete( $params->{branchcode} );
52
53     my $or =
54       $branchcode
55       ? {
56         '-or' => [
57             'authorised_values_branches.branchcode' => undef,
58             'authorised_values_branches.branchcode' => $branchcode,
59         ]
60       }
61       : {};
62     my $join = $branchcode ? { join => 'authorised_values_branches' } : {};
63     $attributes //= {};
64     $attributes = { %$attributes, %$join };
65     return $self->SUPER::search( { %$params, %$or, }, $attributes );
66 }
67
68 sub search_by_marc_field {
69     my ( $self, $params ) = @_;
70     my $frameworkcode = $params->{frameworkcode} || '';
71     my $tagfield      = $params->{tagfield};
72     my $tagsubfield   = $params->{tagsubfield};
73
74     return unless $tagfield or $tagsubfield;
75
76     return $self->SUPER::search(
77         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
78             ( defined $tagfield    ? ( 'marc_subfield_structures.tagfield'    => $tagfield )    : () ),
79             ( defined $tagsubfield ? ( 'marc_subfield_structures.tagsubfield' => $tagsubfield ) : () ),
80         },
81         { join => { category => 'marc_subfield_structures' } }
82     );
83 }
84
85 sub search_by_koha_field {
86     my ( $self, $params ) = @_;
87     my $frameworkcode    = $params->{frameworkcode} || '';
88     my $kohafield        = $params->{kohafield};
89     my $category         = $params->{category};
90
91     return unless $kohafield;
92
93     return $self->SUPER::search(
94         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
95             'marc_subfield_structures.kohafield'     => $kohafield,
96             ( defined $category ? ( category_name    => $category )         : () ),
97         },
98         {   join     => { category => 'marc_subfield_structures' },
99             distinct => 1,
100         }
101     );
102 }
103
104 sub find_by_koha_field {
105     my ( $self, $params ) = @_;
106     my $frameworkcode    = $params->{frameworkcode} || '';
107     my $kohafield        = $params->{kohafield};
108     my $authorised_value = $params->{authorised_value};
109
110     my $av = $self->SUPER::search(
111         {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
112             'marc_subfield_structures.kohafield'     => $kohafield,
113             'me.authorised_value'                    => $authorised_value,
114         },
115         {   join     => { category => 'marc_subfield_structures' },
116             distinct => 1,
117         }
118     );
119     return $av->count ? $av->next : undef;
120 }
121
122 sub categories {
123     my ( $self ) = @_;
124     my $rs = $self->_resultset->search(
125         undef,
126         {
127             select => ['category'],
128             distinct => 1,
129             order_by => 'category',
130         },
131     );
132     return map $_->get_column('category'), $rs->all;
133 }
134
135 =head3 type
136
137 =cut
138
139 sub _type {
140     return 'AuthorisedValue';
141 }
142
143 sub object_class {
144     return 'Koha::AuthorisedValue';
145 }
146
147 =head1 AUTHOR
148
149 Kyle M Hall <kyle@bywatersolutions.com>
150
151 =cut
152
153 1;