Revert "Portuguese staff updates"
[koha.git] / opac / opac-tags.pl
index d085003..2b9dfd7 100755 (executable)
@@ -38,7 +38,7 @@ use C4::Output 3.02 qw(:html :ajax pagination_bar);
 use C4::Dates qw(format_date);
 use C4::Scrubber;
 use C4::Biblio;
-use C4::Tags qw(add_tag get_tags get_tag_rows remove_tag);
+use C4::Tags qw(add_tag get_approval_rows get_tag_rows remove_tag);
 
 my %newtags = ();
 my @deltags = ();
@@ -132,13 +132,13 @@ if (scalar @newtags_keys) {
                                }
                        }
                        my $result = ($openadds) ?
-                               add_tag($biblionumber,$clean_tag,$loggedinuser,0) : # pre-approved
+                               add_tag($biblionumber,$clean_tag,$loggedinuser,$loggedinuser) : # pre-approved
                                add_tag($biblionumber,$clean_tag,$loggedinuser)   ;
                        if ($result) {
                                $counts{$biblionumber}++;
                        } else {
                                push @errors, {failed_add_tag=>$clean_tag};
-                               warn "add_tag($biblionumber,$clean_tag,$loggedinuser...) returned bad result ($result)";
+                               $debug and warn "add_tag($biblionumber,$clean_tag,$loggedinuser...) returned bad result (" . (defined $result ? $result : 'UNDEF') .")";
                        }
                }
        }
@@ -155,15 +155,18 @@ foreach (@deltags) {
 if ($is_ajax) {
        my $sum = 0;
        foreach (values %counts) {$sum += $_;}
-       my $js_reply = sprintf("response = {\n\tadded: %d,\n\tdeleted: %d,\n\terrors: %d,",$sum,$dels,scalar @errors);
+       my $js_reply = sprintf("response = {\n\tadded: %d,\n\tdeleted: %d,\n\terrors: %d",$sum,$dels,scalar @errors);
        my $err_string = '';
        if (scalar @errors) {
-               $err_string = "\n\talerts: [";  # open response_function
+               $err_string = ",\n\talerts: ["; # open response_function
+               my $i = 1;
                foreach (@errors) {
                        my $key = (keys %$_)[0];
-                       $err_string .= "\n\t\t KOHA.Tags.tag_message.$key(\"" . $_->{$key} . '"),';
+                       $err_string .= "\n\t\t KOHA.Tags.tag_message.$key(\"" . $_->{$key} . '")';
+                       if($i < scalar @errors){ $err_string .= ","; }
+                       $i++;
                }
-               $err_string .= "\n\t],\n";      # close response_function
+               $err_string .= "\n\t]\n";       # close response_function
        }
        output_ajax_with_http_headers($query, "$js_reply\n$err_string};");
        exit;
@@ -172,7 +175,7 @@ if ($is_ajax) {
 my $results = [];
 my $my_tags = [];
 
-if ($loggedinuser) {
+if ($loggedinuser and not $query->param('hidemytags')) {
        $my_tags = get_tag_rows({borrowernumber=>$loggedinuser});
        foreach (@$my_tags) {
                my $biblio = GetBiblioData($_->{biblionumber});
@@ -184,7 +187,10 @@ if ($loggedinuser) {
                $_->{date_created_display} = format_date($_->{date_created});
        }
 }
-$template->param(tagsview => 1,);
+
+$template->param(tagsview => 1,
+dateformat => C4::Context->preference("dateformat"));
+
 if ($add_op) {
        my $adds = 0;
        for (values %counts) {$adds += $_;}
@@ -194,23 +200,49 @@ if ($add_op) {
                deleted_count => $dels,
        );
 } else {
-       my ($arg,$limit,$tmpresults);
+       my ($arg,$limit);
        my $hardmax = 100;      # you might disagree what this value should be, but there definitely should be a max
        $limit = $query->param('limit') || $hardmax;
        ($limit =~ /^\d+$/ and $limit <= $hardmax) or $limit = $hardmax;
+       $template->param(limit => $limit);
+       my $arghash = {approved=>1, limit=>$limit, 'sort'=>'-weight_total'};
+       # ($openadds) or $arghash->{approved} = 1;
        if ($arg = $query->param('tag')) {
-               $tmpresults = get_tags({term => $arg, limit=>$limit, 'sort'=>'-weight'});
+               $arghash->{term} = $arg;
        } elsif ($arg = $query->param('biblionumber')) {
-               $tmpresults = get_tags({biblionumber => $arg, limit=>$limit, 'sort'=>'-weight'});
-       } else {
-               $tmpresults = get_tags({limit=>$limit, 'sort'=>'-weight'});
+               $arghash->{biblionumber} = $arg;
        }
-       my %uniq;
-       foreach (@$tmpresults) {
-               $uniq{$_->{term}}++ and next;
-               push @$results, $_;
+       $results = get_approval_rows($arghash);
+
+       my $count = scalar @$results;
+       $template->param(TAGLOOP_COUNT => $count);
+       # Here we make a halfhearted attempt to separate the tags into "strata" based on weight_total
+       # FIXME: code4lib probably has a better algorithm, iirc
+       # FIXME: when we get a better algorithm, move to C4
+       my $maxstrata = 5;
+       my $strata = 1;
+       my $previous = 0;
+       my $chunk = ($count/$maxstrata)/2;
+       my $total = 0;
+       my %cloud;
+       foreach (reverse @$results) {
+               my $current = $_->{weight_total};
+               $total++;
+               $cloud{$strata}++;
+               if ($current == $previous) {
+                       $_->{cloudweight} = $strata;
+                       next;
+               } 
+               if ($strata < $maxstrata and 
+                       ($cloud{$strata} > $chunk or 
+                       $count-$total <= $maxstrata-$strata)) {
+                       $strata++;
+               }
+               $_->{cloudweight} = $strata;
+               $previous = $current;
        }
 }
+$query->param('hidemytags') and $template->param(hidemytags => 1);
 (scalar @errors  ) and $template->param(ERRORS  => \@errors);
 (scalar @$results) and $template->param(TAGLOOP => $results);
 (scalar @$my_tags) and $template->param(MY_TAGS => $my_tags);