search catalog using barcode
[koha.git] / ffzg / rfid / koha-rfid.js
1 /*
2  * RFID support for Koha
3  *
4  * Writtern by Dobrica Pavlinusic <dpavlin@rot13.org> under GPL v2 or later
5  *
6  * This provides example how to intergrate JSONP interface from
7  *
8  * scripts/RFID-JSONP-server.pl
9  *
10  * to provide overlay for tags in range and emulate form fill for Koha Library System
11  * which allows check-in and checkout-operations without touching html interface
12  *
13  * You will have to inject remote javascript in Koha intranetuserjs using:
14
15 // inject JavaScript RFID support
16 $.getScript('http://localhost:9000/examples/koha-rfid.js');
17
18  */
19
20 function barcode_on_screen(barcode) {
21         var found = 0;
22         $('table tr td a:contains(130)').each( function(i,o) {
23                 var possible = $(o).text();
24                 if ( possible == barcode ) found++;
25         })
26         return found;
27 }
28
29 var rfid_refresh = 1500; // ms
30
31 function rfid_secure_json(t,val, success) {
32         if ( t.security.toUpperCase() == val.toUpperCase() ) return;
33         rfid_refresh = 0; // disable rfid pull until secure call returns
34         console.log('rfid_secure_json', t, val);
35         $.getJSON( 'http://localhost:9000/secure.js?' + t.sid + '=' + val + ';callback=?', success );
36 }
37
38 function rfid_secure_check(t,val) {
39         if ( barcode_on_screen(t.content) ) {
40                 rfid_secure_json(t, val);
41         }
42 }
43
44
45 var rfid_reset_field = false;
46
47 function rfid_scan(data,textStatus) {
48
49         console.debug( 'rfid_scan', data, textStatus );
50
51         var span = $('span#rfid');
52
53         if ( span.size() == 0 ) // insert last in language bar on bottom
54                 span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
55
56         if ( span.size() == 0 ) // or before login on top
57                 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
58
59         span = $('span#rfid');
60
61
62         if ( data.tags ) {
63                 if ( data.tags.length === 1 ) {
64                         var t = data.tags[0];
65 //                      if ( span.text() != t.content ) {
66                         if ( 1 ) { // force update of security
67
68                                 var script_name = document.location.pathname.split(/\//).pop();
69                                 var tab_active  = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
70                                 console.debug('tab_active', tab_active);
71                                 var circulation = script_name == 'circulation.pl';
72                                 var returns     = script_name == 'returns.pl' || tab_active == 'checkin_search';
73
74                                 if ( t.content.length == 0 ) { // empty tag
75
76                                         span.text( t.sid + ' empty' ).css('color', 'red' );
77
78                                 } else if ( t.content.substr(0,3) == '130' ) { // books
79
80                                         var color = 'blue';
81                                         if ( t.security.toUpperCase() == 'DA' ) color = 'red';
82                                         if ( t.security.toUpperCase() == 'D7' ) color = 'green';
83                                         span.text( t.content ).css('color', color);
84
85
86                                         if ( tab_active == 'catalog_search' ) {
87                                                 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
88                                                         $('input[name=q]').val( 'bc:' + t.content ).closest('form').submit();
89                                                 }
90                                         }
91
92                                         if ( ! barcode_on_screen( t.content ) || returns ) {
93                                                 rfid_reset_field = 'barcode';
94
95                                                 // return must be first to catch change of tab to check-in
96                                                 var afi_secure    = returns ? 'DA' : 'D7';
97                                                 var form_selector = returns ? 'first' : 'last';
98                                                 if ( returns || circulation ) {
99                                                         var i = $('input[name=barcode]:'+form_selector);
100                                                         if ( i.val() != t.content )  {
101                                                                 rfid_secure_json( t, afi_secure, function(data) {
102                                                                         console.log('secure', afi_secure, data);
103                                                                         i.val( t.content ).closest('form').submit();
104                                                                 });
105                                                         }
106                                                 } else {
107                                                         console.error('not in circulation or returns');
108                                                 }
109                                         }
110
111                                 } else {
112                                         span.text( t.content ).css('color', 'blue' );
113
114                                         if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
115                                                 console.debug('not submitting', t.contains);
116                                         } else {
117                                                 rfid_refresh = 0; // stop rfid scan while submitting form
118                                                 rfid_reset_field = 'findborrower';
119                                                 $('input[name=findborrower]').val( t.content )
120                                                         .parent().submit();
121                                         }
122                                 }
123                         }
124                 } else {
125                         var error = data.tags.length + ' tags near reader: ';
126                         $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
127                         span.text( error ).css( 'color', 'red' );
128                 }
129
130         } else {
131                 span.text( 'no tags in range' ).css('color','gray');
132                 if ( rfid_reset_field ) {
133                         $('input[name='+rfid_reset_field+']').val( '' );
134                         rfid_reset_field = false;
135                 }
136         }
137
138         if (rfid_refresh > 1) {
139                 window.setTimeout( function() {
140                         $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
141                 }, rfid_refresh );
142         } else {
143                 console.debug('rfid_refresh disabled',rfid_refresh);
144         }
145 }
146
147 $(document).ready( function() {
148         $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
149 });