item rework: moved various accessor functions
[koha.git] / authorities / auth_finder.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::Output;
25 use C4::Auth;
26 use C4::Context;
27 use C4::AuthoritiesMarc;
28 use C4::Acquisition;
29 use C4::Koha;    # XXX subfield_is_koha_internal_p
30
31 my $query        = new CGI;
32 my $op           = $query->param('op');
33 my $authtypecode = $query->param('authtypecode');
34 my $index        = $query->param('index');
35 my $tagid        = $query->param('tagid');
36 my $resultstring = $query->param('result');
37 my $dbh          = C4::Context->dbh;
38
39 my $startfrom = $query->param('startfrom');
40 $startfrom = 0 if ( !defined $startfrom );
41 my ( $template, $loggedinuser, $cookie );
42 my $resultsperpage;
43
44 my $authtypes = getauthtypes;
45 my @authtypesloop;
46 foreach my $thisauthtype ( keys %$authtypes ) {
47     my $selected = 1 if $thisauthtype eq $authtypecode;
48     my %row = (
49         value        => $thisauthtype,
50         selected     => $selected,
51         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
52         index        => $index,
53     );
54     push @authtypesloop, \%row;
55 }
56
57 if ( $op eq "do_search" ) {
58     my @marclist  = $query->param('marclist');
59     my @and_or    = $query->param('and_or');
60     my @excluding = $query->param('excluding');
61     my @operator  = $query->param('operator');
62     my @value     = $query->param('value');
63     my $orderby   = $query->param('orderby');
64
65     $resultsperpage = $query->param('resultsperpage');
66     $resultsperpage = 19 if ( !defined $resultsperpage );
67
68     my ( $results, $total ) =
69       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator, \@value,
70         $startfrom * $resultsperpage,
71         $resultsperpage, $authtypecode, $orderby);
72     # multi page display gestion
73     my $displaynext = 0;
74     my $displayprev = $startfrom;
75     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
76         $displaynext = 1;
77     }
78
79     my @field_data = ();
80
81     my @marclist_ini =
82       $query->param('marclist')
83       ; # get marclist again, as the previous one has been modified by catalogsearch (mainentry replaced by field name
84     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
85         push @field_data, { term => "marclist",  val => $marclist_ini[$i] };
86         push @field_data, { term => "and_or",    val => $and_or[$i] };
87         push @field_data, { term => "excluding", val => $excluding[$i] };
88         push @field_data, { term => "operator",  val => $operator[$i] };
89         push @field_data, { term => "value",     val => $value[$i] };
90     }
91
92     my @numbers = ();
93
94     if ( $total > $resultsperpage ) {
95         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
96             if ( $i < 16 ) {
97                 my $highlight = 0;
98                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
99                 push @numbers,
100                   {
101                     number     => $i,
102                     highlight  => $highlight,
103                     searchdata => \@field_data,
104                     startfrom  => ( $i - 1 )
105                   };
106             }
107         }
108     }
109
110     my $from = $startfrom * $resultsperpage + 1;
111     my $to;
112
113     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
114         $to = $total;
115     }
116     else {
117         $to = ( ( $startfrom + 1 ) * $resultsperpage );
118     }
119     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
120         {
121             template_name   => "authorities/searchresultlist-auth.tmpl",
122             query           => $query,
123             type            => 'intranet',
124             authnotrequired => 0,
125             flagsrequired   => { catalogue => 1 },
126         }
127     );
128
129     $template->param( result => $results ) if $results;
130     $template->param(
131         startfrom      => $startfrom,
132     displaynext    => $displaynext,
133     displayprev    => $displayprev,
134     resultsperpage => $resultsperpage,
135     startfromnext  => $startfrom + 1,
136     startfromprev  => $startfrom - 1,
137         searchdata     => \@field_data,
138         total          => $total,
139         from           => $from,
140         to             => $to,
141         numbers        => \@numbers,
142         authtypecode   => $authtypecode,
143         mainmainstring => $value[0],
144         mainstring     => $value[1],
145         anystring      => $value[2],
146     );
147 } else {
148     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
149         {
150             template_name   => "authorities/auth_finder.tmpl",
151             query           => $query,
152             type            => 'intranet',
153             authnotrequired => 0,
154             flagsrequired   => { catalogue => 1 },
155         }
156     );
157
158     $template->param(
159         resultstring => $resultstring,
160     );
161 }
162
163 $template->param(
164     tagid         => $tagid,
165     index         => $index,
166     authtypesloop => \@authtypesloop,
167     authtypecode  => $authtypecode,
168 );
169
170 # Print the page
171 output_html_with_http_headers $query, $cookie, $template->output;
172
173 # Local Variables:
174 # tab-width: 4
175 # End: