Fixed bug in unwilling elder filter
[eq/.git] / parse_ward_data
1 #!/usr/bin/perl
2
3 use DBI;
4 use Getopt::Std;
5 ###################################################
6 # GLOBALS
7 $dbname = "phpgroupware";
8 $dbhost = "192.168.0.2";
9 $dbport = 3306;
10 $dbuser = "phpgroupware";
11 $dbpass = "phpgroupware";
12 %hometeaching_data = ();
13 %membership_data = ();
14 getopts('vsn:o:b');
15
16 $monthname2num{'Jan'} = '01';
17 $monthname2num{'Feb'} = '02';
18 $monthname2num{'Mar'} = '03';
19 $monthname2num{'Apr'} = '04';
20 $monthname2num{'May'} = '05';
21 $monthname2num{'Jun'} = '06';
22 $monthname2num{'Jul'} = '07';
23 $monthname2num{'Aug'} = '08';
24 $monthname2num{'Sep'} = '09';
25 $monthname2num{'Oct'} = '10';
26 $monthname2num{'Nov'} = '11';
27 $monthname2num{'Dec'} = '12';
28
29 ######################################################################
30 # SUBROUTINES
31 ######################################################################
32 sub csv_to_hash
33 {
34     my ($filename, $hashref) = @_;
35
36     open(FILE,$filename) || die "-E- Could not open $filename for reading\n";
37
38     my $found_header = 0; my $index = 0;
39     while(<FILE>)
40     {
41         $line = $_;
42         @data = split /\",/, $line;
43         if(!$found_header) { @header = @data; $found_header = 1; }
44         else {
45             foreach $i (0..$#data-1) {
46                 $data[$i] =~ s/\"//g;
47                 $header[$i] =~ s/\"//g;
48                 $hashref->{$index}{$header[$i]} = $data[$i];
49                 #print "$index: $i: $header[$i]: $data[$i]\n";
50             }
51             $index++;
52         }
53     }
54     
55     close(FILE);
56 }
57
58 ######################################################################
59 sub print_hash
60 {
61     my ($hashref) = @_;
62
63     foreach $key (sort {$a <=> $b} keys %$hashref) {
64         print "Index: $key\n";
65         foreach $field (keys %{$hashref->{$key}}) {
66             print "$field: $hashref->{$key}{$field}\n";
67         }
68         print "\n";
69     }
70 }
71
72 ######################################################################
73 sub print_birthdays
74 {
75     my ($hashref) = @_;
76
77     foreach $key (sort {$a <=> $b} keys %$hashref) {
78         $name = "";
79         $birthday = "";
80         foreach $field (keys %{$hashref->{$key}}) {
81             if($field =~ /Full Name/) { $name = $hashref->{$key}{$field}; }
82             if($field =~ /Birth/) { $birthday = $hashref->{$key}{$field}; }
83         }
84         if($name ne "" && $birthday ne "") { printf "%-30s %-10s\n",$name,$birthday; }
85     }
86 }
87
88 ######################################################################
89 # MAIN
90 ######################################################################
91
92 ###################################################
93 # Open a connection to the database
94 $dbh=DBI->connect("dbi:mysql:dbname=$dbname;host=$dbhost port=$dbport",$dbuser,$dbpass,{
95     AutoCommit=>0,
96     PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
97
98 ###################################################
99 # Process command line options
100 if(defined $opt_n) { $datadir = $opt_n; }
101 else { $datadir = shift(@ARGV); }
102 print "-> Processing all ward data files in $datadir\n";
103
104 ###################################################
105 # Parse Ward Data Files
106 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
107 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
108
109 if($opt_v) {
110     print "-> Membership Data Dump\n\n";
111     &print_hash(\%membership_data);
112     print "-> HomeTeaching Data Dump\n\n";
113     &print_hash(\%hometeaching_data);
114 }
115
116 if($opt_b) { &print_birthdays(\%membership_data); }
117
118 if($opt_s) { $dbh->disconnect(); exit; }
119
120 ###################################################
121 # Disconnect from the database
122 $dbh->disconnect();
123
124 ######################################################################
125
126
127
128
129
130
131
132
133
134
135
136