2 BUGFIX (1 being unimarc specific) : change in plugin handling
[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 require Exporter;
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
43 function Focus$function_name(subfield_managed) {
44 return 1;
45 }
46
47 function Blur$function_name(subfield_managed) {
48         return 1;
49 }
50
51 function Clic$function_name(i) {
52         defaultvalue=document.f.field_value[i].value;
53         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');
54
55 }
56 //]]>
57 </script>
58 ";
59
60 return ($function_name,$res);
61 }
62
63 sub plugin {
64         my ($input) = @_;
65         my $dbh=C4::Context->dbh;
66         my $query = new CGI;
67         my $op = $query->param('op');
68         my $cat_auth=$query->param('cat_auth');
69
70         my $startfrom=$query->param('startfrom');
71         $startfrom=0 if(!defined $startfrom);
72         my ($template, $loggedinuser, $cookie);
73         my $resultsperpage;
74         my $search = $query->param('search');
75         
76         if ($op eq "do_search") {
77         
78                 $resultsperpage= $query->param('resultsperpage');
79                 $resultsperpage = 19 if(!defined $resultsperpage);
80 #               my $upperlimit=$startfrom+$resultsperpage;
81                 # builds tag and subfield arrays
82                 my $strquery = "SELECT authorised_value, lib from authorised_values where category = ? and lib like ?";
83 #               $strquery .= " LIMIT $startfrom,$upperlimit";
84                 
85                 warn 'category : '.$cat_auth.' recherche :'.$search;
86                 warn "$strquery";
87                 $search=~s/\*/%/g;
88                 my $sth = $dbh->prepare($strquery);
89                 $sth->execute($cat_auth,$search);
90                 $search=~s/%/\*/g;
91                 
92                 
93                 my @results;
94                 my $total;
95                 while (my $data = $sth->fetchrow_hashref){
96                         my $libjs=$data->{'lib'};
97                         $libjs=~s#\'#\\\'#g;
98                         my $authjs=$data->{'authorised_value'};
99                         $authjs=~s#\'#\\\'#g;
100                         push @results, {'libjs'=>$libjs,
101                                                         'lib'=>$data->{'lib'},
102                                                         'authjs'=>$authjs,
103                                                         'auth_value'=>$data->{'authorised_value'}} 
104                                                         unless (($total<$startfrom) or ($total>$startfrom+$resultsperpage));
105                         $total++;
106                 }
107                 
108                 ($template, $loggedinuser, $cookie)
109                         = get_template_and_user({template_name => "value_builder/labs_theses.tmpl",
110                                         query => $query,
111                                         type => 'intranet',
112                                         authnotrequired => 1,
113                                         debug => 1,
114                                         });
115         
116                 # multi page display gestion
117                 my $displaynext=0;
118                 my $displayprev=$startfrom;
119                 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
120                         $displaynext = 1;
121                 }
122         
123                 my @numbers = ();
124         
125                 if ($total>$resultsperpage)
126                 {
127                         for (my $i=1; (($i<$total/$resultsperpage+1) && ($i<16)); $i++)
128                         {
129                                         my $highlight=0;
130                                         ($startfrom==($i-1)) && ($highlight=1);
131                                         push @numbers, { number => $i,
132                                                 highlight => $highlight ,
133                                                 search=> $search,
134                                                 startfrom => $resultsperpage*($i-1)};
135                         }
136                 }
137         
138                 my $from = $startfrom+1;
139                 my $to;
140         
141                 if($total < (($startfrom+1)*$resultsperpage))
142                 {
143                         $to = $total;
144                 } else {
145                         $to = (($startfrom+1)*$resultsperpage);
146                 }
147                 $template->param(catresult => \@results,
148                                                 cat_auth=>$cat_auth,
149                                                 index => $query->param('index')."",
150                                                                 startfrom=> $startfrom,
151                                                                 displaynext=> $displaynext,
152                                                                 displayprev=> $displayprev,
153                                                                 resultsperpage => $resultsperpage,
154                                                                 startfromnext => $startfrom+$resultsperpage,
155                                                                 startfromprev => $startfrom-$resultsperpage,
156                                                                 search=>$search,
157                                                                 total=>$total,
158                                                                 from=>$from,
159                                                                 to=>$to,
160                                                                 numbers=>\@numbers,
161                                                                 resultlist=>1
162                                                                 );
163         
164         } else {
165                 ($template, $loggedinuser, $cookie)
166                         = get_template_and_user({template_name => "value_builder/labs_theses.tmpl",
167                                                 query => $query,
168                                                 type => "intranet",
169                                                 authnotrequired => 1,
170                                         });
171                 
172                 $template->param(
173                                                 'search'=>$query->param('search'),
174                 );
175                 $template->param(
176                                                 'index'=>''.$query->param('index')
177                 ) if ($query->param('index'));
178                 warn 'index : '.$query->param('index');
179                 $template->param(
180                                                 'cat_auth'=>$cat_auth
181                 ) if ($cat_auth);
182         }       
183         output_html_with_http_headers $query, $cookie, $template->output ;
184 }
185
186 1;