/
csvImportTask.class.php
1205 lines (1065 loc) · 42.4 KB
/
csvImportTask.class.php
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* This file is part of the Access to Memory (AtoM) software.
*
* Access to Memory (AtoM) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Access to Memory (AtoM) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Access to Memory (AtoM). If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Import csv data
*
* @package symfony
* @subpackage task
* @author Mike Cantelon <mike@artefactual.com>
*/
class csvImportTask extends csvImportBaseTask
{
protected $namespace = 'csv';
protected $name = 'import';
protected $briefDescription = 'Import csv information object data';
protected $detailedDescription = <<<EOF
Import CSV data
EOF;
/**
* @see sfTask
*/
protected function configure()
{
parent::configure();
$this->addOptions(array(
new sfCommandOption(
'source-name',
null,
sfCommandOption::PARAMETER_OPTIONAL,
'Source name to use when inserting keymap entries.'
),
new sfCommandOption(
'default-parent-slug',
null,
sfCommandOption::PARAMETER_OPTIONAL,
'Parent slug under which imported items, with no parent specified, will be added.'
),
new sfCommandOption(
'default-legacy-parent-id',
null,
sfCommandOption::PARAMETER_OPTIONAL,
'Legacy parent ID under which imported items, with no parent specified, will be added.'
),
new sfCommandOption(
'skip-nested-set-build',
null,
sfCommandOption::PARAMETER_NONE,
"Don't build the nested set upon import completion."
),
new sfCommandOption(
'index',
null,
sfCommandOption::PARAMETER_NONE,
"Index for search during import."
)
));
}
/**
* @see sfTask
*/
public function execute($arguments = array(), $options = array())
{
parent::execute($arguments, $options);
$this->validateOptions($options);
$skipRows = ($options['skip-rows']) ? $options['skip-rows'] : 0;
// source name can be specified so, if importing from multiple
// sources, you can accommodate legacy ID collisions in files
// you import from different places
$sourceName = ($options['source-name'])
? $options['source-name']
: basename($arguments['filename']);
if (false === $fh = fopen($arguments['filename'], 'rb'))
{
throw new sfException('You must specify a valid filename');
}
$databaseManager = new sfDatabaseManager($this->configuration);
$conn = $databaseManager->getDatabase('propel')->getConnection();
// set default publication status
$results = $conn->query('SELECT i18n.value
FROM setting INNER JOIN setting_i18n i18n ON setting.id = i18n.id
WHERE setting.name=\'defaultPubStatus\'');
if ($results)
{
$defaultStatusId = $results->fetchColumn();
}
else
{
$defaultStatusId = QubitTerm::PUBLICATION_STATUS_PUBLISHED_ID;
}
// TODO: this may be unnecessary as it may now be part of Qubit trunk
// create note term if it doesn't yet exist
QubitFlatfileImport::createOrFetchTerm(
QubitTaxonomy::NOTE_TYPE_ID,
'Language note'
);
QubitFlatfileImport::createOrFetchTerm(
QubitTaxonomy::RAD_NOTE_ID,
'Cast note'
);
QubitFlatfileImport::createOrFetchTerm(
QubitTaxonomy::RAD_NOTE_ID,
'Credits note'
);
QubitFlatfileImport::createOrFetchTerm(
QubitTaxonomy::RAD_NOTE_ID,
'Signatures note'
);
// Load taxonomies into variables to avoid use of magic numbers
$termData = QubitFlatfileImport::loadTermsFromTaxonomies(array(
QubitTaxonomy::DESCRIPTION_STATUS_ID => 'descriptionStatusTypes',
QubitTaxonomy::PUBLICATION_STATUS_ID => 'pubStatusTypes',
QubitTaxonomy::DESCRIPTION_DETAIL_LEVEL_ID => 'levelOfDetailTypes',
QubitTaxonomy::NOTE_TYPE_ID => 'noteTypes',
QubitTaxonomy::RAD_NOTE_ID => 'radNoteTypes',
QubitTaxonomy::RAD_TITLE_NOTE_ID => 'titleNoteTypes',
QubitTaxonomy::MATERIAL_TYPE_ID => 'materialTypes',
QubitTaxonomy::RIGHT_ACT_ID => 'copyrightActTypes',
QubitTaxonomy::COPYRIGHT_STATUS_ID => 'copyrightStatusTypes',
QubitTaxonomy::PHYSICAL_OBJECT_TYPE_ID => 'physicalObjectTypes'
));
// Allow default parent ID to be overridden by CLI options
if ($options['default-parent-slug'])
{
$defaultParentId = getIdCorrespondingToSlug($options['default-parent-slug']);
if (!$options['quiet'])
{
print 'Parent ID of slug "'. $options['default-parent-slug'] .'" is '. $defaultParentId;
}
}
else if ($options['default-legacy-parent-id'])
{
// attempt to fetch keymap entry
$keyMapEntry = QubitFlatfileImport::fetchKeymapEntryBySourceAndTargetName(
$options['default-legacy-parent-id'],
$sourceName,
'information_object'
);
if ($keyMapEntry)
{
$defaultParentId = $keyMapEntry->target_id;
}
else
{
throw new sfException('Could not find Qubit ID corresponding to legacy ID.');
}
print 'Using default parent ID '. $defaultParentId .' (legacy parent ID '. $options['default-legacy-parent-id'] .")\n";
}
else
{
$defaultParentId = QubitInformationObject::ROOT_ID;
}
// Define import
$import = new QubitFlatfileImport(array(
/* Pass context */
'context' => sfContext::createInstance($this->configuration),
/* What type of object are we importing? */
'className' => 'QubitInformationObject',
/* Allow silencing of progress info */
'displayProgress' => ($options['quiet']) ? false : true,
/* How many rows should import until we display an import status update? */
'rowsUntilProgressDisplay' => $options['rows-until-update'],
/* Where to log errors to */
'errorLog' => $options['error-log'],
/* the status array is a place to put data that should be accessible
from closure logic using the getStatus method */
'status' => array(
'options' => $options,
'sourceName' => $sourceName,
'defaultParentId' => $defaultParentId,
'copyrightStatusTypes' => $termData['copyrightStatusTypes'],
'copyrightActTypes' => $termData['copyrightActTypes'],
'defaultStatusId' => $defaultStatusId,
'descriptionStatusTypes' => $termData['descriptionStatusTypes'],
'pubStatusTypes' => $termData['pubStatusTypes'],
'levelOfDetailTypes' => $termData['levelOfDetailTypes'],
'materialTypes' => $termData['materialTypes'],
'physicalObjectTypes' => $termData['physicalObjectTypes'],
),
/* import columns that map directory to QubitInformationObject properties */
'standardColumns' => array(
'updatedAt',
'createdAt',
'accessConditions',
'accruals',
'acquisition',
'alternateTitle',
'appraisal',
'archivalHistory',
'arrangement',
'culture',
'descriptionIdentifier',
'extentAndMedium',
'findingAids',
'identifier',
'locationOfCopies',
'locationOfOriginals',
'physicalCharacteristics',
'relatedUnitsOfDescription',
'reproductionConditions',
'revisionHistory',
'rules',
'scopeAndContent',
'sources',
'title'
),
/* import columns that should be redirected to QubitInformationObject
properties (and optionally transformed)
Example:
'columnMap' => array(
'Archival History' => 'archivalHistory',
'Revision history' => array(
'column' => 'revision',
'transformationLogic' => function(&$self, $text)
{
return $self->appendWithLineBreakIfNeeded(
$self->object->revision,
$text
);
}
)
),
*/
'columnMap' => array(
'radEdition' => 'edition',
'institutionIdentifier' => 'institutionResponsibleIdentifier'
),
/* import columns that can be added using the
QubitInformationObject::addProperty method */
'propertyMap' => array(
'radOtherTitleInformation' => 'otherTitleInformation',
'radTitleStatementOfResponsibility' => 'titleStatementOfResponsibility',
'radStatementOfProjection' => 'statementOfProjection',
'radStatementOfCoordinates' => 'statementOfCoordinates',
'radStatementOfScaleArchitectural' => 'statementOfScaleArchitectural',
'radStatementOfScaleCartographic' => 'statementOfScaleCartographic',
'radPublishersSeriesNote' => 'noteOnPublishersSeries',
'radIssuingJurisdiction' => 'issuingJurisdictionAndDenomination',
'radEditionStatementOfResponsibility' => 'editionStatementOfResponsibility',
'radTitleProperOfPublishersSeries' => 'titleProperOfPublishersSeries',
'radParallelTitlesOfPublishersSeries' => 'parallelTitleOfPublishersSeries',
'radOtherTitleInformationOfPublishersSeries' => 'otherTitleInformationOfPublishersSeries',
'radStatementOfResponsibilityRelatingToPublishersSeries' => 'statementOfResponsibilityRelatingToPublishersSeries',
'radNumberingWithinPublishersSeries' => 'numberingWithinPublishersSeries',
'radStandardNumber' => 'standardNumber'
),
/* import columns that can be added as QubitNote objects */
'noteMap' => array(
'languageNote' => array(
'typeId' => array_search('Language note', $termData['noteTypes'])
),
'publicationNote' => array(
'typeId' => array_search('Publication note', $termData['noteTypes'])
),
'generalNote' => array(
'typeId' => array_search('General note', $termData['noteTypes'])
),
'archivistNote' => array(
'typeId' => array_search("Archivist's note", $termData['noteTypes'])
),
'radNoteCast' => array(
'typeId' => array_search('Cast note', $termData['radNoteTypes'])
),
'radNoteCredits' => array(
'typeId' => array_search('Credits note', $termData['radNoteTypes'])
),
'radNoteSignaturesInscriptions' => array(
'typeId' => array_search('Signatures note', $termData['radNoteTypes'])
),
'radNoteConservation' => array(
'typeId' => array_search('Conservation', $termData['radNoteTypes'])
),
'radNoteGeneral' => array(
'typeId' => array_search('General note', $termData['noteTypes'])
),
'radNotePhysicalDescription' => array(
'typeId' => array_search('Physical description', $termData['radNoteTypes'])
),
'radNotePublishersSeries' => array(
'typeId' => array_search("Publisher's series", $termData['radNoteTypes'])
),
'radNoteRights' => array(
'typeId' => array_search('Rights', $termData['radNoteTypes'])
),
'radNoteAccompanyingMaterial' => array(
'typeId' => array_search('Accompanying material', $termData['radNoteTypes'])
),
'radNoteAlphaNumericDesignation' => array(
'typeId' => array_search('Alpha-numeric designations', $termData['radNoteTypes'])
),
'radNoteEdition' => array(
'typeId' => array_search('Edition', $termData['radNoteTypes'])
),
'radTitleStatementOfResponsibilityNote' => array(
'typeId' => array_search('Statements of responsibility', $termData['titleNoteTypes'])
),
'radTitleParallelTitles' => array(
'typeId' => array_search('Parallel titles and other title information', $termData['titleNoteTypes'])
),
'radTitleSourceOfTitleProper' => array(
'typeId' => array_search('Source of title proper', $termData['titleNoteTypes'])
),
'radTitleVariationsInTitle' => array(
'typeId' => array_search('Variations in title', $termData['titleNoteTypes'])
),
'radTitleAttributionsAndConjectures' => array(
'typeId' => array_search('Attributions and conjectures', $termData['titleNoteTypes'])
),
'radTitleContinues' => array(
'typeId' => array_search('Continuation of title', $termData['titleNoteTypes'])
),
'radTitleNoteContinuationOfTitle' => array(
'typeId' => array_search('Continuation of title', $termData['titleNoteTypes'])
)
),
/* these values get stored to the rowStatusVars array */
'variableColumns' => array(
'legacyId',
'parentId',
'copyrightStatus',
'copyrightExpires',
'copyrightHolder',
'qubitParentSlug',
'descriptionStatus',
'publicationStatus',
'levelOfDetail',
'repository',
'language',
'script',
'languageOfDescription',
'scriptOfDescription',
'physicalObjectName',
'physicalObjectLocation',
'physicalObjectType',
'physicalStorageLocation',
'digitalObjectPath',
'digitalObjectURI'
),
/* these values get exploded and stored to the rowStatusVars array */
'arrayColumns' => array(
'accessionNumber' => '|',
'creators' => '|',
'creatorHistories' => '|',
'creationDates' => '|',
'creationDateNotes' => '|',
'creationDatesStart' => '|',
'creationDatesEnd' => '|',
'creatorDates' => '|', // These 4 columns are for backwards compatibility
'creatorDatesStart' => '|',
'creatorDatesEnd' => '|',
'creatorDateNotes' => '|',
'nameAccessPoints' => '|',
'nameAccessPointHistories' => '|',
'placeAccessPoints' => '|',
'placeAccessPointHistories' => '|', // not yet implemented
'subjectAccessPoints' => '|',
'subjectAccessPointScopes' => '|', // not yet implemented
'eventActors' => '|',
'eventTypes' => '|',
'eventPlaces' => '|',
'eventDates' => '|',
'eventStartDates' => '|',
'eventEndDates' => '|',
'eventDescriptions' => '|',
'alternativeIdentifiers' => '|',
'alternativeIdentifierLabels' => '|'
),
/* import logic to execute before saving information object */
'preSaveLogic' => function(&$self)
{
// set repository
if (isset($self->rowStatusVars['repository']) && $self->rowStatusVars['repository'])
{
$repository = $self->createOrFetchRepository($self->rowStatusVars['repository']);
$self->object->repositoryId = $repository->id;
}
// set level of detail
if (isset($self->rowStatusVars['levelOfDetail']) && 0 < strlen($self->rowStatusVars['levelOfDetail']))
{
$levelOfDetail = trim($self->rowStatusVars['levelOfDetail']);
$levelOfDetailTermId = array_search_case_insensitive($levelOfDetail, $self->status['levelOfDetailTypes']);
if ($levelOfDetailTermId === false)
{
print "\nTerm $levelOfDetail not found in description details level taxonomy, creating it...\n";
$culture = isset($self->object->culture) ? $self->object->culture : 'en';
$newTerm = QubitFlatfileImport::createTerm(
QubitTaxonomy::DESCRIPTION_DETAIL_LEVEL_ID,
$levelOfDetail,
$culture
);
$levelOfDetailTermId = $newTerm->id;
$self->status['levelOfDetailTypes'] = refreshTaxonomyTerms(QubitTaxonomy::DESCRIPTION_DETAIL_LEVEL_ID);
}
$self->object->descriptionDetailId = $levelOfDetailTermId;
}
// storage language-related properties as serialized data
$languageProperties = array(
'language',
'script',
'languageOfDescription',
'scriptOfDescription'
);
foreach ($languageProperties as $serializeProperty)
{
if (isset($self->rowStatusVars[$serializeProperty]) && 0 < strlen($self->rowStatusVars[$serializeProperty]))
{
$data = explode('|', $self->rowStatusVars[$serializeProperty]);
$self->object->addProperty($serializeProperty, serialize($data));
}
}
// add alternative identifiers
if (array_key_exists('alternativeIdentifiers', $self->rowStatusVars) &&
array_key_exists('alternativeIdentifierLabels', $self->rowStatusVars))
{
setAlternativeIdentifiers(
$self->object,
$self->rowStatusVars['alternativeIdentifiers'],
$self->rowStatusVars['alternativeIdentifierLabels']
);
}
// set description status
if (isset($self->rowStatusVars['descriptionStatus']) && 0 < strlen($self->rowStatusVars['descriptionStatus']))
{
$descStatus = trim($self->rowStatusVars['descriptionStatus']);
$statusTermId = array_search_case_insensitive($descStatus, $self->status['descriptionStatusTypes']);
if (false !== $statusTermId)
{
$self->object->descriptionStatusId = $statusTermId;
}
else
{
print "\nTerm $descStatus not found in description status taxonomy, creating it...\n";
$culture = isset($self->object->culture) ? $self->object->culture : 'en';
$newTerm = QubitFlatfileImport::createTerm(QubitTaxonomy::DESCRIPTION_STATUS_ID, $descStatus, $culture);
$self->status['descriptionStatusTypes'] = refreshTaxonomyTerms(QubitTaxonomy::DESCRIPTION_STATUS_ID);
$self->object->descriptionStatusId = $newTerm->id;
}
}
// set publication status
if (isset($self->rowStatusVars['publicationStatus']) && 0 < strlen($self->rowStatusVars['publicationStatus']))
{
$pubStatusTermId = array_search_case_insensitive(
$self->rowStatusVars['publicationStatus'],
$self->status['pubStatusTypes']
);
if (!$pubStatusTermId)
{
print "\nPublication status: '". $self->rowStatusVars['publicationStatus'] ."' is invalid. Using default.\n";
$pubStatusTermId = $self->status['defaultStatusId'];
}
}
else
{
$pubStatusTermId = $self->status['defaultStatusId'];
}
$self->object->setPublicationStatus($pubStatusTermId);
if (isset($self->rowStatusVars['qubitParentSlug']) && $self->rowStatusVars['qubitParentSlug'])
{
$parentId = getIdCorrespondingToSlug($self->rowStatusVars['qubitParentSlug']);
}
else
{
if (!isset($self->rowStatusVars['parentId']) || !$self->rowStatusVars['parentId'])
{
// Don't overwrite valid parentId when adding an i18n row
if (!isset($self->object->parentId))
{
$parentId = $self->status['defaultParentId'];
}
}
else
{
if ($mapEntry = $self->fetchKeymapEntryBySourceAndTargetName(
$self->rowStatusVars['parentId'],
$self->getStatus('sourceName'),
'information_object'
))
{
$parentId = $mapEntry->target_id;
}
else
{
$error = 'For legacyId '
. $self->rowStatusVars['legacyId']
.' Could not find parentId '
. $self->rowStatusVars['parentId']
.' in key_map table';
print $self->logError($error);
}
}
}
if (isset($parentId))
{
$self->object->parentId = $parentId;
}
},
/* import logic to execute after saving information object */
'postSaveLogic' => function(&$self)
{
if (!$self->object->id)
{
throw new sfException('Information object save failed');
}
// add keymap entry
$keymap = new QubitKeymap;
$keymap->sourceId = $self->rowStatusVars['legacyId'];
$keymap->sourceName = $self->getStatus('sourceName');
$keymap->targetId = $self->object->id;
$keymap->targetName = 'information_object';
$keymap->save();
// inherit repository instead of duplicating the association to it
// if applicable
if ($self->object->canInheritRepository($self->object->repositoryId))
{
// Use raw SQL since we don't want an entire save() here.
$sql = 'UPDATE information_object SET repository_id = NULL WHERE id = ?';
QubitPdo::prepareAndExecute($sql, array($self->object->id));
$self->object->repositoryId = null;
}
// add physical objects
if (isset($self->rowStatusVars['physicalObjectName']) &&
$self->rowStatusVars['physicalObjectName'])
{
$names = explode('|', $self->rowStatusVars['physicalObjectName']);
$locations = explode('|', $self->rowStatusVars['physicalObjectLocation']);
$types = (isset($self->rowStatusVars['physicalObjectType']))
? explode('|', $self->rowStatusVars['physicalObjectType'])
: array();
foreach ($names as $index => $name)
{
// if location column populated
if ($self->rowStatusVars['physicalObjectLocation'])
{
// if current index applicable
if (isset($locations[$index]))
{
$location = $locations[$index];
}
else
{
$location = $locations[0];
}
}
else
{
$location = '';
}
// if object type column populated
if ($self->rowStatusVars['physicalObjectType'])
{
// if current index applicable
if (isset($types[$index]))
{
$type = $types[$index];
}
else
{
$type = $types[0];
}
}
else
{
$type = 'Box';
}
$physicalObjectTypeId = array_search_case_insensitive($type, $self->getStatus('physicalObjectTypes'));
// Create new physical object type if not found
if ($physicalObjectTypeId === false)
{
print "\nTerm $type not found in physical object type taxonomy, creating it...\n";
$culture = isset($self->object->culture) ? $self->object->culture : 'en';
$newTerm = QubitFlatfileImport::createTerm(QubitTaxonomy::PHYSICAL_OBJECT_TYPE_ID, $type, $culture);
$self->status['physicalObjectTypes'] = refreshTaxonomyTerms(QubitTaxonomy::PHYSICAL_OBJECT_TYPE_ID);
$physicalObjectTypeId = $newTerm->id;
}
$container = $self->createOrFetchPhysicalObject($name, $location, $physicalObjectTypeId);
// associate container with information object
$self->createRelation($container->id, $self->object->id, QubitTerm::HAS_PHYSICAL_OBJECT_ID);
}
}
// add subject access points
$accessPointColumns = array(
'subjectAccessPoints' => QubitTaxonomy::SUBJECT_ID,
'placeAccessPoints' => QubitTaxonomy::PLACE_ID,
);
foreach ($accessPointColumns as $columnName => $taxonomyId)
{
if (isset($self->rowStatusVars[$columnName]))
{
$index = 0;
foreach ($self->rowStatusVars[$columnName] as $subject)
{
if ($subject)
{
$scope = false;
if (isset($self->rowStatusVars['subjectAccessPointScopes'][$index]))
{
$scope = $self->rowStatusVars['subjectAccessPointScopes'][$index];
}
$self->createAccessPoint($taxonomyId, $subject);
if ($scope)
{
// get term ID
$query = "SELECT t.id FROM term t \r
INNER JOIN term_i18n i ON t.id=i.id \r
WHERE i.name=? AND t.taxonomy_id=? AND culture='en'";
$statement = QubitFlatfileImport::sqlQuery(
$query,
array($subject, $taxonomyId)
);
$result = $statement->fetch(PDO::FETCH_OBJ);
if ($result)
{
$termId = $result->id;
// check if a scope note already exists for this term
$query = "SELECT n.id FROM note n INNER JOIN note_i18n i ON n.id=i.id WHERE n.object_id=? AND n.type_id=?";
$statement = QubitFlatfileImport::sqlQuery(
$query,
array($termId, QubitTerm::SCOPE_NOTE_ID)
);
$result = $statement->fetch(PDO::FETCH_OBJ);
if (!$result)
{
// add scope note if it doesn't exist
$note = new QubitNote;
$note->objectId = $termId;
$note->typeId = QubitTerm::SCOPE_NOTE_ID;
$note->content = $self->content($scope);
$note->scope = 'QubitTerm'; # not sure if this is needed
$note->save();
}
}
else
{
throw new sfException('Could not find term "'. $subject .'"');
}
}
}
$index++;
}
}
}
// add name access points
if (isset($self->rowStatusVars['nameAccessPoints']))
{
// add name access points
$index = 0;
foreach ($self->rowStatusVars['nameAccessPoints'] as $name)
{
// skip blank names
if ($name)
{
$actorOptions = array();
if (isset($self->rowStatusVars['nameAccessPointHistories'][$index]))
{
$actorOptions['history'] = $self->rowStatusVars['nameAccessPointHistories'][$index];
}
if (null !== $repo = $self->object->getRepository(array('inherit' => true)))
{
$actorOptions['repositoryId'] = $repo->id;
}
$actor = $self->createOrFetchActor($name, $actorOptions);
$self->createRelation($self->object->id, $actor->id, QubitTerm::NAME_ACCESS_POINT_ID);
}
$index++;
}
}
// add accessions
if (isset($self->rowStatusVars['accessionNumber']) &&
count($self->rowStatusVars['accessionNumber']))
{
foreach ($self->rowStatusVars['accessionNumber'] as $accessionNumber)
{
// attempt to fetch keymap entry
$accessionMapEntry = $self->fetchKeymapEntryBySourceAndTargetName(
$accessionNumber,
$self->getStatus('sourceName'),
'accession'
);
// if no entry found, create accession and entry
if (!$accessionMapEntry)
{
print "\nCreating accession # ". $accessionNumber ."\n";
// create new accession
$accession = new QubitAccession;
$accession->identifier = $accessionNumber;
$accession->save();
// create keymap entry for accession
$keymap = new QubitKeymap;
$keymap->sourceId = $accessionNumber;
$keymap->sourceName = $self->getStatus('sourceName');
$keymap->targetId = $accession->id;
$keymap->targetName = 'accession';
$keymap->save();
$accessionId = $accession->id;
}
else
{
$accessionId = $accessionMapEntry->target_id;
}
print "\nAssociating accession # ". $accessionNumber ." with ". $self->object->title ."\n";
// add relationship between information object and accession
$self->createRelation($self->object->id, $accessionId, QubitTerm::ACCESSION_ID);
}
}
// add material-related term relation
if (isset($self->rowStatusVars['radGeneralMaterialDesignation']))
{
foreach ($self->rowStatusVars['radGeneralMaterialDesignation'] as $material)
{
$self->createObjectTermRelation($self->object->id, $material);
}
}
// add copyright info
// TODO: handle this via a separate import
if (isset($self->rowStatusVars['copyrightStatus']) && $self->rowStatusVars['copyrightStatus'])
{
switch (strtolower($self->rowStatusVars['copyrightStatus']))
{
case 'under copyright':
print "Adding rights for ". $self->object->title ."...\n";
$rightsHolderId = false;
$rightsHolderNames = explode('|', $self->rowStatusVars['copyrightHolder']);
if ($self->rowStatusVars['copyrightExpires'])
{
$endDates = explode('|', $self->rowStatusVars['copyrightExpires']);
}
foreach ($rightsHolderNames as $index => $rightsHolderName)
{
$rightsHolderName = ($rightsHolderName) ? $rightsHolderName : 'Unknown';
$rightsHolder = $self->createOrFetchRightsHolder($rightsHolderName);
$rightsHolderId = $rightsHolder->id;
$rightsHolderName = trim(strtolower($rightsHolderName));
if ($rightsHolderName == 'city of vancouver' || strpos($rightsHolderName, 'city of vancouver') === 0)
{
$restriction = 1;
}
else
{
$restriction = 0;
}
$rightAndRelation = array(
'restriction' => $restriction,
'basisId' => QubitTerm::RIGHT_BASIS_COPYRIGHT_ID,
'actId' => array_search(
'Replicate',
$self->getStatus('copyrightActTypes')
),
'copyrightStatusId' => array_search(
'Under copyright',
$self->getStatus('copyrightStatusTypes')
)
);
if (isset($endDates))
{
// if rightsholder/expiry dates and paired, use
// corresponding date ...otherwise just use the
// first expiry date
$rightAndRelation['endDate']
= (count($endDates) == count($rightsHolderNames))
? $endDates[$index]
: $endDates[0];
if (!is_numeric($rightAndRelation['endDate']))
{
throw new sfException('Copyright expiry '. $rightAndRelation['endDate']
.' is invalid.');
}
}
if ($rightsHolderId) $rightAndRelation['rightsHolderId'] = $rightsHolderId;
$self->createRightAndRelation($rightAndRelation);
}
break;
case 'unknown':
$rightsHolder = $self->createOrFetchRightsHolder('Unknown');
$rightsHolderId = $rightsHolder->id;
$rightAndRelation = array(
'rightsHolderId' => $rightsHolderId,
'restriction' => 0,
'basisId' => QubitTerm::RIGHT_BASIS_COPYRIGHT_ID,
'actId' => array_search(
'Replicate',
$self->getStatus('copyrightActTypes')
),
'copyrightStatusId' => array_search(
'Unknown',
$self->getStatus('copyrightStatusTypes')
)
);
if ($self->rowStatusVars['copyrightExpires'])
{
$rightAndRelation['endDate'] = $self->rowStatusVars['copyrightExpires'];
}
$self->createRightAndRelation($rightAndRelation);
break;
case 'public domain':
$rightAndRelation = array(
'restriction' => 1,
'basisId' => QubitTerm::RIGHT_BASIS_COPYRIGHT_ID,
'actId' => array_search(
'Replicate',
$self->getStatus('copyrightActTypes')
),
'copyrightStatusId' => array_search(
'Public domain',
$self->getStatus('copyrightStatusTypes')
)
);
if ($self->rowStatusVars['copyrightExpires'])
{
$rightAndRelation['endDate'] = $self->rowStatusVars['copyrightExpires'];
}
$self->createRightAndRelation($rightAndRelation);
break;
default:
throw new sfException('Copyright status "'
. $self->rowStatusVars['copyrightStatus']
.'" not handled: adjust script or import data');
break;
}
}
// add ad-hoc events
if (isset($self->rowStatusVars['eventActors']))
{
foreach ($self->rowStatusVars['eventActors'] as $index => $actor)
{
// initialize data that'll be used to create the event
$eventData = array(
'actorName' => $actor
);
// define whether each event-related column's values go directly
// into an event property or put into a varibale for further
// processing
$eventColumns = array(
'eventTypes' => array(
'variable' => 'eventType',
'requiredError' => 'You have populated the eventActors column but not the eventTypes column.'
),
'eventPlaces' => array('variable' => 'place'),
'eventDates' => array('property' => 'date'),
'eventStartDates' => array('property' => 'startDate'),
'eventEndDates' => array('property' => 'endDate'),
'eventDescriptions' => array('property' => 'description')
);
// handle each of the event-related columns
$eventType = false;
$place = false;
foreach ($eventColumns as $column => $definition)
{
if (isset($self->rowStatusVars[$column]))
{
$value
= (count($self->rowStatusVars['eventActors']) == count($self->rowStatusVars[$column]))
? $self->rowStatusVars[$column][$index]
: $self->rowStatusVars[$column][0];
// allow column value(s) to set event property
if (isset($definition['property']))
{
$eventData[($definition['property'])] = $value;
}
// allow column values(s) to set variable
if (isset($definition['variable']))
{
$$definition['variable'] = $value;
}
}
else if (isset($definition['requiredError']))
{
throw new sfException('You have populated the eventActors column but not the eventTypes column.');
}
}
// if an event type has been specified, attempt to create the event
if ($eventType)
{