Bug 20581: (follow-up) Do not use PK for AV FK
[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.authorised_value == current_alias %]
207                                             <option value="[% alias.authorised_value | html %]" selected>
208                                             [% ELSE %]
209                                             <option value="[% alias.authorised_value | html %]">
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;
580                             if (row.status_alias) {
581                                 resolvedName = row.status_alias.lib;
582                             } else {
583                                 resolvedName = getStatusName(
584                                     oData[0].capabilities[row.status].name
585                                 );
586                             }
587                             uniques[resolvedName] = 1
588                         });
589                         Object.keys(uniques).sort().forEach(function(unique) {
590                             $('#illfilter_status').append(
591                                 '<option value="' + unique  +
592                                 '">' + unique +  '</option>'
593                             );
594                         });
595                     },
596                     listener: function() {
597                         var me = 'status';
598                         $('#illfilter_status').change(function() {
599                             var sel = $('#illfilter_status option:selected').val();
600                             if (sel && sel.length > 0) {
601                                 activeFilters[me] = function() {
602                                     table.column(5).search(sel);
603                                 }
604                             } else {
605                                 if (activeFilters.hasOwnProperty(me)) {
606                                     delete activeFilters[me];
607                                 }
608                             }
609                         });
610                     },
611                     clear: function() {
612                         $('#illfilter_status').val('');
613                     }
614                 },
615                 pickupBranch: {
616                     prep: function(tableData, oData) {
617                         var uniques = {};
618                         tableData.forEach(function(row) {
619                             uniques[row.library.branchname] = 1
620                         });
621                         Object.keys(uniques).sort().forEach(function(unique) {
622                             $('#illfilter_branchname').append(
623                                 '<option value="' + unique  +
624                                 '">' + unique +  '</option>'
625                             );
626                         });
627                     },
628                     listener: function() {
629                         var me = 'pickupBranch';
630                         $('#illfilter_branchname').change(function() {
631                             var sel = $('#illfilter_branchname option:selected').val();
632                             if (sel && sel.length > 0) {
633                                 activeFilters[me] = function() {
634                                     table.column(4).search(sel);
635                                 }
636                             } else {
637                                 if (activeFilters.hasOwnProperty(me)) {
638                                     delete activeFilters[me];
639                                 }
640                             }
641                         });
642                     },
643                     clear: function() {
644                         $('#illfilter_branchname').val('');
645                     }
646                 },
647                 barcode: {
648                     listener: function() {
649                         var me = 'barcode';
650                         $('#illfilter_barcode').change(function() {
651                             var val = $('#illfilter_barcode').val();
652                             if (val && val.length > 0) {
653                                 activeFilters[me] = function() {
654                                     table.column(12).search(val);
655                                 }
656                             } else {
657                                 if (activeFilters.hasOwnProperty(me)) {
658                                     delete activeFilters[me];
659                                 }
660                             }
661                         });
662                     },
663                     clear: function() {
664                         $('#illfilter_barcode').val('');
665                     }
666                 },
667                 dateModified: {
668                     clear: function() {
669                         $('#illfilter_datemodified_start, #illfilter_datemodified_end').val('');
670                     }
671                 },
672                 datePlaced: {
673                     clear: function() {
674                         $('#illfilter_dateplaced_start, #illfilter_dateplaced_end').val('');
675                     }
676                 }
677             };
678
679             // Remove any fields we're ignoring
680             var removeIgnore = function(dataObj) {
681                 dataObj.forEach(function(thisRow) {
682                     ignore.forEach(function(thisIgnore) {
683                         if (thisRow.hasOwnProperty(thisIgnore)) {
684                             delete thisRow[thisIgnore];
685                         }
686                     });
687                 });
688             };
689
690             // Expand any fields we're expanding
691             var expandExpand = function(row) {
692                 expand.forEach(function(thisExpand) {
693                     if (row.hasOwnProperty(thisExpand)) {
694                         if (!expanded.hasOwnProperty(thisExpand)) {
695                             expanded[thisExpand] = [];
696                         }
697                         var expandObj = row[thisExpand];
698                         Object.keys(expandObj).forEach(
699                             function(thisExpandCol) {
700                                 var expColName = thisExpand + '_' + thisExpandCol;
701                                 // Keep a list of fields that have been expanded
702                                 // so we can create toggle links for them
703                                 if (expanded[thisExpand].indexOf(expColName) == -1) {
704                                     expanded[thisExpand].push(expColName);
705                                 }
706                                 expandObj[expColName] =
707                                     expandObj[thisExpandCol];
708                                 delete expandObj[thisExpandCol];
709                             }
710                         );
711                         $.extend(true, row, expandObj);
712                         delete row[thisExpand];
713                     }
714                 });
715             };
716
717             // Build a de-duped list of all column names
718             var allCols = {};
719             core.map(function(thisCore) {
720                 allCols[thisCore] = 1;
721             });
722
723             // Strip the expand prefix if it exists, we do this for display
724             var stripPrefix = function(value) {
725                 expand.forEach(function(thisExpand) {
726                     var regex = new RegExp(thisExpand + '_', 'g');
727                     value = value.replace(regex, '');
728                 });
729                 return value;
730             };
731
732             // Our 'render' function for borrowerlink
733             var createPatronLink = function(data, type, row) {
734                 var patronLink = '<a title="' + _("View borrower details") + '" ' +
735                     'href="/cgi-bin/koha/members/moremember.pl?' +
736                     'borrowernumber='+row.borrowernumber+'">';
737                 if ( row.patron_firstname ) {
738                     patronLink = patronLink + row.patron_firstname + ' ';
739                 }
740                 patronLink = patronLink + row.patron_surname + '</a>';
741                 return patronLink;
742             };
743
744             // Our 'render' function for the library name
745             var createLibrary = function(data, type, row) {
746                 return row.library.branchname;
747             };
748
749             // Render function for request ID
750             var createRequestId = function(data, type, row) {
751                 return row.id_prefix + row.illrequest_id;
752             };
753
754             // Render function for request status
755             var createStatus = function(data, type, row, meta) {
756                 if (row.status_alias) {
757                     return row.status_alias.lib
758                         ? row.status_alias.lib
759                         : row.status_alias.authorised_value;
760                 } else {
761                     var origData = meta.settings.oInit.originalData;
762                     if (origData.length > 0) {
763                         var status_name = meta.settings.oInit.originalData[0].capabilities[
764                             row.status
765                         ].name;
766                         return getStatusName(status_name);
767                     } else {
768                         return '';
769                     }
770                 }
771             };
772
773             var getStatusName = function(origName) {
774                 switch( origName ) {
775                     case "New request":
776                         return _("New request");
777                     case "Requested":
778                         return _("Requested");
779                     case "Requested from partners":
780                         return _("Requested from partners");
781                     case "Request reverted":
782                         return _("Request reverted");
783                     case "Queued request":
784                         return _("Queued request");
785                     case "Cancellation requested":
786                         return _("Cancellation requested");
787                     case "Completed":
788                         return _("Completed");
789                     case "Delete request":
790                         return _("Delete request");
791                     default:
792                         return origName;
793                 }
794             };
795
796             // Render function for creating a row's action link
797             var createActionLink = function(data, type, row) {
798                 return '<a class="btn btn-default btn-sm" ' +
799                     'href="/cgi-bin/koha/ill/ill-requests.pl?' +
800                     'method=illview&amp;illrequest_id=' +
801                     row.illrequest_id +
802                     '">' + _("Manage request") + '</a>';
803             };
804
805             // Columns that require special treatment
806             var specialCols = {
807                 action: {
808                     name: '',
809                     func: createActionLink
810                 },
811                 borrowername: {
812                     name: _("Patron"),
813                     func: createPatronLink
814                 },
815                 illrequest_id: {
816                     name: _("Request number"),
817                     func: createRequestId
818                 },
819                 status: {
820                     name: _("Status"),
821                     func: createStatus
822                 },
823                 biblio_id: {
824                     name: _("Biblio ID")
825                 },
826                 library: {
827                     name: _("Library"),
828                     func: createLibrary
829                 },
830                 updated: {
831                     name: _("Updated on"),
832                 },
833                 patron_cardnumber: {
834                     name: _("Cardnumber")
835                 }
836             };
837
838             // Toggle request attributes in Illview
839             $('#toggle_requestattributes').on('click', function(e) {
840                 e.preventDefault();
841                 $('#requestattributes').toggleClass('content_hidden');
842             });
843
844             // Toggle new comment form in Illview
845             $('#toggle_addcomment').on('click', function(e) {
846                 e.preventDefault();
847                 $('#addcomment').toggleClass('content_hidden');
848             });
849
850             // Filter partner list
851             $('#partner_filter').keyup(function() {
852                 var needle = $('#partner_filter').val();
853                 $('#partners > option').each(function() {
854                     var regex = new RegExp(needle, 'i');
855                     if (
856                         needle.length == 0 ||
857                         $(this).is(':selected') ||
858                         $(this).text().match(regex)
859                     ) {
860                         $(this).show();
861                     } else {
862                         $(this).hide();
863                     }
864                 });
865             });
866
867         // Display the modal containing request supplier metadata
868         $('#ill-request-display-metadata').on('click', function(e) {
869             e.preventDefault();
870             $('#dataPreview').modal({show:true});
871         });
872
873             // Get our data from the API and process it prior to passing
874             // it to datatables
875             var ajax = $.ajax(
876                 '/api/v1/illrequests?embed=metadata,patron,capabilities,library,status_alias'
877                 ).done(function() {
878                     var data = JSON.parse(ajax.responseText);
879                     // Make a copy, we'll be removing columns next and need
880                     // to be able to refer to data that has been removed
881                     var dataCopy = $.extend(true, [], data);
882                     // Remove all columns we're not interested in
883                     removeIgnore(dataCopy);
884                     // Expand columns that need it and create an array
885                     // of all column names
886                     $.each(dataCopy, function(k, row) {
887                         expandExpand(row);
888                     });
889
890                     // Assemble an array of column definitions for passing
891                     // to datatables
892                     var colData = [];
893                     Object.keys(allCols).forEach(function(thisCol) {
894                         // Create the base column object
895                         var colObj = {
896                             name: thisCol,
897                             className: thisCol,
898                             defaultContent: ''
899                         };
900                         // We may need to process the data going in this
901                         // column, so do it if necessary
902                         if (
903                             specialCols.hasOwnProperty(thisCol) &&
904                             specialCols[thisCol].hasOwnProperty('func')
905                         ) {
906                             colObj.render = specialCols[thisCol].func;
907                         } else {
908                             colObj.data = thisCol;
909                         }
910                         colData.push(colObj);
911                     });
912
913                     // Initialise the datatable
914                     table = $('#ill-requests').DataTable($.extend(true, {}, dataTablesDefaults, {
915                         'aoColumnDefs': [
916                             { // Last column shouldn't be sortable or searchable
917                                 'aTargets': [ 'actions' ],
918                                 'bSortable': false,
919                                 'bSearchable': false
920                             },
921                             { // Hide the two date columns we use just for sorting
922                                 'aTargets': [ 'placed', 'updated' ],
923                                 'bVisible': false,
924                                 'bSearchable': true
925                             },
926                             { // When sorting 'placed', we want to use the
927                               // unformatted column
928                               'aTargets': [ 'placed_formatted'],
929                               'iDataSort': 7
930                             },
931                             { // When sorting 'updated', we want to use the
932                               // unformatted column
933                               'aTargets': [ 'updated_formatted'],
934                               'iDataSort': 9
935                             },
936                             {
937                               'aTargets': [ 'patron_cardnumber' ],
938                               'bVisible': false,
939                               'bSearchable': true
940                             }
941                         ],
942                         'aaSorting': [[ 9, 'desc' ]], // Default sort, updated descending
943                         'processing': true, // Display a message when manipulating
944                         'sPaginationType': "full_numbers", // Pagination display
945                         'deferRender': true, // Improve performance on big datasets
946                         'data': dataCopy,
947                         'columns': colData,
948                         'originalData': data, // Enable render functions to access
949                                               // our original data
950                         'initComplete': function() {
951
952                             // Prepare any filter elements that need it
953                             for (var el in filterable) {
954                                 if (filterable.hasOwnProperty(el)) {
955                                     if (filterable[el].hasOwnProperty('prep')) {
956                                         filterable[el].prep(dataCopy, data);
957                                     }
958                                     if (filterable[el].hasOwnProperty('listener')) {
959                                         filterable[el].listener();
960                                     }
961                                 }
962                             }
963
964                         }
965                     }));
966
967                     // Custom date range filtering
968                     $.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
969                         var placedStart = $('#illfilter_dateplaced_start').datepicker('getDate');
970                         var placedEnd = $('#illfilter_dateplaced_end').datepicker('getDate');
971                         var modifiedStart = $('#illfilter_datemodified_start').datepicker('getDate');
972                         var modifiedEnd = $('#illfilter_datemodified_end').datepicker('getDate');
973                         var rowPlaced = data[6] ? new Date(data[6]) : null;
974                         var rowModified = data[8] ? new Date(data[8]) : null;
975                         var placedPassed = true;
976                         var modifiedPassed = true;
977                         if (placedStart && rowPlaced && rowPlaced < placedStart) {
978                             placedPassed = false
979                         };
980                         if (placedEnd && rowPlaced && rowPlaced > placedEnd) {
981                             placedPassed = false;
982                         }
983                         if (modifiedStart && rowModified && rowModified < modifiedStart) {
984                             modifiedPassed = false
985                         };
986                         if (modifiedEnd && rowModified && rowModified > modifiedEnd) {
987                             modifiedPassed = false;
988                         }
989
990                         return placedPassed && modifiedPassed;
991
992                     });
993
994                 }
995             );
996
997             var clearSearch = function() {
998                 table.search('').columns().search('');
999                 activeFilters = {};
1000                 for (var filter in filterable) {
1001                     if (
1002                         filterable.hasOwnProperty(filter) &&
1003                         filterable[filter].hasOwnProperty('clear')
1004                     ) {
1005                         filterable[filter].clear();
1006                     }
1007                 }
1008                 table.draw();
1009             };
1010
1011             // Apply any search filters, or clear any previous
1012             // ones
1013             $('#illfilter_form').submit(function(event) {
1014                 event.preventDefault();
1015                 table.search('').columns().search('');
1016                 for (var active in activeFilters) {
1017                     if (activeFilters.hasOwnProperty(active)) {
1018                         activeFilters[active]();
1019                     }
1020                 }
1021                 table.draw();
1022             });
1023
1024             // Clear all filters
1025             $('#clear_search').click(function() {
1026                 clearSearch();
1027             });
1028
1029         });
1030     </script>
1031 [% END %]
1032
1033 [% INCLUDE 'intranet-bottom.inc' %]