-
Notifications
You must be signed in to change notification settings - Fork 0
/
cals_importer.exporter.inc
executable file
·833 lines (743 loc) · 23.8 KB
/
cals_importer.exporter.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
<?php
require 'File/MARCXML.php';
require 'File/MARC/Lint.php';
module_load_include('inc', 'cals_importer', 'cals_importer.exporter_callbacks');
module_load_include('inc', 'cals_importer', 'cals_importer.exporter_file_snippets');
/**
* Simple wrapper so we can invoke from main repo node page for testing purposes
*
* @param $node
* the node passed to the parser
*/
function _cals_importer_create_marc_stub_node($node) {
_cals_importer_create_marc_stub($node);
//drupal_goto("node/" . $node->nid);
drupal_goto("node/{$node->nid}/view-xml-stub");
}
/**
* VBO to add populate the stub marc-xml string
*
* @param $entity
* the node passed to the parser
*/
function _cals_importer_create_marc_stub_vbo(&$entity, $context = array()) {
require 'File/MARCXML.php';
require 'File/MARC/Lint.php';
if (isset($entity->nid)) {
$node = node_load($entity->nid);
//safety check to make sure we're parsing repos items!!
$content_types = array('repository_item');
if (in_array($node->type, $content_types) ) {
_cals_importer_create_marc_stub($node);
}
else{
drupal_set_message(t("Invalid selection: not a repos item"));
}
}//end main if
return "";
}
/**
* Updates the node with stub marc-xml record
*
* @param $node
* the node passed to the parser
*
*/
function _cals_importer_create_marc_stub($node) {
$tags = _cals_importer_get_tag_array(); //tag mappings
$lint = new File_MARC_Lint();
$xml_string = $node->field_xml_string->value();
$xml_string = MARC_HEADER . $xml_string . '</collection>';
$records = new File_MARCXML($xml_string, File_MARC::SOURCE_STRING, "http://www.loc.gov/MARC21/slim");
$n = 0;
while ($record = $records->next()) {
//$xml = simplexml_load_string($xml_string);
_cals_importer_exporter_marcxml_remove_tags($record, $tags);
//get marc-ready array
$marcfields = _cals_importer_exporter_get_marc_fields($node, $record, $tags);
//Serialized Stub
$node->field_xml_stub->set(serialize($marcfields));
//with new datafields, generate the new record
_cals_importer_exporter_build_new_record($record, $marcfields);
$valid = $lint->checkRecord($record);
if(count($valid)) {
//NO - error log these, do not spam the message for each record
$message = "XML Stub Created with following errors: @errors";
$variables = array('@errors' => $valid);
watchdog('cals_importer_exporter_FileMARC_Linter',
$message,
$variables,
$severity = WATCHDOG_NOTICE,
$link = NULL);
}
//Do not save XML
//$new_xml_stub = $record->toXML("UTF-8", FALSE, FALSE);
//$node->field_xml_stub_xml[LANGUAGE_NONE][0]['value'] = $new_xml_stub;
//_cals_importer_update_admin_audit($node, $action);
$node->save();
}
}
/**
* Returns a slightly cleaned up array of all tags
* @param $entity
* @return array|void
*/
function _cals_importer_exporter_rebuild_array_marc_tag($entity) {
$me = array();
$wrapper = $entity->wrapper();
$tag = trim($wrapper->field_marc_tag->value());
$tag_type = $wrapper->field_marc_tag_type->value();
if($tag_type < 2) return;
$me['tag'] = $tag;
$me['tag_type'] = $tag_type;
$me['callback'] = '_cals_importer_exporter_callback_' . $tag;
$me['callback_exists'] = (function_exists($me['callback'])) ? TRUE : FALSE;
$me['ind1'] = $wrapper->field_indc1->value(); // $wrapper->field_indc1->value();
$me['ind2'] = $wrapper->field_ind2->value(); //$wrapper->field_ind2->value();
$fields = array(
//'id',
'title',
'field_include_in_export',
'field_functions',
'field_source_value',
'field_marc_tag_type',
);
foreach($fields as $field) {
$info = $wrapper->{$field}->info();
$abbrev = str_replace("field_", '', $field);
switch($field) {
case 'title':
$me[$abbrev] = $wrapper->{$field}->value(array('sanitize' => TRUE));
break;
case 'field_description':
$me[$abbrev] = $wrapper->{$field}->value(array('decode' => TRUE));
break;
default:
$value = $wrapper->{$field}->value();
if(is_string($value)) $me[$abbrev] = trim($value);
break;
}
}
//if tag type = datafield
if($tag_type == 3) {
$subs = array();
$me['subfields'] = _cals_importer_get_subfields_collection_fields($wrapper);
}
//$me['description'] = trim($wrapper->field_description->value());
return $me;
}
/**
* @param $wrapper
* @return array
*/
function _cals_importer_get_subfields_collection_fields($wrapper) {
$subfields = array();
foreach($wrapper->field_marc_subfield_collection as $fc_wrapper) {
$info = $fc_wrapper->getPropertyInfo();
$subs = array();
$code = $fc_wrapper->field_subfield_code->value();
foreach($fc_wrapper as $fieldname => $array) {
if(substr($fieldname, 0, 6) == 'field_' && $fieldname != 'field_name') {
$str = str_replace("field_", '', $fieldname);
$fieldtype = trim($info[$fieldname]['type']) . "x"; //for some reason, can't check this.
if($str == 'drupal_fieldname') {
$val = $fc_wrapper->{$fieldname}[0]->value();
}
else {
$val = trim($fc_wrapper->{$fieldname}->value());
}
if($str != 'description')
$subs[$str] = $val;
}
}
$subfields[$code] = $subs;
}
return $subfields;
}
/**
* Returns an array of all tags in structure similar to this:
* $me['datafields'][245] = array(
* 'ind1'=> 1, 'ind2 => 2,
*
* 'subfields' = array(
* 'a' => array('value' => 'Value of 245$a tag', 'required' => TRUE|FALSE ),
* 'b' => array('value' => 'Value of 245$b tag', 'required' => TRUE|FALSE ),
* )
* );
* @return array
*/
function _cals_importer_get_tag_array() {
//$rs = db_query("select * from {eck_marc_tag} ");
$es = entity_load("marc_tag", FALSE, array(), TRUE);
$me = array();
foreach($es as $id => $e) {
if(!empty($e->field_marc_tag_type) && !empty($e->field_export_type) &&
$e->field_marc_tag_type[LANGUAGE_NONE][0]['value'] > 1 &&
$e->field_export_type[LANGUAGE_NONE][0]['value'] == 1) {
$tagset = _cals_importer_exporter_rebuild_array_marc_tag($e);
if(is_array($tagset)) {
$key = $tagset['tag'];
$type = ($e->field_marc_tag_type[LANGUAGE_NONE][0]['value'] == 3) ? 'datafields' : 'controlfields';
//printPre("type = " . $type, $tagset['tag'], $e->field_marc_tag_type[LANGUAGE_NONE][0]['value']);
$me[$type][$key] = $tagset;
}
}
}
ksort($me['datafields']);
//printAndDie($me);
return $me;
}
/**
* Returns array of values for a given node + fieldname.
* Used by various callbacks.
*
* @param $node
* @param $fieldname
* @return mixed
*/
function _cals_importer_exporter_field_value($node, $fieldname) {
// Ensure what is passed here is a stdClass node and not an
// EntityDrupalWrapper or other wrapper as used elsewhere.
// This causes deep break in entity_translation module.
if (is_a($node, 'EntityDrupalWrapper')) $node = node_load($node->getIdentifier());
$values = field_view_field('node', $node, $fieldname, array('default'));
return $values;
}
/**
* Returns array of values for a given node + fieldname.
* Used by various callbacks.
*
* @param $node
* @param $fieldname
* @return mixed
*/
function _cals_importer_exporter_field_value_taxonomy_term($node, $fieldname) {
//printAndDie($values);
$values = field_view_field('taxonomy_term', $node, $fieldname, array('default'));
return $values;
}
/**
* Returns the subfield array when provided w/ a Drupal fieldname + code +
* wrappper
*
* @param $code
* @param $fieldname
* @param $wrapper
* @return array
*/
function _cals_importer_exporter_get_subfield_array_from_drupal_fields($code, $fieldname, $wrapper, $enumerations = NULL) {
$arr = array();
if(!empty($wrapper->{$fieldname}->value())) {
$values = $wrapper->{$fieldname}->value();
if(is_string($values) && !empty(trim($values))) {
$arr[][$code] = (is_array($enumerations)) ? $enumerations[$values] : $values;
}
if(is_array($values)) {
foreach($values as $value) {
$arr[][$code] = (is_array($enumerations)) ? $enumerations[$values] : $value;
}
}
}
//if($code == 'z' and $fieldname == 'field_isbn') printAndDie($arr);
return $arr; // = array();
}
/**
* Returns a subfield array when given a tag and existing subfield array;
*
* @param $tag
* @param $subfields
* @return array
*
*/
function _cals_build_marc_subfield_array($tag, $subfields) {
if(is_array($subfields) && count($subfields)) {
return array($tag => array(
'subfields' => $subfields,
));
}
return array();
}
/**
* Returns a normalized title field
* @param $value
*/
function normalize_title(&$value) {
$last = substr($value, -1);
if($last != '.') $value .= '.';
}
/**
* @param $code
* @return string
*/
function _cals_importer_exporter_get_normalized_code($code) {
$code = trim($code);
if(is_numeric($code)) {
if($code > 10 && $code < 100 ) return "0" . $code;
if($code < 10) return "00" . $code;
return $code;
}
return $code;
}
/**
* Remove tags from record
* @param $record
*/
function _cals_importer_exporter_marcxml_remove_tags(&$record, $tags) {
$tags = array();
//control tags to delete
$arr = array('001', '005', '008');
foreach ($record->getFields() as $tag=>$value) {
if ($value instanceof File_MARC_Data_Field || in_array($tag, $arr) ) {
$tags[] = $tag;
}
else {
if($tag == 0051111) $tags[] = $tag;
}
}
foreach($tags as $tag) {
$n = $record->deleteFields($tag);
}
}
/**
* Parses the original marc file to find values associated w/ specific tag / indicators
* @param $tagset
* @param $code
* @param $record
* @param $xml
* @return array|void
*/
function _cals_importer_exporter_parse_original_marc($tagset, $code, $record, $xml) {
$ind1 = $tagset['ind1'];
$ind2 = $tagset['ind2'];
$tag = $tagset['tag'];
//$subdata = $tagset['subfields'][$code];
$values = _cals_importer_get_xpath_values($tag, NULL, NULL, $code, $xml);
//if($tag == 720) printAndDie($code, $tagset, $subfields, $values);
if(empty($values)) return;
return $values;
}
/**
* @param $node_wrapper
* @param $record
* @param $tags
* @return array
*/
function _cals_importer_exporter_get_marc_fields($node_wrapper, &$record, $tags) {
$fields = array();
$xml = _cals_get_marc_xml_string($node_wrapper);
//Load the stdClass node in case of legacy calls
$node = node_load($node_wrapper->getIdentifier());
/* Note:
* _cals_importer_exporter_parse_(controlfields|datafields) called below
* $function($node_wrapper, $record, $tagset, $xml)
*/
foreach ($tags as $type => $tagset) {
$function = '_cals_importer_exporter_parse_' . $type;
$tmp = $function($node, $node_wrapper, $record, $tagset, $xml);
if(!empty($tmp))
$fields = $fields + $tmp;
}
ksort($fields);
return $fields;
}
/**
* @param $node
* @param $node_wrapper
* @param $record
* @param $tags
* @param $xml
* @return mixed
*/
function _cals_importer_exporter_parse_controlfields($node, $node_wrapper, &$record, $tags, $xml) {
foreach($tags as $tag => $arr) {
switch($tag) {
case 'leader':
case 'ldr':
//$val = _cals_importer_exporter_callback_leader($node_wrapper);
$val = $record->getLeader();
break;
case '001':
//see https://github.com/BCLibCoop/nnels/issues/130
//and https://github.com/BCLibCoop/nnels/issues/193
$val = 'BVNNELS' . $node_wrapper->getIdentifier();
break;
case '003':
$val = 'BVNNELS';
break;
case '005':
//yyyymmddhhmmss.f
if (is_numeric($node_wrapper->changed->value())) $val = date('YmdHis.0', $node_wrapper->changed->value());
break;
case '008':
// Not in use
// $xml = _cals_get_marc_xml_string($node);
// $val = _cals_importer_export_callback_008($node, $node_wrapper, $xml);
break;
}
$tmp['controlfields'][$tag] = array(
'value' => $val,
'required' => $arr['include_in_export']
);
}
return $tmp;
}
/**
* @param $node
* @param $node_wrapper
* @param $record
* @param $tags
* @param $xml
* @return array
*/
function _cals_importer_exporter_parse_datafields($node ,$node_wrapper, &$record, $tags, $xml) {
//$xml = _cals_get_marc_xml_string($node);
$arr = array();
$tmp = array();
foreach($tags as $tag => $tagset) {
$callback_exists = $tagset['callback_exists'];
try {
$subs = $tagset['subfields'];
//Treat parent tag as a unit if callback exists.
if ($callback_exists) {
$vars = array(
'node' => $node,
'tag' => $tag,
'node_wrapper' => $node_wrapper,
'xml' => $xml,
);
$callback = $tagset['callback'];
$tmp = $callback($vars);
}
else {
//parse subfields
$tmp = _cals_importer_exporter_parse_subfields($tagset, $node_wrapper, $record, $xml);
if (is_array($tmp) && !empty($tmp[$tag]['subfields'])) {
$ind1 = $tagset['ind1'];
$ind2 = $tagset['ind2'];
//if($ind1 = "''") $ind1 = '';
if($ind2 = "''") $ind2 = '';
if ($ind1 == 'use MarcXML indicators') {
$ind1 = _cals_importer_get_marc_indicators($xml, $tag, 1);
}
if ($ind2 == 'use MarcXML indicators') {
$ind2 = _cals_importer_get_marc_indicators($xml, $tag, 2);
}
$tmp[$tag]['ind1'] = $ind1;
$tmp[$tag]['ind2'] = $ind2;
}
}
//include the array even if empty so we can track invalid records
if(!empty($tmp[$tag]['subfields']) ||
(isset($tmp[$tag]['required']) && $tmp[$tag]['required'] == 1)) {
$arr['datafields'][$tag] = $tmp[$tag];
$arr['datafields'][$tag]['required'] = $tagset['include_in_export'];
}
}
//catch exception
catch(Exception $e) {
drupal_set_message('Message: issue with ' . $tag . ": " .$e->getMessage());
}
}
return $arr;
}
/**
* @param $tagset
* @param $wrapper
* @param $record
* @param $xml
* @return array
*/
function _cals_importer_exporter_parse_subfields($tagset, $wrapper, $record, $xml) {
$tag = $tagset['tag'];
$datafields = $tagset['subfields'];
$subfields = array();
foreach($datafields as $code => $fields) {
$code = trim($fields['subcode']);
$cb = "_cals_importer_exporter_callback_" . $tag . "_" . $code;
//check to see if there is a callback for specific code
if (function_exists($cb)) {
//still working on these!!
$subfields[$code] = array("YO PLACEHOLDER");
}
//for the rare fields that we pre-assign values to, e.g., 300$a and 300$b
elseif (!empty($fields['static_value'])) {
$tmp = $fields['static_value'];
$subfields[$code][] = $tmp;
}
//or do we extract the value directly from Drupal?
elseif (!empty($fields['drupal_fieldname'])) {
$tmp = $fields['drupal_fieldname'];
if (isset($wrapper->{$tmp})) {
$values = $wrapper->{$tmp}->value();
$subfields[$code] = $values;
}
else {
drupal_set_message( t("Field not found: please check 'marc-tags-entity-view'
to ensure there's no mismatch on fieldname {$tmp} entered for tag = $tag
and subcode = $code") );
}
}
//Otherwise, reparse the original marc record string
else {
$tmp = _cals_importer_exporter_parse_original_marc($tagset, $code, $record, $xml);
if (!empty($tmp)) {
foreach($tmp as $k => $v) {
$subfields[$k][$code] = trim($v);
}
}
}
}
$me[$tag]['subfields'] = $subfields;
return $me;
}
/**
* Loops thru control and datafields array to return new MARC XML record
*
* @param $record
* @param $marcfields
*/
function _cals_importer_exporter_build_new_record(&$record, $marcfields) {
foreach ($marcfields as $type => $arr) {
$fields = $marcfields[$type];
$function = '_cals_importer_exporter_iterate_' . $type;
$function($record, $fields);
}
}
/**
* Iterate thru array of controlfields to insert xml controlfields
* @param $record
* @param $controlfields
*/
function _cals_importer_exporter_iterate_controlfields(&$record, $controlfields) {
foreach($controlfields as $tag => $arr) {
//Skip ldr, we handle it specially after initial record build,
//see *_build_final_leader called from *_generate_marcxml_files_search_interface
if ($tag == 'ldr') break;
$data = $arr['value'];
$field = new File_MARC_Control_Field($tag, $data, null, null);
if (isset($arr['required']) && $arr['required'] == 1 && empty($data)) {
drupal_set_message("Missing value for tag $tag. Invalid XML Record Stub.");
return;
}
_cals_importer_exporter_insert_field($record, $field, $tag);
}
}
/**
* Iterate thru array of datafields to insert xml datafields
* @param $record
* @param $datafields
* @throws \File_MARC_Exception
*/
function _cals_importer_exporter_iterate_datafields(&$record, $datafields) {
$marc_subfields = array();
foreach($datafields as $tag => $arr) {
$subfields = $arr['subfields'];
if(empty($subfields)) {
if($arr['required'] == 1)
//throw error msg
drupal_set_message("Missing subfields for tag $tag. Invalid XML Record Stub.");
}
else {
//if($code == 720) printAndDie($marc_subfields);
if($tag != 7200) {
foreach($subfields as $subfield) {
$marc_subfields = _cals_importer_exporter_get_marc_subfield($subfield);
$field = new File_MARC_Data_Field($tag, $marc_subfields, 0, null);
if ( isset( $arr['ind1'] ) ) $field->setIndicator(1, "{$arr['ind1']}");
if ( isset( $arr['ind2'] ) ) $field->setIndicator(2, "{$arr['ind2']}");
_cals_importer_exporter_insert_field($record, $field, $tag);
//printAndDie($field);
/*
foreach($marc_subfields as $marc_subfield) {
$tmp = array($marc_subfield);
printAndDie($code, $tmp, $subfields);
$field = new File_MARC_Data_Field($code, $tmp, 0, null);
$field->setIndicator(1, "{$arr['ind1']}");
$field->setIndicator(2, "{$arr['ind2']}");
_cals_importer_exporter_insert_field($record, $field, $tag);
}
*/
}
}
else { //old method - does this get called?
$field = new File_MARC_Data_Field($tag, $marc_subfields, 0, null);
$field->setIndicator(1, "{$arr['ind1']}");
$field->setIndicator(2, "{$arr['ind2']}");
_cals_importer_exporter_insert_field($record, $field, $tag);
}
}
}
}
/**
* Get MARC formatted subfields (see FILE_MARC)
* @param $arr
* @return array
*/
function _cals_importer_exporter_get_marc_subfield($arr) {
$subfields = array();
foreach ($arr as $code => $value) {
$subfields[] = new File_MARC_Subfield($code, $value);
//foreach($values as $value) { }
}
return $subfields;
}
/**
* Add field to record structure
* @param $record
* @param $field
* @param $id
*
*/
function _cals_importer_exporter_insert_field(&$record, $field, $id) {
//e.g., id = 650
//_cals_importer_exporter_get_datafields($record);
$record->appendField($field);
}
/**
* View marc-xml stub for repository item
* @param $node
* @return array|mixed|string|null
* @throws \Exception
*/
function _cals_importer_view_marcxml_stub($node) {
if (!empty($node->field_xml_stub)) {
$array = unserialize($node->field_xml_stub[LANGUAGE_NONE][0]['value']);
$items = array();
foreach ($array as $type => $tags) {
foreach ($tags as $tag => $content) {
if ($type == 'controlfields') {
$str = $content['value'];
if($tag == '008') $str = str_replace(' ', '.', $str);;
$items[] = "$tag = $str";
}
else {
if (!empty($content['subfields'])) {
foreach ($content['subfields'] as $i => $subfields) {
$ind1 = (isset($content['ind1'])) ? $content['ind1'] : '';
$ind2 = (isset($content['ind2'])) ? $content['ind2'] : '';
if (is_array($subfields)) {
foreach ($subfields as $subcode => $str) {
if (empty($subcode)) {
drupal_set_message("Issue: $tag requires a valid code");
return '';
}
$items[] = "$tag\$" . $subcode . "($ind1)($ind2) = $str";
}
}
}
}
}
}
}
$vars = array(
'title' => t('Currently saved array of MARC ready fields'),
'items' => $items,
'type' => 'ul',
'attributes' => array()
);
$html = theme('item_list', $vars);
// if(!empty($node->field_xml_stub_xml)) {
// $html .= t('<h2>New MARC XML</h2>') .
// _cals_importer_exporter_formatted_marcxml(
// $node->field_xml_stub_xml[LANGUAGE_NONE][0]['safe_value']
// );
// }
if(!empty($node->field_xml_string)) {
$html .= t('<h2>Original MARC XML</h2>') .
_cals_importer_exporter_formatted_marcxml(
$node->field_xml_string[LANGUAGE_NONE][0]['safe_value']
);
}
return $html;
}
else{
return t('Stub has not been defined. You can click on Create XML Stub entry to generate this');
}
}
/**
* Pretties up the marc xml string
* @param $str
* @return mixed
*/
function _cals_importer_exporter_formatted_marcxml($str) {
$str = str_replace("marc:", "", $str);
$str = str_replace("<leader", "<br/> " . "<leader", $str);
$str = str_replace("<controlfield", "<br/> " . "<controlfield", $str);
$str = str_replace("<datafield", "<br/> " . "<datafield", $str);
$str = str_replace("</datafield", "<br/> " . "</datafield", $str);
$str = str_replace("<subfield", "<br/> " . "<subfield", $str);
$str = str_replace("<record", "<br/>" . "<record", $str);
$str = str_replace("</record", "<br/>" . "</record", $str);
return $str;
}
/**
* @param $node
* @return mixed
*/
function _cals_importer_exporter_build_marcxml_from_array($node) {
$record = new File_MARC_Record(NULL);
$marcfields = unserialize($node->field_xml_stub[LANGUAGE_NONE][0]['value']);
if(function_exists('dpm')) dpm($marcfields);
_cals_importer_exporter_array_to_marcxml($record, $marcfields);
$lint = new File_MARC_Lint();
$valid = $lint->checkRecord($record);
if(count($valid)) {
$message = "XML Stub Created with following errors: @errors";
$variables = array('@errors' => $valid);
watchdog('cals_importer_exporter_FileMARC_Linter',
$message,
$variables,
$severity = WATCHDOG_NOTICE,
$link = NULL);
}
return $record->__toString();
//drupal_goto("node/{$node->nid}/build-validate-marcxml-output");
}
/**
* Returns a new record based on datafields
*
* @param $record
* @param $datafields
*/
function _cals_importer_exporter_array_to_marcxml(&$record, $marcfields) {
$xml = array();
foreach ($marcfields as $type => $arr) {
$function = '_cals_importer_exporter_array_to_marcxml_' . $type;
if ($function) {
$function($record, $arr);
}
}
}
/**
* Create marcxml controlfields
* @param $record
* @param $tags
*/
function _cals_importer_exporter_array_to_marcxml_controlfields(&$record, $tags) {
$xml = array();
foreach($tags as $tag => $value) {
$field = new File_MARC_Control_Field($tag, $value['value'], null, null);
$record->appendField($field);
}
}
/**
* Create marcxml datafields
* @param $record
* @param $tags
*/
function _cals_importer_exporter_array_to_marcxml_datafields(&$record, $tags) {
$xml = array();
foreach($tags as $tag => $tagset) {
$subfields = array();
$ind1 = $tagset['ind1'];
$ind2 = $tagset['ind2'];
foreach ($tagset['subfields'] as $code => $values) {
foreach ($values as $value) {
$subfields[] = new File_MARC_Subfield($code, $value);
}
$field = new File_MARC_Data_Field($tag, $subfields, "$ind1", "$ind2");
//dpm("tag = $tag, ind1 = $ind1, ind2 = $ind2");
$record->appendField($field);
}
}
}