Bug 11559: Rancor: advanced cataloging interface
[koha.git] / koha-tmpl / intranet-tmpl / lib / koha / cateditor / search.js
1 /**
2  * Copyright 2015 ByWater Solutions
3  *
4  * This file is part of Koha.
5  *
6  * Koha is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Koha is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Koha; if not, see <http://www.gnu.org/licenses>.
18  */
19
20 define( [ 'marc-record' ], function( MARC ) {
21     var _options;
22     var _records = {};
23     var _last;
24
25     var _pqfMapping = {
26         author: '1=1004', // s=al',
27         cn_dewey: '1=13',
28         cn_lc: '1=16',
29         date: '1=30', // r=r',
30         isbn: '1=7',
31         issn: '1=8',
32         lccn: '1=9',
33         local_number: '1=12',
34         music_identifier: '1=51',
35         standard_identifier: '1=1007',
36         subject: '1=21', // s=al',
37         term: '1=1016', // t=l,r s=al',
38         title: '1=4', // s=al',
39     }
40
41     var Search = {
42         Init: function( options ) {
43             _options = options;
44         },
45         JoinTerms: function( terms ) {
46             var q = '';
47
48             $.each( terms, function( i, term ) {
49                 var term = '@attr ' + _pqfMapping[ term[0] ] + ' "' + term[1].replace( '"', '\\"' ) + '"'
50
51                 if ( q ) {
52                     q = '@and ' + q + ' ' + term;
53                 } else {
54                     q = term;
55                 }
56             } );
57
58             return q;
59         },
60         Run: function( servers, q, options ) {
61             options = $.extend( {
62                 offset: 0,
63                 page_size: 20,
64             }, _options, options );
65
66             Search.includedServers = [];
67             _records = {};
68             _last = {
69                 servers: servers,
70                 q: q,
71                 options: options,
72             };
73
74             $.each( servers, function ( id, info ) {
75                 if ( info.checked ) Search.includedServers.push( id );
76             } );
77
78             $.get(
79                 '/cgi-bin/koha/svc/cataloguing/metasearch',
80                 {
81                     q: q,
82                     servers: Search.includedServers.join( ',' ),
83                     offset: options.offset,
84                     page_size: options.page_size,
85                     sort_direction: options.sort_direction,
86                     sort_key: options.sort_key,
87                     resultset: options.resultset,
88                 }
89             )
90                 .done( function( data ) {
91                     _last.options.resultset = data.resultset;
92                     $.each( data.hits, function( undef, hit ) {
93                         var record = new MARC.Record();
94                         record.loadMARCXML( hit.record );
95                         hit.record = record;
96                     } );
97
98                     _options.onresults( data );
99                 } )
100                 .fail( function( error ) {
101                     _options.onerror( error );
102                 } );
103
104             return true;
105         },
106         Fetch: function( options ) {
107             if ( !_last ) return;
108             $.extend( _last.options, options );
109             Search.Run( _last.servers, _last.q, _last.options );
110         }
111     };
112
113     return Search;
114 } );