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