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