b20aacfaadd4659442fb78fb3c858d9af870fed5
[koha-bibliografija] / html.pl
1 #!/usr/bin/perl
2
3 # LC_COLLATE=hr_HR.utf8 KOHA_CONF=/etc/koha/sites/ffzg/koha-conf.xml ./html.pl
4
5 use warnings;
6 use strict;
7
8 use DBI;
9 use Data::Dump qw(dump);
10 use autodie;
11 use locale;
12 use Text::Unaccent;
13 use Carp qw(confess);
14 use utf8;
15
16 use lib '/srv/koha_ffzg';
17 use C4::Context;
18 use XML::LibXML;
19 use XML::LibXSLT;
20
21 my $dbh = C4::Context->dbh;
22
23 sub debug {
24         my ($title, $data) = @_;
25         print "# $title ",dump($data), $/ if $ENV{DEBUG};
26 }
27
28 my $xslfilename = 'compact.xsl';
29
30 my $azvo_group_title = {
31 'znanstveno nastavni' => qr/(profes|docent|znanstveni savjetnik|znanstveni suradnik)/i,
32 'lektori i predavači' => qr/(lektor|predavač)/i,
33 'asistenti i novaci' => qr/(asistent|novak)/i,
34 };
35
36 my $department_groups = {
37 'AAB_humanističke'             => qr/(anglistiku|arheologiju|antropologiju|filozofiju|fonetiku|germanistiku|hungarologiju|indologiju|slavenske|filologiju|komparativnu|kroatistiku|lingvistiku|povijest|romanistiku|talijanistiku)/i,
38 'AAC_društvene'                        => qr/(informacijske|pedagogiju|psihologiju|sociologiju)/i,
39 };
40
41 my $auth_header;
42 my $auth_department;
43 my $auth_group;
44 my @authors;
45 my $department_in_sum;
46 my $department_in_group;
47
48 my $skip;
49
50 my $sth_auth = $dbh->prepare(q{
51 select
52         authid,
53         ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="a"]') as full_name,
54         ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="a"]') as department,
55         ExtractValue(marcxml,'//datafield[@tag="680"]/subfield[@code="i"]') as academic_title
56 from auth_header
57 });
58
59 $sth_auth->execute();
60 while( my $row = $sth_auth->fetchrow_hashref ) {
61         if ( $row->{department} !~ m/Filozofski fakultet u Zagrebu/ ) {
62                 push @{ $skip->{nije_ffzg} }, $row;
63                 next;
64         }
65         $auth_header->{ $row->{authid} } = $row->{full_name};
66         $row->{department} =~ s/, Filozofski fakultet u Zagrebu.*$//;
67         $row->{department} =~ s/^.+\.\s*//;
68         $row->{department} =~ s/\s+$//s;
69         my $group;
70         foreach my $title ( keys %$azvo_group_title ) {
71                 if ( $row->{academic_title} =~ $azvo_group_title->{$title} ) {
72                         $group = $title;
73                         last;
74                 }
75         }
76         if ( $group ) {
77                 $row->{academic_group} = $group;
78                 $auth_group->{ $row->{authid} } = $group;
79                 $skip->{group_stat}->{$group}++;
80         } else {
81                 push @{ $skip->{no_academic_group} }, $row;
82         }
83
84 #       warn "# ", dump( $row );
85         push @{ $auth_department->{ $row->{department} } }, $row->{authid};
86         push @authors, $row;
87         $department_in_sum->{ $row->{department} }++;
88         foreach my $name ( keys %$department_groups ) {
89                 my $regex = $department_groups->{$name};
90                 if ( $row->{department} =~ $regex ) {
91                         $department_in_group->{ $row->{department} } = $name;
92                         last;
93                 }
94         }
95 }
96
97 debug 'department_in_group' => $department_in_group;
98
99 foreach my $department ( keys %$department_in_sum ) {
100         $department_in_sum->{$department} = 0 unless $department =~ m/(centar|croaticum|katedra|odsjek)/i;
101 }
102
103 debug 'auth_department' => $auth_department;
104 debug 'auth_group' => $auth_group;
105 debug 'department_in_sum' => $department_in_sum;
106
107
108 my $authors;
109 my $marcxml;
110
111 my $sth_select_authors  = $dbh->prepare(q{
112 select
113         biblionumber,
114         itemtype,
115         marcxml
116 from biblioitems
117 where
118         agerestriction > 0
119 });
120
121 =for sql
122 --      ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
123 --      ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
124 --      ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
125
126 --      and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
127 -- order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
128 =cut
129
130 my $biblio_year;
131 my $type_stats;
132
133 my $parser = XML::LibXML->new();
134 $parser->recover_silently(0); # don't die when you find &, >, etc
135 my $style_doc = $parser->parse_file($xslfilename);
136 my $xslt = XML::LibXSLT->new();
137 my $parsed = $xslt->parse_stylesheet($style_doc);
138
139 my $biblio_html;
140 my $biblio_parsed;
141 my $biblio_data;
142 my $biblio_author_external;
143
144 open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
145
146 sub biblioitem_html {
147         my ($biblionumber, $parse_only) = @_;
148
149         return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber} && ! $parse_only;
150
151         my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
152
153         print $xml_fh $xmlrecord if $ENV{XML};
154
155         my $source = eval { $parser->parse_string($xmlrecord) };
156         if ( $@ ) {
157 #               warn "SKIP $biblionumber corrupt XML";
158                 push @{ $skip->{XML_corrupt} }, $biblionumber;
159                 return;
160         }
161
162         if ( $parse_only ) {
163                 $biblio_parsed->{$biblionumber} = $source;
164                 return $source;
165         }
166
167         my $transformed = $parsed->transform($source);
168         $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
169
170         delete $biblio_parsed->{$biblionumber};
171
172         return $biblio_html->{$biblionumber};
173 }
174
175 $sth_select_authors->execute();
176 while( my $row = $sth_select_authors->fetchrow_hashref ) {
177 #       warn dump($row),$/;
178
179         my $biblio;
180
181         $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
182
183         my $doc = biblioitem_html( $row->{biblionumber}, 1 );
184         if ( ! $doc ) {
185 #               warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
186                 next;
187         }
188
189         my $root = $doc->documentElement;
190 =for leader
191         my @leaders = $root->getElementsByLocalName('leader');
192         if (@leaders) {
193                 my $leader = $leaders[0]->textContent;
194                 warn "leader $leader\n";
195         }
196 =cut
197
198         my $extract = {
199                 '008' => undef,
200                 '100' => '(9|a)',
201                 '680' => 'i',
202                 '700' => '(9|4|a)',
203                 '942' => '(t|r|v)'
204         };
205
206         my $data;
207
208         foreach my $elt ($root->getChildrenByLocalName('*')) {
209                 my $tag = $elt->getAttribute('tag');
210                 next if ! $tag;
211                 next unless exists $extract->{ $tag };
212
213         if ($elt->localname eq 'controlfield') {
214                         if ( $tag eq '008' ) {
215                                 my $content = $elt->textContent;
216                                 my $year = substr($content, 7, 4 );
217                                 if ( $year !~ m/^\d+$/ ) {
218                                         $year = 0;
219                                         push @{ $skip->{invalid_year} }, $row->{biblionumber};
220                                 }
221                                 $biblio_year->{ $row->{biblionumber} } = $data->{year} = $year;
222                                 $data->{'008'} = $content;
223                         }
224                         next;
225         } elsif ($elt->localname eq 'datafield') {
226                         my $sf_data;
227             foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
228                 my $sf = $sfelt->getAttribute('code');
229                                 next unless $sf =~ m/$extract->{$tag}/;
230                                 if ( exists $sf_data->{$sf} ) {
231                                         $sf_data->{$sf} .= " " . $sfelt->textContent();
232                                 } else {
233                                         $sf_data->{$sf} = $sfelt->textContent();
234                                 }
235                         }
236                         push @{ $data->{$tag} }, $sf_data if $sf_data;
237                 }
238         }
239
240         if ( ! defined $data->{year} ) {
241                 warn "MISSING year in ", $row->{biblionumber};
242 =for remove-year-limit
243         } elsif ( $data->{year} < 2008 ) {
244                 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
245                 next;
246         } elsif ( $data->{year} > 2013 ) {
247                 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
248                 next;
249 =cut
250         }
251
252 #       warn "# ", $row->{biblionumber}, " data ",dump($data);
253
254         my $category = $data->{942}->[0]->{'t'};
255         if ( ! $category ) {
256 #               warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
257                 push @{ $skip->{no_category} }, $row->{biblionumber};
258                 next;
259         }
260
261         my $have_100 = 1;
262
263         if ( exists $data->{100} ) {
264                         my @first_author =
265                                 map { $_->{'9'} }
266                                 grep {
267                                         if ( ! exists $_->{9} ) {
268                                                 $biblio_author_external->{ $row->{biblionumber} }++;
269                                                 0;
270                                         } elsif ( exists $auth_header->{ $_->{9} } ) {
271                                                 1; # from FFZXG
272                                         } else {
273                                                 0;
274                                         }
275                                 }
276                                 @{ $data->{100} };
277                         foreach my $authid ( @first_author ) {
278                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
279                         }
280         } else {
281                 $have_100 = 0;
282         }
283
284         my $have_edt;
285
286         if ( exists $data->{700} ) {
287                         my @other_authors =
288                                 grep {
289                                         if ( ! exists $_->{9} ) {
290                                                 $biblio_author_external->{ $row->{biblionumber} }++;
291                                                 0;
292                                         } elsif ( exists $auth_header->{ $_->{9} } ) {
293                                                 1; # from FFZXG
294                                         } else {
295                                                 0;
296                                         }
297                                 }
298                                 @{ $data->{700} };
299                         foreach my $auth ( @other_authors ) {
300                                 my $authid = $auth->{9} || next;
301                                 my $type   = $auth->{4} || next; #die "no 4 in ",dump($data);
302
303                                 $type_stats->{$type}++;
304
305                                 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
306                                         push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
307                                         push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
308                                 } elsif ( $type =~ m/aut/ ) {
309                                         if ( ! $have_100 ) {
310                                                 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
311                                                 if ( $have_edt ) {
312                                                         $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
313                                                 } else {
314                                                         push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
315                                                 }
316                                         } else {
317                                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
318                                         }
319                                 } else {
320 #                                       warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
321                                         $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
322                                 }
323                         }
324                         delete $data->{700};
325         }
326
327         $biblio_data->{ $row->{biblionumber} } = $data;
328
329 }
330
331 debug 'authors' => $authors;
332 debug 'type_stats' => $type_stats;
333 debug 'skip' => $skip;
334 debug 'biblio_year' => $biblio_year;
335 debug 'biblio_data' => $biblio_data;
336 debug 'biblio_author_external' => $biblio_author_external;
337
338 my $category_label;
339 my $sth_categories = $dbh->prepare(q{
340 select authorised_value, lib from authorised_values where category = 'BIBCAT'
341 });
342 $sth_categories->execute();
343 while( my $row = $sth_categories->fetchrow_hashref ) {
344         $category_label->{ $row->{authorised_value} } = $row->{lib};
345
346 }
347 debug 'category_label' => $category_label;
348
349 sub html_title {
350         return qq|<html>
351 <head>
352 <meta charset="UTF-8">
353 <title>|, join(" ", @_), qq|</title>
354 <link href="style.css" type="text/css" rel="stylesheet" />
355 </head>
356 <body>
357 |;
358 }
359
360 sub html_end {
361         return qq|</body>\n</html>\n|;
362 }
363
364 mkdir 'html' unless -d 'html';
365
366 open(my $index, '>:encoding(utf-8)', 'html/index.new');
367 print $index html_title('Bibliografija Filozofskog fakulteta');
368
369 my $first_letter = '';
370
371 debug 'authors' => \@authors;
372
373 sub li_biblio {
374         my ($biblionumber) = @_;
375         return qq|<li>|,
376                 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
377                 biblioitem_html($biblionumber),
378                 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
379                 qq|</li>\n|;
380 }
381
382 sub author_html {
383         my ( $fh, $authid, $type, $label ) = @_;
384
385         return unless exists $authors->{$authid}->{$type};
386
387         print $fh qq|<h2>$label</h2>\n|;
388
389         foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
390                 my $label = $category_label->{$category} || 'Bez kategorije';
391                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
392                 foreach my $biblionumber ( sort {
393                                 $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b
394                         } @{ $authors->{$authid}->{$type}->{$category} } ) {
395                         print $fh li_biblio( $biblionumber );
396                 }
397                 print $fh qq|</ul>\n|;
398         }
399 }
400
401 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
402
403         my $first = substr( $row->{full_name}, 0, 1 );
404         if ( $first ne $first_letter ) {
405                 print $index qq{</ul>\n} if $first_letter;
406                 $first_letter = $first;
407                 print $index qq{<h1>$first</h1>\n<ul>\n};
408         }
409         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
410
411         my $path = "html/$row->{authid}";
412         open(my $fh, '>:encoding(utf-8)', "$path.new");
413         print $fh html_title($row->{full_name}, "bibliografija");
414         print $fh qq|<h1>$row->{full_name} - bibliografija</h1>|;
415
416         author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
417         author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
418
419         print $fh html_end;
420         close($fh);
421         rename "$path.new", "$path.html";
422
423 }
424
425 print $index html_end;
426 close($index);
427 rename 'html/index.new', 'html/index.html';
428
429 debug 'auth_header' => $auth_header;
430
431
432 my $department_category_author;
433 foreach my $department ( sort keys %$auth_department ) {
434         foreach my $authid ( sort @{ $auth_department->{$department} } ) {
435                 my   @categories = keys %{ $authors->{$authid}->{aut} };
436                 push @categories,  keys %{ $authors->{$authid}->{sec} };
437                 foreach my $category ( sort @categories ) {
438                         push @{ $department_category_author->{$department}->{$category} }, $authid;
439                         push @{ $department_category_author->{'AAA_ukupno'}->{$category} }, $authid if $department_in_sum->{$department};
440                         if ( my $group = $department_in_group->{ $department } ) {
441                                 push @{ $department_category_author->{$group}->{$category} }, $authid;
442                         } else {
443                                 $skip->{'department_not_in_group'}->{ $department }++;
444                         }
445                 }
446         }
447 }
448
449 debug 'department_category_author' => $department_category_author;
450
451 mkdir 'html/departments' unless -d 'html/departments';
452
453 sub unique_biblionumber {
454         my @v = @_;
455         my $u;
456         $u->{$_}++ foreach @v;
457         return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
458 }
459
460 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
461 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
462 foreach my $department ( sort keys %$department_category_author ) {
463         my $dep = $department || 'Nema odsjeka';
464         my $dep_file = unac_string('utf-8',$dep);
465         print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
466         open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
467
468         print $fh html_title($department . ' bibliografija');
469         print $fh qq|<h1>$department bibliografija</h1>\n|;
470
471         print $fh qq|<h2>Primarno autorstvo</h2>\n|;
472
473         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
474
475                 my @authids = @{ $department_category_author->{$department}->{$category} };
476                 next unless @authids;
477
478                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
479                 my $unique;
480                 $unique->{$_}++ foreach @biblionumber;
481
482                 next unless @biblionumber;
483
484                 my $label = $category_label->{$category} || 'Bez kategorije';
485                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
486
487                 print $fh li_biblio( $_ ) foreach @biblionumber;
488
489                 print $fh qq|</ul>|;
490         }
491
492
493         print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
494
495         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
496
497                 my @authids = @{ $department_category_author->{$department}->{$category} };
498                 next unless @authids;
499
500                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
501
502                 next unless @biblionumber;
503
504                 my $label = $category_label->{$category} || 'Bez kategorije';
505                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
506
507                 print $fh li_biblio( $_ ) foreach @biblionumber;
508
509                 print $fh qq|</ul>|;
510         }
511
512
513         print $fh html_end;
514         close($fh);
515         rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
516 }
517 print $dep_fh qq|</ul>\n|, html_end;
518 close($dep_fh);
519 rename 'html/departments/index.new', 'html/departments/index.html';
520
521 my $azvo_stat_biblio;
522
523 foreach my $department ( sort keys %$department_category_author ) {
524         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
525                 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
526                         my $group = $auth_group->{$authid};
527                         if ( ! $group ) {
528                                 push @{ $skip->{no_auth_group} }, $authid;
529                                 next;
530                         }
531                         foreach my $type ( keys %{ $authors->{$authid} } ) {
532                                 next unless exists $authors->{$authid}->{$type}->{$category};
533                                 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} },  @{ $authors->{$authid}->{$type}->{$category} };
534                                 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} },  @{ $authors->{$authid}->{$type}->{$category} };
535                         }
536                 }
537                 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
538                         foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
539                                 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
540                                 $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
541                         }
542                 }
543         }
544 }
545
546 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
547
548 my @report_lines;
549 my @report_labels;
550
551 my $label;
552 my $sub_labels;
553 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
554 while( <$report> ) {
555         chomp;
556         if ( /^([^\t]+)\t+(.+)/ ) {
557                 $label = $1;
558                 push @report_labels, $label;
559                 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
560                 push @report_lines, [ $label, @$type ];
561         } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
562                 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
563                 my $sub_label = $1;
564                 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
565                 push @report_labels, $label . $sub_label;
566         } else {
567                 die "ERROR: [$_]\n";
568         }
569 }
570
571 debug 'report_lines', \@report_lines;
572 debug 'sub_labels', $sub_labels;
573 debug 'report_labels', \@report_labels;
574
575 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
576
577 debug 'departments' => \@departments;
578
579 my $department2col;
580 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
581 my $label2row;
582 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
583
584 my $table;
585
586 sub table_count {
587         my $label = shift @_;
588         my $department = shift @_;
589         my $group = shift @_;
590         my @biblionumbers = @_;
591         my $unique;
592         $unique->{$_}++ foreach @biblionumbers;
593         my @bibs = keys %$unique;
594         $table->{ffzg}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar @bibs;
595         $table->{external}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar grep { $biblio_author_external->{$_} } @bibs;
596 }
597
598 foreach my $group ( '', keys %$azvo_group_title ) {
599
600 foreach my $department ( @departments ) {
601         foreach my $line ( @report_lines ) {
602                 my $label = $line->[0];
603                 my @biblionumbers;
604                 foreach ( 1 .. $#$line ) {
605                         my ( $category, $type ) = @{ $line->[ $_ ] };
606                         my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type}->{$group};
607                         push @biblionumbers, @$b if $b;
608                 }
609                 if ( $sub_labels->{$label} ) {
610                         my $sub_stats;
611                         foreach my $biblionumber ( @biblionumbers ) {
612                                 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
613                                 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
614                                         my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
615                                         if ( ! $regex ) {
616                                                 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
617                                                 last;
618                                         }
619                                         if ( $field < 100 ) {
620                                                 if ( $data->{$field} =~ m/$regex/ ) {
621                                                         push @{ $sub_stats->{ $sub_label } }, $biblionumber;
622                                                         last;
623                                                 }
624                                         } else {
625                                                 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
626                                                         push @{ $sub_stats->{ $sub_label } }, $biblionumber;
627                                                         last;
628                                                 }
629                                         }
630                                 }
631                         }
632                         foreach my $sub_label ( keys %$sub_stats ) {
633                                 my $full_label = $label . $sub_label;
634                                 table_count $full_label, $department, $group, @{ $sub_stats->{$sub_label} };
635                         }
636                 } else {
637                         table_count $label, $department, $group, @biblionumbers;
638                 }
639         }
640 }
641
642 } # group
643
644 debug 'table', $table;
645
646 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
647 open(my $fh2, '>:encoding(utf-8)', 'html/azvo2.new');
648
649 sub print_fh {
650         print $fh @_;
651         print $fh2 @_;
652 }
653
654 print $fh html_title('AZVO tablica - FFZG');
655 print $fh2 html_title('AZVO tablica - kolaboracija sa FFZG');
656
657 foreach my $group ( keys %{ $table->{ffzg} } ) {
658
659                 print_fh "<h1>$group</h1>" if $group;
660
661                 print_fh "<table border=1>\n";
662                 print_fh "<tr><th></th>";
663                 print_fh "<th>$_</th>" foreach @departments;
664                 print_fh "</tr>\n";
665
666                 foreach my $row ( 0 .. $#{ $table->{ffzg}->{$group} } ) {
667                         print_fh "<tr><th>", $report_labels[$row], "</th>\n";
668                         foreach ( 0 .. $#departments ) {
669                                 print_fh "<td>";
670                                 print $fh $table->{ffzg}->{$group}->[ $row ]->[ $_ ] || '';
671                                 print $fh2 $table->{external}->{$group}->[ $row ]->[ $_ ] || '';
672                                 print_fh "</td>\n"
673                         }
674                         print_fh "</tr>\n";
675                 }
676
677                 print_fh "</table>\n";
678
679 } # group
680
681 print_fh html_end;
682 close($fh);
683 close($fh2);
684 rename 'html/azvo.new', 'html/azvo.html';
685 rename 'html/azvo2.new', 'html/azvo2.html';
686