0c1daae17ffedb00eba275dd8f33c565b97dca01
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / includes / js-patron-format.inc
1 <script>
2     (function() {
3         /**
4         * Format the patron response from a Koha RESTful API request.
5         * @param  {Object}  patron  The patron json object as returned from the Koha RESTful API
6         * @param  {Object}  config  A configuration object
7         *                           Valid keys are: `invert_name`, `display_cardnumber` and `url`
8         * @return {string}          The formatted HTML string
9         */
10         window.$patron_to_html = function( patron, config ) {
11
12             if ( patron === undefined ) {
13                 return ''; // empty string for no patron
14             }
15
16             var title = null;
17             if ( patron.title != null && patron.title != '' ) {
18                 title = '<span class="patron-title">' + escape_str(patron.title) + '</span>';
19             }
20
21             var name;
22             var firstname = escape_str(patron.firstname);
23             var surname   = escape_str(patron.surname);
24
25             if ( patron.other_name != null && patron.other_name != '' ) {
26                 firstname += ' (' + escape_str(patron.other_name) + ')';
27             }
28             if ( config && config.invert_name ) {
29                 name = surname + ( firstname ? ', ' + firstname : '' );
30             }
31             else {
32                 name = firstname + ' ' + surname;
33             }
34
35             if ( config && config.display_cardnumber ) {
36                 name = name + ' (' + escape_str(patron.cardnumber)  + ')';
37             }
38
39             if (config && config.url) {
40                 if ( config.url === 'circulation_reserves' ) {
41                     name = '<a href="/cgi-bin/koha/circ/circulation.pl?borrowernumber='+ encodeURIComponent(patron.patron_id) +'#reserves">' + name + '</a>';
42                 }
43                 else {
44                     name = '<a href="/cgi-bin/koha/members/moremember.pl?borrowernumber='+ encodeURIComponent(patron.patron_id) +'">' + name + '</a>';
45                 }
46             }
47
48             return name;
49         };
50     })();
51 </script>