From 565139eeee4856ac9d103452942c55c11b5e2918 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 3 Mar 2021 10:29:03 +0100 Subject: [PATCH] Bug 27767: Update the number of elements in cart if biblio is removed If a bibliographic record is added to the cart then removed (from the detail page or using the batch delete tool), the number of elements in the cart is never updated. This patch reworks how the cookie "intranet_bib_list" is handled in basket.js. The current code is really bad and error prone (parsing a string). Here we are handling the biblionumbers using a JSON serialised array, that makes everything much more simple. --- basket/basket.pl | 13 +- koha-tmpl/intranet-tmpl/prog/js/basket.js | 152 ++++++++-------------- 2 files changed, 62 insertions(+), 103 deletions(-) diff --git a/basket/basket.pl b/basket/basket.pl index aa4cd97dcf..947b0b0ad4 100755 --- a/basket/basket.pl +++ b/basket/basket.pl @@ -18,6 +18,7 @@ use Modern::Perl; use CGI qw ( -utf8 ); +use JSON qw( to_json ); use C4::Koha; use C4::Biblio; use C4::Items; @@ -55,7 +56,6 @@ if (C4::Context->preference('TagsEnabled')) { } } - foreach my $biblionumber ( @bibs ) { $template->param( biblionumber => $biblionumber ); @@ -116,7 +116,14 @@ foreach my $biblionumber ( @bibs ) { my $resultsarray = \@results; -# my $itemsarray=\@items; +# Rebuild the bib_list (in case some records have been deleted) +my @biblionumber = map { int($_->{biblionumber}) } @results; +my $cookie_bib_list = $query->cookie( + -name => 'intranet_bib_list', + -value => to_json( \@biblionumber ), + -HttpOnly => 0, + -secure => ( C4::Context->https_enabled() ? 1 : 0 ), +); $template->param( BIBLIO_RESULTS => $resultsarray, @@ -124,4 +131,4 @@ $template->param( bib_list => $bib_list, ); -output_html_with_http_headers $query, $cookie, $template->output; +output_html_with_http_headers $query, [$cookie, $cookie_bib_list], $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/js/basket.js b/koha-tmpl/intranet-tmpl/prog/js/basket.js index fa0ffc7058..4e7ab4927e 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/basket.js +++ b/koha-tmpl/intranet-tmpl/prog/js/basket.js @@ -9,56 +9,25 @@ var CGIBIN = "/cgi-bin/koha/"; var nameCookie = "intranet_bib_list"; var nameParam = "bib_list"; -var valCookie = readCookie(nameCookie); -var basketcount; +var bib_list = readCookie(nameCookie); +var basketcount = bib_list && bib_list.length > 0 ? bib_list.length : ""; -if(valCookie){ - var arrayRecords = valCookie.split("/"); - if(arrayRecords.length > 0){ - basketcount = arrayRecords.length-1; - } else { - basketcount = ""; - } -} else { - basketcount = ""; -} - -function writeCookie(name, val, wd) { - if (wd) { - parent.opener.document.cookie = name + "=" + val + "; path=/"; - } - else { - parent.document.cookie = name + "=" + val + "; path=/"; - } +function writeCookie(name, val) { + var str = JSON.stringify(val); + Cookies.set(name, str, { path: '/' }); } -function readCookie(name, wd) { - var str_name = name + "="; - var str_cookie = ""; - if (wd) { - str_cookie = parent.opener.document.cookie; - } - else { - str_cookie = parent.document.cookie; - } - // fixed - getting the part of the basket that is bib_list - var cookie_parts = str_cookie.split(";"); - for(var i=0;i < cookie_parts.length;i++) { - var c = cookie_parts[i]; - while (c.charAt(0)==' ') c = c.substring(1,c.length); - if(c.indexOf(str_name) === 0) return c.substring(str_name.length,c.length); +function readCookie(name) { + var r = Cookies.get(name); + if ( r ) { + return JSON.parse(r); } - return null; + r = new Array(); + return r; } -function delCookie(name) { - var exp = new Date(); - exp.setTime(exp.getTime()-1); - if(parent.opener){ - parent.opener.document.cookie = name + "=null; path=/; expires=" + exp.toGMTString(); - } else { - document.cookie = name + "=null; path=/; expires=" + exp.toGMTString(); - } +function removeCookie(name) { + Cookies.remove(name); } /////////////////////////////////////////////////////////////////// @@ -67,9 +36,9 @@ function delCookie(name) { function openBasket() { var strCookie = ""; - var valCookie = readCookie(nameCookie); - if ( valCookie ) { - strCookie = nameParam + "=" + valCookie; + var arrayRecords = readCookie(nameCookie); + if ( arrayRecords.length ) { + strCookie = nameParam + "=" + arrayRecords.join("/"); } if ( strCookie ) { @@ -85,42 +54,28 @@ function openBasket() { } } +function unique(array) { + return $.grep(array, function(el, index) { + return index === $.inArray(el, array); + }); +} + function addRecord(val, selection,NoMsgAlert) { - var valCookie = readCookie(nameCookie); - var write = 0; - if ( ! valCookie ) { // empty basket - valCookie = val + '/'; - write = 1; - updateBasket(1); - } - else { - // is this record already in the basket ? - var found = false; - var arrayRecords = valCookie.split("/"); - for (var i = 0; i < valCookie.length - 1; i++) { - if (val == arrayRecords[i]) { - found = true; - break; - } - } - if ( found ) { - if (selection) { - return 0; - } - if (! NoMsgAlert ) { - showCartUpdate( __("This item is already in your cart") ); - } + var bib_list = readCookie(nameCookie); + + if ($.inArray(val, bib_list) != -1) { + if (selection) { + return 0; } - else { - valCookie += val + '/'; - write = 1; - updateBasket(arrayRecords.length); + if (! NoMsgAlert ) { + showCartUpdate( __("This item is already in your cart") ); } - } + } else { + bib_list.push(val); - if (write) { - writeCookie(nameCookie, valCookie); + writeCookie(nameCookie, bib_list); + updateBasket(bib_list.length); if (selection) { // when adding a selection of records updateLink(val,"add"); return 1; @@ -154,37 +109,37 @@ function SelectAll(){ function addMultiple(biblist){ var c_value = ""; var i = 0; + var new_bibs = []; if( biblist && biblist.length > 0 ) { for ( i=0; i < biblist.length; i++ ) { if (biblist[i].checked) { - c_value = c_value + biblist[i].value + "/"; + new_bibs.push(biblist[i].value); } } } else { var bibnums = getContextBiblioNumbers(); if ( bibnums.length > 0 ) { for ( i = 0 ; i < bibnums.length ; i++ ) { - c_value = c_value + bibnums[i] + "/"; + new_bibs.push(bibnums[i]); } } else { if(document.bookbag_form.biblionumber.length > 0) { for ( i=0; i < document.bookbag_form.biblionumber.length; i++ ) { if (document.bookbag_form.biblionumber[i].checked) { - c_value = c_value + document.bookbag_form.biblionumber[i].value + "/"; + new_bibs.push(document.bookbag_form.biblionumber[i].value); } } } else { - c_value = c_value + document.bookbag_form.biblionumber.value + "/"; + new_bibs.push(document.bookbag_form.biblionumber.value); } } } - addSelRecords(c_value); + addSelRecords(new_bibs); } /* function for adding a selection of biblios to the basket from the results list */ -function addSelRecords(valSel) { - var arrayRecords = valSel.split("/"); +function addSelRecords(arrayRecords) { var i = 0; var nbAdd = 0; for (i=0;i 0){ recordsSel = 1; - var str2 = valCookie; + var str2 = arrayRecords.join('/'); while (!end){ s = str.indexOf("/"); if (s>0){ @@ -279,7 +232,7 @@ function delSelRecords() { var rep = false; rep = confirm(__("Are you sure you want to empty your cart?")); if (rep) { - delCookie(nameCookie); + removeCookie(nameCookie); document.location = "about:blank"; updateBasket(0,top.opener); window.close(); @@ -287,16 +240,15 @@ function delSelRecords() { return; } } else { - writeCookie(nameCookie, str2, 1); + var new_arrayRecords = str2.split('/'); + writeCookie(nameCookie, new_arrayRecords); } } } if (recordsSel) { - var strCookie = ""; - valCookie = readCookie(nameCookie, 1); - strCookie = nameParam + "=" + valCookie; - var arrayRecords = valCookie.split("/"); + var arrayRecords = readCookie(nameCookie); + var strCookie = nameParam + "=" + arrayRecords.join("/"); updateBasket(arrayRecords.length-1,top.opener); document.location = CGIBIN + "basket/basket.pl?" + strCookie; } @@ -333,13 +285,13 @@ function delBasket(context,rep) { } if (rep) { if(context == "popup"){ - delCookie(nameCookie); + removeCookie(nameCookie); updateAllLinks(top.opener); document.location = "about:blank"; updateBasket(0,top.opener); window.close(); } else { - delCookie(nameCookie); + removeCookie(nameCookie); updateBasket(0,top.opener); } } -- 2.20.1