*** empty log message ***
[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
26 use C4::Context;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Interface::CGI::Output;
30 use C4::AuthoritiesMarc;
31 use C4::Acquisition;
32 use C4::Koha; # XXX subfield_is_koha_internal_p
33 use C4::Biblio;
34
35 my $query=new CGI;
36 my $op = $query->param('op');
37 my $authtypecode = $query->param('authtypecode');
38 my $dbh = C4::Context->dbh;
39
40 my $authid=$query->param('authid');
41 my ($template, $loggedinuser, $cookie);
42
43 my $authtypes = getauthtypes;
44 my @authtypesloop;
45 foreach my $thisauthtype (sort { $authtypes->{$a} <=> $authtypes->{$b} } keys %$authtypes) {
46         my $selected = 1 if $thisauthtype eq $authtypecode;
47         my %row =(value => $thisauthtype,
48                                 selected => $selected,
49                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
50                         );
51         push @authtypesloop, \%row;
52 }
53
54 if ($op eq "do_search") {
55         my @marclist = $query->param('marclist');
56         my @and_or = $query->param('and_or');
57         my @excluding = $query->param('excluding');
58         my @operator = $query->param('operator');
59         my $orderby = $query->param('orderby');
60         my @value = $query->param('value');
61
62     my $startfrom = $query->param('startfrom') || 1;
63     my $resultsperpage = $query->param('resultsperpage') || 19;
64
65         my ($results,$total) = authoritysearch(
66         \@marclist,
67         \@and_or,
68         \@excluding,
69         \@operator,
70         \@value,
71         ($startfrom - 1)*$resultsperpage,
72         $resultsperpage,
73         $authtypecode,
74         $orderby
75     );
76
77         ($template, $loggedinuser, $cookie)
78                 = get_template_and_user({
79             template_name => "authorities/searchresultlist.tmpl",
80             query => $query,
81             type => 'intranet',
82             authnotrequired => 0,
83             flagsrequired => {catalogue => 1},
84             debug => 1,
85         });
86
87         my @field_data = ();
88
89         # we must get parameters once again. Because if there is a mainentry, it
90         # has been replaced by something else during the search, thus the links
91         # next/previous would not work anymore
92         my @marclist_ini = $query->param('marclist');
93         for(my $i = 0 ; $i <= $#marclist ; $i++)
94         {
95                 push @field_data, { term => "marclist"  , val=>$marclist_ini[$i] };
96                 push @field_data, { term => "and_or"    , val=>$and_or[$i] };
97                 push @field_data, { term => "excluding" , val=>$excluding[$i] };
98                 push @field_data, { term => "operator"  , val=>$operator[$i] };
99                 push @field_data, { term => "value"     , val=>$value[$i] };
100         }
101
102     # construction of the url of each page
103     my $base_url =
104         'authorities-home.pl?'
105         .join(
106             '&amp;',
107             map { $_->{term}.'='.$_->{val} } @field_data
108         )
109         .'&amp;'
110         .join(
111             '&amp;',
112             map { $_->{term}.'='.$_->{val} } (
113                 {term => 'resultsperpage', val => $resultsperpage},
114                 {term => 'type'          , val => 'intranet'},
115                 {term => 'op'            , val => 'do_search'},
116                 {term => 'authtypecode'  , val => $authtypecode},
117                 {term => 'orderby'       , val => $orderby},
118             )
119         )
120         ;
121
122         my $from = ($startfrom - 1) * $resultsperpage + 1;
123         my $to;
124
125         if ($total < $startfrom * $resultsperpage) {
126                 $to = $total;
127         }
128     else {
129                 $to = $startfrom * $resultsperpage;
130         }
131
132         $template->param(result => $results) if $results;
133
134         $template->param(
135         pagination_bar => pagination_bar(
136             $base_url,
137             int($total/$resultsperpage)+1,
138             $startfrom,
139             'startfrom'
140             ),
141         total=>$total,
142         from=>$from,
143         to=>$to,
144         isEDITORS => $authtypecode eq 'EDITORS',
145     );
146
147 } elsif ($op eq "delete") {
148
149         &AUTHdelauthority($dbh,$authid, 1);
150
151         ($template, $loggedinuser, $cookie)
152                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
153                                 query => $query,
154                                 type => 'intranet',
155                                 authnotrequired => 0,
156                                 flagsrequired => {catalogue => 1},
157                                 debug => 1,
158                                 });
159 #       $template->param("statements" => \@statements,
160 #                                               "nbstatements" => $nbstatements);
161 }
162 elsif ($op eq "AddStatement") {
163
164         ($template, $loggedinuser, $cookie)
165                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
166                                 query => $query,
167                                 type => 'intranet',
168                                 authnotrequired => 0,
169                                 flagsrequired => {catalogue => 1},
170                                 debug => 1,
171                                 });
172
173         # Gets the entered information
174         my @marcfields = $query->param('marclist');
175         my @and_or = $query->param('and_or');
176         my @excluding = $query->param('excluding');
177         my @operator = $query->param('operator');
178         my @value = $query->param('value');
179
180         my @statements = ();
181
182         # List of the marc tags to display
183         my $marcarray = create_marclist();
184
185         my $nbstatements = $query->param('nbstatements');
186         $nbstatements = 1 if(!defined $nbstatements);
187
188         for(my $i = 0 ; $i < $nbstatements ; $i++)
189         {
190                 my %fields = ();
191
192                 # Recreates the old scrolling lists with the previously selected values
193                 my $marclist = create_scrolling_list({name=>"marclist",
194                                         values=> $marcarray,
195                                         size=> 1,
196                                         -tabindex=>'',
197                                         default=>$marcfields[$i],
198                                         onChange => "sql_update()"}
199                                         );
200
201                 $fields{'marclist'} = $marclist;
202                 $fields{'first'} = 1 if($i == 0);
203
204                 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
205                 $fields{'or'} = 1 if($and_or[$i] eq "or");
206
207                 #Restores the "not" parameters
208                 $fields{'not'} = 1 if($excluding[$i]);
209
210                 #Restores the operators (most common operators first);
211                 if($operator[$i] eq "=") { $fields{'eq'} = 1; }
212                 elsif($operator[$i] eq "contains") { $fields{'contains'} = 1; }
213                 elsif($operator[$i] eq "start") { $fields{'start'} = 1; }
214                 elsif($operator[$i] eq ">") { $fields{'gt'} = 1; }      #greater than
215                 elsif($operator[$i] eq ">=") { $fields{'ge'} = 1; } #greater or equal
216                 elsif($operator[$i] eq "<") { $fields{'lt'} = 1; } #lower than
217                 elsif($operator[$i] eq "<=") { $fields{'le'} = 1; } #lower or equal
218
219                 #Restores the value
220                 $fields{'value'} = $value[$i];
221
222                 push @statements, \%fields;
223         }
224         $nbstatements++;
225
226         # The new scrolling list
227         my $marclist = create_scrolling_list({name=>"marclist",
228                                 values=> $marcarray,
229                                 size=>1,
230                                 -tabindex=>'',
231                                 onChange => "sql_update()"});
232         push @statements, {"marclist" => $marclist };
233
234         $template->param("statements" => \@statements,
235                                                 "nbstatements" => $nbstatements);
236
237 }
238 else {
239         ($template, $loggedinuser, $cookie)
240                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
241                                 query => $query,
242                                 type => 'intranet',
243                                 authnotrequired => 0,
244                                 flagsrequired => {catalogue => 1},
245                                 debug => 1,
246                                 });
247
248 }
249
250 $template->param(authtypesloop => \@authtypesloop,
251                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
252                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
253                 IntranetNav => C4::Context->preference("IntranetNav"),
254                 );
255
256 # Print the page
257 output_html_with_http_headers $query, $cookie, $template->output;
258
259 # Local Variables:
260 # tab-width: 4
261 # End: