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