BugFixing unimarc_field_210c.pl was badly broken. It allows searches.
[koha.git] / cataloguing / value_builder / unimarc_field_210c.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 require Exporter;
23 use C4::AuthoritiesMarc;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Output;
27 use CGI;
28 use C4::Search;
29 use MARC::Record;
30 use C4::Koha;
31
32 ###TODO To rewrite in order to use SearchAuthorities
33
34 =head1
35
36 plugin_parameters : other parameters added when the plugin is called by the dopop function
37
38 =cut
39 sub plugin_parameters {
40 my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
41 return "";
42 }
43
44 =head1
45
46 plugin_javascript : the javascript function called when the user enters the subfield.
47 contain 3 javascript functions :
48 * one called when the field is entered (OnFocus). Named FocusXXX
49 * one called when the field is leaved (onBlur). Named BlurXXX
50 * one called when the ... link is clicked (<a href="javascript:function">) named ClicXXX
51
52 returns :
53 * XXX
54 * a variable containing the 3 scripts.
55 the 3 scripts are inserted after the <input> in the html code
56
57 =cut
58 sub plugin_javascript {
59 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
60 my $function_name= $field_number;
61 #---- build editors list.
62 #---- the editor list is built from the "EDITORS" thesaurus
63 #---- this thesaurus category must be filled as follow :
64 #---- 200$a for isbn
65 #---- 200$b for editor
66 #---- 200$c (repeated) for collections
67  my $sth 
68 = $dbh->prepare("select auth_subfield_table.authid,subfieldvalue from auth_subfield_table 
69                         left join auth_header on auth_subfield_table.authid=auth_header.authid
70                         where authtypecode='EDITORS' and tag='200' and subfieldcode='a'");
71  my $sth2
72  = $dbh->prepare("select subfieldvalue from auth_subfield_table where tag='200' and subfieldcode='b' and authid=?");
73 $sth->execute;
74 my @editors;
75 my $authoritysep = C4::Context->preference("authoritysep");
76 while (my ($authid,$isbn) = $sth->fetchrow) {
77     $sth2->execute($authid);
78     my ($editor) = $sth2->fetchrow;
79     push(@editors,"$isbn $authoritysep $editor");
80 }
81 my $res  = "
82 <script type=\"text/javascript\">
83 function Focus$function_name(index) {
84 var isbn_array = [ ";
85 foreach my $editor (@editors) {
86     my @arr = split (/ $authoritysep /,$editor);
87     $res .='["'.$arr[0].'","'.$arr[1].'","'.$arr[2].'"],';
88 }
89 chop $res;
90 $res .= "
91 ];
92     // search isbn subfield. it''s 010a
93     var isbn_found;
94     var nb_fields = document.f.field_value.length;
95     for (i=0 ; i< nb_fields; i++) {
96         if (document.f.tag[i].value == '010' && document.f.subfield[i].value == 'a') {
97             isbn_found=document.f.field_value[i].value;
98             break;
99         }
100     }
101     try{
102         isbn_found.getAttribute('value'); // throw an exception if doesn't (if no 010a)
103     }
104     catch(e){
105         return;
106     }
107     for (i=0;i<=isbn_array.length;i++) {
108         if (isbn_found.substr(0,isbn_array[i][0].length) == isbn_array[i][0]) {
109             document.f.field_value[index].value =isbn_array[i][1];
110         }
111     }
112 }
113
114 function Blur$function_name(subfield_managed) {
115     return 1;
116 }
117
118 function Clic$function_name(subfield_managed) {
119     defaultvalue=escape(document.getElementById(\"$field_number\").value);
120     newin=window.open(\"plugin_launcher.pl?plugin_name=unimarc_field_210c.pl&index=\"+subfield_managed,\"unimarc 225a\",'width=500,height=600,toolbar=false,scrollbars=yes');
121 }
122 </script>
123 ";
124 return ($function_name,$res);
125 }
126
127 =head1
128
129 plugin : the true value_builded. The screen that is open in the popup window.
130
131 =cut
132
133 sub plugin {
134 my ($input) = @_;
135     my $index = $input->param("index");
136     my $result =  $input->param("result");
137
138     my $query=new CGI;
139     my $op = $query->param('op');
140     my $authtypecode = $query->param('authtypecode');
141     my $index = $query->param('index');
142     my $category = $query->param('category');
143     my $resultstring = $query->param('result');
144     my $dbh = C4::Context->dbh;
145     
146     my $startfrom=$query->param('startfrom');
147     $startfrom=0 if(!defined $startfrom);
148     my ($template, $loggedinuser, $cookie);
149     my $resultsperpage;
150     
151     my $authtypes = getauthtypes;
152     my @authtypesloop;
153     foreach my $thisauthtype (keys %$authtypes) {
154         my $selected = 1 if $thisauthtype eq $authtypecode;
155         my %row =(value => $thisauthtype,
156                     selected => $selected,
157                     authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
158                 index => $index,
159                 );
160         push @authtypesloop, \%row;
161     }
162
163     if ($op eq "do_search") {
164         my @marclist = $query->param('marclist');
165         my @and_or = $query->param('and_or');
166         my @excluding = $query->param('excluding');
167         my @operator = $query->param('operator');
168         my @value = $query->param('value');
169     
170         $resultsperpage= $query->param('resultsperpage');
171         $resultsperpage = 19 if(!defined $resultsperpage);
172     
173         # builds tag and subfield arrays
174         my @tags;
175     
176         my ($results,$total) = SearchAuthorities( \@tags,\@and_or,
177                                             \@excluding, \@operator, \@value,
178                                             $startfrom*$resultsperpage, $resultsperpage,$authtypecode);# $orderby);
179     
180         ($template, $loggedinuser, $cookie)
181             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tmpl",
182                     query => $query,
183                     type => 'intranet',
184                     authnotrequired => 0,
185                     flagsrequired => {editcatalogue => 1},
186                     debug => 1,
187                     });
188     
189         # multi page display gestion
190         my $displaynext=0;
191         my $displayprev=$startfrom;
192         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
193             $displaynext = 1;
194         }
195     
196         my @numbers = ();
197     
198         if ($total>$resultsperpage) {
199             for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
200                 if ($i<16) {
201                     my $highlight=0;
202                     ($startfrom==($i-1)) && ($highlight=1);
203                     push @numbers, { number => $i,
204                         highlight => $highlight ,
205                         startfrom => ($i-1)};
206                 }
207             }
208         }
209     
210         my $from = $startfrom*$resultsperpage+1;
211         my $to;
212     
213         if($total < (($startfrom+1)*$resultsperpage)) {
214             $to = $total;
215         } else {
216             $to = (($startfrom+1)*$resultsperpage);
217         }
218         my $link="plugin_launcher.pl?plugin_name=unimarc_field_210c.pl&amp;authtypecode=EDITORS&and_or=and&operator=contains&".join("&",map {"value=".$_} @value)."&op=do_search&type=intranet&index=$index";
219         warn "$link ,".getnbpages($total, $resultsperpage);
220         $template->param(result => $results) if $results;
221         $template->param('index' => $query->param('index'));
222         $template->param(startfrom=> $startfrom,
223                                 displaynext=> $displaynext,
224                                 displayprev=> $displayprev,
225                                 resultsperpage => $resultsperpage,
226                                 startfromnext => $startfrom+1,
227                                 startfromprev => $startfrom-1,
228                                 total=>$total,
229                                 from=>$from,
230                                 to=>$to,
231                                 numbers=>\@numbers,
232                                 authtypecode =>$authtypecode,
233                                 resultstring =>$value[0],
234                                 pagination_bar => pagination_bar(
235                                     $link,
236                                     getnbpages($total, $resultsperpage),
237                                     $startfrom,
238                                     'startfrom'
239                                 ),
240                                 );
241     } else {
242         ($template, $loggedinuser, $cookie)
243             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tmpl",
244                     query => $query,
245                     type => 'intranet',
246                     authnotrequired => 0,
247                     flagsrequired => {editcatalogue => 1},
248                     debug => 1,
249                     });
250     
251         $template->param(index => $index,
252                         resultstring => $resultstring
253                         );
254     }
255     
256     $template->param(authtypesloop => \@authtypesloop);
257     $template->param(category => $category);
258     
259     # Print the page
260     output_html_with_http_headers $query, $cookie, $template->output;
261 }
262
263 1;