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