1 package Koha
::AdditionalField
;
5 use base
qw(Class::Accessor);
9 __PACKAGE__
->mk_accessors(qw( id tablename name authorised_value_category marcfield searchable values ));
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 );
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
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
};
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;
63 UPDATE additional_fields
65 authorised_value_category
= ?
,
69 |, {}, ( $self->{name
}, $self->{authorised_value_category
}, $self->{marcfield
}, $self->{searchable
}, $self->{id
} ) );
74 return unless $self->{id
};
75 my $dbh = C4
::Context
->dbh;
76 local $dbh->{RaiseError
} = 1;
78 DELETE FROM additional_fields WHERE id
= ?
79 |, {}, ( $self->{id
} ) );
84 my $dbh = C4
::Context
->dbh;
85 local $dbh->{RaiseError
} = 1;
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
};
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
105 |, {}, ( $value, $self->{id
}, $record_id ));
106 if ( $updated eq '0E0' ) {
108 INSERT INTO additional_field_values
( field_id
, record_id
, value
)
110 |, {}, ( $self->{id
}, $record_id, $value ));
116 my ( $self, $args ) = @_;
117 my $record_id = $args->{record_id
};
118 my $dbh = C4
::Context
->dbh;
119 my $values = $dbh->selectall_arrayref(
122 FROM additional_fields af
, additional_field_values afv
123 WHERE af
.id
= afv
.field_id
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
};
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;
144 SELECT
* FROM additional_fields WHERE
1
146 $query .= q
| AND 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 : ()
159 for my $r ( @
$results ) {
160 push @fields, Koha
::AdditionalField
->new({
162 tablename
=> $r->{tablename
},
164 authorised_value_category
=> $r->{authorised_value_category
},
165 marcfield
=> $r->{marcfield
},
166 searchable
=> $r->{searchable
},
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
189 | . ( $record_id ? q
| AND afv
.record_id
= ?
| : q
|| ),
190 {Slice
=> {}}, ( $tablename, ($record_id ?
$record_id : ()) )
194 for my $v ( @
$values ) {
195 $r->{$v->{record_id
}}{$v->{name
}} = $v->{value
};
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 );
213 for my $field ( @
$fields ) {
216 SELECT record_id
, field
$i.name AS field
${i
}_name
217 FROM additional_field_values afv
220 SELECT afv
.id
, af
.name
, afv
.value
221 FROM additional_field_values afv
, additional_fields af
222 WHERE afv
.field_id
= af
.id
226 ) AS field
$i USING
(id
)
227 WHERE field
$i.id IS NOT NULL
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 );
239 map { $_->{record_id
} } @
$values
249 Koha::AdditionalField
253 use Koha::AdditionalField;
254 my $af1 = Koha::AdditionalField->new({id => $id});
255 my $af2 = Koha::AuthorisedValue->new({
256 tablename => 'my_table',
258 authorised_value_category => 'LOST',
259 marcfield => '200$a',
263 $av2->{name} = 'another_name';
268 Class for managing additional fields into Koha.
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.
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.
291 The information will be retrieved from the database.
295 If the AdditionalField object has been modified and the values have to be modified into the database, call this method.
299 Remove a the record in the database using the id the object.
303 Insert a new AdditionalField object into the database.
307 Insert new values for a record.
309 my $af = Koha::AdditionalField({ id => $id })->fetch;
311 record_id1 => 'my value',
312 record_id2 => 'another value',
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:
327 field_name1 => 'value1',
328 field_name2 => 'value2',
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:
355 field_name1 => 'value1',
356 field_name2 => 'value2',
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.
373 name => 'field_name',
374 value => 'field_value',
377 my $ids = Koha::AdditionalField->get_matching_record_ids(
379 tablename => 'subscription',
386 Jonathan Druart <jonathan.druart at biblibre.com>
390 Copyright 2013 BibLibre
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
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>.