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