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