2 * Copyright 2015 ByWater Solutions
4 * This file is part of Koha.
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.
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.
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>.
20 define( [ 'marc-record' ], function( MARC ) {
22 RecordToText: function( record ) {
24 var fields = record.fields();
26 for ( var i = 0; i < fields.length; i++ ) {
27 var field = fields[i];
29 if ( field.isControlField() ) {
30 lines.push( field.tagnumber() + ' ' + field.subfield('@') );
32 var result = [ field.tagnumber() + ' ' ];
34 result.push( field.indicator(0) == ' ' ? '_' : field.indicator(0), ' ' );
35 result.push( field.indicator(1) == ' ' ? '_' : field.indicator(1), ' ' );
37 $.each( field.subfields(), function( i, subfield ) {
38 result.push( '‡' + subfield[0] + subfield[1] );
41 lines.push( result.join('') );
45 return lines.join('\n');
48 TextToRecord: function( text ) {
49 var record = new MARC.Record();
51 $.each( text.split('\n'), function( i, line ) {
52 if (line === "") {return};
53 var tagNumber = line.match( /^([A-Za-z0-9]{3}) / );
55 errors.push( { type: 'noTag', line: i } );
58 tagNumber = tagNumber[1];
60 if ( tagNumber < '010' ) {
61 var field = new MARC.Field( tagNumber, ' ', ' ', [ [ '@', line.substring( 4 ) ] ] );
63 record.addField( field );
65 var indicators = line.match( /^... ([0-9A-Za-z_ ]) ([0-9A-Za-z_ ])/ );
67 errors.push( { type: 'noIndicators', line: i } );
71 var field = new MARC.Field( tagNumber, ( indicators[1] == '_' ? ' ' : indicators[1] ), ( indicators[2] == '_' ? ' ' : indicators[2] ), [] );
73 var matcher = /‡([a-zA-Z0-9%])/g;
78 while ( ( match = matcher.exec(line) ) ) {
79 subfields.push( { code: match[1], ch: match.index } );
82 if ( !subfields.length ) {
83 errors.push( { type: 'noSubfields', line: i } );
87 $.each( subfields, function( i, subfield ) {
88 var next = subfields[ i + 1 ];
90 field.addSubfield( [ subfield.code, line.substring( subfield.ch + 2, next ? next.ch : line.length ) ] );
94 record.addField( field );
98 return errors.length ? { errors: errors } : record;