Bug 32030: ERM - Refactoring
[srvgit] / 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.middle_name != null && patron.middle_name != '' ) {
26                 firstname += ' ' + escape_str(patron.middle_name);
27             }
28
29             if ( patron.other_name != null && patron.other_name != '' ) {
30                 firstname += ' (' + escape_str(patron.other_name) + ')';
31             }
32             if ( config && config.invert_name ) {
33                 name = surname + ( firstname ? ', ' + firstname : '' );
34             }
35             else {
36                 name = firstname + ' ' + surname;
37             }
38
39             if ( config && config.display_cardnumber ) {
40                 name = name + ' (' + escape_str(patron.cardnumber)  + ')';
41             }
42
43             if (config && config.url) {
44                 if ( config.url === 'circulation_reserves' ) {
45                     name = '<a href="/cgi-bin/koha/circ/circulation.pl?borrowernumber='+ encodeURIComponent(patron.patron_id) +'#reserves">' + name + '</a>';
46                 }
47                 else {
48                     name = '<a href="/cgi-bin/koha/members/moremember.pl?borrowernumber='+ encodeURIComponent(patron.patron_id) +'">' + name + '</a>';
49                 }
50             }
51
52             return name;
53         };
54     })();
55 </script>