Bug 10855: Add the new package Koha::AdditionalField
[koha.git] / Koha / AdditionalField.pm
blobde6f763319f078d6bd9da33f910e10113e7a1693
1 package Koha::AdditionalField;
3 use Modern::Perl;
5 use base qw(Class::Accessor);
7 use C4::Context;
9 __PACKAGE__->mk_accessors(qw( id tablename name authorised_value_category marcfield searchable values ));
11 sub new {
12 my ( $class, $args ) = @_;
14 my $additional_field = {
15 id => $args->{id} // q||,
16 tablename => $args->{tablename} // q||,
17 name => $args->{name} // q||,
18 authorised_value_category => $args->{authorised_value_category} // q||,
19 marcfield => $args->{marcfield} // q||,
20 searchable => $args->{searchable} // 0,
21 values => $args->{values} // {},
24 my $self = $class->SUPER::new( $additional_field );
26 bless $self, $class;
27 return $self;
30 sub fetch {
31 my ( $self ) = @_;
32 my $dbh = C4::Context->dbh;
33 my $field_id = $self->id;
34 return unless $field_id;
35 my $data = $dbh->selectrow_hashref(
37 SELECT id, tablename, name, authorised_value_category, marcfield, searchable
38 FROM additional_fields
39 WHERE id = ?
41 {}, ( $field_id )
44 die "This additional field does not exist (id=$field_id)" unless $data;
45 $self->{id} = $data->{id};
46 $self->{tablename} = $data->{tablename};
47 $self->{name} = $data->{name};
48 $self->{authorised_value_category} = $data->{authorised_value_category};
49 $self->{marcfield} = $data->{marcfield};
50 $self->{searchable} = $data->{searchable};
51 return $self;
54 sub update {
55 my ( $self ) = @_;
57 die "There is no id defined for this additional field. I cannot update it" unless $self->{id};
59 my $dbh = C4::Context->dbh;
60 local $dbh->{RaiseError} = 1;
62 return $dbh->do(q|
63 UPDATE additional_fields
64 SET name = ?,
65 authorised_value_category = ?,
66 marcfield = ?,
67 searchable = ?
68 WHERE id = ?
69 |, {}, ( $self->{name}, $self->{authorised_value_category}, $self->{marcfield}, $self->{searchable}, $self->{id} ) );
72 sub delete {
73 my ( $self ) = @_;
74 return unless $self->{id};
75 my $dbh = C4::Context->dbh;
76 local $dbh->{RaiseError} = 1;
77 return $dbh->do(q|
78 DELETE FROM additional_fields WHERE id = ?
79 |, {}, ( $self->{id} ) );
82 sub insert {
83 my ( $self ) = @_;
84 my $dbh = C4::Context->dbh;
85 local $dbh->{RaiseError} = 1;
86 $dbh->do(q|
87 INSERT INTO additional_fields
88 ( tablename, name, authorised_value_category, marcfield, searchable )
89 VALUES ( ?, ?, ?, ?, ? )
90 |, {}, ( $self->{tablename}, $self->{name}, $self->{authorised_value_category}, $self->{marcfield}, $self->{searchable} ) );
91 $self->{id} = $dbh->{mysql_insertid};
94 sub insert_values {
95 my ( $self ) = @_;
97 my $dbh = C4::Context->dbh;
98 local $dbh->{RaiseError} = 1;
99 while ( my ( $record_id, $value ) = each %{$self->{values}} ) {
100 my $updated = $dbh->do(q|
101 UPDATE additional_field_values
102 SET value = ?
103 WHERE field_id = ?
104 AND record_id = ?
105 |, {}, ( $value, $self->{id}, $record_id ));
106 if ( $updated eq '0E0' ) {
107 $dbh->do(q|
108 INSERT INTO additional_field_values( field_id, record_id, value )
109 VALUES( ?, ?, ?)
110 |, {}, ( $self->{id}, $record_id, $value ));
115 sub fetch_values {
116 my ( $self, $args ) = @_;
117 my $record_id = $args->{record_id};
118 my $dbh = C4::Context->dbh;
119 my $values = $dbh->selectall_arrayref(
121 SELECT *
122 FROM additional_fields af, additional_field_values afv
123 WHERE af.id = afv.field_id
124 AND af.tablename = ?
125 AND af.name = ?
126 | . ( $record_id ? q|AND afv.record_id = ?| : '' ),
127 {Slice => {}}, ( $self->{tablename}, $self->{name}, ($record_id ? $record_id : () ) )
130 $self->{values} = {};
131 for my $v ( @$values ) {
132 $self->{values}{$v->{record_id}} = $v->{value};
136 sub all {
137 my ( $class, $args ) = @_;
138 die "BAD CALL: Don't use fetch_all_values as a static method"
139 if ref $class and UNIVERSAL::can($class,'can');
140 my $tablename = $args->{tablename};
141 my $searchable = $args->{searchable};
142 my $dbh = C4::Context->dbh;
143 my $query = q|
144 SELECT * FROM additional_fields WHERE 1
146 $query .= q| AND tablename = ?|
147 if $tablename;
149 $query .= q| AND searchable = ?|
150 if defined $searchable;
152 my $results = $dbh->selectall_arrayref(
153 $query, {Slice => {}}, (
154 $tablename ? $tablename : (),
155 defined $searchable ? $searchable : ()
158 my @fields;
159 for my $r ( @$results ) {
160 push @fields, Koha::AdditionalField->new({
161 id => $r->{id},
162 tablename => $r->{tablename},
163 name => $r->{name},
164 authorised_value_category => $r->{authorised_value_category},
165 marcfield => $r->{marcfield},
166 searchable => $r->{searchable},
169 return \@fields;
173 sub fetch_all_values {
174 my ( $class, $args ) = @_;
175 die "BAD CALL: Don't use fetch_all_values as a static method"
176 if ref $class and UNIVERSAL::can($class,'can');
178 my $record_id = $args->{record_id};
179 my $tablename = $args->{tablename};
180 return unless $tablename;
182 my $dbh = C4::Context->dbh;
183 my $values = $dbh->selectall_arrayref(
185 SELECT afv.record_id, af.name, afv.value
186 FROM additional_fields af, additional_field_values afv
187 WHERE af.id = afv.field_id
188 AND af.tablename = ?
189 | . ( $record_id ? q| AND afv.record_id = ?| : q|| ),
190 {Slice => {}}, ( $tablename, ($record_id ? $record_id : ()) )
193 my $r;
194 for my $v ( @$values ) {
195 $r->{$v->{record_id}}{$v->{name}} = $v->{value};
197 return $r;
200 sub get_matching_record_ids {
201 my ( $class, $args ) = @_;
202 die "BAD CALL: Don't use fetch_all_values as a static method"
203 if ref $class and UNIVERSAL::can($class,'can');
205 my $fields = $args->{fields} // [];
206 my $tablename = $args->{tablename};
207 return [] unless @$fields;
209 my $dbh = C4::Context->dbh;
210 my $query = q|SELECT * FROM |;
211 my ( @subqueries, @args );
212 my $i = 0;
213 for my $field ( @$fields ) {
214 $i++;
215 my $subquery = qq|(
216 SELECT record_id, field$i.name AS field${i}_name
217 FROM additional_field_values afv
218 LEFT JOIN
220 SELECT afv.id, af.name, afv.value
221 FROM additional_field_values afv, additional_fields af
222 WHERE afv.field_id = af.id
223 AND af.name = ?
224 AND af.tablename = ?
225 AND value = ?
226 ) AS field$i USING (id)
227 WHERE field$i.id IS NOT NULL
228 ) AS values$i |;
229 $subquery .= ' USING (record_id)' if $i > 1;
230 push @subqueries, $subquery;
231 push @args, $field->{name}, $tablename, $field->{value};
233 $query .= join( ' LEFT JOIN ', @subqueries ) . ' WHERE 1';
234 for my $j ( 1 .. $i ) {
235 $query .= qq| AND field${j}_name IS NOT NULL|;
237 my $values = $dbh->selectall_arrayref( $query, {Slice => {}}, @args );
238 return [
239 map { $_->{record_id} } @$values
245 __END__
247 =head1 NAME
249 Koha::AdditionalField
251 =head1 SYNOPSIS
253 use Koha::AdditionalField;
254 my $af1 = Koha::AdditionalField->new({id => $id});
255 my $af2 = Koha::AuthorisedValue->new({
256 tablename => 'my_table',
257 name => 'a_name',
258 authorised_value_category => 'LOST',
259 marcfield => '200$a',
260 searchable => 1,
262 $av1->delete;
263 $av2->{name} = 'another_name';
264 $av2->update;
266 =head1 DESCRIPTION
268 Class for managing additional fields into Koha.
270 =head1 METHODS
272 =head2 new
274 Create a new Koha::AdditionalField object. This method can be called using several ways.
275 Either with the id for existing field or with different values for a new one.
277 =over 4
279 =item B<id>
281 The caller just knows the id of the additional field and want to retrieve all values.
283 =item B<tablename, name, authorised_value_category, marcfield and searchable>
285 The caller wants to create a new additional field.
287 =back
289 =head2 fetch
291 The information will be retrieved from the database.
293 =head2 update
295 If the AdditionalField object has been modified and the values have to be modified into the database, call this method.
297 =head2 delete
299 Remove a the record in the database using the id the object.
301 =head2 insert
303 Insert a new AdditionalField object into the database.
305 =head2 insert_values
307 Insert new values for a record.
309 my $af = Koha::AdditionalField({ id => $id })->fetch;
310 my $af->{values} = {
311 record_id1 => 'my value',
312 record_id2 => 'another value',
314 $af->insert_values;
316 =head2 fetch_values
318 Retrieve values from the database for a given record_id.
319 The record_id argument is optional.
321 my $af = Koha::AdditionalField({ id => $id })->fetch;
322 my $values = $af->fetch_values({record_id => $record_id});
324 $values will be equal to something like:
326 record_id => {
327 field_name1 => 'value1',
328 field_name2 => 'value2',
332 =head2 all
334 Retrieve all additional fields in the database given some parameters.
335 Parameters are optional.
336 This method returns a list of AdditionalField objects.
337 This is a static method.
339 my $fields = Koha::AdditionalField->all;
341 my $fields = Koha::AdditionalField->all{(tablename => 'my_table'});
343 my $fields = Koha::AdditionalField->all({searchable => 1});
345 =head2 fetch_all_values
347 Retrieve all values for a table name.
348 This is a static method.
350 my $values = Koha::AdditionalField({ tablename => 'my_table' });
352 $values will be equel to something like:
354 record_id1 => {
355 field_name1 => 'value1',
356 field_name2 => 'value2',
358 record_id2 => {
359 field_name1 => 'value3',
360 field_name2 => 'value4',
365 =head2 get_matching_record_ids
367 Retrieve all record_ids for records matching the field values given in parameter.
368 This method returns a list of ids.
369 This is a static method.
371 my $fields = [
373 name => 'field_name',
374 value => 'field_value',
377 my $ids = Koha::AdditionalField->get_matching_record_ids(
379 tablename => 'subscription',
380 fields => $fields
384 =head1 AUTHOR
386 Jonathan Druart <jonathan.druart at biblibre.com>
388 =head1 COPYRIGHT
390 Copyright 2013 BibLibre
392 =head1 LICENSE
394 This file is part of Koha.
396 Koha is free software; you can redistribute it and/or modify it under the
397 terms of the GNU General Public License as published by the Free Software
398 Foundation; either version 3 of the License, or (at your option) any later
399 version.
401 Koha is distributed in the hope that it will be useful, but WITHOUT ANY
402 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
403 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
405 You should have received a copy of the GNU General Public License along
406 with Koha; if not, see <http://www.gnu.org/licenses>.