| 1 | package Ftree::DataParsers::GedcomFormat; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use version; our $VERSION = qv('2.3.41'); | ||
| 5 | use Gedcom; | ||
| 6 | use Ftree::DataParsers::FieldValidatorParser; | ||
| 7 | use Ftree::FamilyTreeData; | ||
| 8 | # use CGI::Carp qw(fatalsToBrowser); | ||
| 9 | use Encode qw(decode_utf8); | ||
| 10 | |||
| 11 | my %genderTogedcomsex = ( | ||
| 12 | U => undef, | ||
| 13 | F => 1, | ||
| 14 | M => 0, | ||
| 15 | ); | ||
| 16 | |||
| 17 | my %montharray = ( | ||
| 18 | JAN => 1, | ||
| 19 | FEB => 2, | ||
| 20 | MAR => 3, | ||
| 21 | APR => 4, | ||
| 22 | MAY => 5, | ||
| 23 | JUN => 6, | ||
| 24 | JUL => 7, | ||
| 25 | AUG => 8, | ||
| 26 | SEP => 9, | ||
| 27 | OCT => 10, | ||
| 28 | NOV => 11, | ||
| 29 | DEC => 12, | ||
| 30 | |||
| 31 | |||
| 32 | ); | ||
| 33 | |||
| 34 | sub createFamilyTreeDataFromFile { | ||
| 35 | my ($config_) = @_; | ||
| 36 | my $file_name = $config_->{file_name} or die "No file_name is given in config"; | ||
| 37 | |||
| 38 | my $ged = Gedcom->new(gedcom_file => $file_name); | ||
| 39 | my $family_tree_data = Ftree::FamilyTreeData->new(); | ||
| 40 | for my $i ($ged->individuals) | ||
| 41 | { | ||
| 42 | $family_tree_data->add_person({ | ||
| 43 | id => $i->{xref}, | ||
| 44 | first_name => decode_utf8($i->given_names(0)), | ||
| 45 | mid_name => undef, | ||
| 46 | last_name => decode_utf8($i->surname), | ||
| 47 | father_id => (defined $i->father) ? decode_utf8($i->father->{xref}) : undef, | ||
| 48 | mother_id => (defined $i->mother) ? decode_utf8($i->mother->{xref}) : undef, | ||
| 49 | email => getEmail($i), | ||
| 50 | homepage => getHomepage($i), | ||
| 51 | date_of_birth => getDate("birth date", $i), | ||
| 52 | date_of_death => getDate("death date", $i), | ||
| 53 | gender => $genderTogedcomsex{$i->sex()}, | ||
| 54 | place_of_birth => getPlace("birth place", $i), | ||
| 55 | place_of_death => getPlace("death place", $i), | ||
| 56 | cemetery => getPlace("burial place",$i), | ||
| 57 | }) | ||
| 58 | } | ||
| 59 | return $family_tree_data; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub getDate { | ||
| 63 | my ($date_type, $i) = @_; | ||
| 64 | my $date = $i->get_value($date_type); | ||
| 65 | if(defined $date) { | ||
| 66 | $date = (split(/, /, $date))[-1]; #separated by comma, whitespace pair | ||
| 67 | my @date_a = split(/ /, $date); | ||
| 68 | if(2 < @date_a) { | ||
| 69 | $date_a[1] = $montharray{$date_a[1]} ; | ||
| 70 | } elsif (1 < @date_a) { | ||
| 71 | $date_a[0] = $montharray{$date_a[0]} ; | ||
| 72 | } | ||
| 73 | return join('/', @date_a); | ||
| 74 | } | ||
| 75 | return undef; | ||
| 76 | } | ||
| 77 | sub getPlace { | ||
| 78 | my ($place_type, $i) = @_; | ||
| 79 | my $place = decode_utf8($i->get_value($place_type)); | ||
| 80 | if(defined $place) { | ||
| 81 | $place =~ s/, /" "/g; #separated by comma, whitespace pair | ||
| 82 | $place =~ s/^/"/g; | ||
| 83 | $place =~ s/$/"/g; | ||
| 84 | return $place; | ||
| 85 | } | ||
| 86 | return undef; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub getEmail { | ||
| 90 | my ($i) = @_; | ||
| 91 | my @data = decode_utf8($i->get_value("Object File")); | ||
| 92 | my @emails = grep {defined $_ && FieldValidatorParser::validEmail($_)} @data; | ||
| 93 | return $emails[0]; | ||
| 94 | |||
| 95 | } | ||
| 96 | sub getHomepage { | ||
| 97 | my ($i) = @_; | ||
| 98 | my @data = decode_utf8($i->get_value("Object File")); | ||
| 99 | my @urls = grep {defined $_ && FieldValidatorParser::validURL($_)} @data; | ||
| 100 | return $urls[0]; | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | 1; |