MT 1110 : Balance cart and lists : cart is now in the intranet
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / js / tags.js
1 if (typeof KOHA == "undefined" || !KOHA) {
2     var KOHA = {};
3 }
4
5 /**
6 * A namespace for Tags related functions.
7 * readCookie is expected to already be declared.  That's why the assignment below is unscoped.
8 * readCookie should be from basket.js or undefined.
9
10 $.ajaxSetup({
11         url: "/cgi-bin/koha/tags/tags.pl",
12         type: "POST",
13         dataType: "script"
14 });
15 */
16 if (typeof(readCookie) == "undefined") {
17         readCookie = function (name) { // from http://www.quirksmode.org/js/cookies.html
18                 var nameEQ = name + "=";
19                 var ca = document.cookie.split(';');
20                 for (var i=0;i < ca.length;i++) {
21                         var c = ca[i];
22                         while (c.charAt(0)==' '){ c = c.substring(1,c.length); }
23                         if (c.indexOf(nameEQ) == 0){ return c.substring(nameEQ.length,c.length); }
24                 }
25                 return null;
26         }
27 }
28 KOHA.Tags = {
29         add_tag_button: function(){
30                 var mybibnum = $(this).attr("title");
31                 var mynewtag = "newtag" + mybibnum;
32                 var mytagid = "#" + mynewtag;
33                 var mydata = {CGISESSID: readCookie('CGISESSID')};      // Someday this should be OPACSESSID
34                 mydata[mynewtag] = $(mytagid).val();    // need [bracket] for variable property id
35                 var response;   // AJAX from server will assign value to response.
36                 $.post(
37                         "/cgi-bin/koha/tags/tags.pl",
38                         mydata,
39                         function(data){
40                                 // alert("AJAX Response: " + data);
41                                 eval(data);
42                                 // alert("counts: " + response["added"] + response["deleted"] + response["errors"]);
43                                 KOHA.Tags.set_tag_status(
44                                         mytagid + "_status",
45                                         KOHA.Tags.common_status(response["added"], response["deleted"], response["errors"])
46                                 );
47                                 if (response.alerts) {
48                                         alert(response.alerts.join("\n\n"));
49                                 }
50                         },
51                         'script'
52                 );
53                 return false;
54         },
55         common_status : function(addcount, delcount, errcount) {
56             var cstat = "";
57             if (addcount && addcount > 0) {cstat += _("Added ")   + addcount + (addcount==1 ? _(" tag") : _(" tags")) + ".  " ;}
58             if (delcount && delcount > 0) {cstat += _("Deleted ") + delcount + (delcount==1 ? _(" tag") : _(" tags")) + ".  " ;}
59             if (errcount && errcount > 0) {cstat += (errcount==1 ? _("ERROR") : errcount + _(" ERRORS")) + _(" during operation.");}
60             return cstat;
61         },
62         set_tag_status : function(tagid, newstatus) {
63                 $(tagid).html(newstatus);
64                 $(tagid).css({display:"inline"});
65         },
66         append_tag_status : function(tagid, newstatus) {
67                 $(tagid).append(newstatus);
68                 $(tagid).css({display:"inline"});
69         },
70
71         tag_message: {
72         tagsdisabled : function(arg) {return (_("Sorry, tags are not enabled on this system."));},
73         scrubbed_all_bad : function(arg) {return (_("Error! Your tag was entirely markup code.  It was NOT added.  Please try again with plain text."));},
74         badparam : function(arg) {return (_("Error! Illegal parameter '" +arg+ "'."));},
75         scrubbed : function(arg) {return (_("Note: your tag contained markup code that was removed. The tag was added as '" +arg+ "'."));},
76     failed_add_tag : function(arg) {return (_("Error! The add_tag operation failed on '" +arg+ "'.  Note: you can only tag an item with a given term once.  Check 'My Tags' to see your current tags."));},
77     failed_delete  : function(arg) {return (_("Error! You cannot delete the tag '" +arg+ "'.  Note: you can only delete your own tags."));},
78         login : function(arg) {return (_("You must be logged in to add tags."));}
79         },
80
81     // Used to tag multiple items at once.  The main difference
82     // is that status is displayed on a per item basis.
83     add_multitags_button : function(bibarray, tag){
84                 var mydata = {CGISESSID: readCookie('CGISESSID')};      // Someday this should be OPACSESSID
85         for (var i = 0; i < bibarray.length; i++) {
86             var mynewtag = "newtag" + bibarray[i];
87             mydata[mynewtag] = tag;
88         }
89                 var response;   // AJAX from server will assign value to response.
90                 $.post(
91                         "/cgi-bin/koha/tags/tags.pl",
92                         mydata,
93                         function(data){
94                                 eval(data);
95                 $(".tagstatus").empty();
96                 var bibErrors = false;
97
98                 // Display the status for each tagged bib
99                 for (var i = 0; i < bibarray.length; i++) {
100                     var bib = bibarray[i];
101                     var mytagid = "#newtag" + bib;
102                     var status = "";
103
104                     // Number of tags added.
105                     if (response[bib]) {
106                         var added = response[bib]["added"];
107                         if (added > 0) {
108                             status = "Added " + added + (added == 1 ? " tag" : " tags") + ".  ";
109                                         KOHA.Tags.set_tag_status(mytagid + "_status", status);
110                         }
111
112                         // Show a link that opens an error dialog, if necessary.
113                         var errors = response[bib]["errors"];
114                         if (errors.length > 0) {
115                             bibErrors = true;    
116                             var errid = "tagerr_" + bib;
117                             var errstat = "<a id=\"" + errid + "\" class=\"tagerror\" href=\"#\">";
118                             errstat += "Error" + (errors.length > 1 ? "s" : "") + " adding tag.";
119                             errstat += "</a>";
120                                             KOHA.Tags.append_tag_status(mytagid + "_status", errstat);
121                             var errmsg = "";
122                             for (var e = 0; e < errors.length; e++){
123                                 if (e) {
124                                     errmsg += "\n\n";
125                                 }
126                                 errmsg += errors[e];
127                             }
128                             $("#" + errid).click(function(){
129                                 alert(errmsg);
130                             });
131                         }
132                     }
133                 }
134
135                 if (bibErrors || response["global_errors"]) {
136                     var msg = "";
137                     if (bibErrors) {
138                         msg = "Unable to add one or more tags.";
139                     }
140
141                     // Show global errors in a dialog.
142                     if (response["global_errors"]) {
143                         var global_errors = response["global_errors"];
144                         var msg;
145                         for (var e = 0; e < global_errors.length; e++) {
146                             msg += "\n\n";
147                             msg += response.alerts[global_errors[e]];
148                         }
149                     }
150                     alert(msg);
151                 }
152                         },
153                         'script'
154                 );
155                 return false;
156     }
157 };
158