1.49KiB; Perl | 2018-12-22 15:10:36+01 | Statements 37 | SLOC 49
1
package Ged2site::DB::locations;
2
3
use XML::Simple;
4
use Ged2site::DB;
5
6 2
our @ISA = ('Ged2site::DB');
7
8
# The database associated with the locations template file
9
10
sub new {
11
	my $proto = shift;
12
	my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
13
14
	my $class = ref($proto) || $proto;
15
16
	die "$class: where are the files?" unless($directory || $args{'directory'});
17
	# init(\%args);
18
19
	return bless {
20
		logger => $args{'logger'} || $logger,
21
		directory => $args{'directory'} || $directory,	# The directory conainting the tables in XML, SQLite or CSV format
22
		cache => $args{'cache'} || $cache,
23
		table => $args{'table'}	# The name of the file containing the table, defaults to the class name
24
	}, $class;
25
}
26
27
sub locations {
28
	my $self = shift;
29
	my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
30
31
	if(!defined($self->{'locations'})) {
32
		$self->_open();
33
	}
34
	return $self->{'locations'};
35
}
36
37
sub head {
38
	my $self = shift;
39
	my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
40
41
	my $year = $args{'year'} || shift;
42
43
	if(!defined($self->{'locations'})) {
44
		$self->_open();
45
	}
46
	# print Data::Dumper->new([$self->{'locations'}->{'maps'}->{'map'}])->Dump();
47
	# exit;
48
	foreach my $location(@{$self->{'locations'}->{'maps'}->{'map'}}) {
49
		if($location->{'year'} == $year) {
50
			return $location->{'head'};
51
		}
52
	}
53
}
54
55
sub _open {
56
	my $self = shift;
57
58
	my $xmlfile = File::Spec->catfile($self->{'directory'}, 'locations.xml');
59
	$self->{'locations'} = XML::Simple::XMLin($xmlfile);
60
61
	my @statb = stat($xmlfile);
62
	$self->{'_updated'} = $statb[9];
63
}
64
65
1;