MARC authority management (1st draft. works really poorly)
[koha.git] / authorities / authorities-home.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::Auth;
25 use HTML::Template;
26 use C4::Context;
27 use C4::Search;
28 use C4::Auth;
29 use C4::Output;
30 use C4::Interface::CGI::Output;
31 use C4::AuthoritiesMarc;
32 use C4::SearchMarc;
33 use C4::Catalogue;
34 use C4::Koha; # XXX subfield_is_koha_internal_p
35
36 # Creates the list of active tags using the active MARC configuration
37 sub create_marclist {
38         my $dbh = C4::Context->dbh;
39         my $tagslib = AUTHgettagslib($dbh,1);
40         my @marcarray;
41         push @marcarray,"";
42         my $widest_menu_item_width = 0;
43         for (my $pass = 1; $pass <= 2; $pass += 1)
44         {
45                 for (my $tabloop = 0; $tabloop<=9;$tabloop++)
46                 {
47                         my $separator_inserted_p = 0; # FIXME... should not use!!
48                         foreach my $tag (sort(keys (%{$tagslib})))
49                         {
50                                 foreach my $subfield (sort(keys %{$tagslib->{$tag}}))
51                                 {
52                                         next if subfield_is_koha_internal_p($subfield);
53                                         next unless ($tagslib->{$tag}->{$subfield}->{tab} eq $tabloop);
54                                         my $menu_item = "$tag$subfield - $tagslib->{$tag}->{$subfield}->{lib}";
55                                         if ($pass == 1)
56                                         {
57                                                 $widest_menu_item_width = length $menu_item if($widest_menu_item_width < length $menu_item);
58                                         } else {
59                                                 if (!$separator_inserted_p)
60                                                 {
61                                                         my $w = int(($widest_menu_item_width - 3 + 0.5)/2);
62                                                         my $s = ('-' x ($w * 4/5));
63                                                         push @marcarray,  "$s $tabloop $s";
64                                                         $separator_inserted_p = 1;
65                                                 }
66                                         push @marcarray, $menu_item;
67                                         }
68                                 }
69                         }
70                 }
71         }
72         return \@marcarray;
73 }
74
75 # Creates a scrolling list with the associated default value.
76 # Using more than one scrolling list in a CGI assigns the same default value to all the
77 # scrolling lists on the page !?!? That's why this function was written.
78 sub create_scrolling_list {
79         my ($params) = @_;
80         my $scrollist = sprintf("<select name=\"%s\" size=\"%d\" onChange='%s'>\n", $params->{'name'}, $params->{'size'}, $params->{'onChange'});
81
82         foreach my $tag (@{$params->{'values'}})
83         {
84                 my $selected = "selected " if($params->{'default'} eq $tag);
85                 $scrollist .= sprintf("<option %svalue=\"%s\">%s</option>\n", $selected, $tag, $tag);
86         }
87
88         $scrollist .= "</select>\n";
89
90         return $scrollist;
91 }
92
93 my $query=new CGI;
94 my $op = $query->param('op');
95 my $authtypecode = $query->param('authtypecode');
96 my $dbh = C4::Context->dbh;
97
98 my $startfrom=$query->param('startfrom');
99 $startfrom=0 if(!defined $startfrom);
100 my ($template, $loggedinuser, $cookie);
101 my $resultsperpage;
102
103 my $authtypes = getauthtypes;
104 my @authtypesloop;
105 foreach my $thisauthtype (keys %$authtypes) {
106         my $selected = 1 if $thisauthtype eq $authtypecode;
107         my %row =(value => $thisauthtype,
108                                 selected => $selected,
109                                 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
110                         );
111         warn "X = $authtypes->{$thisauthtype}{'authtypetext'}";
112         push @authtypesloop, \%row;
113 }
114
115 if ($op eq "do_search") {
116         my @marclist = $query->param('marclist');
117         my @and_or = $query->param('and_or');
118         my @excluding = $query->param('excluding');
119         my @operator = $query->param('operator');
120         my @value = $query->param('value');
121
122         $resultsperpage= $query->param('resultsperpage');
123         $resultsperpage = 19 if(!defined $resultsperpage);
124         my $orderby = $query->param('orderby');
125
126         # builds tag and subfield arrays
127         my @tags;
128
129         foreach my $marc (@marclist) {
130                 if ($marc) {
131                         my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc);
132                         if ($tag) {
133                                 push @tags,$dbh->quote("$tag$subfield");
134                         } else {
135                                 push @tags, $dbh->quote(substr($marc,0,4));
136                         }
137                 } else {
138                         push @tags, "";
139                 }
140         }
141         findseealso($dbh,\@tags);
142         my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
143                                                                                 \@excluding, \@operator, \@value,
144                                                                                 $startfrom*$resultsperpage, $resultsperpage,$orderby);
145
146         ($template, $loggedinuser, $cookie)
147                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
148                                 query => $query,
149                                 type => 'intranet',
150                                 authnotrequired => 0,
151                                 flagsrequired => {borrowers => 1},
152                                 flagsrequired => {catalogue => 1},
153                                 debug => 1,
154                                 });
155
156         # multi page display gestion
157         my $displaynext=0;
158         my $displayprev=$startfrom;
159         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
160                 $displaynext = 1;
161         }
162
163         my @field_data = ();
164
165
166         for(my $i = 0 ; $i <= $#marclist ; $i++)
167         {
168                 push @field_data, { term => "marclist", val=>$marclist[$i] };
169                 push @field_data, { term => "and_or", val=>$and_or[$i] };
170                 push @field_data, { term => "excluding", val=>$excluding[$i] };
171                 push @field_data, { term => "operator", val=>$operator[$i] };
172                 push @field_data, { term => "value", val=>$value[$i] };
173         }
174
175         my @numbers = ();
176
177         if ($total>$resultsperpage)
178         {
179                 for (my $i=1; $i<$total/$resultsperpage+1; $i++)
180                 {
181                         if ($i<16)
182                         {
183                         my $highlight=0;
184                         ($startfrom==($i-1)) && ($highlight=1);
185                         push @numbers, { number => $i,
186                                         highlight => $highlight ,
187                                         searchdata=> \@field_data,
188                                         startfrom => ($i-1)};
189                         }
190         }
191         }
192
193         my $from = $startfrom*$resultsperpage+1;
194         my $to;
195
196         if($total < (($startfrom+1)*$resultsperpage))
197         {
198                 $to = $total;
199         } else {
200                 $to = (($startfrom+1)*$resultsperpage);
201         }
202         $template->param(result => $results,
203                                                         startfrom=> $startfrom,
204                                                         displaynext=> $displaynext,
205                                                         displayprev=> $displayprev,
206                                                         resultsperpage => $resultsperpage,
207                                                         startfromnext => $startfrom+1,
208                                                         startfromprev => $startfrom-1,
209                                                         searchdata=>\@field_data,
210                                                         total=>$total,
211                                                         from=>$from,
212                                                         to=>$to,
213                                                         numbers=>\@numbers,
214                                                         );
215
216 } elsif ($op eq "AddStatement") {
217
218         ($template, $loggedinuser, $cookie)
219                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
220                                 query => $query,
221                                 type => 'intranet',
222                                 authnotrequired => 0,
223                                 flagsrequired => {catalogue => 1},
224                                 debug => 1,
225                                 });
226
227         # Gets the entered information
228         my @marcfields = $query->param('marclist');
229         my @and_or = $query->param('and_or');
230         my @excluding = $query->param('excluding');
231         my @operator = $query->param('operator');
232         my @value = $query->param('value');
233
234         my @statements = ();
235
236         # List of the marc tags to display
237         my $marcarray = create_marclist();
238
239         my $nbstatements = $query->param('nbstatements');
240         $nbstatements = 1 if(!defined $nbstatements);
241
242         for(my $i = 0 ; $i < $nbstatements ; $i++)
243         {
244                 my %fields = ();
245
246                 # Recreates the old scrolling lists with the previously selected values
247                 my $marclist = create_scrolling_list({name=>"marclist",
248                                         values=> $marcarray,
249                                         size=> 1,
250                                         default=>$marcfields[$i],
251                                         onChange => "sql_update()"}
252                                         );
253
254                 $fields{'marclist'} = $marclist;
255                 $fields{'first'} = 1 if($i == 0);
256
257                 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
258                 $fields{'or'} = 1 if($and_or[$i] eq "or");
259
260                 #Restores the "not" parameters
261                 $fields{'not'} = 1 if($excluding[$i]);
262
263                 #Restores the operators (most common operators first);
264                 if($operator[$i] eq "=") { $fields{'eq'} = 1; }
265                 elsif($operator[$i] eq "contains") { $fields{'contains'} = 1; }
266                 elsif($operator[$i] eq "start") { $fields{'start'} = 1; }
267                 elsif($operator[$i] eq ">") { $fields{'gt'} = 1; }      #greater than
268                 elsif($operator[$i] eq ">=") { $fields{'ge'} = 1; } #greater or equal
269                 elsif($operator[$i] eq "<") { $fields{'lt'} = 1; } #lower than
270                 elsif($operator[$i] eq "<=") { $fields{'le'} = 1; } #lower or equal
271
272                 #Restores the value
273                 $fields{'value'} = $value[$i];
274
275                 push @statements, \%fields;
276         }
277         $nbstatements++;
278
279         # The new scrolling list
280         my $marclist = create_scrolling_list({name=>"marclist",
281                                 values=> $marcarray,
282                                 size=>1,
283                                 onChange => "sql_update()"});
284         push @statements, {"marclist" => $marclist };
285
286         $template->param("statements" => \@statements,
287                                                 "nbstatements" => $nbstatements);
288
289 }
290 else {
291         ($template, $loggedinuser, $cookie)
292                 = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
293                                 query => $query,
294                                 type => 'intranet',
295                                 authnotrequired => 0,
296                                 flagsrequired => {catalogue => 1},
297                                 debug => 1,
298                                 });
299         #$template->param(loggedinuser => $loggedinuser);
300
301         my $marcarray = create_marclist();
302
303         my $marclist = CGI::scrolling_list(-name=>"marclist",
304                                         -values=> $marcarray,
305                                         -size=>1,
306                                         -multiple=>0,
307                                         -onChange => "sql_update()",
308                                         );
309
310         my @statements = ();
311
312         # Considering initial search with 3 criterias
313         push @statements, { "marclist" => $marclist, "first" => 1 };
314         push @statements, { "marclist" => $marclist, "first" => 0 };
315         push @statements, { "marclist" => $marclist, "first" => 0 };
316         my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
317         $sth->execute;
318         my  @itemtype;
319         my %itemtypes;
320         push @itemtype, "";
321         $itemtypes{''} = "";
322         while (my ($value,$lib) = $sth->fetchrow_array) {
323                 push @itemtype, $value;
324                 $itemtypes{$value}=$lib;
325         }
326
327         my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
328                                 -values   => \@itemtype,
329                                 -labels   => \%itemtypes,
330                                 -size     => 1,
331                                 -multiple => 0 );
332         $sth->finish;
333
334         my @branches;
335         my @select_branch;
336         my %select_branches;
337         my ($count2,@branches)=branches();
338         push @select_branch, "";
339         $select_branches{''} = "";
340         for (my $i=0;$i<$count2;$i++){
341                 push @select_branch, $branches[$i]->{'branchcode'};#
342                 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
343         }
344         my $CGIbranch=CGI::scrolling_list( -name     => 'value',
345                                 -values   => \@select_branch,
346                                 -labels   => \%select_branches,
347                                 -size     => 1,
348                                 -multiple => 0 );
349         $sth->finish;
350
351
352         $template->param("statements" => \@statements,
353                         "nbstatements" => 3,
354                         CGIitemtype => $CGIitemtype,
355                         CGIbranch => $CGIbranch,
356                         );
357 }
358
359 $template->param(authtypesloop => \@authtypesloop);
360
361 # Print the page
362 output_html_with_http_headers $query, $cookie, $template->output;
363
364 # Local Variables:
365 # tab-width: 4
366 # End: