Bug 11559: Rancor: advanced cataloging interface
[koha.git] / koha-tmpl / intranet-tmpl / lib / koha / cateditor / text-marc.js
1 /**
2  * Copyright 2015 ByWater Solutions
3  *
4  * This file is part of Koha.
5  *
6  * Koha is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Koha is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Koha; if not, see <http://www.gnu.org/licenses>.
18  */
19
20 define( [ 'marc-record' ], function( MARC ) {
21     // Convert any characters for display
22     function _sanitize( text ) {
23         return text.replace( '$', '{dollar}' );
24     }
25
26     // Undo conversion
27     function _desanitize( text ) {
28         return text.replace( '{dollar}', '$' );
29     }
30     return {
31         RecordToText: function( record ) {
32             var lines = [];
33             var fields = record.fields();
34
35             for ( var i = 0; i < fields.length; i++ ) {
36                 var field = fields[i];
37
38                 if ( field.isControlField() ) {
39                     lines.push( field.tagnumber() + ' ' + _sanitize( field.subfield( '@' ) ) );
40                 } else {
41                     var result = [ field.tagnumber() + ' ' ];
42
43                     result.push( field.indicator(0) == ' ' ? '_' : field.indicator(0), ' ' );
44                     result.push( field.indicator(1) == ' ' ? '_' : field.indicator(1), ' ' );
45
46                     $.each( field.subfields(), function( i, subfield ) {
47                         result.push( '$' + subfield[0] + ' ' + _sanitize( subfield[1] ) );
48                     } );
49
50                     lines.push( result.join('') );
51                 }
52             }
53
54             return lines.join('\n');
55         },
56
57         TextToRecord: function( text ) {
58             var record = new MARC.Record();
59             var errors = [];
60
61             $.each( text.split('\n'), function( i, line ) {
62                 var tagNumber = line.match( /^([A-Za-z0-9]{3}) / );
63
64                 if ( !tagNumber ) {
65                     errors.push( { type: 'noTag', line: i } );
66                     return;
67                 }
68                 tagNumber = tagNumber[1];
69
70                 if ( tagNumber < '010' ) {
71                     var field = new MARC.Field( tagNumber, ' ', ' ', [ [ '@', _desanitize( line.substring( 4 ) ) ] ] );
72                     field.sourceLine = i;
73                     record.addField( field );
74                 } else {
75                     var indicators = line.match( /^... ([0-9A-Za-z_]) ([0-9A-Za-z_])/ );
76                     if ( !indicators ) {
77                         errors.push( { type: 'noIndicators', line: i } );
78                         return;
79                     }
80
81                     var field = new MARC.Field( tagNumber, ( indicators[1] == '_' ? ' ' : indicators[1] ), ( indicators[2] == '_' ? ' ' : indicators[2] ), [] );
82
83                     var matcher = /[$|ǂ‡]([a-z0-9%]) /g;
84                     var match;
85
86                     var subfields = [];
87
88                     while ( ( match = matcher.exec(line) ) ) {
89                         subfields.push( { code: match[1], ch: match.index } );
90                     }
91
92                     $.each( subfields, function( i, subfield ) {
93                         var next = subfields[ i + 1 ];
94
95                         field.addSubfield( [ subfield.code, _desanitize( line.substring( subfield.ch + 3, next ? next.ch : line.length ) ) ] );
96                     } );
97
98                     field.sourceLine = i;
99                     record.addField( field );
100                 }
101             } );
102
103             return errors.length ? { errors: errors } : record;
104         }
105     };
106 } );