3 # Copyright 2011 BibLibre SARL
5 # This file is part of Koha.
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 2 of the License, or (at your option) any later
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.
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.
20 # This script search in items table if a value for a given field exists.
21 # It is used in check_additem (additem.js)
22 # Parameters are a list of 'field', which must be field names in items table
23 # and a list of 'value', which are the corresponding value to check
24 # Eg. @field = ('barcode', 'barcode', 'stocknumber')
25 # @value = ('1234', '1235', 'ABC')
26 # The script will check if there is already an item with barcode '1234',
27 # then an item with barcode '1235', and finally check if there is an item
28 # with stocknumber 'ABC'
29 # It returns a JSON string which contains what have been found
30 # Eg. { barcode: ['1234', '1235'], stocknumber: ['ABC'] }
41 my @field = $input->param('field');
42 my @value = $input->param('value');
44 my $dbh = C4::Context->dbh;
46 my $query = "SHOW COLUMNS FROM items";
47 my $sth = $dbh->prepare($query);
49 my $results = $sth->fetchall_hashref('Field');
50 my @columns = keys %$results;
54 for my $f ( @field ) {
55 if(0 < grep /^$f$/, @columns) {
56 $query = "SELECT $f FROM items WHERE $f = ?";
57 $sth = $dbh->prepare( $query );
58 $sth->execute( $value[$index] );
59 my @values = $sth->fetchrow_array;
62 push @{ $r->{$f} }, $values[0];
68 output_with_http_headers $input, undef, to_json($r), 'json';