Bug 20600: (follow-up) Rebase on current master
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / modules / ill / ill-requests.tt
1 [% USE raw %]
2 [% USE Asset %]
3 [% USE Branches %]
4 [% USE Koha %]
5 [% USE KohaDates %]
6 [% SET footerjs = 1 %]
7 [% INCLUDE 'doc-head-open.inc' %]
8 <title>Koha &rsaquo; ILL requests</title>
9 [% INCLUDE 'doc-head-close.inc' %]
10 [% Asset.css("css/datatables.css") | $raw %]
11 </head>
12
13 <body id="illrequests" class="ill">
14 [% INCLUDE 'header.inc' %]
15 [% INCLUDE 'cat-search.inc' %]
16
17 <div id="breadcrumbs">
18     <a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo;
19     <a href="/cgi-bin/koha/ill/ill-requests.pl">ILL requests</a>
20     [% IF query_type == 'create' %]
21          &rsaquo; New request
22     [% ELSIF query_type == 'status' %]
23          &rsaquo; Status
24     [% END %]
25 </div>
26
27 <div id="doc3" class="yui-t2">
28     <div id="bd">
29         [% IF query_type == 'illlist' %]
30         <div id="illfilter_yui_column" class="yui-b">
31             <form method="get" id="illfilter_form">
32                 <fieldset class="brief">
33                     <h3>Filters</h3>
34                     <ol>
35                         <li>
36                             <label for="illfilter_status">Status:</label>
37                             <select name="illfilter_status" id="illfilter_status">
38                                 <option value=""></option>
39                             </select>
40                         </li>
41                         <li>
42                             <label for="illfilter_dateplaced_start">Date placed between:</label>
43                             <input type="text" name="illfilter_dateplaced_start" id="illfilter_dateplaced_start" class="datepicker" />
44                         </li>
45                         <li>
46                             <label for="illfilter_dateplaced_end">and:</label>
47                             <input type="text" name="illfilter_dateplaced_end" id="illfilter_dateplaced_end" class="datepicker" />
48                         </li>
49                         <li>
50                             <label for="illfilter_datemodified_start">Updated between:</label>
51                             <input type="text" name="illfilter_datemodified_start" id="illfilter_datemodified_start" class="datepicker" />
52                         </li>
53                         <li>
54                             <label for="illfilter_datemodified_end">and:</label>
55                             <input type="text" name="illfilter_datemodified_end" id="illfilter_datemodified_end" class="datepicker" />
56                         </li>
57                         <li>
58                             <label for="illfilter_branchname">Library:</label>
59                             <select name="illfilter_branchname" id="illfilter_branchname">
60                                 <option value=""></option>
61                             </select>
62                         </li>
63                         <li>
64                             <label for="illfilter_barcode">Patron barcode:</label>
65                             <input type="text" name="illfilter_barcode" id="illfilter_barcode" />
66                         </li>
67                     </ol>
68                     <fieldset class="action">
69                         <input type="submit" value="Search" />
70                         <input type="button" value="Clear" id="clear_search" />
71                     </fieldset>
72                 </fieldset>
73             </form>
74         </div>
75         [% END %]
76         <div id="yui-main">
77             <div id="interlibraryloans" class="yui-b">
78         [% IF !backends_available || !has_branch %]
79             <div class="dialog message">ILL module configuration problem. Take a look at the <a href="/cgi-bin/koha/about.pl#sysinfo">about page</a></div>
80         [% ELSE %]
81                 [% INCLUDE 'ill-toolbar.inc' %]
82
83                 [% IF whole.error %]
84                     <h1>Error performing operation</h1>
85                     <!-- Dispatch on Status -->
86                     <p>We encountered an error:</p>
87                     <p>
88                       <pre>[% whole.message | html %] ([% whole.status | html %])</pre>
89                     </p>
90                 [% END %]
91
92                 [% IF query_type == 'create' %]
93                     <h1>New ILL request</h1>
94                     [% PROCESS $whole.template %]
95
96                 [% ELSIF query_type == 'confirm' %]
97                     <h1>Confirm ILL request</h1>
98                     [% PROCESS $whole.template %]
99
100                 [% ELSIF query_type == 'cancel' and !whole.error %]
101                     <h1>Cancel a confirmed request</h1>
102                     [% PROCESS $whole.template %]
103
104                 [% ELSIF query_type == 'generic_confirm' %]
105                     <h1>Place request with partner libraries</h1>
106                   [% IF error %]
107                     [% IF error == 'no_target_email' %]
108                         <div class="alert">
109                             No target email addresses found. Either select at least
110                             one partner or check your ILL partner library records.
111                         </div>
112                     [% ELSIF error == 'no_library_email' %]
113                         <div class="alert">
114                             Your library has no usable email address. Please set it.
115                         </div>
116                     [% ELSIF error == 'unkown_error' %]
117                         <div class="alert">
118                             Unknown error processing your request. Contact your administrator.
119                         </div>
120                     [% END %]
121                   [% END %]
122                     <!-- Start of GENERIC_EMAIL case -->
123                     [% IF whole.value.partners %]
124                        [% ill_url = "/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=" _ request.illrequest_id %]
125                         <form method="POST" action="/cgi-bin/koha/ill/ill-requests.pl">
126                             <fieldset class="rows">
127                                 <legend>Interlibrary loan request details</legend>
128                                 <ol>
129                                     <li>
130                                         <label for="partner_filter">Filter partner libraries:</label>
131                                         <input type="text" id="partner_filter" />
132                                     </li>
133                                     <li>
134                                         <label for="partners" class="required">Select partner libraries:</label>
135                                         <select size="5" multiple="true" id="partners" name="partners" required="required">
136                                             [% FOREACH partner IN whole.value.partners %]
137                                                 <option value=[% partner.email | html %]>
138                                                     [% partner.branchcode _ " - " _ partner.surname %]
139                                                 </option>
140                                             [% END %]
141                                         </select>
142
143                                     </li>
144                                     <li>
145                                         <label for="subject" class="required">Subject Line</label>
146                                         <input type="text" name="subject" id="subject" type="text" value="[% whole.value.draft.subject | html %]" required="required" />
147                                     </li>
148                                     <li>
149                                         <label for="body" class="required">Email text:</label>
150                                         <textarea name="body" id="body" rows="20" cols="80" required="required">[% whole.value.draft.body | html %]</textarea>
151                                     </li>
152                                 </ol>
153                                 <input type="hidden" value="generic_confirm" name="method">
154                                 <input type="hidden" value="draft" name="stage">
155                                 <input type="hidden" value="[% request.illrequest_id | html %]" name="illrequest_id">
156                             </fieldset>
157                             <fieldset class="action">
158                                 <input type="submit" class="btn btn-default" value="Send email"/>
159                                 <span><a href="[% ill_url | url %]" title="Return to request details">Cancel</a></span>
160                             </fieldset>
161                         </form>
162                     [% ELSE %]
163                         <fieldset class="rows">
164                             <legend>Interlibrary loan request details</legend>
165                             <p>No partners have been defined yet. Please create appropriate patron records (by default ILLLIBS category).</p>
166                             <p>Be sure to provide email addresses for these patrons.</p>
167                             <p><span><a href="[% ill_url | url %]" title="Return to request details">Cancel</a></span></p>
168                         </fieldset>
169                     [% END %]
170                 <!-- generic_confirm ends here -->
171
172                 [% ELSIF query_type == 'edit_action' %]
173                     <form method="POST" action="/cgi-bin/koha/ill/ill-requests.pl">
174                         <fieldset class="rows">
175                             <legend>Request details</legend>
176                             <ol>
177                                 [% type = request.get_type %]
178                                 <li class="borrowernumber">
179                                     <label for="borrowernumber">Patron ID:</label>
180                                     [% request.borrowernumber | html %]
181                                 </li>
182                                 <li class="biblio_id">
183                                     <label for="biblio_id" class="biblio_id">Bibliographic record ID:</label>
184                                     <input name="biblio_id" id="biblio_id" type="text" value="[% request.biblio_id | html %]">
185                                 </li>
186                                 <li class="branchcode">
187                                     <label for="library" class="branchcode">Library:</label>
188                                     <select name="branchcode" id="library">
189                                         [% PROCESS options_for_libraries libraries => Branches.all( selected => request.branchcode ) %]
190                                     </select>
191                                 </li>
192                                 <li class="status">
193                                     <label class="status">Status:</label>
194                                     [% stat = request.status %]
195                                     [% request.capabilities.$stat.name | html %]
196                                 </li>
197                                 <li class="updated">
198                                     <label class="updated">Last updated:</label>
199                                     [% request.updated | $KohaDates  with_hours => 1 %]
200                                 </li>
201                                 <li class="medium">
202                                     <label class="medium">Request type:</label>
203                                     [% IF type %][% type | html %][% ELSE %]<span>N/A</span>[% END %]
204                                 </li>
205                                 <li class="cost">
206                                     <label class="cost">Cost:</label>
207                                     [% IF request.cost %][% request.cost | html %][% ELSE %]<span>N/A</span>[% END %]
208                                 </li>
209                                 <li class="price_paid">
210                                     <label class="price_paid">Price paid:</label>
211                                     <input name="price_paid" id="price_paid" type="text" value="[% request.price_paid | html %]">
212                                 </li>
213                                 <li class="req_id">
214                                     <label class="req_id">Request number:</label>
215                                     [% request.id_prefix _ request.illrequest_id | html %]
216                                 </li>
217                                 <li class="notesstaff">
218                                     <label for="notesstaff" class="notesstaff">Staff notes:</label>
219                                     <textarea name="notesstaff" id="notesstaff" rows="5">[% request.notesstaff | html %]</textarea>
220                                 </li>
221                                 <li class="notesopac">
222                                     <label for="notesopac" class="notesopac">Opac notes:</label>
223                                     <textarea name="notesopac" id="notesopac" rows="5">[% request.notesopac | html %]</textarea>
224                                 </li>
225                             </ol>
226                         </fieldset>
227                         <fieldset class="action">
228                             <input type="hidden" value="edit_action" name="method">
229                             <input type="hidden" value="form" name="stage">
230                             <input type="hidden" value="[% request.illrequest_id | html %]" name="illrequest_id">
231                             <input type="hidden" value="[% request.borrowernumber | html %]" name="borrowernumber">
232                             <input type="submit" value="Submit">
233                             <a class="cancel" href="/cgi-bin/koha/ill/ill-requests.pl?method=illview&amp;illrequest_id=[% request.id | html %]">Cancel</a>
234                         </fieldset>
235                     </form>
236
237                 [% ELSIF query_type == 'delete_confirm' %]
238
239                     <div class="dialog alert">
240                         <h3>Are you sure you wish to delete this request?</h3>
241                         <form action="/cgi-bin/koha/ill/ill-requests.pl" method="post">
242                             <input type="hidden" name="method" value="delete" />
243                             <input type="hidden" name="confirmed" value="1" />
244                             <input type="hidden" name="illrequest_id" value="[% request.id | html %]" />
245                             <button type="submit" class="btn btn-default btn-sm approve"><i class="fa fa-fw fa-check"></i> Yes, delete</button>
246                         </form>
247                         <a class="btn btn-default btn-sm deny" href="/cgi-bin/koha/ill/ill-requests.pl?method=illview&amp;illrequest_id=[% request.id | html %]"><i class="fa fa-fw fa-remove"></i>No, do not delete</a>
248                     </div>
249
250                 [% ELSIF query_type == 'illview' %]
251                     [% req_status = request.status %]
252
253                     [% IF error %]
254                       [% IF error == 'migrate_target' %]
255                           <div class="alert">
256                               The backend you tried to migrate to does not yet support migrations, please try again with an alternative target.
257                           </div>
258                       [% END %]
259                     [% END %]
260
261                     <h1>Manage ILL request</h1>
262                     <div id="request-toolbar" class="btn-toolbar">
263                         <a title="Edit request" id="ill-toolbar-btn-edit-action" class="btn btn-sm btn-default" href="/cgi-bin/koha/ill/ill-requests.pl?method=edit_action&amp;illrequest_id=[% request.illrequest_id | html %]">
264                         <span class="fa fa-pencil"></span>
265                         Edit request
266                         </a>
267                         [% FOREACH action IN request.available_actions %]
268                             [% IF action.method == 'migrate' %]
269                                 [% IF backends.size > 2 %]
270                                     <div class="dropdown btn-group">
271                                         <button class="btn btn-sm btn-default dropdown-toggle" type="button" id="ill-migrate-dropdown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
272                                             <i class="fa [% action.ui_method_icon | html %]"></i> [% action.ui_method_name | html %] <span class="caret"></span>
273                                         </button>
274                                         <ul class="dropdown-menu" aria-labelledby="ill-migrate-dropdown">
275                                             [% FOREACH backend IN backends %]
276                                                 [% IF backend != request.backend %]
277                                                     <li><a href="/cgi-bin/koha/ill/ill-requests.pl?method=[% action.method | uri %]&amp;illrequest_id=[% request.illrequest_id | uri %]&amp;backend=[% backend | uri %]">[% backend | html %]</a></li>
278                                                 [% END %]
279                                             [% END %]
280                                         </ul>
281                                     </div>
282                                 [% ELSIF backends.size == 2 %]
283                                     [% FOREACH backend IN backends %]
284                                         [% IF backend != request.backend %]
285                                             <a title="[% action.ui_method_name | html %]" id="ill-toolbar-btn-[% action.id | lower | html %]" class="btn btn-sm btn-default" href="/cgi-bin/koha/ill/ill-requests.pl?method=[% action.method | uri %]&amp;illrequest_id=[% request.illrequest_id | uri %]&amp;backend=[% backend | uri %]">
286                                             <span class="fa [% action.ui_method_icon | html %]"></span>
287                                             [% action.ui_method_name | html %]
288                                             </a>
289                                         [% END %]
290                                     [% END %]
291                                 [% END %]
292                             [% ELSIF action.method != 0 %]
293                                 <a title="[% action.ui_method_name | html %]" id="ill-toolbar-btn-[% action.id | lower | html %]" class="btn btn-sm btn-default" href="/cgi-bin/koha/ill/ill-requests.pl?method=[% action.method | uri %]&amp;illrequest_id=[% request.illrequest_id | uri %]">
294                                 <span class="fa [% action.ui_method_icon | html %]"></span>
295                                 [% action.ui_method_name | html %]
296                                 </a>
297                             [% END %]
298                         [% END %]
299                         <a title="Display supplier metadata" id="ill-request-display-metadata" class="btn btn-sm btn-default pull-right" href="#">
300                             <span class="fa fa-eye"></span>
301                             Display supplier metadata
302                         </a>
303                     </div>
304                     <div class="ill-view-panel panel panel-default">
305                         <div class="panel-heading">
306                             <h3>Request details</h3>
307                         </div>
308                         <div class="panel-body">
309                             <h4>Details from library</h4>
310                             <div class="rows">
311                                 <ol>
312                                     <li class="orderid">
313                                         <span class="label orderid">Order ID:</span>
314                                         [% IF request.orderid %][% request.orderid | html %][% ELSE %]<span>N/A</span>[% END %]
315                                     </li>
316                                     <li class="borrowernumber">
317                                         <span class="label borrowernumber">Patron:</span>
318                                         [% borrowerlink = "/cgi-bin/koha/members/moremember.pl" _ "?borrowernumber=" _ request.patron.borrowernumber %]
319                                         <a href="[% borrowerlink | url %]" title="View borrower details">
320                                         [% request.patron.firstname _ " " _ request.patron.surname _ " [" _ request.patron.cardnumber _ "]" | html %]
321                                         </a>
322                                     </li>
323
324                                     <li class="biblio_id">
325                                         <span class="label biblio_id">Bibliographic record ID:</span>
326                                         [% IF request.biblio_id %]
327                                             <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% request.biblio_id | uri %]">[% request.biblio_id | html %]</a>
328                                         [% ELSE %]
329                                             <span>N/A</span>
330                                         [% END %]
331                                     </li>
332                                     <li class="branchcode">
333                                         <span class="label branchcode">Library:</span>
334                                         [% Branches.GetName(request.branchcode) | html %]
335                                     </li>
336                                     <li class="status">
337                                         <span class="label status">Status:</span>
338                                         [% request.capabilities.$req_status.name | html %]
339                                     </li>
340                                     <li class="updated">
341                                         <span class="label updated">Last updated:</span>
342                                         [% request.updated | $KohaDates  with_hours => 1 %]
343                                     </li>
344                                     <li class="medium">
345                                         <span class="label medium">Request type:</span>
346                                         [% type = request.get_type %]
347                                         [% IF type %][% type | html %][% ELSE %]<span>N/A</span>[% END %]
348                                     </li>
349                                     <li class="cost">
350                                         <span class="label cost">Cost:</span>
351                                         [% IF request.cost %][% request.cost | html %][% ELSE %]<span>N/A</span>[% END %]
352                                     </li>
353                                     <li class="price_paid">
354                                         <span class="label price_paid">Price paid:</span>
355                                         [% IF request.price_paid %][% request.price_paid | html %][% ELSE %]<span>N/A</span>[% END %]
356                                     </li>
357                                     <li class="req_id">
358                                         <span class="label req_id">Request number:</span>
359                                         [% request.id_prefix _ request.illrequest_id | html %]
360                                     </li>
361                                     <li class="notesstaff">
362                                         <span class="label notes_staff">Staff notes:</span>
363                                         <p>[% request.notesstaff | html %]</p>
364                                     </li>
365                                     <li class="notesopac">
366                                         <span class="label notes_opac">Notes:</span>
367                                         <p>[% request.notesopac | html %]</p>
368                                     </li>
369                                 </ol>
370                             </div>
371                             <div class="rows">
372                                 <h4>Details from supplier ([% request.backend | html %])</h4>
373                                 <ol>
374                                     [% FOREACH meta IN request.metadata %]
375                                         <li class="requestmeta-[% meta.key.replace('\s','_') | html %]">
376                                             <span class="label">[% meta.key | html %]:</span>
377                                             [% meta.value | html %]
378                                         </li>
379                                     [% END %]
380                                 </ol>
381                             </div>
382                         </div>
383                     </div>
384
385                     <div id="dataPreview" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="dataPreviewLabel" aria-hidden="true">
386                         <div class="modal-dialog">
387                             <div class="modal-content">
388                                 <div class="modal-header">
389                                     <button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">×</button>
390                                     <h3 id="dataPreviewLabel"> Supplier metadata</h3>
391                                 </div>
392                                 <div class="modal-body">
393                                     <div id="requestattributes">
394                                         [% FOREACH attr IN request.illrequestattributes %]
395                                         <div class="requestattr-[% attr.type | html %]">
396                                             <span class="label">[% attr.type | html %]:</span>
397                                             [% attr.value | html %]
398                                         </div>
399                                             [% END %]
400                                     </div>
401                                 </div>
402                                 <div class="modal-footer">
403                                     <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Close</button>
404                                 </div>
405                             </div>
406                         </div>
407                     </div>
408
409                     <div class="ill-view-panel panel panel-default">
410                         <div class="panel-heading">
411                             <h3>[% request.illcomments.count | html %] comments</h3>
412                         </div>
413                         <div class="panel-body">
414                             [% IF request.illcomments.count && request.illcomments.count > 0 %]
415                                 [% FOREACH comment IN request.illcomments %]
416                                     <div class="rows comment_[% comment.patron.categorycode | html %]">
417                                     <h5>Comment by:
418                                     <a href="[% borrowerlink | url %]" title="View borrower details">
419                                     [% comment.patron.firstname _ " " _ comment.patron.surname _ " [" _ comment.patron.cardnumber _ "]" | html %]</a>
420                                     [% comment.timestamp | $KohaDates with_hours => 1 %]</h5>
421                                     <p>[% comment.comment | html %]</p>
422                                     </div>
423                                 [% END %]
424                             [% END %]
425                                 <div class="rows">
426                                     <h3><a id="toggle_addcomment" href="#">Add comment</a></h3>
427                                     <div id="addcomment" class="content_hidden">
428                                         <form class="validated" method="post" action="/cgi-bin/koha/ill/ill-requests.pl">
429                                             <input type="hidden" value="save_comment" name="method">
430                                             <input type="hidden" value="[% csrf_token | html %]" name="csrf_token">
431                                             <input type="hidden" value="[% request.illrequest_id | html %]" name="illrequest_id">
432                                             <fieldset class="rows">
433                                                 <ol>
434                                                     <li>
435                                                         <label class="required" for="comment">Comment: </label>
436                                                         <textarea class="required" required="required" cols="80" rows="10" id="comment" name="comment"></textarea>
437                                                         <span class="required">Required</span>
438                                                     </li>
439                                                 </ol>
440                                             </fieldset>
441                                             <fieldset class="action">
442                                                 <input type="submit" value="Submit">
443                                             </fieldset>
444                                         </form>
445                                     </div>
446                                 </div>
447                             </div>
448                     </div>
449
450                 [% ELSIF query_type == 'illlist' %]
451                     <!-- illlist -->
452                     <h1>View ILL requests</h1>
453                     <div id="results">
454                         <h3>Details for all requests</h3>
455
456                         <table id="ill-requests">
457                             <thead>
458                                 <tr id="illview-header">
459                                     <th>Author</th>
460                                     <th>Title</th>
461                                     <th>Patron</th>
462                                     <th>Bibliographic record ID</th>
463                                     <th>Library</th>
464                                     <th>Status</th>
465                                     <th class="placed">&nbsp;</th>
466                                     <th class="placed_formatted">Date placed</th>
467                                     <th class="updated">&nbsp;</th>
468                                     <th class="updated_formatted">Updated on</th>
469                                     <th>Request number</th>
470                                     <th>Comments</th>
471                                     <th class="patron_cardnumber">Patron barcode</th>
472                                     <th class="actions"></th>
473                                 </tr>
474                             </thead>
475                             <tbody id="illview-body">
476                             </tbody>
477                         </table>
478                     </div>
479                 [% ELSE %]
480                 <!-- Custom Backend Action -->
481                 [% PROCESS $whole.template %]
482
483                 [% END %]
484         [% END %]
485             </div>
486         </div>
487     </div>
488
489 [% TRY %]
490 [% PROCESS backend_jsinclude %]
491 [% CATCH %]
492 [% END %]
493
494 [% MACRO jsinclude BLOCK %]
495     [% INCLUDE 'datatables.inc' %]
496     [% INCLUDE 'calendar.inc' %]
497     [% Asset.js("lib/jquery/plugins/jquery.checkboxes.min.js") | $raw %]
498     <script>
499         $(document).ready(function() {
500
501             // Illview Datatable setup
502
503             var table;
504
505             // Filters that are active
506             var activeFilters = {};
507
508             // Fields we don't want to display
509             var ignore = [
510                 'accessurl',
511                 'backend',
512                 'branchcode',
513                 'completed',
514                 'capabilities',
515                 'cost',
516                 'medium',
517                 'notesopac',
518                 'notesstaff',
519                 'replied'
520             ];
521
522             // Fields we need to expand (flatten)
523             var expand = [
524                 'metadata',
525                 'patron'
526             ];
527
528             // Expanded fields
529             // This is auto populated
530             var expanded = {};
531
532             // The core fields that should be displayed first
533             var core = [
534                 'metadata_author',
535                 'metadata_title',
536                 'borrowername',
537                 'biblio_id',
538                 'library',
539                 'status',
540                 'placed',
541                 'placed_formatted',
542                 'updated',
543                 'updated_formatted',
544                 'illrequest_id',
545                 'comments',
546                 'patron_cardnumber',
547                 'action'
548             ];
549
550             // Filterable columns
551             var filterable = {
552                 status: {
553                     prep: function(tableData, oData) {
554                         var uniques = {};
555                         tableData.forEach(function(row) {
556                             var resolvedName = getStatusName(
557                                 oData[0].capabilities[row.status].name
558                             );
559                             uniques[resolvedName] = 1
560                         });
561                         Object.keys(uniques).sort().forEach(function(unique) {
562                             $('#illfilter_status').append(
563                                 '<option value="' + unique  +
564                                 '">' + unique +  '</option>'
565                             );
566                         });
567                     },
568                     listener: function() {
569                         var me = 'status';
570                         $('#illfilter_status').change(function() {
571                             var sel = $('#illfilter_status option:selected').val();
572                             if (sel && sel.length > 0) {
573                                 activeFilters[me] = function() {
574                                     table.column(5).search(sel);
575                                 }
576                             } else {
577                                 if (activeFilters.hasOwnProperty(me)) {
578                                     delete activeFilters[me];
579                                 }
580                             }
581                         });
582                     },
583                     clear: function() {
584                         $('#illfilter_status').val('');
585                     }
586                 },
587                 pickupBranch: {
588                     prep: function(tableData, oData) {
589                         var uniques = {};
590                         tableData.forEach(function(row) {
591                             uniques[row.library.branchname] = 1
592                         });
593                         Object.keys(uniques).sort().forEach(function(unique) {
594                             $('#illfilter_branchname').append(
595                                 '<option value="' + unique  +
596                                 '">' + unique +  '</option>'
597                             );
598                         });
599                     },
600                     listener: function() {
601                         var me = 'pickupBranch';
602                         $('#illfilter_branchname').change(function() {
603                             var sel = $('#illfilter_branchname option:selected').val();
604                             if (sel && sel.length > 0) {
605                                 activeFilters[me] = function() {
606                                     table.column(4).search(sel);
607                                 }
608                             } else {
609                                 if (activeFilters.hasOwnProperty(me)) {
610                                     delete activeFilters[me];
611                                 }
612                             }
613                         });
614                     },
615                     clear: function() {
616                         $('#illfilter_branchname').val('');
617                     }
618                 },
619                 barcode: {
620                     listener: function() {
621                         var me = 'barcode';
622                         $('#illfilter_barcode').change(function() {
623                             var val = $('#illfilter_barcode').val();
624                             if (val && val.length > 0) {
625                                 activeFilters[me] = function() {
626                                     table.column(12).search(val);
627                                 }
628                             } else {
629                                 if (activeFilters.hasOwnProperty(me)) {
630                                     delete activeFilters[me];
631                                 }
632                             }
633                         });
634                     },
635                     clear: function() {
636                         $('#illfilter_barcode').val('');
637                     }
638                 },
639                 dateModified: {
640                     clear: function() {
641                         $('#illfilter_datemodified_start, #illfilter_datemodified_end').val('');
642                     }
643                 },
644                 datePlaced: {
645                     clear: function() {
646                         $('#illfilter_dateplaced_start, #illfilter_dateplaced_end').val('');
647                     }
648                 }
649             };
650
651             // Remove any fields we're ignoring
652             var removeIgnore = function(dataObj) {
653                 dataObj.forEach(function(thisRow) {
654                     ignore.forEach(function(thisIgnore) {
655                         if (thisRow.hasOwnProperty(thisIgnore)) {
656                             delete thisRow[thisIgnore];
657                         }
658                     });
659                 });
660             };
661
662             // Expand any fields we're expanding
663             var expandExpand = function(row) {
664                 expand.forEach(function(thisExpand) {
665                     if (row.hasOwnProperty(thisExpand)) {
666                         if (!expanded.hasOwnProperty(thisExpand)) {
667                             expanded[thisExpand] = [];
668                         }
669                         var expandObj = row[thisExpand];
670                         Object.keys(expandObj).forEach(
671                             function(thisExpandCol) {
672                                 var expColName = thisExpand + '_' + thisExpandCol;
673                                 // Keep a list of fields that have been expanded
674                                 // so we can create toggle links for them
675                                 if (expanded[thisExpand].indexOf(expColName) == -1) {
676                                     expanded[thisExpand].push(expColName);
677                                 }
678                                 expandObj[expColName] =
679                                     expandObj[thisExpandCol];
680                                 delete expandObj[thisExpandCol];
681                             }
682                         );
683                         $.extend(true, row, expandObj);
684                         delete row[thisExpand];
685                     }
686                 });
687             };
688
689             // Build a de-duped list of all column names
690             var allCols = {};
691             core.map(function(thisCore) {
692                 allCols[thisCore] = 1;
693             });
694
695             // Strip the expand prefix if it exists, we do this for display
696             var stripPrefix = function(value) {
697                 expand.forEach(function(thisExpand) {
698                     var regex = new RegExp(thisExpand + '_', 'g');
699                     value = value.replace(regex, '');
700                 });
701                 return value;
702             };
703
704             // Our 'render' function for borrowerlink
705             var createPatronLink = function(data, type, row) {
706                 return '<a title="' + _("View borrower details") + '" ' +
707                     'href="/cgi-bin/koha/members/moremember.pl?' +
708                     'borrowernumber='+row.borrowernumber+'">' +
709                     row.patron_firstname + ' ' + row.patron_surname +
710                     '</a>';
711             };
712
713             // Our 'render' function for the library name
714             var createLibrary = function(data, type, row) {
715                 return row.library.branchname;
716             };
717
718             // Render function for request ID
719             var createRequestId = function(data, type, row) {
720                 return row.id_prefix + row.illrequest_id;
721             };
722
723             // Render function for request status
724             var createStatus = function(data, type, row, meta) {
725                 var origData = meta.settings.oInit.originalData;
726                 if (origData.length > 0) {
727                     var status_name = meta.settings.oInit.originalData[0].capabilities[
728                         row.status
729                     ].name;
730                     return getStatusName(status_name);
731                 } else {
732                     return '';
733                 }
734             };
735
736             var getStatusName = function(origName) {
737                 switch( origName ) {
738                     case "New request":
739                         return _("New request");
740                     case "Requested":
741                         return _("Requested");
742                     case "Requested from partners":
743                         return _("Requested from partners");
744                     case "Request reverted":
745                         return _("Request reverted");
746                     case "Queued request":
747                         return _("Queued request");
748                     case "Cancellation requested":
749                         return _("Cancellation requested");
750                     case "Completed":
751                         return _("Completed");
752                     case "Delete request":
753                         return _("Delete request");
754                     default:
755                         return origName;
756                 }
757             };
758
759             // Render function for creating a row's action link
760             var createActionLink = function(data, type, row) {
761                 return '<a class="btn btn-default btn-sm" ' +
762                     'href="/cgi-bin/koha/ill/ill-requests.pl?' +
763                     'method=illview&amp;illrequest_id=' +
764                     row.illrequest_id +
765                     '">' + _("Manage request") + '</a>';
766             };
767
768             // Columns that require special treatment
769             var specialCols = {
770                 action: {
771                     name: '',
772                     func: createActionLink
773                 },
774                 borrowername: {
775                     name: _("Patron"),
776                     func: createPatronLink
777                 },
778                 illrequest_id: {
779                     name: _("Request number"),
780                     func: createRequestId
781                 },
782                 status: {
783                     name: _("Status"),
784                     func: createStatus
785                 },
786                 biblio_id: {
787                     name: _("Biblio ID")
788                 },
789                 library: {
790                     name: _("Library"),
791                     func: createLibrary
792                 },
793                 updated: {
794                     name: _("Updated on"),
795                 },
796                 patron_cardnumber: {
797                     name: _("Patron barcode")
798                 }
799             };
800
801             // Toggle request attributes in Illview
802             $('#toggle_requestattributes').on('click', function(e) {
803                 e.preventDefault();
804                 $('#requestattributes').toggleClass('content_hidden');
805             });
806
807             // Toggle new comment form in Illview
808             $('#toggle_addcomment').on('click', function(e) {
809                 e.preventDefault();
810                 $('#addcomment').toggleClass('content_hidden');
811             });
812
813             // Filter partner list
814             $('#partner_filter').keyup(function() {
815                 var needle = $('#partner_filter').val();
816                 $('#partners > option').each(function() {
817                     var regex = new RegExp(needle, 'i');
818                     if (
819                         needle.length == 0 ||
820                         $(this).is(':selected') ||
821                         $(this).text().match(regex)
822                     ) {
823                         $(this).show();
824                     } else {
825                         $(this).hide();
826                     }
827                 });
828             });
829
830         // Display the modal containing request supplier metadata
831         $('#ill-request-display-metadata').on('click', function(e) {
832             e.preventDefault();
833             $('#dataPreview').modal({show:true});
834         });
835
836             // Get our data from the API and process it prior to passing
837             // it to datatables
838             var ajax = $.ajax(
839                 '/api/v1/illrequests?embed=metadata,patron,capabilities,library'
840                 ).done(function() {
841                     var data = JSON.parse(ajax.responseText);
842                     // Make a copy, we'll be removing columns next and need
843                     // to be able to refer to data that has been removed
844                     var dataCopy = $.extend(true, [], data);
845                     // Remove all columns we're not interested in
846                     removeIgnore(dataCopy);
847                     // Expand columns that need it and create an array
848                     // of all column names
849                     $.each(dataCopy, function(k, row) {
850                         expandExpand(row);
851                     });
852
853                     // Assemble an array of column definitions for passing
854                     // to datatables
855                     var colData = [];
856                     Object.keys(allCols).forEach(function(thisCol) {
857                         // Create the base column object
858                         var colObj = {
859                             name: thisCol,
860                             className: thisCol,
861                             defaultContent: ''
862                         };
863                         // We may need to process the data going in this
864                         // column, so do it if necessary
865                         if (
866                             specialCols.hasOwnProperty(thisCol) &&
867                             specialCols[thisCol].hasOwnProperty('func')
868                         ) {
869                             colObj.render = specialCols[thisCol].func;
870                         } else {
871                             colObj.data = thisCol;
872                         }
873                         colData.push(colObj);
874                     });
875
876                     // Initialise the datatable
877                     table = $('#ill-requests').DataTable($.extend(true, {}, dataTablesDefaults, {
878                         'aoColumnDefs': [
879                             { // Last column shouldn't be sortable or searchable
880                                 'aTargets': [ 'actions' ],
881                                 'bSortable': false,
882                                 'bSearchable': false
883                             },
884                             { // Hide the two date columns we use just for sorting
885                                 'aTargets': [ 'placed', 'updated' ],
886                                 'bVisible': false,
887                                 'bSearchable': true
888                             },
889                             { // When sorting 'placed', we want to use the
890                               // unformatted column
891                               'aTargets': [ 'placed_formatted'],
892                               'iDataSort': 7
893                             },
894                             { // When sorting 'updated', we want to use the
895                               // unformatted column
896                               'aTargets': [ 'updated_formatted'],
897                               'iDataSort': 9
898                             },
899                             {
900                               'aTargets': [ 'patron_cardnumber' ],
901                               'bVisible': false,
902                               'bSearchable': true
903                             }
904                         ],
905                         'aaSorting': [[ 9, 'desc' ]], // Default sort, updated descending
906                         'processing': true, // Display a message when manipulating
907                         'sPaginationType': "full_numbers", // Pagination display
908                         'deferRender': true, // Improve performance on big datasets
909                         'data': dataCopy,
910                         'columns': colData,
911                         'originalData': data, // Enable render functions to access
912                                               // our original data
913                         'initComplete': function() {
914
915                             // Prepare any filter elements that need it
916                             for (var el in filterable) {
917                                 if (filterable.hasOwnProperty(el)) {
918                                     if (filterable[el].hasOwnProperty('prep')) {
919                                         filterable[el].prep(dataCopy, data);
920                                     }
921                                     if (filterable[el].hasOwnProperty('listener')) {
922                                         filterable[el].listener();
923                                     }
924                                 }
925                             }
926
927                         }
928                     }));
929
930                     // Custom date range filtering
931                     $.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
932                         var placedStart = $('#illfilter_dateplaced_start').datepicker('getDate');
933                         var placedEnd = $('#illfilter_dateplaced_end').datepicker('getDate');
934                         var modifiedStart = $('#illfilter_datemodified_start').datepicker('getDate');
935                         var modifiedEnd = $('#illfilter_datemodified_end').datepicker('getDate');
936                         var rowPlaced = data[6] ? new Date(data[6]) : null;
937                         var rowModified = data[8] ? new Date(data[8]) : null;
938                         var placedPassed = true;
939                         var modifiedPassed = true;
940                         if (placedStart && rowPlaced && rowPlaced < placedStart) {
941                             placedPassed = false
942                         };
943                         if (placedEnd && rowPlaced && rowPlaced > placedEnd) {
944                             placedPassed = false;
945                         }
946                         if (modifiedStart && rowModified && rowModified < modifiedStart) {
947                             modifiedPassed = false
948                         };
949                         if (modifiedEnd && rowModified && rowModified > modifiedEnd) {
950                             modifiedPassed = false;
951                         }
952
953                         return placedPassed && modifiedPassed;
954
955                     });
956
957                 }
958             );
959
960             var clearSearch = function() {
961                 table.search('').columns().search('');
962                 activeFilters = {};
963                 for (var filter in filterable) {
964                     if (
965                         filterable.hasOwnProperty(filter) &&
966                         filterable[filter].hasOwnProperty('clear')
967                     ) {
968                         filterable[filter].clear();
969                     }
970                 }
971                 table.draw();
972             };
973
974             // Apply any search filters, or clear any previous
975             // ones
976             $('#illfilter_form').submit(function(event) {
977                 event.preventDefault();
978                 table.search('').columns().search('');
979                 for (var active in activeFilters) {
980                     if (activeFilters.hasOwnProperty(active)) {
981                         activeFilters[active]();
982                     }
983                 }
984                 table.draw();
985             });
986
987             // Clear all filters
988             $('#clear_search').click(function() {
989                 clearSearch();
990             });
991
992         });
993     </script>
994 [% END %]
995
996 [% INCLUDE 'intranet-bottom.inc' %]