Bug 11529: Clean up subtitle usage
[koha.git] / koha-tmpl / intranet-tmpl / prog / 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                         "data": { _: "reservedate_formatted", "sort": "reservedate" }
19                     },
20                     {
21                         "mDataProp": function ( oObj ) {
22                             title = "<a href='/cgi-bin/koha/reserve/request.pl?biblionumber="
23                                   + oObj.biblionumber
24                                   + "'>"
25                                   + oObj.title.escapeHtml();
26
27                             $.each(oObj.subtitle, function( index, value ) {
28                                       title += " " + value.escapeHtml();
29                             });
30
31                             title += " " + oObj.part_number + " " + oObj.part_name;
32
33                             title += "</a>";
34
35                             if ( oObj.author ) {
36                                 title += " " + BY.replace( "_AUTHOR_",  oObj.author.escapeHtml() );
37                             }
38
39                             if ( oObj.itemnotes ) {
40                                 var span_class = "";
41                                 if ( $.datepicker.formatDate('yy-mm-dd', new Date(oObj.issuedate) ) == ymd ) {
42                                     span_class = "circ-hlt";
43                                 }
44                                 title += " - <span class='" + span_class + "'>" + oObj.itemnotes.escapeHtml() + "</span>"
45                             }
46
47                             return title;
48                         }
49                     },
50                     {
51                         "mDataProp": function( oObj ) {
52                             return oObj.itemcallnumber && oObj.itemcallnumber.escapeHtml() || "";
53                         }
54                     },
55                     {
56                         "mDataProp": function( oObj ) {
57                             var data = "";
58                             if ( oObj.barcode ) {
59                                 data += " <a href='/cgi-bin/koha/catalogue/moredetail.pl?biblionumber="
60                                   + oObj.biblionumber
61                                   + "&itemnumber="
62                                   + oObj.itemnumber
63                                   + "#item"
64                                   + oObj.itemnumber
65                                   + "'>"
66                                   + oObj.barcode.escapeHtml()
67                                   + "</a>";
68                             }
69                             return data;
70                         }
71                     },
72                     {
73                         "mDataProp": function( oObj ) {
74                             if( oObj.branches.length > 1 && oObj.found !== 'W' && oObj.found !== 'T' ){
75                                 var branchSelect='<select priority='+oObj.priority+' class="hold_location_select" reserve_id="'+oObj.reserve_id+'" name="pick-location">';
76                                 for ( var i=0; i < oObj.branches.length; i++ ){
77                                     var selectedbranch;
78                                     var setbranch;
79                                     if( oObj.branches[i].selected ){
80
81                                         selectedbranch = " selected='selected' ";
82                                         setbranch = CURRENT;
83                                     } else if ( oObj.branches[i].pickup_location == 0 ) {
84                                         continue;
85                                     } else{
86                                         selectedbranch = '';
87                                         setbranch = '';
88                                     }
89                                     branchSelect += '<option value="'+ oObj.branches[i].branchcode.escapeHtml() +'"'+selectedbranch+'>'+oObj.branches[i].branchname.escapeHtml()+setbranch+'</option>';
90                                 }
91                                 branchSelect +='</select>';
92                                 return branchSelect;
93                             }
94                             else { return oObj.branchcode.escapeHtml() || ""; }
95                         }
96                     },
97                     { "data": { _: "expirationdate_formatted", "sort": "expirationdate" } },
98                     {
99                         "mDataProp": function( oObj ) {
100                             if ( oObj.priority && parseInt( oObj.priority ) && parseInt( oObj.priority ) > 0 ) {
101                                 return oObj.priority;
102                             } else {
103                                 return "";
104                             }
105                         }
106                     },
107                     {
108                         "bSortable": false,
109                         "mDataProp": function( oObj ) {
110                             return "<select name='rank-request'>"
111                                  + "<option value='n'>" + NO + "</option>"
112                                  + "<option value='del'>" + YES  + "</option>"
113                                  + "</select>"
114                                  + "<input type='hidden' name='biblionumber' value='" + oObj.biblionumber + "'>"
115                                  + "<input type='hidden' name='borrowernumber' value='" + borrowernumber + "'>"
116                                  + "<input type='hidden' name='reserve_id' value='" + oObj.reserve_id + "'>";
117                         }
118                     },
119                     {
120                         "bSortable": false,
121                         "mDataProp": function( oObj ) {
122                             holds[oObj.reserve_id] = oObj; //Store holds for later use
123
124                             if ( oObj.found ) {
125                                 return "";
126                             } else if ( oObj.suspend == 1 ) {
127                                 return "<a class='hold-resume btn btn-default btn-xs' id='resume" + oObj.reserve_id + "'>"
128                                      + "<i class='fa fa-play'></i> " + RESUME + "</a>";
129                             } else {
130                                 return "<a class='hold-suspend btn btn-default btn-xs' id='suspend" + oObj.reserve_id + "'>"
131                                      + "<i class='fa fa-pause'></i> " + SUSPEND + "</a>";
132                             }
133                         }
134                     },
135                     {
136                         "mDataProp": function( oObj ) {
137                             var data = "";
138
139                             if ( oObj.suspend == 1 ) {
140                                 data += "<p>" + HOLD_IS_SUSPENDED;
141                                 if ( oObj.suspend_until ) {
142                                     data += " " + UNTIL.format( oObj.suspend_until_formatted );
143                                 }
144                                 data += "</p>";
145                             }
146
147                             if ( oObj.itemtype_limit ) {
148                                 data += NEXT_AVAILABLE_ITYPE.format( oObj.itemtype_limit );
149                             }
150
151                             if ( oObj.barcode ) {
152                                 data += "<em>";
153                                 if ( oObj.found == "W" ) {
154
155                                     if ( oObj.waiting_here ) {
156                                         data += ITEM_IS_WAITING_HERE;
157                                     } else {
158                                         data += ITEM_IS_WAITING;
159                                         data += " " + AT.format( oObj.waiting_at );
160                                     }
161
162                                 } else if ( oObj.transferred ) {
163                                     data += ITEM_IS_IN_TRANSIT.format( oObj.from_branch, oObj.date_sent );
164                                 } else if ( oObj.not_transferred ) {
165                                     data += NOT_TRANSFERRED_YET.format( oObj.not_transferred_by );
166                                 }
167                                 data += "</em>";
168                             }
169                             return data;
170                         }
171                    }
172                 ],
173                 "bPaginate": false,
174                 "bProcessing": true,
175                 "bServerSide": false,
176                 "ajax": {
177                     "url": '/cgi-bin/koha/svc/holds',
178                     "data": function ( d ) {
179                         d.borrowernumber = borrowernumber;
180                     }
181                 },
182             });
183
184             $('#holds-table').on( 'draw.dt', function () {
185                 $(".hold-suspend").on( "click", function() {
186                     var id = $(this).attr("id").replace("suspend", "");
187                     var hold = holds[id];
188                     $("#suspend-modal-title").html( hold.title );
189                     $("#suspend-modal-reserve_id").val( hold.reserve_id );
190                     $('#suspend-modal').modal('show');
191                 });
192
193                 $(".hold-resume").on( "click", function() {
194                     var id = $(this).attr("id").replace("resume", "");
195                     var hold = holds[id];
196                     $.post('/cgi-bin/koha/svc/hold/resume', { "reserve_id": hold.reserve_id }, function( data ){
197                       if ( data.success ) {
198                           holdsTable.api().ajax.reload();
199                       } else {
200                         if ( data.error == "HOLD_NOT_FOUND" ) {
201                             alert ( RESUME_HOLD_ERROR_NOT_FOUND );
202                             holdsTable.api().ajax.reload();
203                         }
204                       }
205                     });
206                 });
207
208                 $(".hold_location_select").change(function(){
209                     $(this).prop("disabled",true);
210                     var cur_select = $(this);
211                     var res_id = $(this).attr('reserve_id');
212                     $(this).after('<div id="updating_reserveno'+res_id+'" class="waiting"><img src="/intranet-tmpl/prog/img/spinner-small.gif" alt="" /><span class="waiting_msg"></span></div>');
213                     var api_url = '/api/v1/holds/'+res_id;
214                     var update_info = JSON.stringify({ pickup_library_id: $(this).val(), priority: parseInt($(this).attr("priority"),10) });
215                     $.ajax({
216                         method: "PUT",
217                         url: api_url,
218                         data: update_info ,
219                         success: function( data ){ holdsTable.api().ajax.reload(); },
220                         error: function( jqXHR, textStatus, errorThrown) {
221                             alert('There was an error:'+textStatus+" "+errorThrown);
222                             cur_select.prop("disabled",false);
223                             $("#updating_reserveno"+res_id).remove();
224                             cur_select.val( cur_select.children('option[selected="selected"]').val() );
225                         },
226                     });
227                 });
228
229             });
230
231             if ( $("#holds-table").length ) {
232                 $("#holds-table_processing").position({
233                     of: $( "#holds-table" ),
234                     collision: "none"
235                 });
236             }
237         }
238     }
239
240     $("body").append("\
241         <div id='suspend-modal' class='modal fade' role='dialog' aria-hidden='true'>\
242             <div class='modal-dialog'>\
243             <div class='modal-content'>\
244             <form id='suspend-modal-form' class='form-inline'>\
245                 <div class='modal-header'>\
246                     <button type='button' class='closebtn' data-dismiss='modal' aria-hidden='true'>×</button>\
247                     <h3 id='suspend-modal-label'>" + SUSPEND_HOLD_ON + " <i><span id='suspend-modal-title'></span></i></h3>\
248                 </div>\
249 \
250                 <div class='modal-body'>\
251                     <input type='hidden' id='suspend-modal-reserve_id' name='reserve_id' />\
252 \
253                     <label for='suspend-modal-until'>" + SUSPEND_UNTIL + "</label>\
254                     <input name='suspend_until' id='suspend-modal-until' class='suspend-until' size='10' />\
255 \
256                     <p/><a class='btn btn-link' id='suspend-modal-clear-date' >" + CLEAR_DATE_TO_SUSPEND_INDEFINITELY + "</a></p>\
257 \
258                 </div>\
259 \
260                 <div class='modal-footer'>\
261                     <button id='suspend-modal-submit' class='btn btn-primary' type='submit' name='submit'>" + SUSPEND + "</button>\
262                     <a href='#' data-dismiss='modal' aria-hidden='true' class='cancel'>" + CANCEL + "</a>\
263                 </div>\
264             </form>\
265             </div>\
266             </div>\
267         </div>\
268     ");
269
270     $("#suspend-modal-until").datepicker({ minDate: 1 }); // Require that "until date" be in the future
271     $("#suspend-modal-clear-date").on( "click", function() { $("#suspend-modal-until").val(""); } );
272
273     $("#suspend-modal-submit").on( "click", function( e ) {
274         e.preventDefault();
275         $.post('/cgi-bin/koha/svc/hold/suspend', $('#suspend-modal-form').serialize(), function( data ){
276           $('#suspend-modal').modal('hide');
277           if ( data.success ) {
278               holdsTable.api().ajax.reload();
279           } else {
280             if ( data.error == "INVALID_DATE" ) {
281                 alert( SUSPEND_HOLD_ERROR_DATE );
282             }
283             else if ( data.error == "HOLD_NOT_FOUND" ) {
284                 alert ( SUSPEND_HOLD_ERROR_NOT_FOUND );
285                 holdsTable.api().ajax.reload();
286             }
287           }
288         });
289     });
290
291 });