Bug 16424: Handle framework code properly also when adding a new record
[koha.git] / koha-tmpl / intranet-tmpl / lib / koha / cateditor / koha-backend.js
blob10e9337caad86aa9b3f45cb1cfb0d6d10dcabdaa
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  */
20 define( [ '/cgi-bin/koha/svc/cataloguing/framework?frameworkcode=&callback=define', 'marc-record' ], function( defaultFramework, MARC ) {
21     var _authorised_values = defaultFramework.authorised_values;
22     var _frameworks = {};
23     var _framework_mappings = {};
24     var _framework_kohafields = {};
26     function _fromXMLStruct( data ) {
27         result = {};
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;
33             } else {
34                 result[ this.localName ] = $contents.filter( function() { return this.nodeType != Node.TEXT_NODE || !this.data.match( /^\s+$/ ) } ).toArray();
35             }
36         } );
38         return result;
39     }
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];
48             var subfields = {};
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] ];
54                 }
55             } );
57             _framework_mappings[frameworkcode][tagnum] = $.extend( {}, taginfo, { subfields: subfields } );
58         } );
59     }
61     _importFramework( '', defaultFramework.framework );
63     function _removeBiblionumberFields( record ) {
64         var bibnumTag = KohaBackend.GetSubfieldForKohaField('biblio.biblionumber')[0];
66         while ( record.removeField(bibnumTag) );
67     }
69     function initFramework( frameworkcode, callback ) {
70         if ( typeof _frameworks[frameworkcode] === 'undefined' ) {
71             $.get(
72                 '/cgi-bin/koha/svc/cataloguing/framework?frameworkcode=' + frameworkcode
73             ).done( function( framework ) {
74                 _importFramework( frameworkcode, framework.framework );
75                 callback();
76             } ).fail( function( data ) {
77                 callback( 'Could not fetch framework settings' );
78             } );
79         } else {
80             callback();
81         }
82     }
84     var KohaBackend = {
85         NOT_EMPTY: {}, // Sentinel value
87         InitFramework: initFramework,
89         GetAllTagsInfo: function( frameworkcode, tagnumber ) {
90             return _framework_mappings[frameworkcode];
91         },
93         GetAuthorisedValues: function( category ) {
94             return _authorised_values[category];
95         },
97         GetTagInfo: function( frameworkcode, tagnumber ) {
98             if ( !_framework_mappings[frameworkcode] ) return undefined;
99             return _framework_mappings[frameworkcode][tagnumber];
100         },
102         GetSubfieldForKohaField: function( kohafield ) {
103             return _framework_kohafields[kohafield];
104         },
106         GetRecord: function( id, callback ) {
107             $.get(
108                 '/cgi-bin/koha/svc/bib/' + id
109             ).done( function( metadata ) {
110                 $.get(
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' ) {
118                             callback( record );
119                         } else {
120                             callback( { error: error } );
121                         }
122                     });
123                 } ).fail( function( data ) {
124                     callback( { error: _('Could not fetch frameworkcode for record') } );
125                 } );
126             } );
127         },
129         CreateRecord: function( record, callback ) {
130             var frameworkcode = record.frameworkcode;
131             record = record.clone();
132             _removeBiblionumberFields( record );
134             $.ajax( {
135                 type: 'POST',
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;
143                 }
144                 callback( record );
145             } ).fail( function( data ) {
146                 callback( { error: _('Could not save record') } );
147             } );
148         },
150         SaveRecord: function( id, record, callback ) {
151             var frameworkcode = record.frameworkcode;
152             record = record.clone();
153             _removeBiblionumberFields( record );
155             $.ajax( {
156                 type: 'POST',
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;
164                 }
165                 callback( record );
166             } ).fail( function( data ) {
167                 callback( { error: _('Could not save record') } );
168             } );
169         },
171         GetTagsBy: function( frameworkcode, field, value ) {
172             var result = {};
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;
178             } );
180             return result;
181         },
183         GetSubfieldsBy: function( frameworkcode, field, value ) {
184             var result = {};
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;
196                     }
197                 } );
198             } );
200             return result;
201         },
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 ) || '' ] );
218                         return;
219                     }
220                 }
222                 $.each( taginfo.subfields, function( undef, subfield ) {
223                     var subfieldcode = subfield[0], subfieldinfo = subfield[1];
225                     if ( subfieldinfo.mandatory != "1" && !allTags ) return;
227                     $.each( fields, function( undef, field ) {
228                         if ( !field.hasSubfield(subfieldcode) ) field.addSubfieldGrouped( [ subfieldcode, subfieldinfo.defaultvalue || '' ] );
229                     } );
230                 } );
231             } );
232         },
234         ValidateRecord: function( frameworkcode, record ) {
235             var errors = [];
237             var mandatoryTags = KohaBackend.GetTagsBy( record.frameworkcode, 'mandatory', '1' );
238             var mandatorySubfields = KohaBackend.GetSubfieldsBy( record.frameworkcode, 'mandatory', '1' );
239             var nonRepeatableTags = KohaBackend.GetTagsBy( record.frameworkcode, 'repeatable', '0' );
240             var nonRepeatableSubfields = KohaBackend.GetSubfieldsBy( record.frameworkcode, 'repeatable', '0' );
242             $.each( mandatoryTags, function( tag ) {
243                 if ( !record.hasField( tag ) ) errors.push( { type: 'missingTag', tag: tag } );
244             } );
246             var seenTags = {};
247             var itemTag = KohaBackend.GetSubfieldForKohaField('items.itemnumber')[0];
249             $.each( record.fields(), function( undef, field ) {
250                 if ( field.tagnumber() == itemTag ) {
251                     errors.push( { type: 'itemTagUnsupported', line: field.sourceLine } );
252                     return;
253                 }
255                 if ( seenTags[ field.tagnumber() ] && nonRepeatableTags[ field.tagnumber() ] ) {
256                     errors.push( { type: 'unrepeatableTag', line: field.sourceLine, tag: field.tagnumber() } );
257                     return;
258                 }
260                 seenTags[ field.tagnumber() ] = true;
262                 var seenSubfields = {};
264                 $.each( field.subfields(), function( undef, subfield ) {
265                     if ( seenSubfields[ subfield[0] ] != null && ( nonRepeatableSubfields[ field.tagnumber() ] || {} )[ subfield[0] ] ) {
266                         errors.push( { type: 'unrepeatableSubfield', subfield: subfield[0], line: field.sourceLine } );
267                     } else {
268                         seenSubfields[ subfield[0] ] = subfield[1];
269                     }
270                 } );
272                 $.each( mandatorySubfields[ field.tagnumber() ] || {}, function( subfield ) {
273                     if ( !seenSubfields[ subfield ] ) {
274                         errors.push( { type: 'missingSubfield', subfield: subfield[0], line: field.sourceLine } );
275                     }
276                 } );
277             } );
279             return errors;
280         },
281     };
283     return KohaBackend;
284 } );