bug 9401: remove direct reads of CGISESSID cookie by JavaScript
[koha.git] / koha-tmpl / opac-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
8 $.ajaxSetup({
9         url: "/cgi-bin/koha/opac-tags.pl",
10         type: "POST",
11         dataType: "script"
12 });
13 */
14 KOHA.Tags = {
15     add_tag_button: function(bibnum, tag){
16         var mynewtag = "newtag" + bibnum;
17                 var mytagid = "#" + mynewtag;
18         var mydata = {};
19         mydata[mynewtag] = tag;
20                 var response;   // AJAX from server will assign value to response.
21                 $.post(
22                         "/cgi-bin/koha/opac-tags.pl",
23                         mydata,
24                         function(data){
25                                 // alert("AJAX Response: " + data);
26                                 eval(data);
27                                 // alert("counts: " + response["added"] + response["deleted"] + response["errors"]);
28                                 KOHA.Tags.set_tag_status(
29                                         mytagid + "_status",
30                                         KOHA.Tags.common_status(response["added"], response["deleted"], response["errors"])
31                                 );
32                                 if (response.alerts) {
33                                         alert(response.alerts.join("\n\n"));
34                                 }
35                         },
36                         'script'
37                 );
38                 return false;
39         },
40         common_status : function(addcount, delcount, errcount) {
41             var cstat = "";
42             if (addcount && addcount > 0) {cstat += MSG_TAGS_ADDED + addcount + ".  " ;}
43             if (delcount && delcount > 0) {cstat += MSG_TAGS_DELETED + delcount + ".  " ;}
44             if (errcount && errcount > 0) {cstat += MSG_TAGS_ERRORS + errcount + ". " ;}
45             return cstat;
46         },
47         set_tag_status : function(tagid, newstatus) {
48                 $(tagid).html(newstatus);
49         $(tagid).show();
50         },
51         append_tag_status : function(tagid, newstatus) {
52                 $(tagid).append(newstatus);
53         $(tagid).show();
54         },
55     clear_all_tag_status : function() {
56         $(".tagstatus").empty().hide();
57     },
58
59         tag_message: {
60         tagsdisabled : function(arg) {return (MSG_TAGS_DISABLED);},
61         scrubbed_all_bad : function(arg) {return (MSG_TAG_ALL_BAD);},
62         badparam : function(arg) {return (MSG_ILLEGAL_PARAMETER+" "+arg);},
63         scrubbed : function(arg) {return (MSG_TAG_SCRUBBED+" "+arg);},
64     failed_add_tag : function(arg) {return (MSG_ADD_TAG_FAILED+ " '"+arg+"'. \n"+MSG_ADD_TAG_FAILED_NOTE);},
65     failed_delete  : function(arg) {return (MSG_DELETE_TAG_FAILED+ " '"+arg+"'. \n"+MSG_DELETE_TAG_FAILED_NOTE);},
66         login : function(arg) {return (MSG_LOGIN_REQUIRED);}
67         },
68
69     // Used to tag multiple items at once.  The main difference
70     // is that status is displayed on a per item basis.
71     add_multitags_button : function(bibarray, tag){
72         var mydata = {};
73         for (var i = 0; i < bibarray.length; i++) {
74             var mynewtag = "newtag" + bibarray[i];
75             mydata[mynewtag] = tag;
76         }
77                 var response;   // AJAX from server will assign value to response.
78                 $.post(
79                         "/cgi-bin/koha/opac-tags.pl",
80                         mydata,
81                         function(data){
82                                 eval(data);
83                 KOHA.Tags.clear_all_tag_status();
84                 var bibErrors = false;
85
86                 // Display the status for each tagged bib
87                 for (var i = 0; i < bibarray.length; i++) {
88                     var bib = bibarray[i];
89                     var mytagid = "#newtag" + bib;
90                     var status = "";
91
92                     // Number of tags added.
93                     if (response[bib]) {
94                         var added = response[bib]["added"];
95                         if (added > 0) {
96                             status = MSG_TAGS_ADDED + added + ".  ";
97                                         KOHA.Tags.set_tag_status(mytagid + "_status", status);
98                         }
99
100                         // Show a link that opens an error dialog, if necessary.
101                         var errors = response[bib]["errors"];
102                         if (errors.length > 0) {
103                             bibErrors = true;    
104                             var errid = "tagerr_" + bib;
105                             var errstat = "<a id=\"" + errid + "\" class=\"tagerror\" href=\"#\">";
106                             errstat += MSG_TAGS_ERRORS + errors.length + ". ";
107                             errstat += "</a>";
108                                             KOHA.Tags.append_tag_status(mytagid + "_status", errstat);
109                             var errmsg = "";
110                             for (var e = 0; e < errors.length; e++){
111                                 if (e) {
112                                     errmsg += "\n\n";
113                                 }
114                                 errmsg += errors[e];
115                             }
116                             $("#" + errid).click(function(){
117                                 alert(errmsg);
118                             });
119                         }
120                     }
121                 }
122
123                 if (bibErrors || response["global_errors"]) {
124                     var msg = "";
125                     if (bibErrors) {
126                         msg = MSG_MULTI_ADD_TAG_FAILED;
127                     }
128
129                     // Show global errors in a dialog.
130                     if (response["global_errors"]) {
131                         var global_errors = response["global_errors"];
132                         var msg;
133                         for (var e = 0; e < global_errors.length; e++) {
134                             msg += "\n\n";
135                             msg += response.alerts[global_errors[e]];
136                         }
137                     }
138                     alert(msg);
139                 }
140                         },
141                         'script'
142                 );
143                 return false;
144     }
145 };
146