Bug 17516 - Add CSV export option to item search after displaying output to screen
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / modules / catalogue / itemsearch.tt
1 [% USE CGI %]
2 [% USE JSON.Escape %]
3
4 [% BLOCK form_label %]
5   [% SWITCH label %]
6     [% CASE 'barcode' %]<span>Barcode</span>
7     [% CASE 'itemcallnumber' %]<span>Call number</span>
8     [% CASE 'stocknumber' %]<span>Inventory number</span>
9     [% CASE 'title' %]<span>Title</span>
10     [% CASE 'author' %]<span>Author</span>
11     [% CASE 'publishercode' %]<span>Publisher</span>
12     [% CASE 'publicationyear' %]<span>Publication date</span>
13     [% CASE 'collectiontitle' %]<span>Collection</span>
14     [% CASE 'isbn' %]<span>ISBN</span>
15     [% CASE 'issn' %]<span>ISSN</span>
16     [% CASE 'homebranch' %]<span>Home library</span>
17     [% CASE 'holdingbranch' %]<span>Current location</span>
18     [% CASE 'All libraries' %]<span>All libraries</span>
19     [% CASE 'location' %]<span>Shelving location</span>
20     [% CASE 'All locations' %]<span>All locations</span>
21     [% CASE 'itype' %]<span>Item type</span>
22     [% CASE 'All item types' %]<span>All item types</span>
23     [% CASE 'ccode' %]<span>Collection code</span>
24     [% CASE 'All collection codes' %]<span>All collection codes</span>
25     [% CASE 'notforloan' %]<span>Status</span>
26     [% CASE 'All statuses' %]<span>All statuses</span>
27     [% CASE 'damaged' %]<span>Damaged</span>
28     [% CASE 'itemlost' %]<span>Lost</span>
29   [% END %]
30 [% END %]
31
32 [% BLOCK form_field_select %]
33   <div class="form-field form-field-select">
34     <label class="form-field-label" for="[% name %]">[% INCLUDE form_label label=name %]</label>
35     <select id="[% name %]_op" name="[% name %]_op">
36       <option value="=">is</option>
37       [% IF CGI.param(name _ '_op') == '!=' %]
38         <option value="!=" selected="selected">is not</option>
39       [% ELSE %]
40         <option value="!=" >is not</option>
41       [% END %]
42     </select>
43     [% values = CGI.param(name) %]
44     <select id="[% name %]" name="[% name %]" multiple="multiple" size="[% options.size < 4 ? options.size + 1 : 4 %]">
45       [% IF (values == '') %]
46         <option value="" selected="selected">
47       [% ELSE %]
48         <option value="">
49       [% END %]
50         [% IF (empty_option) %][% INCLUDE form_label label=empty_option %][% ELSE %]<span>All</span>[% END %]
51       </option>
52       [% FOREACH option IN options %]
53         [% IF values != '' && values.grep(option.value).size %]
54           <option value="[% option.value %]" selected="selected">[% option.label %]</option>
55         [% ELSE %]
56           <option value="[% option.value %]">[% option.label %]</option>
57         [% END %]
58       [% END %]
59     </select>
60   </div>
61 [% END %]
62
63 [% BLOCK form_field_select_option %]
64   [% IF params.f == value %]
65     <option value="[% value %]" selected="selected">[% INCLUDE form_label label=value %]</option>
66   [% ELSE %]
67     <option value="[% value %]">[% INCLUDE form_label label=value %]</option>
68   [% END %]
69 [% END %]
70
71 [% BLOCK form_field_select_text %]
72   <div class="form-field form-field-select-text">
73     [% IF params.exists('c') %]
74       <select name="c" class="form-field-conjunction">
75         <option value="and">AND</option>
76         [% IF params.c == 'or' %]
77           <option value="or" selected="selected">OR</option>
78         [% ELSE %]
79           <option value="or">OR</option>
80         [% END %]
81       </select>
82     [% ELSE %]
83       <select name="c" class="form-field-conjunction" disabled="disabled">
84         <option value="and">AND</option>
85         <option value="or">OR</option>
86       </select>
87     [% END %]
88     <select name="f" class="form-field-column">
89       [% INCLUDE form_field_select_option value='barcode' %]
90       [% INCLUDE form_field_select_option value='itemcallnumber' %]
91       [% INCLUDE form_field_select_option value='stocknumber' %]
92       [% INCLUDE form_field_select_option value='title' %]
93       [% INCLUDE form_field_select_option value='author' %]
94       [% INCLUDE form_field_select_option value='publishercode' %]
95       [% INCLUDE form_field_select_option value='publicationyear' %]
96       [% INCLUDE form_field_select_option value='collectiontitle' %]
97       [% INCLUDE form_field_select_option value='isbn' %]
98       [% INCLUDE form_field_select_option value='issn' %]
99       [% IF items_search_fields.size %]
100         <optgroup label="Custom search fields">
101           [% FOREACH field IN items_search_fields %]
102             [% marcfield = field.tagfield %]
103             [% IF field.tagsubfield %]
104               [% marcfield = marcfield _ '$' _ field.tagsubfield %]
105             [% END %]
106             [% IF params.f == "marc:$marcfield" %]
107               <option value="marc:[% marcfield %]" data-authorised-values-category="[% field.authorised_values_category %]" selected="selected">[% field.label %] ([% marcfield %])</option>
108             [% ELSE %]
109               <option value="marc:[% marcfield %]" data-authorised-values-category="[% field.authorised_values_category %]">[% field.label %] ([% marcfield %])</option>
110             [% END %]
111           [% END %]
112         </optgroup>
113       [% END %]
114     </select>
115     <input type="text" name="q" class="form-field-value" value="[% params.q %]" />
116     <input type="hidden" name="op" value="like" />
117   </div>
118 [% END %]
119
120 [% BLOCK form_field_select_text_block %]
121   [% c = CGI.param('c').list %]
122   [% f = CGI.param('f').list %]
123   [% q = CGI.param('q').list %]
124   [% op = CGI.param('op').list %]
125   [% IF q.size %]
126     [% size = q.size - 1 %]
127     [% FOREACH i IN [0 .. size] %]
128       [%
129         params = {
130           f => f.$i
131           q = q.$i
132           op = op.$i
133         }
134       %]
135       [% IF i > 0 %]
136         [% j = i - 1 %]
137         [% params.c = c.$j %]
138       [% END %]
139       [% INCLUDE form_field_select_text params=params %]
140     [% END %]
141   [% ELSE %]
142     [% INCLUDE form_field_select_text %]
143   [% END %]
144 [% END %]
145
146 [% BLOCK form_field_radio_yes_no %]
147   <div class="form-field">
148     <label class="form-field-label">[% INCLUDE form_label label=name %]:</label>
149     <input type="radio" name="[% name %]" id="[% name %]_indifferent" value="" checked="checked"/>
150     <label for="[% name %]_indifferent">Ignore</label>
151     <input type="radio" name="[% name %]" id="[% name %]_yes" value="yes" />
152     <label for="[% name %]_yes">Yes</label>
153     <input type="radio" name="[% name %]" id="[% name %]_no" value="no" />
154     <label for="[% name %]_no">No</label>
155   </div>
156 [% END %]
157
158 [%# Page starts here %]
159
160 [% INCLUDE 'doc-head-open.inc' %]
161   <title>Koha &rsaquo; Catalog &rsaquo; Item search</title>
162   [% INCLUDE 'doc-head-close.inc' %]
163   <link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/datatables.css" />
164   [% INCLUDE 'datatables.inc' %]
165   <script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.dataTables.columnFilter.js"></script>
166   <script type="text/javascript" src="[% interface %]/lib/jquery/plugins/jquery.fixFloat.js"></script>
167   <link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/itemsearchform.css" />
168   <script type="text/javascript">
169     //<![CDATA[
170     var authorised_values = [% authorised_values_json %];
171
172     function loadAuthorisedValuesSelect(select) {
173       var selected = select.find('option:selected');
174       var category = selected.data('authorised-values-category');
175       var form_field_value = select.siblings('.form-field-value');
176       if (category && category in authorised_values) {
177         var values = authorised_values[category];
178         var html = '<select name="q" class="form-field-value">\n';
179         for (i in values) {
180           var value = values[i];
181           html += '<option value="' + value.authorised_value + '">' + value.lib + '</option>\n';
182         }
183         html += '</select>\n';
184         var new_form_field_value = $(html);
185         new_form_field_value.val(form_field_value.val());
186         form_field_value.replaceWith(new_form_field_value);
187       } else {
188         if (form_field_value.prop('tagName').toLowerCase() == 'select') {
189           html = '<input name="q" type="text" class="form-field-value" />';
190           var new_form_field_value = $(html);
191           form_field_value.replaceWith(new_form_field_value);
192         }
193       }
194     }
195
196     function addNewField() {
197       var form_field = $('div.form-field-select-text').last();
198       var copy = form_field.clone(true);
199       copy.find('input,select').not('[type="hidden"]').each(function() {
200         $(this).val('');
201       });
202       copy.find('.form-field-conjunction').prop('disabled', false);
203       form_field.after(copy);
204       copy.find('select.form-field-column').change();
205     }
206
207     function submitForm($form) {
208       var tr = ''
209         + '    <tr>'
210         + '      <th>' + _("Title") + '</th>'
211         + '      <th>' + _("Publication date") + '</th>'
212         + '      <th>' + _("Publisher") + '</th>'
213         + '      <th>' + _("Collection") + '</th>'
214         + '      <th>' + _("Barcode") + '</th>'
215         + '      <th>' + _("Call number") + '</th>'
216         + '      <th>' + _("Home library") + '</th>'
217         + '      <th>' + _("Current location") + '</th>'
218         + '      <th>' + _("Shelving location") + '</th>'
219         + '      <th>' + _("Inventory number") + '</th>'
220         + '      <th>' + _("Status") + '</th>'
221         + '      <th>' + _("Checkouts") + '</th>'
222         + '      <th></th>'
223         + '    </tr>'
224       var table = ''
225         + '<table id="results">'
226         + '  <thead>' + tr + tr + '</thead>'
227         + '  <tbody></tbody>'
228         + '</table>';
229       var results_heading = "<h1>" + _("Item search results") + "</h1>";
230       results_heading += "<p><a href=\"/cgi-bin/koha/catalogue/search.pl\">" + _("Go to advanced search") + "</a></p>";
231       results_heading += "<p><a class=\"editsearchlink\" href=\"#\">" + _("Edit search") + "</a>";
232       results_heading += " | <a class=\"resultstocsv\" href=\"#\">" + _("Output results to csv") + "</a></p>";
233       $('#results-wrapper').empty().html(results_heading + table);
234
235       var params = [];
236       $form.find('select').not(':disabled').find('option:selected').each(function () {
237         var name = $(this).parent('select').attr('name');
238         var value = $(this).val();
239         params.push({ 'name': name, 'value': value });
240       });
241       $form.find('input[type="text"],input[type="hidden"]').not(':disabled').each(function () {
242         params.push({ 'name': $(this).attr('name'), 'value': $(this).val() });
243       });
244       $form.find('input[type="radio"]:checked').each(function() {
245         params.push({ 'name': $(this).attr('name'), 'value': $(this).val() });
246       });
247
248       $('#results').dataTable($.extend(true, {}, dataTablesDefaults, {
249         'bDestroy': true,
250         'bServerSide': true,
251         'bProcessing': true,
252         'sAjaxSource': '/cgi-bin/koha/catalogue/itemsearch.pl',
253         'fnServerData': function(sSource, aoData, fnCallback) {
254           aoData.push( { 'name': 'format', 'value': 'json' } );
255           for (i in params) {
256             aoData.push(params[i]);
257           }
258           $.ajax({
259               'dataType': 'json',
260               'type': 'POST',
261               'url': sSource,
262               'data': aoData,
263               'success': function(json){
264                   fnCallback(json);
265               }
266           });
267         },
268         'sDom': '<"top pager"ilp>t<"bottom pager"ip>r',
269         'aoColumns': [
270           { 'sName': 'title' },
271           { 'sName': 'publicationyear' },
272           { 'sName': 'publishercode' },
273           { 'sName': 'collectiontitle' },
274           { 'sName': 'barcode' },
275           { 'sName': 'itemcallnumber' },
276           { 'sName': 'homebranch' },
277           { 'sName': 'holdingbranch' },
278           { 'sName': 'location' },
279           { 'sName': 'stocknumber' },
280           { 'sName': 'notforloan' },
281           { 'sName': 'issues' },
282           { 'sName': 'checkbox', 'bSortable': false }
283         ],
284         "sPaginationType": "full_numbers"
285       })).columnFilter({
286         'sPlaceHolder': 'head:after',
287         'aoColumns': [
288           { 'type': 'text' },
289           { 'type': 'text' },
290           { 'type': 'text' },
291           { 'type': 'text' },
292           { 'type': 'text' },
293           { 'type': 'text' },
294           { 'type': 'select', 'values': [% branches.json %] },
295           { 'type': 'select', 'values': [% branches.json %] },
296           { 'type': 'select', 'values': [% locations.json %] },
297           { 'type': 'text' },
298           { 'type': 'select', 'values': [% notforloans.json %] },
299           { 'type': 'text' },
300           null
301         ]
302       });
303     }
304
305     function hideForm() {
306       $("#item-search-block").hide();
307       $('.editsearchlink').show();
308     }
309
310     $(document).ready(function () {
311       $('#toolbar').fixFloat();
312       // Add the "New field" link.
313       var form_field = $('div.form-field-select-text').last()
314       var NEW_FIELD = _("New field");
315       var button_field_new = $('<a href="#" class="button-field-new" title="Add a new field">' + NEW_FIELD + '</a>');
316       button_field_new.click(function() {
317         addNewField();
318         return false;
319       });
320       form_field.after(button_field_new);
321
322       // If a field is linked to an authorised values list, display the list.
323       $('div.form-field-select-text select').change(function() {
324         loadAuthorisedValuesSelect($(this));
325       }).change();
326
327       // Prevent user to select the 'All ...' option with other options.
328       $('div.form-field-select').each(function() {
329         $(this).find('select').filter(':last').change(function() {
330           values = $(this).val();
331           if (values.length > 1) {
332             var idx = $.inArray('', values);
333             if (idx != -1) {
334               values.splice(idx, 1);
335               $(this).val(values);
336             }
337           }
338         });
339       });
340
341       $('#itemsearchform').submit(function() {
342         var searchform = $(this);
343         var format = searchform.find('input[name="format"]:checked').val();
344         if (format == 'html') {
345           submitForm(searchform);
346           hideForm();
347           return false;
348         }
349       });
350
351       $("body").on("click",".editsearchlink",function(e) {
352         e.preventDefault();
353         $('#item-search-block').show();
354         $(this).hide();
355         return false;
356       });
357
358       $("body").on("click",".resultstocsv",function(e) {
359         e.preventDefault();
360         $('#format-csv').prop("checked",true);
361         $('#itemsearchform').submit();
362         hideForm();
363         $('#format-html').prop("checked",true);
364         return false;
365       });
366     });
367     //]]>
368   </script>
369 </head>
370 <body id="catalog_itemsearch" class="catalog">
371   [% INCLUDE 'header.inc' %]
372   [% INCLUDE 'home-search.inc' %]
373   <div id="breadcrumbs">
374     <a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/catalogue/search.pl">Catalog</a> &rsaquo; Item search
375   </div>
376
377   <div id="doc" class="yui-t7">
378     <div id="item-search-block">
379       <h1>Item search</h1>
380       <p><a href="/cgi-bin/koha/catalogue/search.pl">Go to advanced search</a></p>
381       <form action="/cgi-bin/koha/catalogue/itemsearch.pl" method="get" id="itemsearchform">
382           <div id="toolbar" class="btn-toolbar">
383               <fieldset class="action">
384                   <div class="btn-group">
385                       <button class="btn btn-small"><i class="fa fa-search"></i> Search</button>
386                   </div>
387               </fieldset>
388           </div>
389           <fieldset>
390             [% INCLUDE form_field_select
391               name="homebranch"
392               options = branches
393               empty_option = "All libraries"
394             %]
395             [% INCLUDE form_field_select
396               name="holdingbranch"
397               options = branches
398               empty_option = "All libraries"
399             %]
400             [% INCLUDE form_field_select
401               name="location"
402               options = locations
403               empty_option = "All locations"
404             %]
405           </fieldset>
406           <fieldset>
407             [% INCLUDE form_field_select
408               name="itype"
409               options = itemtypes
410               empty_option = "All item types"
411             %]
412             [% INCLUDE form_field_select
413               name="ccode"
414               options = ccodes
415               empty_option = "All collection codes"
416             %]
417             [% INCLUDE form_field_select
418               name="notforloan"
419               options = notforloans
420               empty_option = "All statuses"
421             %]
422           </fieldset>
423           <fieldset>
424             [% INCLUDE form_field_select_text_block %]
425             <p class="hint">You can use the following wildcard characters: % _</p>
426             <p class="hint">% matches any number of characters</p>
427             <p class="hint">_ matches only a single character</p>
428           </fieldset>
429           <fieldset>
430             <div class="form-field">
431               <label class="form-field-label" for="itemcallnumber_from">From call number:</label>
432               [% value = CGI.param('itemcallnumber_from') %]
433               <input type="text" id="itemcallnumber_from" name="itemcallnumber_from" value="[% value %]" />
434               <span class="hint">(inclusive)</span>
435             </div>
436             <div class="form-field">
437               [% value = CGI.param('itemcallnumber_to') %]
438               <label class="form-field-label" for="itemcallnumber_to">To call number:</label>
439               <input type="text" id="itemcallnumber_to" name="itemcallnumber_to" value="[% value %]" />
440               <span class="hint">(inclusive)</span>
441             </div>
442             [% INCLUDE form_field_radio_yes_no name="damaged" %]
443             [% INCLUDE form_field_radio_yes_no name="itemlost" %]
444             <div class="form-field">
445               <label class="form-field-label" for="issues_op">Checkout count:</label>
446               <select id="issues_op" name="issues_op">
447                 <option value=">">&gt;</option>
448                 <option value="<">&lt;</option>
449                 <option value="=">=</option>
450                 <option value="!=">!=</option>
451               </select>
452               <input type="text" name="issues" />
453             </div>
454             <div class="form-field">
455               <label class="form-field-label" for="datelastborrowed_op">Last checkout date:</label>
456               <select id="datelastborrowed_op" name="datelastborrowed_op">
457                 <option value=">">After</option>
458                 <option value="<">Before</option>
459                 <option value="=">On</option>
460               </select>
461               <input type="text" name="datelastborrowed" />
462               <span class="hint">ISO Format (YYYY-MM-DD)</span>
463             </div>
464           </fieldset>
465           <fieldset>
466             <div class="form-field-radio">
467               <label>Output:</label>
468               <input type="radio" id="format-html" name="format" value="html" checked="checked" /> <label for="format-html">Screen</label>
469               <input type="radio" id="format-csv" name="format" value="csv" /> <label for="format-csv">CSV</label>
470             </div>
471           </fieldset>
472       </form>
473
474       <p><a id="editsearchlink" href="#" style="display:none">Edit search</a></p>
475     </div>
476   </div>
477   <div id="doc3" class="yui-t7">
478       <div id="results-wrapper">
479         [% IF search_done %]
480           [% IF total_rows > 0 %]
481             <p>Found [% total_rows %] results.</p>
482           [% ELSE %]
483             <p>No results found.</p>
484           [% END %]
485
486           [% IF results %]
487             [% INCLUDE 'catalogue/itemsearch_items.inc' items = results %]
488           [% END %]
489
490           <div class="pages">
491             [% pagination_bar %]
492           </div>
493
494         [% END %]
495       </div>
496
497     [% INCLUDE 'intranet-bottom.inc' %]