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