Bug 20750: Allow logging of arbitrary actions
[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                         <a title="ILL request log" id="ill-request-display-log" class="btn btn-sm btn-default pull-right" href="#">
323                             <span class="fa fa-calendar"></span>
324                             ILL request log
325                         </a>
326                     </div>
327                     <div class="ill-view-panel panel panel-default">
328                         <div class="panel-heading">
329                             <h3>Request details</h3>
330                         </div>
331                         <div class="panel-body">
332                             <h4>Details from library</h4>
333                             <div class="rows">
334                                 <ol>
335                                     <li class="orderid">
336                                         <span class="label orderid">Order ID:</span>
337                                         [% IF request.orderid %][% request.orderid | html %][% ELSE %]<span>N/A</span>[% END %]
338                                     </li>
339                                     <li class="borrowernumber">
340                                         <span class="label borrowernumber">Patron:</span>
341                                         [% borrowerlink = "/cgi-bin/koha/members/moremember.pl" _ "?borrowernumber=" _ request.patron.borrowernumber %]
342                                         <a href="[% borrowerlink | url %]" title="View borrower details">
343                                         [% request.patron.firstname _ " " _ request.patron.surname _ " [" _ request.patron.cardnumber _ "]" | html %]
344                                         </a>
345                                     </li>
346
347                                     <li class="biblio_id">
348                                         <span class="label biblio_id">Bibliographic record ID:</span>
349                                         [% IF request.biblio_id %]
350                                             <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% request.biblio_id | uri %]">[% request.biblio_id | html %]</a>
351                                         [% ELSE %]
352                                             <span>N/A</span>
353                                         [% END %]
354                                     </li>
355                                     <li class="branchcode">
356                                         <span class="label branchcode">Library:</span>
357                                         [% Branches.GetName(request.branchcode) | html %]
358                                     </li>
359                                     <li class="status">
360                                         <span class="label status">Status:</span>
361                                         [% IF request.statusalias %]
362                                             [% request.statusalias.lib | html %]
363                                         [% ELSE %]
364                                             [% request.capabilities.$req_status.name | html%]
365                                         [% END %]
366                                     </li>
367                                     <li class="updated">
368                                         <span class="label updated">Last updated:</span>
369                                         [% request.updated | $KohaDates  with_hours => 1 %]
370                                     </li>
371                                     <li class="medium">
372                                         <span class="label medium">Request type:</span>
373                                         [% type = request.get_type %]
374                                         [% IF type %][% type | html %][% ELSE %]<span>N/A</span>[% END %]
375                                     </li>
376                                     <li class="cost">
377                                         <span class="label cost">Cost:</span>
378                                         [% IF request.cost %][% request.cost | html %][% ELSE %]<span>N/A</span>[% END %]
379                                     </li>
380                                     <li class="price_paid">
381                                         <span class="label price_paid">Price paid:</span>
382                                         [% IF request.price_paid %][% request.price_paid | html %][% ELSE %]<span>N/A</span>[% END %]
383                                     </li>
384                                     <li class="req_id">
385                                         <span class="label req_id">Request number:</span>
386                                         [% request.id_prefix _ request.illrequest_id | html %]
387                                     </li>
388                                     <li class="notesstaff">
389                                         <span class="label notes_staff">Staff notes:</span>
390                                         <p>[% request.notesstaff | html %]</p>
391                                     </li>
392                                     <li class="notesopac">
393                                         <span class="label notes_opac">Notes:</span>
394                                         <p>[% request.notesopac | html %]</p>
395                                     </li>
396                                 </ol>
397                             </div>
398                             <div class="rows">
399                                 <h4>Details from supplier ([% request.backend | html %])</h4>
400                                 <ol>
401                                     [% FOREACH meta IN request.metadata %]
402                                         <li class="requestmeta-[% meta.key.replace('\s','_') | html %]">
403                                             <span class="label">[% meta.key | html %]:</span>
404                                             [% meta.value | html %]
405                                         </li>
406                                     [% END %]
407                                 </ol>
408                             </div>
409                         </div>
410                     </div>
411
412                     <div id="dataPreview" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="dataPreviewLabel" aria-hidden="true">
413                         <div class="modal-dialog">
414                             <div class="modal-content">
415                                 <div class="modal-header">
416                                     <button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">×</button>
417                                     <h3 id="dataPreviewLabel"> Supplier metadata</h3>
418                                 </div>
419                                 <div class="modal-body">
420                                     <div id="requestattributes">
421                                         [% FOREACH attr IN request.illrequestattributes %]
422                                         <div class="requestattr-[% attr.type | html %]">
423                                             <span class="label">[% attr.type | html %]:</span>
424                                             [% attr.value | html %]
425                                         </div>
426                                             [% END %]
427                                     </div>
428                                 </div>
429                                 <div class="modal-footer">
430                                     <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Close</button>
431                                 </div>
432                             </div>
433                         </div>
434                     </div>
435
436                     <div id="requestLog" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="dataPreviewLabel" aria-hidden="true">
437                         <div class="modal-dialog">
438                             <div class="modal-content">
439                                 <div class="modal-header">
440                                     <button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">×</button>
441                                     <h3 id="requestLogLabel"> Request log</h3>
442                                 </div>
443                                 <div class="modal-body">
444                                 [% IF request.logs.size > 0 %]
445                                     [% FOREACH log IN request.logs %]
446                                         [% tpl = log.template %]
447                                         [% INCLUDE $tpl log=log %]
448                                     [% END %]
449                                 [% ELSE %]
450                                     There are no recorded logs for this request
451                                 [% END %]
452                                 </div>
453                                 <div class="modal-footer">
454                                     <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Close</button>
455                                 </div>
456                             </div>
457                         </div>
458                     </div>
459
460                     <div class="ill-view-panel panel panel-default">
461                         <div class="panel-heading">
462                             <h3>[% request.illcomments.count | html %] comments</h3>
463                         </div>
464                         <div class="panel-body">
465                             [% IF request.illcomments.count && request.illcomments.count > 0 %]
466                                 [% FOREACH comment IN request.illcomments %]
467                                     <div class="rows comment_[% comment.patron.categorycode | html %]">
468                                     <h5>Comment by:
469                                     <a href="[% borrowerlink | url %]" title="View borrower details">
470                                     [% comment.patron.firstname _ " " _ comment.patron.surname _ " [" _ comment.patron.cardnumber _ "]" | html %]</a>
471                                     [% comment.timestamp | $KohaDates with_hours => 1 %]</h5>
472                                     <p>[% comment.comment | html %]</p>
473                                     </div>
474                                 [% END %]
475                             [% END %]
476                                 <div class="rows">
477                                     <h3><a id="toggle_addcomment" href="#">Add comment</a></h3>
478                                     <div id="addcomment" class="content_hidden">
479                                         <form class="validated" method="post" action="/cgi-bin/koha/ill/ill-requests.pl">
480                                             <input type="hidden" value="save_comment" name="method">
481                                             <input type="hidden" value="[% csrf_token | html %]" name="csrf_token">
482                                             <input type="hidden" value="[% request.illrequest_id | html %]" name="illrequest_id">
483                                             <fieldset class="rows">
484                                                 <ol>
485                                                     <li>
486                                                         <label class="required" for="comment">Comment: </label>
487                                                         <textarea class="required" required="required" cols="80" rows="10" id="comment" name="comment"></textarea>
488                                                         <span class="required">Required</span>
489                                                     </li>
490                                                 </ol>
491                                             </fieldset>
492                                             <fieldset class="action">
493                                                 <input type="submit" value="Submit">
494                                             </fieldset>
495                                         </form>
496                                     </div>
497                                 </div>
498                         </div>
499                     </div>
500
501                 [% ELSIF query_type == 'illlist' %]
502                     <!-- illlist -->
503                     <h1>View ILL requests</h1>
504                     <div id="results">
505                         <h3>Details for all requests</h3>
506
507                         <table id="ill-requests">
508                             <thead>
509                                 <tr id="illview-header">
510                                     <th>Author</th>
511                                     <th>Title</th>
512                                     <th>Patron</th>
513                                     <th>Bibliographic record ID</th>
514                                     <th>Library</th>
515                                     <th>Status</th>
516                                     <th class="placed">&nbsp;</th>
517                                     <th class="placed_formatted">Date placed</th>
518                                     <th class="updated">&nbsp;</th>
519                                     <th class="updated_formatted">Updated on</th>
520                                     <th>Request number</th>
521                                     <th>Comments</th>
522                                     <th class="patron_cardnumber">Cardnumber</th>
523                                     <th class="actions"></th>
524                                 </tr>
525                             </thead>
526                             <tbody id="illview-body">
527                             </tbody>
528                         </table>
529                     </div>
530                 [% ELSE %]
531                 <!-- Custom Backend Action -->
532                 [% PROCESS $whole.template %]
533
534                 [% END %]
535         [% END %]
536             </div>
537         </div>
538     </div>
539
540 [% TRY %]
541 [% PROCESS backend_jsinclude %]
542 [% CATCH %]
543 [% END %]
544
545 [% MACRO jsinclude BLOCK %]
546     [% INCLUDE 'datatables.inc' %]
547     [% INCLUDE 'calendar.inc' %]
548     [% Asset.js("lib/jquery/plugins/jquery.checkboxes.min.js") | $raw %]
549     <script>
550         $(document).ready(function() {
551
552             // Illview Datatable setup
553
554             var table;
555
556             // Filters that are active
557             var activeFilters = {};
558
559             // Fields we don't want to display
560             var ignore = [
561                 'accessurl',
562                 'backend',
563                 'branchcode',
564                 'completed',
565                 'capabilities',
566                 'cost',
567                 'medium',
568                 'notesopac',
569                 'notesstaff',
570                 'replied'
571             ];
572
573             // Fields we need to expand (flatten)
574             var expand = [
575                 'metadata',
576                 'patron'
577             ];
578
579             // Expanded fields
580             // This is auto populated
581             var expanded = {};
582
583             // The core fields that should be displayed first
584             var core = [
585                 'metadata_author',
586                 'metadata_title',
587                 'borrowername',
588                 'biblio_id',
589                 'library',
590                 'status',
591                 'placed',
592                 'placed_formatted',
593                 'updated',
594                 'updated_formatted',
595                 'illrequest_id',
596                 'comments',
597                 'patron_cardnumber',
598                 'action'
599             ];
600
601             // Filterable columns
602             var filterable = {
603                 status: {
604                     prep: function(tableData, oData) {
605                         var uniques = {};
606                         tableData.forEach(function(row) {
607                             var resolvedName;
608                             if (row.status_alias) {
609                                 resolvedName = row.status_alias.lib;
610                             } else {
611                                 resolvedName = getStatusName(
612                                     oData[0].capabilities[row.status].name
613                                 );
614                             }
615                             uniques[resolvedName] = 1
616                         });
617                         Object.keys(uniques).sort().forEach(function(unique) {
618                             $('#illfilter_status').append(
619                                 '<option value="' + unique  +
620                                 '">' + unique +  '</option>'
621                             );
622                         });
623                     },
624                     listener: function() {
625                         var me = 'status';
626                         $('#illfilter_status').change(function() {
627                             var sel = $('#illfilter_status option:selected').val();
628                             if (sel && sel.length > 0) {
629                                 activeFilters[me] = function() {
630                                     table.column(5).search(sel);
631                                 }
632                             } else {
633                                 if (activeFilters.hasOwnProperty(me)) {
634                                     delete activeFilters[me];
635                                 }
636                             }
637                         });
638                     },
639                     clear: function() {
640                         $('#illfilter_status').val('');
641                     }
642                 },
643                 pickupBranch: {
644                     prep: function(tableData, oData) {
645                         var uniques = {};
646                         tableData.forEach(function(row) {
647                             uniques[row.library.branchname] = 1
648                         });
649                         Object.keys(uniques).sort().forEach(function(unique) {
650                             $('#illfilter_branchname').append(
651                                 '<option value="' + unique  +
652                                 '">' + unique +  '</option>'
653                             );
654                         });
655                     },
656                     listener: function() {
657                         var me = 'pickupBranch';
658                         $('#illfilter_branchname').change(function() {
659                             var sel = $('#illfilter_branchname option:selected').val();
660                             if (sel && sel.length > 0) {
661                                 activeFilters[me] = function() {
662                                     table.column(4).search(sel);
663                                 }
664                             } else {
665                                 if (activeFilters.hasOwnProperty(me)) {
666                                     delete activeFilters[me];
667                                 }
668                             }
669                         });
670                     },
671                     clear: function() {
672                         $('#illfilter_branchname').val('');
673                     }
674                 },
675                 barcode: {
676                     listener: function() {
677                         var me = 'barcode';
678                         $('#illfilter_barcode').change(function() {
679                             var val = $('#illfilter_barcode').val();
680                             if (val && val.length > 0) {
681                                 activeFilters[me] = function() {
682                                     table.column(12).search(val);
683                                 }
684                             } else {
685                                 if (activeFilters.hasOwnProperty(me)) {
686                                     delete activeFilters[me];
687                                 }
688                             }
689                         });
690                     },
691                     clear: function() {
692                         $('#illfilter_barcode').val('');
693                     }
694                 },
695                 dateModified: {
696                     clear: function() {
697                         $('#illfilter_datemodified_start, #illfilter_datemodified_end').val('');
698                     }
699                 },
700                 datePlaced: {
701                     clear: function() {
702                         $('#illfilter_dateplaced_start, #illfilter_dateplaced_end').val('');
703                     }
704                 }
705             };
706
707             // Remove any fields we're ignoring
708             var removeIgnore = function(dataObj) {
709                 dataObj.forEach(function(thisRow) {
710                     ignore.forEach(function(thisIgnore) {
711                         if (thisRow.hasOwnProperty(thisIgnore)) {
712                             delete thisRow[thisIgnore];
713                         }
714                     });
715                 });
716             };
717
718             // Expand any fields we're expanding
719             var expandExpand = function(row) {
720                 expand.forEach(function(thisExpand) {
721                     if (row.hasOwnProperty(thisExpand)) {
722                         if (!expanded.hasOwnProperty(thisExpand)) {
723                             expanded[thisExpand] = [];
724                         }
725                         var expandObj = row[thisExpand];
726                         Object.keys(expandObj).forEach(
727                             function(thisExpandCol) {
728                                 var expColName = thisExpand + '_' + thisExpandCol;
729                                 // Keep a list of fields that have been expanded
730                                 // so we can create toggle links for them
731                                 if (expanded[thisExpand].indexOf(expColName) == -1) {
732                                     expanded[thisExpand].push(expColName);
733                                 }
734                                 expandObj[expColName] =
735                                     expandObj[thisExpandCol];
736                                 delete expandObj[thisExpandCol];
737                             }
738                         );
739                         $.extend(true, row, expandObj);
740                         delete row[thisExpand];
741                     }
742                 });
743             };
744
745             // Build a de-duped list of all column names
746             var allCols = {};
747             core.map(function(thisCore) {
748                 allCols[thisCore] = 1;
749             });
750
751             // Strip the expand prefix if it exists, we do this for display
752             var stripPrefix = function(value) {
753                 expand.forEach(function(thisExpand) {
754                     var regex = new RegExp(thisExpand + '_', 'g');
755                     value = value.replace(regex, '');
756                 });
757                 return value;
758             };
759
760             // Our 'render' function for borrowerlink
761             var createPatronLink = function(data, type, row) {
762                 var patronLink = '<a title="' + _("View borrower details") + '" ' +
763                     'href="/cgi-bin/koha/members/moremember.pl?' +
764                     'borrowernumber='+row.borrowernumber+'">';
765                 if ( row.patron_firstname ) {
766                     patronLink = patronLink + row.patron_firstname + ' ';
767                 }
768                 patronLink = patronLink + row.patron_surname + '</a>';
769                 return patronLink;
770             };
771
772             // Our 'render' function for the library name
773             var createLibrary = function(data, type, row) {
774                 return row.library.branchname;
775             };
776
777             // Render function for request ID
778             var createRequestId = function(data, type, row) {
779                 return row.id_prefix + row.illrequest_id;
780             };
781
782             // Render function for request status
783             var createStatus = function(data, type, row, meta) {
784                 if (row.status_alias) {
785                     return row.status_alias.lib
786                         ? row.status_alias.lib
787                         : row.status_alias.authorised_value;
788                 } else {
789                     var origData = meta.settings.oInit.originalData;
790                     if (origData.length > 0) {
791                         var status_name = meta.settings.oInit.originalData[0].capabilities[
792                             row.status
793                         ].name;
794                         return getStatusName(status_name);
795                     } else {
796                         return '';
797                     }
798                 }
799             };
800
801             var getStatusName = function(origName) {
802                 switch( origName ) {
803                     case "New request":
804                         return _("New request");
805                     case "Requested":
806                         return _("Requested");
807                     case "Requested from partners":
808                         return _("Requested from partners");
809                     case "Request reverted":
810                         return _("Request reverted");
811                     case "Queued request":
812                         return _("Queued request");
813                     case "Cancellation requested":
814                         return _("Cancellation requested");
815                     case "Completed":
816                         return _("Completed");
817                     case "Delete request":
818                         return _("Delete request");
819                     default:
820                         return origName;
821                 }
822             };
823
824             // Render function for creating a row's action link
825             var createActionLink = function(data, type, row) {
826                 return '<a class="btn btn-default btn-sm" ' +
827                     'href="/cgi-bin/koha/ill/ill-requests.pl?' +
828                     'method=illview&amp;illrequest_id=' +
829                     row.illrequest_id +
830                     '">' + _("Manage request") + '</a>';
831             };
832
833             // Columns that require special treatment
834             var specialCols = {
835                 action: {
836                     name: '',
837                     func: createActionLink
838                 },
839                 borrowername: {
840                     name: _("Patron"),
841                     func: createPatronLink
842                 },
843                 illrequest_id: {
844                     name: _("Request number"),
845                     func: createRequestId
846                 },
847                 status: {
848                     name: _("Status"),
849                     func: createStatus
850                 },
851                 biblio_id: {
852                     name: _("Biblio ID")
853                 },
854                 library: {
855                     name: _("Library"),
856                     func: createLibrary
857                 },
858                 updated: {
859                     name: _("Updated on"),
860                 },
861                 patron_cardnumber: {
862                     name: _("Cardnumber")
863                 }
864             };
865
866             // Display the modal containing request supplier metadata
867             $('#ill-request-display-log').on('click', function(e) {
868                 e.preventDefault();
869                 $('#requestLog').modal({show:true});
870             });
871
872             // Toggle request attributes in Illview
873             $('#toggle_requestattributes').on('click', function(e) {
874                 e.preventDefault();
875                 $('#requestattributes').toggleClass('content_hidden');
876             });
877
878             // Toggle new comment form in Illview
879             $('#toggle_addcomment').on('click', function(e) {
880                 e.preventDefault();
881                 $('#addcomment').toggleClass('content_hidden');
882             });
883
884             // Filter partner list
885             $('#partner_filter').keyup(function() {
886                 var needle = $('#partner_filter').val();
887                 $('#partners > option').each(function() {
888                     var regex = new RegExp(needle, 'i');
889                     if (
890                         needle.length == 0 ||
891                         $(this).is(':selected') ||
892                         $(this).text().match(regex)
893                     ) {
894                         $(this).show();
895                     } else {
896                         $(this).hide();
897                     }
898                 });
899             });
900
901         // Display the modal containing request supplier metadata
902         $('#ill-request-display-metadata').on('click', function(e) {
903             e.preventDefault();
904             $('#dataPreview').modal({show:true});
905         });
906
907             // Get our data from the API and process it prior to passing
908             // it to datatables
909             var ajax = $.ajax(
910                 '/api/v1/illrequests?embed=metadata,patron,capabilities,library,status_alias'
911                 ).done(function() {
912                     var data = JSON.parse(ajax.responseText);
913                     // Make a copy, we'll be removing columns next and need
914                     // to be able to refer to data that has been removed
915                     var dataCopy = $.extend(true, [], data);
916                     // Remove all columns we're not interested in
917                     removeIgnore(dataCopy);
918                     // Expand columns that need it and create an array
919                     // of all column names
920                     $.each(dataCopy, function(k, row) {
921                         expandExpand(row);
922                     });
923
924                     // Assemble an array of column definitions for passing
925                     // to datatables
926                     var colData = [];
927                     Object.keys(allCols).forEach(function(thisCol) {
928                         // Create the base column object
929                         var colObj = {
930                             name: thisCol,
931                             className: thisCol,
932                             defaultContent: ''
933                         };
934                         // We may need to process the data going in this
935                         // column, so do it if necessary
936                         if (
937                             specialCols.hasOwnProperty(thisCol) &&
938                             specialCols[thisCol].hasOwnProperty('func')
939                         ) {
940                             colObj.render = specialCols[thisCol].func;
941                         } else {
942                             colObj.data = thisCol;
943                         }
944                         colData.push(colObj);
945                     });
946
947                     // Initialise the datatable
948                     table = $('#ill-requests').DataTable($.extend(true, {}, dataTablesDefaults, {
949                         'aoColumnDefs': [
950                             { // Last column shouldn't be sortable or searchable
951                                 'aTargets': [ 'actions' ],
952                                 'bSortable': false,
953                                 'bSearchable': false
954                             },
955                             { // Hide the two date columns we use just for sorting
956                                 'aTargets': [ 'placed', 'updated' ],
957                                 'bVisible': false,
958                                 'bSearchable': true
959                             },
960                             { // When sorting 'placed', we want to use the
961                               // unformatted column
962                               'aTargets': [ 'placed_formatted'],
963                               'iDataSort': 7
964                             },
965                             { // When sorting 'updated', we want to use the
966                               // unformatted column
967                               'aTargets': [ 'updated_formatted'],
968                               'iDataSort': 9
969                             },
970                             {
971                               'aTargets': [ 'patron_cardnumber' ],
972                               'bVisible': false,
973                               'bSearchable': true
974                             }
975                         ],
976                         'aaSorting': [[ 9, 'desc' ]], // Default sort, updated descending
977                         'processing': true, // Display a message when manipulating
978                         'sPaginationType': "full_numbers", // Pagination display
979                         'deferRender': true, // Improve performance on big datasets
980                         'data': dataCopy,
981                         'columns': colData,
982                         'originalData': data, // Enable render functions to access
983                                               // our original data
984                         'initComplete': function() {
985
986                             // Prepare any filter elements that need it
987                             for (var el in filterable) {
988                                 if (filterable.hasOwnProperty(el)) {
989                                     if (filterable[el].hasOwnProperty('prep')) {
990                                         filterable[el].prep(dataCopy, data);
991                                     }
992                                     if (filterable[el].hasOwnProperty('listener')) {
993                                         filterable[el].listener();
994                                     }
995                                 }
996                             }
997
998                         }
999                     }));
1000
1001                     // Custom date range filtering
1002                     $.fn.dataTable.ext.search.push(function(settings, data, dataIndex) {
1003                         var placedStart = $('#illfilter_dateplaced_start').datepicker('getDate');
1004                         var placedEnd = $('#illfilter_dateplaced_end').datepicker('getDate');
1005                         var modifiedStart = $('#illfilter_datemodified_start').datepicker('getDate');
1006                         var modifiedEnd = $('#illfilter_datemodified_end').datepicker('getDate');
1007                         var rowPlaced = data[6] ? new Date(data[6]) : null;
1008                         var rowModified = data[8] ? new Date(data[8]) : null;
1009                         var placedPassed = true;
1010                         var modifiedPassed = true;
1011                         if (placedStart && rowPlaced && rowPlaced < placedStart) {
1012                             placedPassed = false
1013                         };
1014                         if (placedEnd && rowPlaced && rowPlaced > placedEnd) {
1015                             placedPassed = false;
1016                         }
1017                         if (modifiedStart && rowModified && rowModified < modifiedStart) {
1018                             modifiedPassed = false
1019                         };
1020                         if (modifiedEnd && rowModified && rowModified > modifiedEnd) {
1021                             modifiedPassed = false;
1022                         }
1023
1024                         return placedPassed && modifiedPassed;
1025
1026                     });
1027
1028                 }
1029             );
1030
1031             var clearSearch = function() {
1032                 table.search('').columns().search('');
1033                 activeFilters = {};
1034                 for (var filter in filterable) {
1035                     if (
1036                         filterable.hasOwnProperty(filter) &&
1037                         filterable[filter].hasOwnProperty('clear')
1038                     ) {
1039                         filterable[filter].clear();
1040                     }
1041                 }
1042                 table.draw();
1043             };
1044
1045             // Apply any search filters, or clear any previous
1046             // ones
1047             $('#illfilter_form').submit(function(event) {
1048                 event.preventDefault();
1049                 table.search('').columns().search('');
1050                 for (var active in activeFilters) {
1051                     if (activeFilters.hasOwnProperty(active)) {
1052                         activeFilters[active]();
1053                     }
1054                 }
1055                 table.draw();
1056             });
1057
1058             // Clear all filters
1059             $('#clear_search').click(function() {
1060                 clearSearch();
1061             });
1062
1063         });
1064     </script>
1065 [% END %]
1066
1067 [% INCLUDE 'intranet-bottom.inc' %]