Add faceted search code
[wip/koha-chris_n.git] / acqui / newordersuggestion.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 # $Id$
19
20 =head1 NAME
21 newordersuggestion.pl
22
23 =head1 DESCRIPTION
24 this script allow to add an order from a existing suggestion.
25
26 =head1 CGI PARAMETERS
27
28 =over 4
29
30 =item basketno
31 the number of this basket.
32
33 =item booksellerid
34 the bookseller who sells this record.
35
36 =item title
37 the title of this record suggested.
38
39 =item author
40 the author of this suggestion.
41
42 =item note
43 this param allow to enter a note with this suggestion.
44
45 =item copyrightdate
46 the copyright date for this suggestion.
47
48 =item publishercode
49
50 =item volumedesc
51
52 =item publicationyear
53 the publication year of this record.
54
55 =item place
56
57 =item isbn
58 the isbn of this suggestion.
59
60 =item duplicateNumber
61 is the biblionumber to put to the new suggestion.
62
63 =item suggestionid
64 the id of the suggestion to select.
65
66 =item op
67 can be equal to
68     * connectDuplicate :
69         then call to the function : ConnectSuggestionAndBiblio.
70         i.e set the biblionumber of this suggestion.
71     * else :
72         is the default value.
73 =back
74
75 =cut
76
77 use strict;
78 require Exporter;
79 use CGI;
80 use C4::Auth;       # get_template_and_user
81 use C4::Interface::CGI::Output;
82 use C4::Suggestions;
83 use C4::Biblio;
84 use C4::Search;
85
86 my $input = new CGI;
87
88 my $basketno = $input->param('basketno');
89 my $supplierid = $input->param('booksellerid');
90 my $title = $input->param('title');
91 my $author = $input->param('author');
92 my $note = $input->param('note');
93 my $copyrightdate =$input->param('copyrightdate');
94 my $publishercode = $input->param('publishercode');
95 my $volumedesc = $input->param('volumedesc');
96 my $publicationyear = $input->param('publicationyear');
97 my $place = $input->param('place');
98 my $isbn = $input->param('isbn');
99 my $duplicateNumber = $input->param('duplicateNumber');
100 my $suggestionid = $input->param('suggestionid');
101
102 my $status = 'ACCEPTED'; # the suggestion had to be accepeted before to order it.
103 my $suggestedbyme = -1; # search ALL suggestors
104 my $op = $input->param('op');
105 $op = 'else' unless $op;
106
107 my $dbh = C4::Context->dbh;
108 my ($template, $borrowernumber, $cookie)
109     = get_template_and_user({template_name => "acqui/newordersuggestion.tmpl",
110                              type => "intranet",
111                              query => $input,
112                              authnotrequired => 1,
113                              flagsrequired => {acquisition => 1},
114                          });
115
116 if ($op eq 'connectDuplicate') {
117         ConnectSuggestionAndBiblio($suggestionid,$duplicateNumber);
118 }
119 my $suggestions_loop= &SearchSuggestion($borrowernumber,$author,$title,$publishercode,$status,$suggestedbyme);
120 foreach (@$suggestions_loop) {
121         unless ($_->{biblionumber}) {
122                 my (@kohafields, @and_or, @value, @relation,  $offset,$length);
123                 # search on biblio.title
124                 if ($_->{title}) {
125                         push @kohafields, "title";
126                         push @and_or, "\@and";
127                         push @relation, "\@attr 5=1";
128                         push @value, $_->{title};
129                 }
130                 if ($_->{author}) {
131                         push @kohafields, "author";
132                         push @and_or, "\@and";
133                         push @relation, "";
134                         push @value, $_->{author};
135                 }
136                 # ... and on publicationyear.
137                 if ($_->{publicationyear}) {
138                         push @kohafields, "copyrightdate";
139                         push @and_or, "\@and";
140                         push @relation, "";
141                         push @value, $_->{publicationyear};
142                 }
143                 # ... and on publisher.
144                 if ($_->{publishercode}) {
145                         push @kohafields, "publishercode";
146                         push @and_or, "\@and";
147                         push @relation, "";
148                         push @value, $_->{publishercode};
149                 }
150         
151                 my ($nbresult,$facets,@finalresult) = ZEBRAsearch_kohafields(\@kohafields,\@value,\@relation,"",\@and_or,0,"",0,1);
152
153                 # there is at least 1 result => return the 1st one
154                 if ($nbresult) {
155                         $_->{duplicateBiblionumber} = $finalresult[0]->{biblionumber};
156                 }
157         }
158 }
159 $template->param(suggestions_loop => $suggestions_loop,
160                                 title => $title,
161                                 author => $author,
162                                 publishercode => $publishercode,
163                                 status => $status,
164                                 suggestedbyme => $suggestedbyme,
165                                 basketno => $basketno,
166                                 supplierid => $supplierid,
167                                 "op_$op" => 1,
168                                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
169                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
170                 IntranetNav => C4::Context->preference("IntranetNav"),
171 );
172 output_html_with_http_headers $input, $cookie, $template->output;