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