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