Bug 14310 [QA Followup] - Deal with error conditions
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / js / holds.js
1 $(document).ready(function() {
2     var holdsTable;
3
4     // Don't load holds table unless it is clicked on
5     $("#holds-tab").on( "click", function(){ load_holds_table() } );
6
7     // If the holds tab is preselected on load, we need to load the table
8     if ( $("#holds-tab").parent().hasClass('ui-state-active') ) { load_holds_table() }
9
10     function load_holds_table() {
11         var holds = new Array();
12         if ( ! holdsTable ) {
13             holdsTable = $("#holds-table").dataTable({
14                 "bAutoWidth": false,
15                 "sDom": "rt",
16                 "columns": [
17                     {
18                         "mDataProp": "reservedate_formatted"
19                     },
20                     {
21                         "mDataProp": function ( oObj ) {
22                             title = "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber="
23                                   + oObj.biblionumber
24                                   + "'>"
25                                   + oObj.title;
26
27                             $.each(oObj.subtitle, function( index, value ) {
28                                       title += " " + value.subfield;
29                             });
30
31                             title += "</a>";
32
33                             if ( oObj.author ) {
34                                 title += " " + BY.replace( "_AUTHOR_",  oObj.author );
35                             }
36
37                             if ( oObj.itemnotes ) {
38                                 var span_class = "";
39                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
40                                     span_class = "circ-hlt";
41                                 }
42                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes + "</span>"
43                             }
44
45                             return title;
46                         }
47                     },
48                     {
49                         "mDataProp": function( oObj ) {
50                             return oObj.itemcallnumber || "";
51                         }
52                     },
53                     {
54                         "mDataProp": function( oObj ) {
55                             var data = "";
56
57                             if ( oObj.suspend == 1 ) {
58                                 data += "<p>" + HOLD_IS_SUSPENDED;
59                                 if ( oObj.suspend_until ) {
60                                     data += " " + UNTIL.format( oObj.suspend_until_formatted );
61                                 }
62                                 data += "</p>";
63                             }
64
65                             if ( oObj.barcode ) {
66                                 data += "<em>";
67                                 if ( oObj.found == "W" ) {
68
69                                     if ( oObj.waiting_here ) {
70                                         data += ITEM_IS_WAITING_HERE;
71                                     } else {
72                                         data += ITEM_IS_WAITING;
73                                         data += " " + AT.format( oObj.waiting_at );
74                                     }
75
76                                 } else if ( oObj.transferred ) {
77                                     data += ITEM_IS_IN_TRANSIT.format( oObj.from_branch, oObj.date_sent );
78                                 } else if ( oObj.not_transferred ) {
79                                     data += NOT_TRANSFERRED_YET.format( oObj.not_transferred_by );
80                                 }
81                                 data += "</em>";
82
83                                 data += " <a href='/cgi-bin/koha/catalogue/detail.pl?biblionumber="
84                                   + oObj.biblionumber
85                                   + "&itemnumber="
86                                   + oObj.itemnumber
87                                   + "#"
88                                   + oObj.itemnumber
89                                   + "'>"
90                                   + oObj.barcode
91                                   + "</a>";
92                             }
93
94                             return data;
95                         }
96                     },
97                     {
98                         "mDataProp": function( oObj ) {
99                             return oObj.branchcode || "";
100                         }
101                     },
102                     { "mDataProp": "expirationdate_formatted" },
103                     {
104                         "mDataProp": function( oObj ) {
105                             if ( oObj.priority && parseInt( oObj.priority ) && parseInt( oObj.priority ) > 0 ) {
106                                 return oObj.priority;
107                             } else {
108                                 return "";
109                             }
110                         }
111                     },
112                     {
113                         "bSortable": false,
114                         "mDataProp": function( oObj ) {
115                             return "<select name='rank-request'>"
116                                  + "<option value='n'>" + NO + "</option>"
117                                  + "<option value='del'>" + YES  + "</option>"
118                                  + "</select>"
119                                  + "<input type='hidden' name='biblionumber' value='" + oObj.biblionumber + "'>"
120                                  + "<input type='hidden' name='borrowernumber' value='" + borrowernumber + "'>"
121                                  + "<input type='hidden' name='reserve_id' value='" + oObj.reserve_id + "'>";
122                         }
123                     },
124                     {
125                         "bSortable": false,
126                         "mDataProp": function( oObj ) {
127                             holds[oObj.reserve_id] = oObj; //Store holds for later use
128
129                             if ( oObj.found ) {
130                                 return "";
131                             } else if ( oObj.suspend == 1 ) {
132                                 return "<a class='hold-resume btn btn-link' id='resume" + oObj.reserve_id + "' style='display: inline; white-space: nowrap;'>"
133                                      + "<i class='icon-play'></i> " + RESUME + "</a>";
134                             } else {
135                                 return "<a class='hold-suspend btn btn-link' id='suspend" + oObj.reserve_id + "' style='display: inline; white-space: nowrap;'>"
136                                      + "<i class='icon-pause'></i> " + SUSPEND + "</a>";
137                             }
138                         }
139                     }
140                 ],
141                 "bPaginate": false,
142                 "bProcessing": true,
143                 "bServerSide": false,
144                 "ajax": {
145                     "url": '/cgi-bin/koha/svc/holds',
146                     "data": function ( d ) {
147                         d.borrowernumber = borrowernumber;
148                     }
149                 },
150             });
151
152             $('#holds-table').on( 'draw.dt', function () {
153                 $(".hold-suspend").on( "click", function() {
154                     var id = $(this).attr("id").replace("suspend", "");
155                     var hold = holds[id];
156                     $("#suspend-modal-title").html( hold.title );
157                     $("#suspend-modal-reserve_id").val( hold.reserve_id );
158                     $('#suspend-modal').modal('show');
159                 });
160
161                 $(".hold-resume").on( "click", function() {
162                     var id = $(this).attr("id").replace("resume", "");
163                     var hold = holds[id];
164                     $.post('/cgi-bin/koha/svc/hold/resume', { "reserve_id": hold.reserve_id }, function( data ){
165                       if ( data.success ) {
166                           holdsTable.api().ajax.reload();
167                       } else {
168                         if ( data.error == "HOLD_NOT_FOUND" ) {
169                             alert ( RESUME_HOLD_ERROR_NOT_FOUND );
170                             holdsTable.api().ajax.reload();
171                         }
172                       }
173                     });
174                 });
175             });
176
177             if ( $("#holds-table").length ) {
178                 $("#holds-table_processing").position({
179                     of: $( "#holds-table" ),
180                     collision: "none"
181                 });
182             }
183         }
184     }
185
186     $("body").append("\
187         <div id='suspend-modal' class='modal hide fade' tabindex='-1' role='dialog' aria-hidden='true'>\
188             <form id='suspend-modal-form' class='form-inline'>\
189                 <div class='modal-header'>\
190                     <button type='button' class='closebtn' data-dismiss='modal' aria-hidden='true'>×</button>\
191                     <h3 id='suspend-modal-label'>" + SUSPEND_HOLD_ON + " <i><span id='suspend-modal-title'></span></i></h3>\
192                 </div>\
193 \
194                 <div class='modal-body'>\
195                     <input type='hidden' id='suspend-modal-reserve_id' name='reserve_id' />\
196 \
197                     <label for='suspend-modal-until'>Suspend until:</label>\
198                     <input name='suspend_until' id='suspend-modal-until' class='suspend-until' size='10' />\
199 \
200                     <p/><a class='btn btn-link' id='suspend-modal-clear-date' >" + CLEAR_DATE_TO_SUSPEND_INDEFINITELY + "</a></p>\
201 \
202                 </div>\
203 \
204                 <div class='modal-footer'>\
205                     <button id='suspend-modal-submit' class='btn btn-primary' type='submit' name='submit'>" + SUSPEND + "</button>\
206                     <a href='#' data-dismiss='modal' aria-hidden='true' class='cancel'>" + CANCEL + "</a>\
207                 </div>\
208             </form>\
209         </div>\
210     ");
211
212     $("#suspend-modal-until").datepicker({ minDate: 1 }); // Require that "until date" be in the future
213     $("#suspend-modal-clear-date").on( "click", function() { $("#suspend-modal-until").val(""); } );
214
215     $("#suspend-modal-submit").on( "click", function( e ) {
216         e.preventDefault();
217         $.post('/cgi-bin/koha/svc/hold/suspend', $('#suspend-modal-form').serialize(), function( data ){
218           $('#suspend-modal').modal('hide');
219           if ( data.success ) {
220               holdsTable.api().ajax.reload();
221           } else {
222             if ( data.error == "INVALID_DATE" ) {
223                 alert( SUSPEND_HOLD_ERROR_DATE );
224             }
225             else if ( data.error == "HOLD_NOT_FOUND" ) {
226                 alert ( SUSPEND_HOLD_ERROR_NOT_FOUND );
227                 holdsTable.api().ajax.reload();
228             }
229           }
230         });
231     });
232
233 });