adding full paths
[migration-tools.git] / sql / base / 21-fixed-fields.sql
1 DROP TABLE IF EXISTS migration_tools.search_format_map;
2 CREATE TABLE migration_tools.search_format_map (code TEXT, itype CHAR(1), iform CHAR(1), phy CHAR(1), phyv CHAR(1), phyp SMALLINT,
3     biblevel CHAR(1), iform_exclude CHAR(1)[], srform_exclude CHAR(1)[] );
4 INSERT INTO migration_tools.search_format_map (code, itype, iform, phy, phyv, phyp, biblevel, iform_exclude, srform_exclude) VALUES
5     --                  itype iform phy   phyv  phyp  bib   itemform exclude     sr format exclude
6      ('blu-ray',        'g',  NULL, 'v',  's',  4,    NULL, NULL,                NULL)
7     ,('book',           'a',  NULL, NULL, NULL, NULL, 'a',  '{a,b,c,f,o,q,r,s}', NULL)
8     ,('braille',        'a',  'f',  NULL, NULL, NULL, NULL, NULL,                NULL)
9     ,('casaudiobook',   'i',  NULL, 's',  'l',  3,    NULL, NULL,                NULL)
10     ,('casmusic',       'j',  NULL, 's',  'l',  3,    NULL, NULL,                NULL)
11     ,('cdaudiobook',    'i',  NULL, 's',  'f',  3,    NULL, NULL,                NULL)
12     ,('cdmusic',        'j',  NULL, 's',  'f',  3,    NULL, NULL,                NULL)
13     ,('dvd',            'g',  NULL, 'v',  'v',  4,    NULL, NULL,                NULL)
14     ,('eaudio',         'i',  'o',  NULL, NULL, NULL, NULL, NULL,                NULL)
15     ,('ebook',          'a',  's',  NULL, NULL, NULL, 'a' , NULL,                NULL)
16     ,('electronic',     's',  'o',  NULL, NULL, NULL, NULL, NULL,                NULL)
17     ,('equip',          'r',  NULL, NULL, NULL, NULL, NULL, NULL,                NULL)
18     ,('evideo',         'g',  'o',  NULL, NULL, NULL, NULL, NULL,                NULL)
19     ,('kit',            'o',  NULL, NULL, NULL, NULL, NULL, NULL,                NULL)
20     ,('lpbook',         'a',  'd',  NULL, NULL, NULL, 'a' , NULL,                NULL)
21     ,('map',            'e',  NULL, NULL, NULL, NULL, NULL, NULL,                NULL)
22     ,('microform',      'a',  'b',  NULL, NULL, NULL, NULL, NULL,                NULL)
23     ,('music',          'j',  NULL, NULL, NULL, NULL, NULL, NULL,                '{a,b,c,d,e,f}')
24     ,('phonomusic',     'j',  NULL, 's',  'a',  3,    NULL, NULL,                NULL)
25     ,('phonospoken',    'i',  NULL, 's',  'a',  3,    NULL, NULL,                NULL)
26     ,('picture',        'k',  NULL, NULL, NULL, NULL, NULL, NULL,                NULL)
27     ,('preloadedaudio', 'i',  'q',  NULL, NULL, NULL, NULL, NULL,                '{a,b,c,d,e,f,s}')
28     ,('score',          'c',  NULL, NULL, NULL, NULL, NULL, NULL,                NULL)
29     ,('serial',         NULL, NULL, NULL, NULL, NULL, 's' , NULL,                NULL)
30     ,('software',       'm',  NULL, NULL, NULL, NULL, NULL, NULL,                NULL)
31     ,('vhs',            'g',  NULL, 'v',  'b',  4,    NULL, NULL,                NULL)
32 ;
33
34 CREATE OR REPLACE FUNCTION migration_tools.reingest_staged_record_attributes (rid BIGINT, pattr_list TEXT[] DEFAULT NULL, prmarc TEXT DEFAULT NULL, rdeleted BOOL DEFAULT TRUE) RETURNS INTEGER[] AS $func$
35 DECLARE
36     transformed_xml TEXT;
37     rmarc           TEXT := prmarc;
38     tmp_val         TEXT;
39     prev_xfrm       TEXT;
40     normalizer      RECORD;
41     xfrm            config.xml_transform%ROWTYPE;
42     attr_vector     INT[] := '{}'::INT[];
43     attr_vector_tmp INT[];
44     attr_list       TEXT[] := pattr_list;
45     attr_value      TEXT[];
46     norm_attr_value TEXT[];
47     tmp_xml         TEXT;
48     tmp_array       TEXT[];
49     attr_def        config.record_attr_definition%ROWTYPE;
50     ccvm_row        config.coded_value_map%ROWTYPE;
51     jump_past       BOOL;
52 BEGIN
53
54     IF attr_list IS NULL OR rdeleted THEN -- need to do the full dance on INSERT or undelete
55         SELECT ARRAY_AGG(name) INTO attr_list FROM config.record_attr_definition
56         WHERE ( 
57             tag IS NOT NULL OR 
58             fixed_field IS NOT NULL OR
59             xpath IS NOT NULL OR
60             phys_char_sf IS NOT NULL OR
61             composite
62         ) AND (
63             filter OR sorter
64         );
65     END IF;
66     IF rmarc IS NULL THEN
67         SELECT marc INTO rmarc FROM biblio_record_entry_legacy WHERE id = rid;
68     END IF;
69
70     FOR attr_def IN SELECT * FROM config.record_attr_definition WHERE NOT composite AND name = ANY( attr_list ) ORDER BY format LOOP
71
72         jump_past := FALSE; -- This gets set when we are non-multi and have found something
73         attr_value := '{}'::TEXT[];
74         norm_attr_value := '{}'::TEXT[];
75         attr_vector_tmp := '{}'::INT[];
76
77         SELECT * INTO ccvm_row FROM config.coded_value_map c WHERE c.ctype = attr_def.name LIMIT 1;
78
79         IF attr_def.tag IS NOT NULL THEN -- tag (and optional subfield list) selection
80             SELECT  ARRAY_AGG(value) INTO attr_value
81               FROM  (SELECT * FROM metabib.full_rec ORDER BY tag, subfield) AS x
82               WHERE record = rid
83                     AND tag LIKE attr_def.tag
84                     AND CASE
85                         WHEN attr_def.sf_list IS NOT NULL
86                             THEN POSITION(subfield IN attr_def.sf_list) > 0
87                         ELSE TRUE
88                     END
89               GROUP BY tag
90               ORDER BY tag;
91
92             IF NOT attr_def.multi THEN
93                 attr_value := ARRAY[ARRAY_TO_STRING(attr_value, COALESCE(attr_def.joiner,' '))];
94                 jump_past := TRUE;
95             END IF;
96         END IF;
97
98         IF NOT jump_past AND attr_def.fixed_field IS NOT NULL THEN -- a named fixed field, see config.marc21_ff_pos_map.fixed_field
99             attr_value := attr_value || vandelay.marc21_extract_fixed_field_list(rmarc, attr_def.fixed_field);
100
101             IF NOT attr_def.multi THEN
102                 attr_value := ARRAY[attr_value[1]];
103                 jump_past := TRUE;
104             END IF;
105         END IF;
106         
107                 IF NOT jump_past AND attr_def.xpath IS NOT NULL THEN -- and xpath expression
108
109                     SELECT INTO xfrm * FROM config.xml_transform WHERE name = attr_def.format;
110
111                     -- See if we can skip the XSLT ... it's expensive
112                     IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
113                         -- Can't skip the transform
114                         IF xfrm.xslt <> '---' THEN
115                             transformed_xml := oils_xslt_process(rmarc,xfrm.xslt);
116                         ELSE
117                             transformed_xml := rmarc;
118                         END IF;
119
120                         prev_xfrm := xfrm.name;
121                     END IF;
122
123                     IF xfrm.name IS NULL THEN
124                         -- just grab the marcxml (empty) transform
125                         SELECT INTO xfrm * FROM config.xml_transform WHERE xslt = '---' LIMIT 1;
126                         prev_xfrm := xfrm.name;
127                     END IF;
128
129                     FOR tmp_xml IN SELECT UNNEST(oils_xpath(attr_def.xpath, transformed_xml, ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]])) LOOP
130                         tmp_val := oils_xpath_string(
131                                         '//*',
132                                         tmp_xml,
133                                         COALESCE(attr_def.joiner,' '),
134                                         ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]
135                                     );
136                         IF tmp_val IS NOT NULL AND BTRIM(tmp_val) <> '' THEN
137                             attr_value := attr_value || tmp_val;
138                             EXIT WHEN NOT attr_def.multi;
139                         END IF;
140                     END LOOP;
141                 END IF;
142
143                 IF NOT jump_past AND attr_def.phys_char_sf IS NOT NULL THEN -- a named Physical Characteristic, see config.marc21_physical_characteristic_*_map
144                     SELECT  ARRAY_AGG(m.value) INTO tmp_array
145                       FROM  vandelay.marc21_physical_characteristics(rmarc) v
146                             LEFT JOIN config.marc21_physical_characteristic_value_map m ON (m.id = v.value)
147                       WHERE v.subfield = attr_def.phys_char_sf AND (m.value IS NOT NULL AND BTRIM(m.value) <> '')
148                             AND ( ccvm_row.id IS NULL OR ( ccvm_row.id IS NOT NULL AND v.id IS NOT NULL) );
149
150                     attr_value := attr_value || tmp_array;
151
152                     IF NOT attr_def.multi THEN
153                         attr_value := ARRAY[attr_value[1]];
154                     END IF;
155
156                 END IF;
157
158                 -- apply index normalizers to attr_value
159         FOR tmp_val IN SELECT value FROM UNNEST(attr_value) x(value) LOOP
160             FOR normalizer IN
161                 SELECT  n.func AS func,
162                         n.param_count AS param_count,
163                         m.params AS params
164                   FROM  config.index_normalizer n
165                         JOIN config.record_attr_index_norm_map m ON (m.norm = n.id)
166                   WHERE attr = attr_def.name
167                   ORDER BY m.pos LOOP
168                     EXECUTE 'SELECT ' || normalizer.func || '(' ||
169                     COALESCE( quote_literal( tmp_val ), 'NULL' ) ||
170                         CASE
171                             WHEN normalizer.param_count > 0
172                                 THEN ',' || REPLACE(REPLACE(BTRIM(normalizer.params,'[]'),E'\'',E'\\\''),E'"',E'\'')
173                                 ELSE ''
174                             END ||
175                     ')' INTO tmp_val;
176
177             END LOOP;
178             IF tmp_val IS NOT NULL AND tmp_val <> '' THEN
179                 -- note that a string that contains only blanks
180                 -- is a valid value for some attributes
181                 norm_attr_value := norm_attr_value || tmp_val;
182             END IF;
183         END LOOP;
184
185         IF attr_def.filter THEN
186             -- Create unknown uncontrolled values and find the IDs of the values
187             IF ccvm_row.id IS NULL THEN
188                 FOR tmp_val IN SELECT value FROM UNNEST(norm_attr_value) x(value) LOOP
189                     IF tmp_val IS NOT NULL AND BTRIM(tmp_val) <> '' THEN
190                         BEGIN -- use subtransaction to isolate unique constraint violations
191                             INSERT INTO metabib.uncontrolled_record_attr_value ( attr, value ) VALUES ( attr_def.name, tmp_val );
192                         EXCEPTION WHEN unique_violation THEN END;
193                     END IF;
194                 END LOOP;
195
196                 SELECT ARRAY_AGG(id) INTO attr_vector_tmp FROM metabib.uncontrolled_record_attr_value WHERE attr = attr_def.name AND value = ANY( norm_attr_value );
197             ELSE
198                 SELECT ARRAY_AGG(id) INTO attr_vector_tmp FROM config.coded_value_map WHERE ctype = attr_def.name AND code = ANY( norm_attr_value );
199             END IF;
200
201             -- Add the new value to the vector
202             attr_vector := attr_vector || attr_vector_tmp;
203         END IF;
204     END LOOP;
205
206     
207         IF ARRAY_LENGTH(pattr_list, 1) > 0 THEN
208             SELECT vlist INTO attr_vector_tmp FROM metabib.record_attr_vector_list WHERE source = rid;
209             SELECT attr_vector_tmp - ARRAY_AGG(id::INT) INTO attr_vector_tmp FROM metabib.full_attr_id_map WHERE attr = ANY (pattr_list);
210             attr_vector := attr_vector || attr_vector_tmp;
211         END IF;
212
213         -- On to composite attributes, now that the record attrs have been pulled.  Processed in name order, so later composite
214         -- attributes can depend on earlier ones.
215         PERFORM metabib.compile_composite_attr_cache_init();
216         FOR attr_def IN SELECT * FROM config.record_attr_definition WHERE composite AND name = ANY( attr_list ) ORDER BY name LOOP
217
218             FOR ccvm_row IN SELECT * FROM config.coded_value_map c WHERE c.ctype = attr_def.name ORDER BY value LOOP
219
220                 tmp_val := metabib.compile_composite_attr( ccvm_row.id );
221                 CONTINUE WHEN tmp_val IS NULL OR tmp_val = ''; -- nothing to do
222
223                 IF attr_def.filter THEN
224                     IF attr_vector @@ tmp_val::query_int THEN
225                         attr_vector = attr_vector + intset(ccvm_row.id);
226                         EXIT WHEN NOT attr_def.multi;
227                     END IF;
228                 END IF;
229
230                 IF attr_def.sorter THEN
231                     IF attr_vector @@ tmp_val THEN
232                         DELETE FROM metabib.record_sorter WHERE source = rid AND attr = attr_def.name;
233                         INSERT INTO metabib.record_sorter (source, attr, value) VALUES (rid, attr_def.name, ccvm_row.code);
234                     END IF;
235                 END IF;
236
237             END LOOP;
238
239         END LOOP;
240
241         return attr_vector;
242     END;
243     $func$ LANGUAGE PLPGSQL;
244
245
246 CREATE OR REPLACE FUNCTION migration_tools.view_staged_vlist (rid BIGINT) RETURNS TABLE (r_ctype text, r_code text, r_value text) AS $func$
247 DECLARE
248     search  TEXT[];
249     icon    TEXT[];
250     vlist   INTEGER[];
251 BEGIN
252     SELECT migration_tools.reingest_staged_record_attributes(rid) INTO vlist;
253
254     RETURN QUERY SELECT ctype, code, value FROM config.coded_value_map WHERE id IN (SELECT UNNEST(vlist));
255 END;
256 $func$ LANGUAGE PLPGSQL;
257
258 CREATE OR REPLACE FUNCTION migration_tools.stage_vlist (rid BIGINT) RETURNS VOID AS $func$
259 DECLARE 
260         search  TEXT[];
261         vlist   INTEGER[];
262 BEGIN
263         SELECT migration_tools.reingest_staged_record_attributes(rid) INTO vlist;
264
265         SELECT ARRAY_AGG(code) FROM config.coded_value_map WHERE id IN (SELECT UNNEST(vlist)) 
266                 AND ctype = 'search_format' INTO search;
267
268         UPDATE biblio_record_entry_legacy SET x_search_format = search  WHERE id = rid;
269 END;
270 $func$ LANGUAGE PLPGSQL;
271
272 CREATE OR REPLACE FUNCTION migration_tools.show_staged_vlist (rid BIGINT) RETURNS TEXT[] AS $func$
273 DECLARE
274     search  TEXT[];
275     vlist   INTEGER[];
276 BEGIN
277     SELECT migration_tools.reingest_staged_record_attributes(rid) INTO vlist;
278
279     SELECT ARRAY_AGG(code) FROM config.coded_value_map WHERE id IN (SELECT UNNEST(vlist))
280         AND ctype = 'search_format' INTO search;
281
282         RETURN search;
283 END;
284 $func$ LANGUAGE PLPGSQL;
285
286
287 CREATE OR REPLACE FUNCTION migration_tools.postfix_vlist (rid BIGINT) RETURNS VOID AS $func$
288 DECLARE
289     search  TEXT[];
290     vlist   INTEGER[];
291 BEGIN
292     SELECT migration_tools.reingest_staged_record_attributes(rid) INTO vlist;
293
294     SELECT ARRAY_AGG(code) FROM config.coded_value_map WHERE id IN (SELECT UNNEST(vlist))
295         AND ctype = 'search_format' INTO search;
296
297     UPDATE biblio_record_entry_legacy SET x_after_search_format = search WHERE id = rid;
298 END;
299 $func$ LANGUAGE PLPGSQL;
300
301
302 CREATE OR REPLACE FUNCTION migration_tools.set_exp_sfs (rid BIGINT) RETURNS VOID AS $func$
303 DECLARE
304     cms TEXT[];
305     y   TEXT;
306     w   TEXT[];
307 BEGIN
308         SELECT circ_mods FROM biblio_record_entry_legacy WHERE id = rid INTO cms;
309     IF cms IS NOT NULL THEN
310         FOREACH y IN ARRAY cms LOOP
311                 w := w || (SELECT sf1 FROM circ_mod_to_sf_map WHERE circ_mod = y);
312             w := w || (SELECT sf2 FROM circ_mod_to_sf_map WHERE circ_mod = y);
313             w := w || (SELECT sf3 FROM circ_mod_to_sf_map WHERE circ_mod = y);
314         END LOOP;
315         UPDATE biblio_record_entry_legacy SET expected_sfs = w WHERE id = rid;
316     END IF;
317 END;
318 $func$ LANGUAGE PLPGSQL;
319
320 DROP AGGREGATE IF EXISTS anyarray_agg(anyarray);
321 CREATE AGGREGATE anyarray_agg(anyarray) (
322         SFUNC = anyarray_agg_statefunc,
323         STYPE = anyarray
324 );
325 COMMENT ON AGGREGATE anyarray_agg(anyarray) IS
326 'Concatenates arrays into a single array when aggregating.';
327
328 DROP FUNCTION IF EXISTS migration_tools.anyarray_agg_statefunc(anyarray, anyarray);
329 CREATE FUNCTION anyarray_agg_statefunc(state anyarray, value anyarray)
330         RETURNS anyarray AS
331 $BODY$
332         SELECT array_cat($1, $2)
333 $BODY$
334         LANGUAGE sql IMMUTABLE;
335 COMMENT ON FUNCTION anyarray_agg_statefunc(anyarray, anyarray) IS
336 'Used internally by aggregate anyarray_agg(anyarray).';
337
338 DROP FUNCTION IF EXISTS migration_tools.anyarray_sort(anyarray);
339 CREATE OR REPLACE FUNCTION migration_tools.anyarray_sort(with_array anyarray)
340     RETURNS anyarray AS
341 $BODY$
342     DECLARE
343         return_array with_array%TYPE := '{}';
344     BEGIN
345         SELECT ARRAY_AGG(sorted_vals.val) AS array_value
346         FROM
347             (   SELECT UNNEST(with_array) AS val
348                 ORDER BY val
349             ) AS sorted_vals INTO return_array;
350         RETURN return_array;
351     END;
352 $BODY$ LANGUAGE plpgsql;
353
354 DROP FUNCTION IF EXISTS migration_tools.anyarray_uniq(anyarray);
355 CREATE OR REPLACE FUNCTION migration_tools.anyarray_uniq(with_array anyarray)
356     RETURNS anyarray AS
357 $BODY$
358     DECLARE
359         -- The variable used to track iteration over "with_array".
360         loop_offset integer;
361
362         -- The array to be returned by this function.
363         return_array with_array%TYPE := '{}';
364     BEGIN
365         IF with_array IS NULL THEN
366             return NULL;
367         END IF;
368
369         IF with_array = '{}' THEN
370             return return_array;
371         END IF;
372
373         -- Iterate over each element in "concat_array".
374         FOR loop_offset IN ARRAY_LOWER(with_array, 1)..ARRAY_UPPER(with_array, 1) LOOP
375             IF with_array[loop_offset] IS NULL THEN
376                 IF NOT EXISTS
377                     ( SELECT 1 FROM UNNEST(return_array) AS s(a)
378                     WHERE a IS NULL )
379                 THEN return_array = ARRAY_APPEND(return_array, with_array[loop_offset]);
380                 END IF;
381             -- When an array contains a NULL value, ANY() returns NULL instead of FALSE...
382             ELSEIF NOT(with_array[loop_offset] = ANY(return_array)) OR NOT(NULL IS DISTINCT FROM (with_array[loop_offset] = ANY(return_array))) THEN
383                 return_array = ARRAY_APPEND(return_array, with_array[loop_offset]);
384             END IF;
385         END LOOP;
386
387     RETURN return_array;
388  END;
389 $BODY$ LANGUAGE plpgsql;
390
391 DROP FUNCTION IF EXISTS migration_tools.modify_staged_fixed_fields (BIGINT,TEXT);
392 CREATE OR REPLACE FUNCTION migration_tools.modify_staged_fixed_fields (bib_id BIGINT, xcode TEXT)
393  RETURNS BOOLEAN
394  LANGUAGE plpgsql
395 AS $function$
396 DECLARE
397     r           TEXT;
398     xitype      CHAR(1);
399     xiform      CHAR(1);
400     xphy        CHAR(1);
401     xphyv       CHAR(1);
402     xphyp       SMALLINT;
403     xbiblevel   CHAR(1);
404     xiform_exclude      CHAR(1)[];
405     xsrform_exclude     CHAR(1)[];
406         yiform_exclude          TEXT;
407         ysrform_exclude     TEXT;
408 BEGIN
409     SELECT itype, iform, phy, phyv, phyp, biblevel, iform_exclude, srform_exclude FROM migration_tools.search_format_map WHERE code = xcode
410         INTO xitype, xiform, xphy, xphyv, xphyp, xbiblevel, xiform_exclude, xsrform_exclude;
411         IF xiform_exclude IS NOT NULL THEN 
412                 yiform_exclude := ARRAY_TO_STRING(xiform_exclude,',');
413         ELSE 
414                 yiform_exclude := '';
415         END IF;
416         IF xsrform_exclude IS NOT NULL THEN 
417                 ysrform_exclude := ARRAY_TO_STRING(ysrform_exclude,',');
418         ELSE
419                 ysrform_exclude := '';
420         END IF;
421     SELECT modify_fixed_fields(marc,xcode,xitype,xiform,xphy,xphyv,xphyp,xbiblevel,yiform_exclude,ysrform_exclude) FROM biblio_record_entry_legacy WHERE id = bib_id INTO r;
422     UPDATE biblio_record_entry_legacy SET marc = r WHERE id = bib_id;
423     RETURN TRUE;
424 END;
425 $function$;
426
427 DROP FUNCTION IF EXISTS migration_tools.modify_fixed_fields (TEXT, TEXT, CHAR(1), CHAR(1), CHAR(1), CHAR(1), SMALLINT, CHAR(1), TEXT, TEXT);
428 CREATE OR REPLACE FUNCTION migration_tools.modify_fixed_fields (TEXT, TEXT, CHAR(1), CHAR(1), CHAR(1), CHAR(1), SMALLINT, CHAR(1), TEXT, TEXT)
429 RETURNS TEXT
430  LANGUAGE plperlu
431 AS $function$
432
433 # assumption is that there should only be a single format per item
434
435 use strict;
436 use warnings;
437
438 use MARC::Record;
439 use MARC::File::XML (BinaryEncoding => 'utf8');
440 use MARC::Field;
441 use Data::Dumper;
442
443 my ($marcxml, $code, $itype, $iform, $phy, $phyv, $phyp, $biblevel, $iform_exclude_temp, $srform_exclude_temp) = @_;
444 my $marc;
445 my @iform_exclude;
446 if ($iform_exclude_temp) { @iform_exclude = split /,/, $iform_exclude_temp; }
447 my @srform_exclude;
448 if ($srform_exclude_temp) { @srform_exclude = split /,/, $srform_exclude_temp; }
449
450 $marcxml =~ s/(<leader>.........)./${1}a/;
451 eval {  $marc = MARC::Record->new_from_xml($marcxml, 'UTF-8'); };
452 if ($@) {
453   import MARC::File::XML (BinaryEncoding => 'utf8');
454   return 'failed to parse marcxml';
455 }
456
457 my $ldr = $marc->leader();
458 if ($itype) { substr($ldr,6,1) = $itype; } else { substr($ldr,6,1) = '|'; }
459 if ($biblevel) { substr($ldr,7,1) = $biblevel; } else { substr($ldr,7,1) = '|'; }
460 $marc->leader($ldr);
461
462 my $zedzedeight;
463 my $zze_str = '0000000000000000000000000000000000000000';
464 my $new_zze;
465 my $formchar;
466 $zedzedeight = $marc->field('008');
467 if ($zedzedeight) {
468     $zze_str = $zedzedeight->data();
469 }
470 if (length($zze_str) < 30) {
471     my $nneight = MARC::Field->new( 918, '1', '0', 'a' => $zze_str );
472     $marc->insert_fields_ordered($nneight);
473     $zze_str = '0000000000000000000000000000000000000000';
474 }
475 if ($itype eq 'e' or $itype eq 'g' or $itype eq 'k')
476     { $formchar = substr($zze_str,29,1); }
477     else { $formchar = substr($zze_str,23,1); }
478 if (@iform_exclude and $itype) {
479     if ($itype eq 'e' or $itype eq 'g' or $itype eq 'k')  {      #visual materials
480         if ($formchar ~~ @iform_exclude) { substr($zze_str,29,1) = '|'; }
481     } else { if ($formchar ~~ @iform_exclude) { substr($zze_str,23,1) = '|'; } }
482 }
483 if ($iform) {
484     if ($itype eq 'e' or $itype eq 'g' or $itype eq 'k') {      #visual materials
485         substr($zze_str,29,1) = $iform;
486     } else {
487         substr($zze_str,23,1) = $iform;
488     }
489 } else {
490     if ($itype eq 'e' or $itype eq 'g' or $itype eq 'k') {      #visual materials
491             substr($zze_str,29,1) = '|';
492         } else {
493             substr($zze_str,23,1) = '|';
494         }
495 }
496
497 $new_zze = MARC::Field->new('008',$zze_str);
498 if ($zedzedeight) { $zedzedeight->replace_with($new_zze); } else
499     { $marc->insert_fields_ordered($new_zze); }
500
501 my @todelzzsx = $marc->field('006');
502 #save the old 006s in 916 fields
503 foreach my $sx (@todelzzsx) {
504     my $nfield = MARC::Field->new( 916, '1', '0', 'a' => $sx->data() );
505     $marc->insert_fields_ordered($nfield);
506 }
507 $marc->delete_fields(@todelzzsx);
508
509 my $zzsx_str = '00000000000000000';
510 if ($iform) { substr($zzsx_str,6,1) = $iform; }
511 my $zedzedsix = MARC::Field->new('006', $zzsx_str);
512 $marc->insert_fields_ordered($zedzedsix);
513
514 my @todelzzsv = $marc->field('007');
515 #save the old 007s in 917 fields
516 foreach my $sv (@todelzzsv) {
517     my $nfield = MARC::Field->new( 917, '1', '0', 'a' => $sv->data() );
518     $marc->insert_fields_ordered($nfield);
519 }
520 $marc->delete_fields(@todelzzsv);
521
522 my $nn = MARC::Field->new( 919, '1', '0', 'a' => 'record modified by automated fixed field changes' );
523 $marc->insert_fields_ordered($nn);
524
525 my $zedzedseven;
526 my $zzs_str;
527     if ($phy) {
528             if ($phy eq 'o' or $phy eq 'q' or $phy eq 'z' or $phy eq 't') { $zzs_str = '00'; }
529             if ($phy eq 's' or $phy eq 'c') { $zzs_str = '00000000000000'; }
530             if ($phy eq 'r') { $zzs_str = '00000000000'; }
531             if ($phy eq 'm') { $zzs_str = '00000000000000000000000'; }
532             if ($phy eq 'a') { $zzs_str = '00000000'; }
533             if ($phy eq 'd') { $zzs_str = '000000'; }
534             if ($phy eq 'f') { $zzs_str = '0000000000'; }
535             if ($phy eq 'g') { $zzs_str = '000000000'; }
536             if ($phy eq 'h') { $zzs_str = '0000000000000'; }
537             if ($phy eq 'k') { $zzs_str = '000000'; }
538             if ($phy eq 'v') { $zzs_str = '000000000'; }
539             substr($zzs_str,0,1) = $phy;
540             substr($zzs_str,$phyp,1) = $phyv;
541             $zedzedseven = MARC::Field->new('007', $zzs_str);
542             $marc->insert_fields_ordered($zedzedseven);
543     }
544 return $marc->as_xml_record;
545 $function$;
546