RFID: add icons to show security status
[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( '///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 var rfid_current_sid = false;
47 var rfid_blank_sid   = false;
48
49 function rfid_scan(data,textStatus) {
50
51         $.cookie('rfid', 'localhost'); // set cookie
52
53         console.debug( 'rfid_scan', data, textStatus, $.cookie('rfid') );
54         rfid_current_sid = false;
55         rfid_blank_sid = false;
56
57         var span = $('span#rfid');
58
59         if ( span.size() == 0 ) {
60                 // insert last in language bar on bottom
61 //              span = $('ul#i18nMenu').append('<li><span id=rfid>RFID reader found<span>');
62
63                 // alternative pop-up version
64                 span = $('#breadcrumbs').append('<div id="rfid_popup" style="position: fixed; bottom: 3em; right: 1em; background: #fff; border: 3px solid #ff0; padding: 1em; opacity: 0.7; z-index: 10;"><label for="rfid_active"><input type=checkbox id="rfid_active"> local_ip <span id="rfid">RFID reader</span></label></div>');
65                 $('input#rfid_active').attr('checked',true);
66                 $('input#rfid_active').click(scan_tags);
67         }
68
69
70         if ( span.size() == 0 ) // or before login on top
71                 span = $('div#login').prepend('<span id=rfid>RFID reader found</span>');
72
73         span = $('span#rfid');
74
75
76         if ( data.tags ) {
77                 if ( data.tags.length === 1 ) {
78                         var t = data.tags[0];
79                         rfid_current_sid = t.sid;
80
81 //                      if ( span.text() != t.content ) {
82                         if ( 1 ) { // force update of security
83
84                                 var script_name = document.location.pathname.split(/\//).pop();
85 //                              var tab_active  = $("#header_search .ui-tabs-panel:not(.ui-tabs-hide)").prop('id');
86                                 var tab_active  = $("#header_search li[aria-selected=true]").attr('aria-controls');
87                                 console.debug('tab_active', tab_active);
88                                 var circulation = script_name == 'circulation.pl' || tab_active == 'circ_search' ;
89                                 var returns     = script_name == 'returns.pl' || tab_active == 'checkin_search';
90
91                                 if ( t.content.length == 0 || t.content == 'UUUUUUUUUUUUUUUU' ) { // blank tag (3M is UUU....)
92
93                                         rfid_blank_sid = t.sid;
94                                         span.text( t.sid + ' blank' ).css('color', 'red' );
95
96                                 } else if ( t.content.substr(0,3) == '130' && t.reader == '3M810' ) { // books on 3M reader
97
98                                         var color = 'blue';
99                                         var icon  = '?';
100                                         if ( t.security.toUpperCase() == 'DA' ) { color = 'red'; icon = '&timesb;' }
101                                         if ( t.security.toUpperCase() == 'D7' ) { color = 'green'; icon = '&rarr;' }
102                                         span.html( t.content + '&nbsp;' + icon ).css('color', color);
103
104
105                                         if ( tab_active == 'catalog_search' && script_name != 'moredetail.pl' ) {
106                                                 if ( $('span.term:contains(bc:'+t.content+')').length == 0 ) {
107                                                         $('input[name=q]').val( 'bc:' + t.content ).closest('form').submit();
108                                                 }
109                                         }
110
111                                         if ( ! barcode_on_screen( t.content ) || returns ) {
112                                                 rfid_reset_field = 'barcode';
113
114                                                 // return must be first to catch change of tab to check-in
115                                                 var afi_secure    = returns ? 'DA' : 'D7';
116                                                 var form_selector = returns ? 'first' : 'last';
117                                                 if ( returns || circulation ) {
118                                                         var i = $('input[name=barcode]:'+form_selector);
119                                                         if ( i.val() != t.content )  {
120                                                                 rfid_secure_json( t, afi_secure, function(data) {
121                                                                         console.log('secure', afi_secure, data);
122                                                                         i.val( t.content ).closest('form').submit();
123                                                                 });
124                                                         }
125                                                 } else {
126                                                         console.error('not in circulation or returns');
127                                                 }
128                                         }
129
130                                 } else if ( t.content.substr(0,3) == '130' ) {
131
132                                         span.text( 'Please put book on 3M reader!' ).css( 'color', 'red' );
133
134                                 } else {
135                                         span.html( t.content + '&nbsp;&sstarf;' ).css('color', 'blue' );
136
137                                         if ( $('.patroninfo:contains('+t.content+')').length == 1 ) {
138                                                 console.debug('not submitting', t.contains);
139                                         } else {
140                                                 rfid_refresh = 0; // stop rfid scan while submitting form
141                                                 rfid_reset_field = 'findborrower';
142                                                 $('input[name=findborrower]').val( t.content )
143                                                         .parent().submit();
144                                         }
145                                 }
146                         }
147                 } else {
148                         var error = data.tags.length + ' tags near reader: ';
149                         $.each( data.tags, function(i,tag) { error += tag.content + ' '; } );
150                         span.text( error ).css( 'color', 'red' );
151                 }
152
153         } else {
154                 span.text( 'no tags in range' ).css('color','gray');
155                 if ( rfid_reset_field ) {
156                         $('input[name='+rfid_reset_field+']').val( '' );
157                         rfid_reset_field = false;
158                 }
159         }
160
161         if (rfid_refresh > 1 && $('input#rfid_active').attr('checked') ) {
162                 window.setTimeout( function() {
163                         $('#rfid_popup').css('border','3px solid #ff0');
164                         $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
165                 }, rfid_refresh );
166         } else {
167                 console.debug('rfid_refresh disabled',rfid_refresh);
168         }
169
170         $('#rfid_popup').css('border','3px solid #fff');
171 }
172
173 function scan_tags() {
174         console.info('scan_tags');
175         $.getJSON("///localhost:9000/scan?callback=?", rfid_scan);
176 }
177
178 function activate_scan_tags() {
179         var active = $('input#rfid_active').attr('checked');
180         if ( ! active ) {
181                 $('input#rfid_active').attr('checked',true);
182                 scan_tags();
183         }
184 }
185
186 $(document).ready( function() {
187         console.log('rfid_active', $('input#rfid_active').attr('checked') );
188
189         scan_tags();
190
191         // circulation keyboard shortcuts (FFZG specific!)
192         shortcut.add('Alt+r', function() { activate_scan_tags(); } );
193         shortcut.add('Alt+z', function() { activate_scan_tags(); } );
194         shortcut.add('Alt+k', function() { $('input#rfid_active').attr('checked',false) } );
195
196         // intranet cataloging
197         shortcut.add('F4', function() {
198                 // extract barcode from window title
199                 var barcode = document.title.split(/\(barcode\s+#|\)/)[1];
200                 if ( barcode ) {
201                         if ( ! rfid_blank_sid && rfid_current_sid && confirm('Reprogram this tag to barcode '+barcode) ) {
202                                 rfid_blank_sid = rfid_current_sid;
203                         }
204
205                         console.debug('program barcode', barcode, 'to', rfid_blank_sid);
206                         $.getJSON( '///localhost:9000/program?' + rfid_blank_sid + '=' + barcode + ';callback=?', function(data) {
207                                 console.info('programmed', rfid_blank_sid, barcode, data);
208                         });
209                 } else {
210                         console.error('no barcode in window title');
211                 }
212         });
213
214 });