Fixing Labtheses.pl
[koha.git] / cataloguing / value_builder / labs_theses.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
22 use CGI;
23
24 use C4::Output;
25 use C4::Context;
26 use C4::Auth;
27 use C4::Output;
28
29 use C4::Koha;
30
31 sub plugin_parameters {
32 my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
33 return "";
34 }
35
36 sub plugin_javascript {
37 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
38 my $function_name= "328".(int(rand(100000))+1);
39 my $res="
40 <script type=\"text/javascript\">
41 //<![CDATA[
42 function Focus$function_name(subfield_managed) {return 1;}
43 function Blur$function_name(subfield_managed) {return 1;}
44 function Clic$function_name(i) {
45         defaultvalue=document.getElementById(i).value;
46         newin=window.open(\"plugin_launcher.pl?plugin_name=labs_theses.pl&cat_auth=LABTHE&index=\"+i+\"&result=\"+defaultvalue,\"unimarc_field_328\",'width=700,height=700,toolbar=false,scrollbars=yes');
47   return false; 
48 }
49 //]]>
50 </script>
51 ";
52
53 return ($function_name,$res);
54 }
55
56 sub plugin {
57         my ($input) = @_;
58         my $dbh=C4::Context->dbh;
59         my $query = new CGI;
60         my $op = $query->param('op');
61         my $cat_auth=$query->param('cat_auth');
62
63         my $startfrom=$query->param('startfrom');
64         $startfrom=0 if(!defined $startfrom);
65         my ($template, $loggedinuser, $cookie);
66         my $resultsperpage;
67         my $search = $query->param('search');
68         
69         if ($op eq "do_search") {
70         
71                 $resultsperpage= $query->param('resultsperpage');
72                 $resultsperpage = 19 if(!defined $resultsperpage);
73 #               my $upperlimit=$startfrom+$resultsperpage;
74                 # builds tag and subfield arrays
75                 my $strquery = "SELECT authorised_value, lib from authorised_values where category = ? and lib like ?";
76 #               $strquery .= " LIMIT $startfrom,$upperlimit";
77                 
78                 warn 'category : '.$cat_auth.' recherche :'.$search;
79                 warn "$strquery";
80                 $search=~s/\*/%/g;
81                 my $sth = $dbh->prepare($strquery);
82                 $sth->execute($cat_auth,$search);
83                 $search=~s/%/\*/g;
84                 
85                 
86                 my @results;
87                 my $total;
88                 while (my $data = $sth->fetchrow_hashref){
89                         my $libjs=$data->{'lib'};
90 #                       $libjs=~s#\'#\\\'#g;
91                         my $authjs=$data->{'authorised_value'};
92 #                       $authjs=~s#\'#\\\'#g;
93                         push @results, {'libjs'=>$libjs,
94                                                         'lib'=>$data->{'lib'},
95                                                         'authjs'=>$authjs,
96                                                         'auth_value'=>$data->{'authorised_value'}} 
97                                                         unless (($total<$startfrom) or ($total>$startfrom+$resultsperpage));
98                         $total++;
99                 }
100                 
101                 ($template, $loggedinuser, $cookie)
102                         = get_template_and_user({template_name => "cataloguing/value_builder/labs_theses.tmpl",
103                                         query => $query,
104                                         type => 'intranet',
105                                         authnotrequired => 1,
106                                         debug => 1,
107                                         });
108         
109                 # multi page display gestion
110                 my $displaynext=0;
111                 my $displayprev=$startfrom;
112                 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
113                         $displaynext = 1;
114                 }
115         
116                 my @numbers = ();
117         
118                 if ($total>$resultsperpage)
119                 {
120                         for (my $i=1; (($i<$total/$resultsperpage+1) && ($i<16)); $i++)
121                         {
122                                         my $highlight=0;
123                                         ($startfrom==($i-1)) && ($highlight=1);
124                                         push @numbers, { number => $i,
125                                                 highlight => $highlight ,
126                                                 search=> $search,
127                                                 startfrom => $resultsperpage*($i-1)};
128                         }
129                 }
130         
131                 my $from = $startfrom+1;
132                 my $to;
133         
134                 if($total < (($startfrom+1)*$resultsperpage))
135                 {
136                         $to = $total;
137                 } else {
138                         $to = (($startfrom+1)*$resultsperpage);
139                 }
140                 $template->param(catresult => \@results,
141                                                 cat_auth=>$cat_auth,
142                                                 index => $query->param('index')."",
143                                                                 startfrom=> $startfrom,
144                                                                 displaynext=> $displaynext,
145                                                                 displayprev=> $displayprev,
146                                                                 resultsperpage => $resultsperpage,
147                                                                 startfromnext => $startfrom+$resultsperpage,
148                                                                 startfromprev => $startfrom-$resultsperpage,
149                                                                 search=>$search,
150                                                                 total=>$total,
151                                                                 from=>$from,
152                                                                 to=>$to,
153                                                                 numbers=>\@numbers,
154                                                                 resultlist=>1
155                                                                 );
156         
157         } else {
158                 ($template, $loggedinuser, $cookie)
159                         = get_template_and_user({template_name => "cataloguing/value_builder/labs_theses.tmpl",
160                                                 query => $query,
161                                                 type => "intranet",
162                                                 authnotrequired => 1,
163                                         });
164                 
165                 $template->param(
166                                                 'search'=>$query->param('search'),
167                 );
168                 $template->param(
169                                                 'index'=>''.$query->param('index')
170                 ) if ($query->param('index'));
171                 warn 'index : '.$query->param('index');
172                 $template->param(
173                                                 'cat_auth'=>$cat_auth
174                 ) if ($cat_auth);
175         }       
176         output_html_with_http_headers $query, $cookie, $template->output ;
177 }
178
179 1;