Merge branch 'master' of koha-dev.rot13.org:/srv/koha-bibliografija
[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/(profesor|docent|znanstveni)/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 'AAA_humanističke'             => qr/(anglistiku|arheologiju|antropologiju|filozofiju|fonetiku|germanistiku|hungarologiju|indologiju|slavenske|filologiju|komparativnu|kroatistiku|lingvistiku|povijest|romanistiku|talijanistiku)/i,
38 'AAB_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         } elsif ( $data->{year} < 2008 ) {
243                 push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
244                 next;
245         } elsif ( $data->{year} > 2013 ) {
246                 push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
247                 next;
248         }
249
250 #       warn "# ", $row->{biblionumber}, " data ",dump($data);
251
252         my $category = $data->{942}->[0]->{'t'};
253         if ( ! $category ) {
254 #               warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
255                 push @{ $skip->{no_category} }, $row->{biblionumber};
256                 next;
257         }
258
259         my $have_100 = 1;
260
261         if ( exists $data->{100} ) {
262                         my @first_author =
263                                 map { $_->{'9'} }
264                                 grep {
265                                         if ( ! exists $_->{9} ) {
266                                                 $biblio_author_external->{ $row->{biblionumber} }++;
267                                                 0;
268                                         } elsif ( exists $auth_header->{ $_->{9} } ) {
269                                                 1; # from FFZXG
270                                         } else {
271                                                 0;
272                                         }
273                                 }
274                                 @{ $data->{100} };
275                         foreach my $authid ( @first_author ) {
276                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
277                         }
278         } else {
279                 $have_100 = 0;
280         }
281
282         my $have_edt;
283
284         if ( exists $data->{700} ) {
285                         my @other_authors =
286                                 grep {
287                                         if ( ! exists $_->{9} ) {
288                                                 $biblio_author_external->{ $row->{biblionumber} }++;
289                                                 0;
290                                         } elsif ( exists $auth_header->{ $_->{9} } ) {
291                                                 1; # from FFZXG
292                                         } else {
293                                                 0;
294                                         }
295                                 }
296                                 @{ $data->{700} };
297                         foreach my $auth ( @other_authors ) {
298                                 my $authid = $auth->{9} || next;
299                                 my $type   = $auth->{4} || next; #die "no 4 in ",dump($data);
300
301                                 $type_stats->{$type}++;
302
303                                 if ( $type =~ m/(edt|trl|com|ctb)/ ) {
304                                         push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
305                                         push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
306                                 } elsif ( $type =~ m/aut/ ) {
307                                         if ( ! $have_100 ) {
308                                                 $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
309                                                 if ( $have_edt ) {
310                                                         $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
311                                                 } else {
312                                                         push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
313                                                 }
314                                         } else {
315                                                 push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
316                                         }
317                                 } else {
318 #                                       warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
319                                         $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
320                                 }
321                         }
322                         delete $data->{700};
323         }
324
325         $biblio_data->{ $row->{biblionumber} } = $data;
326
327 }
328
329 debug 'authors' => $authors;
330 debug 'type_stats' => $type_stats;
331 debug 'skip' => $skip;
332 debug 'biblio_year' => $biblio_year;
333 debug 'biblio_data' => $biblio_data;
334 debug 'biblio_author_external' => $biblio_author_external;
335
336 my $category_label;
337 my $sth_categories = $dbh->prepare(q{
338 select authorised_value, lib from authorised_values where category = 'BIBCAT'
339 });
340 $sth_categories->execute();
341 while( my $row = $sth_categories->fetchrow_hashref ) {
342         $category_label->{ $row->{authorised_value} } = $row->{lib};
343
344 }
345 debug 'category_label' => $category_label;
346
347 sub html_title {
348         return qq|<html>
349 <head>
350 <meta charset="UTF-8">
351 <title>|, join(" ", @_), qq|</title>
352 <link href="style.css" type="text/css" rel="stylesheet" />
353 </head>
354 <body>
355 |;
356 }
357
358 sub html_end {
359         return qq|</body>\n</html>\n|;
360 }
361
362 mkdir 'html' unless -d 'html';
363
364 open(my $index, '>:encoding(utf-8)', 'html/index.new');
365 print $index html_title('Bibliografija Filozofskog fakulteta');
366
367 my $first_letter = '';
368
369 debug 'authors' => \@authors;
370
371 sub li_biblio {
372         my ($biblionumber) = @_;
373         return qq|<li>|,
374                 qq|<a href="https://koha.ffzg.hr/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber">$biblionumber</a>|,
375                 biblioitem_html($biblionumber),
376                 qq|<a href="https://koha.ffzg.hr:8443/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber">edit</a>|,
377                 qq|</li>\n|;
378 }
379
380 sub author_html {
381         my ( $fh, $authid, $type, $label ) = @_;
382
383         return unless exists $authors->{$authid}->{$type};
384
385         print $fh qq|<h2>$label</h2>\n|;
386
387         foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
388                 my $label = $category_label->{$category} || 'Bez kategorije';
389                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
390                 foreach my $biblionumber ( @{ $authors->{$authid}->{$type}->{$category} } ) {
391                         print $fh li_biblio( $biblionumber );
392                 }
393                 print $fh qq|</ul>\n|;
394         }
395 }
396
397 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
398
399         my $first = substr( $row->{full_name}, 0, 1 );
400         if ( $first ne $first_letter ) {
401                 print $index qq{</ul>\n} if $first_letter;
402                 $first_letter = $first;
403                 print $index qq{<h1>$first</h1>\n<ul>\n};
404         }
405         print $index qq{<li><a href="}, $row->{authid}, qq{.html">}, $row->{full_name}, "</a></li>\n";
406
407         my $path = "html/$row->{authid}";
408         open(my $fh, '>:encoding(utf-8)', "$path.new");
409         print $fh html_title($row->{full_name}, "bibliografija");
410         print $fh qq|<h1>$row->{full_name} - bibliografija za razdoblje 2008-2013</h1>|;
411
412         author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
413         author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
414
415         print $fh html_end;
416         close($fh);
417         rename "$path.new", "$path.html";
418
419 }
420
421 print $index html_end;
422 close($index);
423 rename 'html/index.new', 'html/index.html';
424
425 debug 'auth_header' => $auth_header;
426
427
428 my $department_category_author;
429 foreach my $department ( sort keys %$auth_department ) {
430         foreach my $authid ( sort @{ $auth_department->{$department} } ) {
431                 my   @categories = keys %{ $authors->{$authid}->{aut} };
432                 push @categories,  keys %{ $authors->{$authid}->{sec} };
433                 foreach my $category ( sort @categories ) {
434                         push @{ $department_category_author->{$department}->{$category} }, $authid;
435                         push @{ $department_category_author->{'AAZ_ukupno'}->{$category} }, $authid if $department_in_sum->{$department};
436                         if ( my $group = $department_in_group->{ $department } ) {
437                                 push @{ $department_category_author->{$group}->{$category} }, $authid;
438                         } else {
439                                 $skip->{'department_not_in_group'}->{ $department }++;
440                         }
441                 }
442         }
443 }
444
445 debug 'department_category_author' => $department_category_author;
446
447 mkdir 'html/departments' unless -d 'html/departments';
448
449 sub unique_biblionumber {
450         my @v = @_;
451         my $u;
452         $u->{$_}++ foreach @v;
453         return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
454 }
455
456 open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
457 print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
458 foreach my $department ( sort keys %$department_category_author ) {
459         my $dep = $department || 'Nema odsjeka';
460         my $dep_file = unac_string('utf-8',$dep);
461         print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
462         open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
463
464         print $fh html_title($department . ' bibliografija');
465         print $fh qq|<h1>$department bibliografija</h1>\n|;
466
467         print $fh qq|<h2>Primarno autorstvo</h2>\n|;
468
469         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
470
471                 my @authids = @{ $department_category_author->{$department}->{$category} };
472                 next unless @authids;
473
474                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
475                 my $unique;
476                 $unique->{$_}++ foreach @biblionumber;
477
478                 next unless @biblionumber;
479
480                 my $label = $category_label->{$category} || 'Bez kategorije';
481                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
482
483                 print $fh li_biblio( $_ ) foreach @biblionumber;
484
485                 print $fh qq|</ul>|;
486         }
487
488
489         print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
490
491         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
492
493                 my @authids = @{ $department_category_author->{$department}->{$category} };
494                 next unless @authids;
495
496                 my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
497
498                 next unless @biblionumber;
499
500                 my $label = $category_label->{$category} || 'Bez kategorije';
501                 print $fh qq|<h3>$label</h3>\n<ul>\n|;
502
503                 print $fh li_biblio( $_ ) foreach @biblionumber;
504
505                 print $fh qq|</ul>|;
506         }
507
508
509         print $fh html_end;
510         close($fh);
511         rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
512 }
513 print $dep_fh qq|</ul>\n|, html_end;
514 close($dep_fh);
515 rename 'html/departments/index.new', 'html/departments/index.html';
516
517 my $azvo_stat_biblio;
518
519 foreach my $department ( sort keys %$department_category_author ) {
520         foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
521                 foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
522                         my $group = $auth_group->{$authid};
523                         if ( ! $group ) {
524                                 push @{ $skip->{no_auth_group} }, $authid;
525                                 next;
526                         }
527                         foreach my $type ( keys %{ $authors->{$authid} } ) {
528                                 next unless exists $authors->{$authid}->{$type}->{$category};
529                                 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} },  @{ $authors->{$authid}->{$type}->{$category} };
530                                 push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} },  @{ $authors->{$authid}->{$type}->{$category} };
531                         }
532                 }
533                 foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
534                         foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
535                                 my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
536                                 $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
537                         }
538                 }
539         }
540 }
541
542 debug 'azvo_stat_biblio' => $azvo_stat_biblio;
543
544 my @report_lines;
545 my @report_labels;
546
547 my $label;
548 my $sub_labels;
549 open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
550 while( <$report> ) {
551         chomp;
552         if ( /^([^\t]+)\t+(.+)/ ) {
553                 $label = $1;
554                 push @report_labels, $label;
555                 my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
556                 push @report_lines, [ $label, @$type ];
557         } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
558                 push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
559                 my $sub_label = $1;
560                 pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
561                 push @report_labels, $label . $sub_label;
562         } else {
563                 die "ERROR: [$_]\n";
564         }
565 }
566
567 debug 'report_lines', \@report_lines;
568 debug 'sub_labels', $sub_labels;
569 debug 'report_labels', \@report_labels;
570
571 my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );
572
573 debug 'departments' => \@departments;
574
575 my $department2col;
576 $department2col->{ $departments[$_] } = $_ foreach ( 0 .. $#departments );
577 my $label2row;
578 $label2row->{ $report_labels[$_] } = $_ foreach ( 0 .. $#report_labels );
579
580 my $table;
581
582 sub table_count {
583         my $label = shift @_;
584         my $department = shift @_;
585         my $group = shift @_;
586         my @biblionumbers = @_;
587         my $unique;
588         $unique->{$_}++ foreach @biblionumbers;
589         my @bibs = keys %$unique;
590         $table->{ffzg}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar @bibs;
591         $table->{external}->{$group}->[ $label2row->{ $label } ]->[ $department2col->{$department} ] = scalar grep { $biblio_author_external->{$_} } @bibs;
592 }
593
594 foreach my $group ( '', keys %$azvo_group_title ) {
595
596 foreach my $department ( @departments ) {
597         foreach my $line ( @report_lines ) {
598                 my $label = $line->[0];
599                 my @biblionumbers;
600                 foreach ( 1 .. $#$line ) {
601                         my ( $category, $type ) = @{ $line->[ $_ ] };
602                         my $b = $azvo_stat_biblio->{ $department }->{$category}->{$type}->{$group};
603                         push @biblionumbers, @$b if $b;
604                 }
605                 if ( $sub_labels->{$label} ) {
606                         my $sub_stats;
607                         foreach my $biblionumber ( @biblionumbers ) {
608                                 my $data = $biblio_data->{$biblionumber} || die "can't find biblionumber $biblionumber";
609                                 foreach my $sub_label ( @{ $sub_labels->{$label} } ) {
610                                         my ( $sub_label, $field, $sf, $regex ) = @$sub_label;
611                                         if ( ! $regex ) {
612                                                 push @{ $sub_stats->{ $sub_label } }, $biblionumber;
613                                                 last;
614                                         }
615                                         if ( $field < 100 ) {
616                                                 if ( $data->{$field} =~ m/$regex/ ) {
617                                                         push @{ $sub_stats->{ $sub_label } }, $biblionumber;
618                                                         last;
619                                                 }
620                                         } else {
621                                                 if ( exists $data->{$field}->[0]->{$sf} && $data->{$field}->[0]->{$sf} =~ m/$regex/ ) {
622                                                         push @{ $sub_stats->{ $sub_label } }, $biblionumber;
623                                                         last;
624                                                 }
625                                         }
626                                 }
627                         }
628                         foreach my $sub_label ( keys %$sub_stats ) {
629                                 my $full_label = $label . $sub_label;
630                                 table_count $full_label, $department, $group, @{ $sub_stats->{$sub_label} };
631                         }
632                 } else {
633                         table_count $label, $department, $group, @biblionumbers;
634                 }
635         }
636 }
637
638 } # group
639
640 debug 'table', $table;
641
642 open(my $fh, '>:encoding(utf-8)', 'html/azvo.new');
643 open(my $fh2, '>:encoding(utf-8)', 'html/azvo2.new');
644
645 sub print_fh {
646         print $fh @_;
647         print $fh2 @_;
648 }
649
650 print $fh html_title('AZVO tablica - FFZG');
651 print $fh2 html_title('AZVO tablica - kolaboracija sa FFZG');
652
653 foreach my $group ( keys %{ $table->{ffzg} } ) {
654
655                 print_fh "<h1>$group</h1>" if $group;
656
657                 print_fh "<table border=1>\n";
658                 print_fh "<tr><th></th>";
659                 print_fh "<th>$_</th>" foreach @departments;
660                 print_fh "</tr>\n";
661
662                 foreach my $row ( 0 .. $#{ $table->{ffzg}->{$group} } ) {
663                         print_fh "<tr><th>", $report_labels[$row], "</th>\n";
664                         foreach ( 0 .. $#departments ) {
665                                 print_fh "<td>";
666                                 print $fh $table->{ffzg}->{$group}->[ $row ]->[ $_ ] || '';
667                                 print $fh2 $table->{external}->{$group}->[ $row ]->[ $_ ] || '';
668                                 print_fh "</td>\n"
669                         }
670                         print_fh "</tr>\n";
671                 }
672
673                 print_fh "</table>\n";
674
675 } # group
676
677 print_fh html_end;
678 close($fh);
679 close($fh2);
680 rename 'html/azvo.new', 'html/azvo.html';
681 rename 'html/azvo2.new', 'html/azvo2.html';
682