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

(-)a/Koha/AdditionalField.pm (-1 / +406 lines)
Line 0 Link Here
0
- 
1
package Koha::AdditionalField;
2
3
use Modern::Perl;
4
5
use base qw(Class::Accessor);
6
7
use C4::Context;
8
9
__PACKAGE__->mk_accessors(qw( id tablename name authorised_value_category marcfield searchable values ));
10
11
sub new {
12
    my ( $class, $args ) = @_;
13
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} // {},
22
    };
23
24
    my $self = $class->SUPER::new( $additional_field );
25
26
    bless $self, $class;
27
    return $self;
28
}
29
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(
36
        q|
37
            SELECT id, tablename, name, authorised_value_category, marcfield, searchable
38
            FROM additional_fields
39
            WHERE id = ?
40
        |,
41
        {}, ( $field_id )
42
    );
43
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;
52
}
53
54
sub update {
55
    my ( $self ) = @_;
56
57
    die "There is no id defined for this additional field. I cannot update it" unless $self->{id};
58
59
    my $dbh = C4::Context->dbh;
60
    local $dbh->{RaiseError} = 1;
61
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} ) );
70
}
71
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} ) );
80
}
81
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};
92
}
93
94
sub insert_values {
95
    my ( $self )  = @_;
96
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 ));
111
        }
112
    }
113
}
114
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(
120
        q|
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 : () ) )
128
    );
129
130
    $self->{values} = {};
131
    for my $v ( @$values ) {
132
        $self->{values}{$v->{record_id}} = $v->{value};
133
    }
134
}
135
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
145
    |;
146
    $query .= q| AND tablename = ?|
147
        if $tablename;
148
149
    $query .= q| AND searchable = ?|
150
        if defined $searchable;
151
152
    my $results = $dbh->selectall_arrayref(
153
        $query, {Slice => {}}, (
154
            $tablename ? $tablename : (),
155
            defined $searchable ? $searchable : ()
156
        )
157
    );
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},
167
        });
168
    }
169
    return \@fields;
170
171
}
172
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');
177
178
    my $record_id = $args->{record_id};
179
    my $tablename = $args->{tablename};
180
    return unless $tablename;
181
182
    my $dbh = C4::Context->dbh;
183
    my $values = $dbh->selectall_arrayref(
184
        q|
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 : ()) )
191
    );
192
193
    my $r;
194
    for my $v ( @$values ) {
195
        $r->{$v->{record_id}}{$v->{name}} = $v->{value};
196
    }
197
    return $r;
198
}
199
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');
204
205
    my $fields = $args->{fields} // [];
206
    my $tablename = $args->{tablename};
207
    return [] unless @$fields;
208
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
219
                (
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};
232
    }
233
    $query .= join( ' LEFT JOIN ', @subqueries ) . ' WHERE 1';
234
    for my $j ( 1 .. $i ) {
235
            $query .= qq| AND field${j}_name IS NOT NULL|;
236
    }
237
    my $values = $dbh->selectall_arrayref( $query, {Slice => {}}, @args );
238
    return [
239
        map { $_->{record_id} } @$values
240
    ]
241
}
242
243
1;
244
245
__END__
246
247
=head1 NAME
248
249
Koha::AdditionalField
250
251
=head1 SYNOPSIS
252
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,
261
    });
262
    $av1->delete;
263
    $av2->{name} = 'another_name';
264
    $av2->update;
265
266
=head1 DESCRIPTION
267
268
Class for managing additional fields into Koha.
269
270
=head1 METHODS
271
272
=head2 new
273
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.
276
277
=over 4
278
279
=item B<id>
280
281
    The caller just knows the id of the additional field and want to retrieve all values.
282
283
=item B<tablename, name, authorised_value_category, marcfield and searchable>
284
285
    The caller wants to create a new additional field.
286
287
=back
288
289
=head2 fetch
290
291
The information will be retrieved from the database.
292
293
=head2 update
294
295
If the AdditionalField object has been modified and the values have to be modified into the database, call this method.
296
297
=head2 delete
298
299
Remove a the record in the database using the id the object.
300
301
=head2 insert
302
303
Insert a new AdditionalField object into the database.
304
305
=head2 insert_values
306
307
Insert new values for a record.
308
309
    my $af = Koha::AdditionalField({ id => $id })->fetch;
310
    my $af->{values} = {
311
        record_id1 => 'my value',
312
        record_id2 => 'another value',
313
    };
314
    $af->insert_values;
315
316
=head2 fetch_values
317
318
Retrieve values from the database for a given record_id.
319
The record_id argument is optional.
320
321
    my $af = Koha::AdditionalField({ id => $id })->fetch;
322
    my $values = $af->fetch_values({record_id => $record_id});
323
324
    $values will be equal to something like:
325
    {
326
        record_id => {
327
            field_name1 => 'value1',
328
            field_name2 => 'value2',
329
        }
330
    }
331
332
=head2 all
333
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.
338
339
    my $fields = Koha::AdditionalField->all;
340
    or
341
    my $fields = Koha::AdditionalField->all{(tablename => 'my_table'});
342
    or
343
    my $fields = Koha::AdditionalField->all({searchable => 1});
344
345
=head2 fetch_all_values
346
347
Retrieve all values for a table name.
348
This is a static method.
349
350
    my $values = Koha::AdditionalField({ tablename => 'my_table' });
351
352
    $values will be equel to something like:
353
    {
354
        record_id1 => {
355
            field_name1 => 'value1',
356
            field_name2 => 'value2',
357
        },
358
        record_id2 => {
359
            field_name1 => 'value3',
360
            field_name2 => 'value4',
361
        }
362
363
    }
364
365
=head2 get_matching_record_ids
366
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.
370
371
    my $fields = [
372
        {
373
            name => 'field_name',
374
            value => 'field_value',
375
        }
376
    ];
377
    my $ids = Koha::AdditionalField->get_matching_record_ids(
378
        {
379
            tablename => 'subscription',
380
            fields => $fields
381
        }
382
    );
383
384
=head1 AUTHOR
385
386
Jonathan Druart <jonathan.druart at biblibre.com>
387
388
=head1 COPYRIGHT
389
390
Copyright 2013 BibLibre
391
392
=head1 LICENSE
393
394
This file is part of Koha.
395
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.
400
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.
404
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>.

Return to bug 10855