Merge remote-tracking branch 'origin/new/bug_7453'
[koha_fer] / opac / opac-tags.pl
index 76c3abc..b5ce340 100755 (executable)
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 
-=head1 
+=head1 NAME
+
+opac-tags.pl
+
+=head1 DESCRIPTION
 
 TODO :: Description here
 
@@ -50,7 +54,7 @@ my $perBibResults = {};
 my @globalErrorIndexes = ();
 
 sub ajax_auth_cgi ($) {     # returns CGI object
-    my $needed_flags = shift;
+       my $needed_flags = shift;
        my %cookies = fetch CGI::Cookie;
        my $input = CGI->new;
        my $sessid = $cookies{'CGISESSID'}->value || $input->param('CGISESSID');
@@ -58,9 +62,9 @@ sub ajax_auth_cgi ($) {     # returns CGI object
        $debug and
        print STDERR "($auth_status, $auth_sessid) = check_cookie_auth($sessid," . Dumper($needed_flags) . ")\n";
        if ($auth_status ne "ok") {
-               output_ajax_with_http_headers $input,
+               output_with_http_headers $input, undef,
                "window.alert('Your CGI session cookie ($sessid) is not current.  " .
-               "Please refresh the page and try again.');\n";
+               "Please refresh the page and try again.');\n", 'js';
                exit 0;
        }
        $debug and print STDERR "AJAX request: " . Dumper($input),
@@ -212,14 +216,14 @@ if ($is_ajax) {
         $js_perbib .= $js_bibres;
     }
 
-       output_ajax_with_http_headers($query, "$js_reply\n$err_string\n$js_perbib\n};");
+       output_with_http_headers($query, undef, "$js_reply\n$err_string\n$js_perbib\n};", 'js');
        exit;
 }
 
 my $results = [];
 my $my_tags = [];
 
-if ($loggedinuser and not $query->param('hidemytags')) {
+if ($loggedinuser) {
        $my_tags = get_tag_rows({borrowernumber=>$loggedinuser});
        foreach (@$my_tags) {
                my $biblio = GetBiblioData($_->{biblionumber});
@@ -244,12 +248,14 @@ if ($add_op) {
                deleted_count => $dels,
        );
 } else {
-       my ($arg,$limit);
+       my ($arg,$limit,$mine);
        my $hardmax = 100;      # you might disagree what this value should be, but there definitely should be a max
        $limit = $query->param('limit') || $hardmax;
+    $mine =  $query->param('mine') || 0; # set if the patron want to see only his own tags.
        ($limit =~ /^\d+$/ and $limit <= $hardmax) or $limit = $hardmax;
        $template->param(limit => $limit);
        my $arghash = {approved=>1, limit=>$limit, 'sort'=>'-weight_total'};
+    $arghash->{'borrowernumber'} = $loggedinuser if $mine;
        # ($openadds) or $arghash->{approved} = 1;
        if ($arg = $query->param('tag')) {
                $arghash->{term} = $arg;
@@ -259,7 +265,7 @@ if ($add_op) {
        $results = get_approval_rows($arghash);
 
        my $count = scalar @$results;
-       $template->param(TAGLOOP_COUNT => $count);
+       $template->param(TAGLOOP_COUNT => $count, mine => $mine);
        # 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
@@ -286,9 +292,9 @@ if ($add_op) {
                $previous = $current;
        }
 }
-$query->param('hidemytags') and $template->param(hidemytags => 1);
 (scalar @errors  ) and $template->param(ERRORS  => \@errors);
-(scalar @$results) and $template->param(TAGLOOP => $results);
+my @orderedresult = sort { uc($a->{'term'}) cmp uc($b->{'term'}) } @$results;
+(scalar @$results) and $template->param(TAGLOOP => \@orderedresult );
 (scalar @$my_tags) and $template->param(MY_TAGS => $my_tags);
 
 output_html_with_http_headers $query, $cookie, $template->output;