Adding double authority search before creating a new authority
[koha.git] / authorities / authorities-home.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 require Exporter;
23 use CGI;
24 use C4::Auth;
25 use HTML::Template;
26 use C4::Context;
27 use C4::Search;
28 use C4::Auth;
29 use C4::Output;
30 use C4::Interface::CGI::Output;
31 use C4::AuthoritiesMarc;
32 use C4::SearchMarc;
33 use C4::Acquisition;
34 use C4::Koha; # XXX subfield_is_koha_internal_p
35
36 my $query=new CGI;
37 my $op = $query->param('op');
38 my $authtypecode = $query->param('authtypecode');
39 my $dbh = C4::Context->dbh;
40
41 my $startfrom=$query->param('startfrom');
42 my $authid=$query->param('authid');
43 $startfrom=0 if(!defined $startfrom);
44 my ($template, $loggedinuser, $cookie);
45 my $resultsperpage;
46
47 my $authtypes = getauthtypes;
48 my @authtypesloop;
49 foreach my $thisauthtype (sort { $authtypes->{$a} <=> $authtypes->{$b} } keys %$authtypes) {
50         my $selected = 1 if $thisauthtype eq $authtypecode;
51         my %row =(value => $thisauthtype,
52                                 selected => $selected,
53                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
54                         );
55         push @authtypesloop, \%row;
56 }
57
58 if ($op eq "do_search") {
59         my @marclist = $query->param('marclist');
60         my @and_or = $query->param('and_or');
61         my @excluding = $query->param('excluding');
62         my @operator = $query->param('operator');
63         my @value = $query->param('value');
64
65         $resultsperpage= $query->param('resultsperpage');
66         $resultsperpage = 19 if(!defined $resultsperpage);
67         my @tags;
68         my ($results,$total) = authoritysearch($dbh, \@marclist,\@and_or,
69                                                                                 \@excluding, \@operator, \@value,
70                                                                                 $startfrom*$resultsperpage, $resultsperpage,$authtypecode);
71         ($template, $loggedinuser, $cookie)
72                 = get_template_and_user({template_name => "authorities/searchresultlist.tmpl",
73                                 query => $query,
74                                 type => 'intranet',
75                                 authnotrequired => 0,
76                                 flagsrequired => {borrowers => 1},
77                                 flagsrequired => {catalogue => 1},
78                                 debug => 1,
79                                 });
80
81         # multi page display gestion
82         my $displaynext=0;
83         my $displayprev=$startfrom;
84         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
85                 $displaynext = 1;
86         }
87
88         my @field_data = ();
89
90
91         for(my $i = 0 ; $i <= $#marclist ; $i++)
92         {
93                 push @field_data, { term => "marclist", val=>$marclist[$i] };
94                 push @field_data, { term => "and_or", val=>$and_or[$i] };
95                 push @field_data, { term => "excluding", val=>$excluding[$i] };
96                 push @field_data, { term => "operator", val=>$operator[$i] };
97                 push @field_data, { term => "value", val=>$value[$i] };
98         }
99
100         my @numbers = ();
101
102         if ($total>$resultsperpage)
103         {
104                 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
105                 {
106                         if ($i<16)
107                         {
108                         my $highlight=0;
109                         ($startfrom==($i-1)) && ($highlight=1);
110                         push @numbers, { number => $i,
111                                         highlight => $highlight ,
112                                         searchdata=> \@field_data,
113                                         startfrom => ($i-1)};
114                         }
115         }
116         }
117
118         my $from = $startfrom*$resultsperpage+1;
119         my $to;
120
121         if($total < (($startfrom+1)*$resultsperpage))
122         {
123                 $to = $total;
124         } else {
125                 $to = (($startfrom+1)*$resultsperpage);
126         }
127         $template->param(result => $results) if $results;
128         $template->param(
129                                                         startfrom=> $startfrom,
130                                                         displaynext=> $displaynext,
131                                                         displayprev=> $displayprev,
132                                                         resultsperpage => $resultsperpage,
133                                                         startfromnext => $startfrom+1,
134                                                         startfromprev => $startfrom-1,
135                                                         searchdata=>\@field_data,
136                                                         total=>$total,
137                                                         from=>$from,
138                                                         to=>$to,
139                                                         numbers=>\@numbers,
140                                                         authtypecode=>$authtypecode,
141                                                         );
142
143 } elsif ($op eq "delete") {
144
145         &AUTHdelauthority($dbh,$authid, 1);
146
147         ($template, $loggedinuser, $cookie)
148                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
149                                 query => $query,
150                                 type => 'intranet',
151                                 authnotrequired => 0,
152                                 flagsrequired => {catalogue => 1},
153                                 debug => 1,
154                                 });
155 #       $template->param("statements" => \@statements,
156 #                                               "nbstatements" => $nbstatements);
157 }
158 elsif ($op eq "AddStatement") {
159
160         ($template, $loggedinuser, $cookie)
161                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
162                                 query => $query,
163                                 type => 'intranet',
164                                 authnotrequired => 0,
165                                 flagsrequired => {catalogue => 1},
166                                 debug => 1,
167                                 });
168
169         # Gets the entered information
170         my @marcfields = $query->param('marclist');
171         my @and_or = $query->param('and_or');
172         my @excluding = $query->param('excluding');
173         my @operator = $query->param('operator');
174         my @value = $query->param('value');
175
176         my @statements = ();
177
178         # List of the marc tags to display
179         my $marcarray = create_marclist();
180
181         my $nbstatements = $query->param('nbstatements');
182         $nbstatements = 1 if(!defined $nbstatements);
183
184         for(my $i = 0 ; $i < $nbstatements ; $i++)
185         {
186                 my %fields = ();
187
188                 # Recreates the old scrolling lists with the previously selected values
189                 my $marclist = create_scrolling_list({name=>"marclist",
190                                         values=> $marcarray,
191                                         size=> 1,
192                                         default=>$marcfields[$i],
193                                         onChange => "sql_update()"}
194                                         );
195
196                 $fields{'marclist'} = $marclist;
197                 $fields{'first'} = 1 if($i == 0);
198
199                 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
200                 $fields{'or'} = 1 if($and_or[$i] eq "or");
201
202                 #Restores the "not" parameters
203                 $fields{'not'} = 1 if($excluding[$i]);
204
205                 #Restores the operators (most common operators first);
206                 if($operator[$i] eq "=") { $fields{'eq'} = 1; }
207                 elsif($operator[$i] eq "contains") { $fields{'contains'} = 1; }
208                 elsif($operator[$i] eq "start") { $fields{'start'} = 1; }
209                 elsif($operator[$i] eq ">") { $fields{'gt'} = 1; }      #greater than
210                 elsif($operator[$i] eq ">=") { $fields{'ge'} = 1; } #greater or equal
211                 elsif($operator[$i] eq "<") { $fields{'lt'} = 1; } #lower than
212                 elsif($operator[$i] eq "<=") { $fields{'le'} = 1; } #lower or equal
213
214                 #Restores the value
215                 $fields{'value'} = $value[$i];
216
217                 push @statements, \%fields;
218         }
219         $nbstatements++;
220
221         # The new scrolling list
222         my $marclist = create_scrolling_list({name=>"marclist",
223                                 values=> $marcarray,
224                                 size=>1,
225                                 onChange => "sql_update()"});
226         push @statements, {"marclist" => $marclist };
227
228         $template->param("statements" => \@statements,
229                                                 "nbstatements" => $nbstatements);
230
231 }
232 else {
233         ($template, $loggedinuser, $cookie)
234                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
235                                 query => $query,
236                                 type => 'intranet',
237                                 authnotrequired => 0,
238                                 flagsrequired => {catalogue => 1},
239                                 debug => 1,
240                                 });
241
242 }
243
244 $template->param(authtypesloop => \@authtypesloop);
245
246 # Print the page
247 output_html_with_http_headers $query, $cookie, $template->output;
248
249 # Local Variables:
250 # tab-width: 4
251 # End: