first try at table of contents
[koha-bibliografija] / html.pl
diff --git a/html.pl b/html.pl
index ee49703..ac4aeb3 100755 (executable)
--- a/html.pl
+++ b/html.pl
@@ -14,6 +14,7 @@ use Carp qw(confess);
 use utf8;
 use JSON;
 use POSIX qw(strftime);
+use Storable;
 
 use lib '/srv/koha_ffzg';
 use C4::Context;
@@ -103,6 +104,7 @@ foreach my $department ( keys %$department_in_sum ) {
 }
 
 debug 'auth_department' => $auth_department;
+store $auth_department, '/dev/shm/auth_department.storable';
 debug 'auth_group' => $auth_group;
 debug 'department_in_sum' => $department_in_sum;
 
@@ -130,6 +132,7 @@ where
 =cut
 
 my $biblio_year;
+my $biblio_full_name;
 my $type_stats;
 
 my $parser = XML::LibXML->new();
@@ -200,6 +203,7 @@ while( my $row = $sth_select_authors->fetchrow_hashref ) {
        my $extract = {
                '008' => undef,
                '100' => '(9|a)',
+               '245' => 'a',
                '680' => 'i',
                '700' => '(9|4|a)',
                '942' => '(t|r|v)'
@@ -279,10 +283,13 @@ while( my $row = $sth_select_authors->fetchrow_hashref ) {
                        foreach my $authid ( @first_author ) {
                                push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
                        }
+                       $biblio_full_name->{ $row->{biblionumber} } = $data->{100}->[0]->{a};
        } else {
                $have_100 = 0;
        }
 
+       $biblio_full_name->{ $row->{biblionumber} } ||= $data->{245}->[0]->{a};
+
        my $have_edt;
 
        if ( exists $data->{700} ) {
@@ -304,23 +311,32 @@ while( my $row = $sth_select_authors->fetchrow_hashref ) {
 
                                $type_stats->{$type}++;
 
-                               if ( $type =~ m/(edt|trl|com|ctb)/ ) {
-                                       push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
-                                       push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
-                               } elsif ( $type =~ m/aut/ ) {
-                                       if ( ! $have_100 ) {
-                                               $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
-                                               if ( $have_edt ) {
-                                                       $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
+                               my @types = split(/\s+/, $type);
+
+                               foreach my $type ( @types ) {
+                                       $type_stats->{_count_each_type}->{$type}++;
+
+                                       if ( $type =~ m/(edt|trl|com|ctb)/ ) {
+                                               push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
+                                               push @{ $authors->{$authid}->{$type}->{ $category } }, $row->{biblionumber};
+                                               $type =~ s/(com|ctb)/_ostalo/;
+                                               push @{ $authors->{$authid}->{$type}->{ $category } }, $row->{biblionumber};
+
+                                       } elsif ( $type =~ m/aut/ ) {
+                                               if ( ! $have_100 ) {
+                                                       $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
+                                                       if ( $have_edt ) {
+                                                               $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
+                                                       } else {
+                                                               push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
+                                                       }
                                                } else {
                                                        push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
                                                }
                                        } else {
-                                               push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
+#                                              warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
+                                               $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
                                        }
-                               } else {
-#                                      warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
-                                       $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
                                }
                        }
                        delete $data->{700};
@@ -331,9 +347,11 @@ while( my $row = $sth_select_authors->fetchrow_hashref ) {
 }
 
 debug 'authors' => $authors;
+store $authors, '/dev/shm/authors.storable';
 debug 'type_stats' => $type_stats;
 debug 'skip' => $skip;
 debug 'biblio_year' => $biblio_year;
+debug 'biblio_full_name' => $biblio_full_name;
 debug 'biblio_data' => $biblio_data;
 debug 'biblio_author_external' => $biblio_author_external;
 
@@ -385,25 +403,35 @@ sub li_biblio {
                qq|</li>\n|;
 }
 
+sub unique_biblionumber {
+       my @v = @_;
+       my $u;
+       $u->{$_}++ foreach @v;
+       return sort {
+               $biblio_year->{$b} <=> $biblio_year->{$a} ||
+               $biblio_full_name->{$a} cmp $biblio_full_name->{$b} ||
+               $a <=> $b
+       } keys %$u;
+}
+
 sub author_html {
        my ( $fh, $authid, $type, $label ) = @_;
 
        return unless exists $authors->{$authid}->{$type};
 
-       print $fh qq|<h2>$label</h2>\n|;
+       print $fh qq|<a name="$type"><h2>$label</h2></a>\n|;
 
        foreach my $category ( sort keys %{ $authors->{$authid}->{$type} } ) {
                my $label = $category_label->{$category} || 'Bez kategorije';
-               print $fh qq|<a name="$type-$category"><h3>$label</h3></a>\n<ul>\n|;
-               foreach my $biblionumber ( sort {
-                               $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b
-                       } @{ $authors->{$authid}->{$type}->{$category} } ) {
+               print $fh qq|<a name="$type-$category"><h3>$label</h3></a>\n<ol>\n|;
+               foreach my $biblionumber ( unique_biblionumber @{ $authors->{$authid}->{$type}->{$category} } ) {
                        print $fh li_biblio( $biblionumber );
                }
-               print $fh qq|</ul>\n|;
+               print $fh qq|</ol>\n|;
        }
 }
 
+
 sub count_author_years {
        my $years = shift;
        my ($authid) = @_;
@@ -417,6 +445,13 @@ sub count_author_years {
        return $years;
 }
 
+my @toc_type_label = (
+'aut' => 'Primarno autorstvo',
+'edt' => 'Uredništva',
+'trl' => 'Prijevodi',
+'_ostalo' => 'Ostalo',
+);
+
 sub html_year_selection {
        my $fh = shift;
        my @authids = @_;
@@ -433,6 +468,9 @@ sub html_year_selection {
                print $fh qq|<label><input name="year_selection" value="$year" type=checkbox onClick="toggle_year($year, this)" checked="checked">$year</label>&nbsp;\n|;
                foreach my $type_cat ( keys %{ $years->{$year} } ) {
                        $type_cat_count->{ $type_cat } += $years->{$year}->{$type_cat};
+                       my ($type,$cat) = split(/-/, $type_cat);
+                       $type_cat_count->{_toc}->{$type}->{$cat}++;
+                       $type_cat_count->{_toc_count}->{$type} += $years->{$year}->{$type_cat};
                }
        }
 
@@ -458,6 +496,7 @@ function year_show(year) {
                        $('a[name="'+type_cat+'"]').show();
                        console.debug(type_cat, 'show');
                }
+               $('#toc-count-'+type_cat.replace('.','-')).text( type_cat_count[ type_cat ] );
        }
 }
 
@@ -469,6 +508,7 @@ function year_hide(year) {
                        $('a[name="'+type_cat+'"]').hide();
                        console.debug(type_cat, 'hide');
                }
+               $('#toc-count-'+type_cat.replace('.','-')).text( type_cat_count[ type_cat ] );
        }
 }
 
@@ -507,9 +547,31 @@ $(document).ready( function() {
 
 });
 </script>
+
        |;
+
+       # TOC
+       print $fh qq|<ul id="toc">\n|;
+       my $i = 0;
+       while ( $i < $#toc_type_label ) {
+               my $type  = $toc_type_label[$i++] || die "type";
+               my $label = $toc_type_label[$i++] || die "label";
+               next unless exists $type_cat_count->{_toc}->{$type};
+               print $fh qq| <li id="toc-$type"><a href="#$type">$label</a> <tt id="toc-count-$type">$type_cat_count->{_toc_count}->{$type}</tt></li>\n <ul>\n|;
+               foreach my $category ( sort keys %{ $type_cat_count->{_toc}->{$type} } ) {
+                       my $label = $category_label->{$category} || 'Bez kategorije';
+                       my $count = $type_cat_count->{ $type . '-' . $category };
+                       my $cat_html = $category;
+                       $cat_html =~ s/\./-/g;
+                       print $fh qq|  <li id="toc-$category"><a href="#$type-$category">$label</a> <tt id="toc-count-$type-$cat_html">$count</tt></li>\n|;
+               }
+               print $fh qq| </ul>\n|;
+       }
+       print $fh qq|</ul>\n|;
+
 }
 
+
 foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
 
        my $first = substr( $row->{full_name}, 0, 1 );
@@ -527,8 +589,12 @@ foreach my $row ( sort { $a->{full_name} cmp $b->{full_name} } @authors ) {
 
        html_year_selection $fh => $row->{authid};
 
-       author_html( $fh, $row->{authid}, 'aut' => 'Primarno autorstvo' );
-       author_html( $fh, $row->{authid}, 'sec' => 'Uredništva, prijevodi, krička izdanja' );
+       my $i = 0;
+       while ( $i < $#toc_type_label ) {
+               my $type  = $toc_type_label[$i++] || die "type";
+               my $label = $toc_type_label[$i++] || die "label";
+               author_html( $fh, $row->{authid}, $type => $label );
+       }
 
        print $fh html_end;
        close($fh);
@@ -562,67 +628,57 @@ foreach my $department ( sort keys %$auth_department ) {
 
 debug 'department_category_author' => $department_category_author;
 
-mkdir 'html/departments' unless -d 'html/departments';
-
-sub unique_biblionumber {
-       my @v = @_;
-       my $u;
-       $u->{$_}++ foreach @v;
-       return sort { $biblio_year->{$b} <=> $biblio_year->{$a} || $a <=> $b } keys %$u;
-}
-
-open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
-print $dep_fh html_title('Odsijeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
-foreach my $department ( sort keys %$department_category_author ) {
-       my $dep = $department || 'Nema odsjeka';
-       my $dep_file = unac_string('utf-8',$dep);
-       print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
-       open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
 
-       print $fh html_title($department . ' bibliografija');
-       print $fh qq|<h1>$department bibliografija</h1>\n|;
+sub department_html {
+       my ( $fh, $department, $type, $label ) = @_;
 
-       print $fh qq|<h2>Primarno autorstvo</h2>\n|;
+       print $fh qq|<a name="$type"><h2>$label</h2></a>\n|;
 
        foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
 
                my @authids = @{ $department_category_author->{$department}->{$category} };
                next unless @authids;
 
-               my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{aut}->{$category} } } grep { exists $authors->{$_}->{aut}->{$category} } @authids;
-               my $unique;
-               $unique->{$_}++ foreach @biblionumber;
+               my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{$type}->{$category} } } grep { exists $authors->{$_}->{$type}->{$category} } @authids;
 
                next unless @biblionumber;
 
                my $label = $category_label->{$category} || 'Bez kategorije';
-               print $fh qq|<h3>$label</h3>\n<ul>\n|;
+               print $fh qq|<a name="$type-$category"><h3>$label</h3></a>\n<ol>\n|;
 
                print $fh li_biblio( $_ ) foreach @biblionumber;
 
-               print $fh qq|</ul>|;
+               print $fh qq|</ol>|;
        }
 
+}
 
-       print $fh qq|<h2>Sekundarno autorstvo</h2>\n|;
-
-       foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
-
-               my @authids = @{ $department_category_author->{$department}->{$category} };
-               next unless @authids;
-
-               my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{sec}->{$category} } } grep { exists $authors->{$_}->{sec}->{$category} } @authids;
 
-               next unless @biblionumber;
+mkdir 'html/departments' unless -d 'html/departments';
 
-               my $label = $category_label->{$category} || 'Bez kategorije';
-               print $fh qq|<h3>$label</h3>\n<ul>\n|;
+open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
+print $dep_fh html_title('Odsjeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
+foreach my $department ( sort keys %$department_category_author ) {
+       my $dep = $department || 'Nema odsjeka';
+       my $dep_file = unac_string('utf-8',$dep);
+       print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
+       open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
 
-               print $fh li_biblio( $_ ) foreach @biblionumber;
+       print $fh html_title($department . ' bibliografija');
+       print $fh qq|<h1>$department bibliografija</h1>\n|;
 
-               print $fh qq|</ul>|;
+       my @authids;
+       foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
+               push @authids, @{ $department_category_author->{$department}->{$category} };
        }
+       html_year_selection $fh => @authids;
+
+       department_html( $fh, $department, 'aut', 'Primarno autorstvo' );
 
+#      department_html( $fh, $department, 'sec', 'Sekundarno autorstvo' );
+       department_html( $fh, $department, 'edt', 'Uredništva' );
+       department_html( $fh, $department, 'trl', 'Prijevodi' );
+       department_html( $fh, $department, '_ostalo', 'Ostalo' );
 
        print $fh html_end;
        close($fh);