Bug 23045: TextMarc errors on blank lines
[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     return {
22         RecordToText: function( record ) {
23             var lines = [];
24             var fields = record.fields();
25
26             for ( var i = 0; i < fields.length; i++ ) {
27                 var field = fields[i];
28
29                 if ( field.isControlField() ) {
30                     lines.push( field.tagnumber() + ' ' + field.subfield('@') );
31                 } else {
32                     var result = [ field.tagnumber() + ' ' ];
33
34                     result.push( field.indicator(0) == ' ' ? '_' : field.indicator(0), ' ' );
35                     result.push( field.indicator(1) == ' ' ? '_' : field.indicator(1), ' ' );
36
37                     $.each( field.subfields(), function( i, subfield ) {
38                         result.push( '‡' + subfield[0] + subfield[1] );
39                     } );
40
41                     lines.push( result.join('') );
42                 }
43             }
44
45             return lines.join('\n');
46         },
47
48         TextToRecord: function( text ) {
49             var record = new MARC.Record();
50             var errors = [];
51             $.each( text.split('\n'), function( i, line ) {
52                 if (line === "") {return};
53                 var tagNumber = line.match( /^([A-Za-z0-9]{3}) / );
54                 if ( !tagNumber ) {
55                     errors.push( { type: 'noTag', line: i } );
56                     return;
57                 }
58                 tagNumber = tagNumber[1];
59
60                 if ( tagNumber < '010' ) {
61                     var field = new MARC.Field( tagNumber, ' ', ' ', [ [ '@', line.substring( 4 ) ] ] );
62                     field.sourceLine = i;
63                     record.addField( field );
64                 } else {
65                     var indicators = line.match( /^... ([0-9A-Za-z_ ]) ([0-9A-Za-z_ ])/ );
66                     if ( !indicators ) {
67                         errors.push( { type: 'noIndicators', line: i } );
68                         return;
69                     }
70
71                     var field = new MARC.Field( tagNumber, ( indicators[1] == '_' ? ' ' : indicators[1] ), ( indicators[2] == '_' ? ' ' : indicators[2] ), [] );
72
73                     var matcher = /‡([a-zA-Z0-9%])/g;
74                     var match;
75
76                     var subfields = [];
77
78                     while ( ( match = matcher.exec(line) ) ) {
79                         subfields.push( { code: match[1], ch: match.index } );
80                     }
81
82                     if ( !subfields.length ) {
83                         errors.push( { type: 'noSubfields', line: i } );
84                         return;
85                     }
86
87                     $.each( subfields, function( i, subfield ) {
88                         var next = subfields[ i + 1 ];
89
90                         field.addSubfield( [ subfield.code, line.substring( subfield.ch + 2, next ? next.ch : line.length ) ] );
91                     } );
92
93                     field.sourceLine = i;
94                     record.addField( field );
95                 }
96             } );
97
98             return errors.length ? { errors: errors } : record;
99         }
100     };
101 } );