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( [ '/cgi-bin/koha/svc/cataloguing/framework?frameworkcode=&callback=define', 'marc-record' ], function( defaultFramework, MARC ) {
21 var _authorised_values = defaultFramework.authorised_values;
23 var _framework_mappings = {};
24 var _framework_kohafields = {};
26 function _fromXMLStruct( data ) {
29 $(data).children().eq(0).children().each( function() {
30 var $contents = $(this).contents();
31 if ( $contents.length == 1 && $contents[0].nodeType == Node.TEXT_NODE ) {
32 result[ this.localName ] = $contents[0].data;
34 result[ this.localName ] = $contents.filter( function() { return this.nodeType != Node.TEXT_NODE || !this.data.match( /^\s+$/ ) } ).toArray();
41 function _importFramework( frameworkcode, frameworkinfo ) {
42 _frameworks[frameworkcode] = frameworkinfo;
43 _framework_mappings[frameworkcode] = {};
45 $.each( frameworkinfo, function( i, tag ) {
46 var tagnum = tag[0], taginfo = tag[1];
50 $.each( taginfo.subfields, function( i, subfield ) {
51 subfields[ subfield[0] ] = subfield[1];
52 if ( frameworkcode == '' && subfield[1].kohafield ) {
53 _framework_kohafields[ subfield[1].kohafield ] = [ tagnum, subfield[0] ];
57 _framework_mappings[frameworkcode][tagnum] = $.extend( {}, taginfo, { subfields: subfields } );
61 _importFramework( '', defaultFramework.framework );
63 function _removeBiblionumberFields( record ) {
64 var bibnumTag = KohaBackend.GetSubfieldForKohaField('biblio.biblionumber')[0];
66 while ( record.removeField(bibnumTag) );
69 function initFramework( frameworkcode, callback ) {
70 if ( typeof _frameworks[frameworkcode] === 'undefined' ) {
72 '/cgi-bin/koha/svc/cataloguing/framework?frameworkcode=' + frameworkcode
73 ).done( function( framework ) {
74 _importFramework( frameworkcode, framework.framework );
76 } ).fail( function( data ) {
77 callback( 'Could not fetch framework settings' );
85 NOT_EMPTY: {}, // Sentinel value
87 InitFramework: initFramework,
89 GetAllTagsInfo: function( frameworkcode, tagnumber ) {
90 return _framework_mappings[frameworkcode];
93 GetAuthorisedValues: function( category ) {
94 return _authorised_values[category];
97 GetTagInfo: function( frameworkcode, tagnumber ) {
98 if ( !_framework_mappings[frameworkcode] ) return undefined;
99 return _framework_mappings[frameworkcode][tagnumber];
102 GetSubfieldForKohaField: function( kohafield ) {
103 return _framework_kohafields[kohafield];
106 GetRecord: function( id, callback ) {
108 '/cgi-bin/koha/svc/bib/' + id
109 ).done( function( metadata ) {
111 '/cgi-bin/koha/svc/bib_framework/' + id
112 ).done( function( frameworkcode ) {
113 var record = new MARC.Record();
114 record.loadMARCXML(metadata);
115 record.frameworkcode = $(frameworkcode).find('frameworkcode').text();
116 initFramework( record.frameworkcode, function( error ) {
117 if ( typeof error === 'undefined' ) {
120 callback( { error: error } );
123 } ).fail( function( data ) {
124 callback( { error: _('Could not fetch frameworkcode for record') } );
129 CreateRecord: function( record, callback ) {
130 var frameworkcode = record.frameworkcode;
131 record = record.clone();
132 _removeBiblionumberFields( record );
136 url: '/cgi-bin/koha/svc/new_bib?frameworkcode=' + encodeURIComponent(frameworkcode),
137 data: record.toXML(),
138 contentType: 'text/xml'
139 } ).done( function( data ) {
140 var record = _fromXMLStruct( data );
141 if ( record.marcxml ) {
142 record.marcxml[0].frameworkcode = frameworkcode;
145 } ).fail( function( data ) {
146 callback( { error: _('Could not save record') } );
150 SaveRecord: function( id, record, callback ) {
151 var frameworkcode = record.frameworkcode;
152 record = record.clone();
153 _removeBiblionumberFields( record );
157 url: '/cgi-bin/koha/svc/bib/' + id + '?frameworkcode=' + encodeURIComponent(frameworkcode),
158 data: record.toXML(),
159 contentType: 'text/xml'
160 } ).done( function( data ) {
161 var record = _fromXMLStruct( data );
162 if ( record.marcxml ) {
163 record.marcxml[0].frameworkcode = frameworkcode;
166 } ).fail( function( data ) {
167 callback( { error: _('Could not save record') } );
171 GetTagsBy: function( frameworkcode, field, value ) {
174 $.each( _frameworks[frameworkcode], function( undef, tag ) {
175 var tagnum = tag[0], taginfo = tag[1];
177 if ( taginfo[field] == value && taginfo.tab != '-1' ) result[tagnum] = true;
183 GetSubfieldsBy: function( frameworkcode, field, value ) {
186 $.each( _frameworks[frameworkcode], function( undef, tag ) {
187 var tagnum = tag[0], taginfo = tag[1];
189 $.each( taginfo.subfields, function( undef, subfield ) {
190 var subfieldcode = subfield[0], subfieldinfo = subfield[1];
192 if ( subfieldinfo[field] == value ) {
193 if ( !result[tagnum] ) result[tagnum] = {};
195 result[tagnum][subfieldcode] = true;
203 FillRecord: function( frameworkcode, record, allTags ) {
204 $.each( _frameworks[frameworkcode], function( undef, tag ) {
205 var tagnum = tag[0], taginfo = tag[1];
207 if ( taginfo.mandatory != "1" && !allTags ) return;
209 var fields = record.fields(tagnum);
211 if ( fields.length == 0 ) {
212 var newField = new MARC.Field( tagnum, ' ', ' ', [] );
213 fields.push( newField );
214 record.addFieldGrouped( newField );
216 if ( tagnum < '010' ) {
217 newField.addSubfield( [ '@', (taginfo.subfields[0] ? taginfo.subfields[0][1].defaultvalue : null ) || '' ] );
222 $.each( taginfo.subfields, function( undef, subfield ) {
223 var subfieldcode = subfield[0], subfieldinfo = subfield[1];
225 if ( subfieldinfo.mandatory != "1" && !subfieldinfo.defaultvalue && !allTags ) return;
227 $.each( fields, function( undef, field ) {
228 if ( !field.hasSubfield(subfieldcode) ) {
229 field.addSubfieldGrouped( [ subfieldcode, subfieldinfo.defaultvalue || '' ] );
230 } else if ( subfieldinfo.defaultvalue && field.subfield( subfieldcode ) === '' ) {
231 field.subfield( subfieldcode, subfieldinfo.defaultvalue );
238 ValidateRecord: function( frameworkcode, record ) {
241 var mandatoryTags = KohaBackend.GetTagsBy( record.frameworkcode, 'mandatory', '1' );
242 var mandatorySubfields = KohaBackend.GetSubfieldsBy( record.frameworkcode, 'mandatory', '1' );
243 var nonRepeatableTags = KohaBackend.GetTagsBy( record.frameworkcode, 'repeatable', '0' );
244 var nonRepeatableSubfields = KohaBackend.GetSubfieldsBy( record.frameworkcode, 'repeatable', '0' );
246 $.each( mandatoryTags, function( tag ) {
247 if ( !record.hasField( tag ) ) errors.push( { type: 'missingTag', tag: tag } );
251 var itemTag = KohaBackend.GetSubfieldForKohaField('items.itemnumber')[0];
253 $.each( record.fields(), function( undef, field ) {
254 if ( field.tagnumber() == itemTag ) {
255 errors.push( { type: 'itemTagUnsupported', line: field.sourceLine } );
259 if ( seenTags[ field.tagnumber() ] && nonRepeatableTags[ field.tagnumber() ] ) {
260 errors.push( { type: 'unrepeatableTag', line: field.sourceLine, tag: field.tagnumber() } );
264 seenTags[ field.tagnumber() ] = true;
266 var seenSubfields = {};
268 $.each( field.subfields(), function( undef, subfield ) {
269 if ( seenSubfields[ subfield[0] ] != null && ( nonRepeatableSubfields[ field.tagnumber() ] || {} )[ subfield[0] ] ) {
270 errors.push( { type: 'unrepeatableSubfield', subfield: subfield[0], line: field.sourceLine } );
272 seenSubfields[ subfield[0] ] = subfield[1];
276 $.each( mandatorySubfields[ field.tagnumber() ] || {}, function( subfield ) {
277 if ( !seenSubfields[ subfield ] ) {
278 errors.push( { type: 'missingSubfield', subfield: subfield[0], line: field.sourceLine } );