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