dd9b470ef380647ecbe3669e594aeb01f3832cbf
[Biblio-RFID.git] / examples / 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
14 function barcode_on_screen(barcode) {
15         var found = 0;
16         $('table tr td a:contains(130)').each( function(i,o) {
17                 var possible = $(o).text();
18 console.debug(i,o,possible, barcode);
19                 if ( possible == barcode ) found++;
20         })
21         return found;
22 }
23
24 function rfid_secure(barcode,sid,val) {
25         console.debug('rfid_secure', barcode, sid, val);
26         if ( barcode_on_screen(barcode) ) 
27                 $.getJSON( 'http://localhost:9000/secure.js?' + sid + '=' + val + ';callback=?' )
28 }
29
30 var rfid_reset_field = false;
31
32 function rfid_scan(data,textStatus) {
33 //      console.debug( 'rfid_scan', data, textStatus );
34
35         var span = $('span#rfid');
36         if ( span.size() == 0 ) {
37                 $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
38                 span = $('span#rfid');
39         }
40
41         if ( data.tags ) {
42                 if ( data.tags.length === 1 ) {
43                         var t = data.tags[0];
44 //                      if ( span.text() != t.content ) {
45                         if ( 1 ) { // force update of security
46
47                                 var url = document.location.toString();
48                                 var circulation = url.substr(-14,14) == 'circulation.pl';
49                                 var returns = url.substr(-10,10) == 'returns.pl';
50
51                                 if ( t.content.substr(0,3) == '130' ) {
52
53                                         if ( circulation )
54                                                  rfid_secure( t.content, t.sid, 'D7' );
55                                         if ( returns )
56                                                  rfid_secure( t.content, t.sid, 'DA' );
57
58                                         var color = 'blue';
59                                         if ( t.security.toUpperCase() == 'DA' ) color = 'red';
60                                         if ( t.security.toUpperCase() == 'D7' ) color = 'green';
61                                         span.text( t.content ).css('color', color);
62
63                                         if ( ! barcode_on_screen( t.content ) ) {
64                                                 rfid_reset_field = 'barcode';
65                                                 var i = $('input[name=barcode]:last');
66                                                 if ( i.val() != t.content ) 
67                                                         i.val( t.content )
68                                                         .closest('form').submit();
69                                         }
70
71                                 } else {
72                                         span.text( t.content ).css('color', 'blue' );
73
74                                         if ( url.substr(-14,14) != 'circulation.pl' || $('form[name=mainform]').size() == 0 ) {
75                                                 rfid_reset_field = 'findborrower';
76                                                 $('input[name=findborrower]').val( t.content )
77                                                         .parent().submit();
78                                         }
79                                 }
80                         }
81                 } else {
82                         var error = data.tags.length + ' tags near reader: ';
83                         $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
84                         span.text( error ).css( 'color', 'red' );
85                 }
86
87         } else {
88                 span.text( 'no tags in range' ).css('color','gray');
89                 if ( rfid_reset_field ) {
90                         $('input[name='+rfid_reset_field+']').val( '' );
91                         rfid_reset_field = false;
92                 }
93         }
94
95         window.setTimeout( function() {
96                 $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
97         }, 1000 ); // 1000ms
98 }
99
100 $(document).ready( function() {
101         $.getJSON("http://localhost:9000/scan?callback=?", rfid_scan);
102 });