| 1 | package Ftree::DataParsers::DBIFormat; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use version; our $VERSION = qv('2.3.41'); | ||
| 6 | |||
| 7 | use Ftree::FamilyTreeData; | ||
| 8 | use Params::Validate qw(:all); | ||
| 9 | use Ftree::DataParsers::ExtendedSimonWardFormat; # for getting pictures. Temporal solution | ||
| 10 | use DBI; | ||
| 11 | use Ftree::DataParsers::FieldValidatorParser; | ||
| 12 | # use CGI::Carp qw(fatalsToBrowser); | ||
| 13 | |||
| 14 | my $picture_directory; | ||
| 15 | |||
| 16 | sub _get_date { | ||
| 17 | my $DBIdate = shift; | ||
| 18 | return undef if(! defined $DBIdate || $DBIdate eq "0000-00-00 00:00:00"); | ||
| 19 | if($DBIdate =~ m/(\d\d\d\d)-(\d\d)-(\d\d)/) { | ||
| 20 | return "$3/$2/$1"; | ||
| 21 | } | ||
| 22 | return undef; | ||
| 23 | } | ||
| 24 | # return: 0, in case of file open error | ||
| 25 | sub getFamilyTreeData { | ||
| 26 | my ($config_) = @_; | ||
| 27 | |||
| 28 | my $family_tree_data = Ftree::FamilyTreeData->new(); | ||
| 29 | |||
| 30 | #this may be a security hole!!! config parameters can be obtained directly!!! | ||
| 31 | my $datasource_name = $config_->{datasource_name} | ||
| 32 | or die "No datasource_name is given in config"; | ||
| 33 | my $db_user_name = $config_->{db_user_name} | ||
| 34 | or die "No db_user_name is given in config"; | ||
| 35 | my $db_password = $config_->{db_password} | ||
| 36 | or die "No db_password is given in config"; | ||
| 37 | my $db_table = $config_->{db_table} | ||
| 38 | or die "No db_table is given in config"; | ||
| 39 | |||
| 40 | my $dbh = DBI->connect($datasource_name, $db_user_name, $db_password) | ||
| 41 | or die "Couldn't connect to database: " . DBI->errstr; | ||
| 42 | my $sth = $dbh->prepare('SELECT * FROM ' . $db_table) | ||
| 43 | or die "Couldn't prepare statement: " . $dbh->errstr; | ||
| 44 | $sth->execute() or die "Couldn't execute statement: " . $sth->errstr; | ||
| 45 | my $column_mapping = $config_->{column_mapping}; | ||
| 46 | |||
| 47 | while (my $row_href = $sth->fetchrow_hashref){ | ||
| 48 | $family_tree_data->add_person({ | ||
| 49 | id => $row_href->{$column_mapping->{id}}, | ||
| 50 | first_name => $row_href->{$column_mapping->{first_name}}, | ||
| 51 | mid_name => (defined $column_mapping->{mid_name}) ? | ||
| 52 | $row_href->{$column_mapping->{mid_name}} : undef, | ||
| 53 | last_name => $row_href->{$column_mapping->{last_name}}, | ||
| 54 | title => (defined $column_mapping->{title}) ? | ||
| 55 | $row_href->{$column_mapping->{title}} : undef, | ||
| 56 | prefix => (defined $column_mapping->{prefix}) ? | ||
| 57 | $row_href->{$column_mapping->{prefix}} : undef, | ||
| 58 | suffix => (defined $column_mapping->{suffix}) ? | ||
| 59 | $row_href->{$column_mapping->{suffix}} : undef, | ||
| 60 | nickname => (defined $column_mapping->{nickname}) ? | ||
| 61 | $row_href->{$column_mapping->{nickname}} : undef, | ||
| 62 | father_id => $row_href->{$column_mapping->{father_id}}, | ||
| 63 | mother_id => $row_href->{$column_mapping->{mother_id}}, | ||
| 64 | email => (defined $column_mapping->{email}) ? | ||
| 65 | $row_href->{$column_mapping->{email}} : undef, | ||
| 66 | homepage => (defined $column_mapping->{homepage}) ? | ||
| 67 | $row_href->{$column_mapping->{homepage}} : undef, | ||
| 68 | date_of_birth => (defined $column_mapping->{date_of_birth}) ? | ||
| 69 | _get_date($row_href->{$column_mapping->{date_of_birth}}) : undef, | ||
| 70 | date_of_death => (defined $column_mapping->{date_of_death}) ? | ||
| 71 | _get_date($row_href->{$column_mapping->{date_of_death}}) : undef, | ||
| 72 | gender => (defined $column_mapping->{gender}) ? | ||
| 73 | $row_href->{$column_mapping->{gender}} : undef, | ||
| 74 | is_living => (defined $column_mapping->{is_living}) ? | ||
| 75 | $row_href->{$column_mapping->{is_living}} : undef, | ||
| 76 | place_of_birth => (defined $column_mapping->{place_of_birth}) ? | ||
| 77 | $row_href->{$column_mapping->{place_of_birth}} : undef, | ||
| 78 | place_of_death => (defined $column_mapping->{place_of_death}) ? | ||
| 79 | $row_href->{$column_mapping->{place_of_death}} : undef, | ||
| 80 | cemetery => (defined $column_mapping->{title}) ? | ||
| 81 | $row_href->{$column_mapping->{title}} : undef, | ||
| 82 | schools => (defined $column_mapping->{schools}) ? | ||
| 83 | [split( /,/, $row_href->{$column_mapping->{schools}})] : undef, | ||
| 84 | jobs => (defined $column_mapping->{jobs}) ? | ||
| 85 | [split( /,/, $row_href->{$column_mapping->{jobs}})] : undef, | ||
| 86 | work_places => (defined $column_mapping->{work_places}) ? | ||
| 87 | [split( /,/, $row_href->{$column_mapping->{work_places}} )] : undef, | ||
| 88 | places_of_living => (defined $column_mapping->{places_of_living}) ? | ||
| 89 | $row_href->{$column_mapping->{places_of_living}} : undef, | ||
| 90 | general => (defined $column_mapping->{general}) ? | ||
| 91 | $row_href->{$column_mapping->{general}} : undef}); | ||
| 92 | } | ||
| 93 | $sth->finish; | ||
| 94 | $dbh->disconnect; | ||
| 95 | |||
| 96 | if (defined $config_->{photo_dir}) { | ||
| 97 | Ftree::DataParsers::ExtendedSimonWardFormat::setPictureDirectory($config_->{photo_dir}); | ||
| 98 | Ftree::DataParsers::ExtendedSimonWardFormat::fill_up_pictures($family_tree_data); | ||
| 99 | } | ||
| 100 | |||
| 101 | return $family_tree_data; | ||
| 102 | } | ||
| 103 | |||
| 104 | 1; | ||
| 105 |