This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
submit-grower.php
174 lines (150 loc) · 4.77 KB
/
submit-grower.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
include('include/Database.inc.php');
include('include/Mail.inc.php');
include('include/autoresponse.inc.php');
$firstname = $_POST['firstname'];
$middlename = $_POST['middlename'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$preferred = $_POST['prefer'];
$street = $_POST['street'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$property = $_POST['property'];
$relationship = $_POST['relationship'];
$tools = $_POST['tools'];
if (!isset($_POST['source']))
$source = 1; // Default to "Others"
else
$source = $_POST['source'];
$notes = $_POST['notes'];
$trees = $_POST['trees'];
$errorMessage = "";
if(empty($firstname)) {
$errorMessage .= "<li>First Name required!</li>";
}
if(empty($lastname)) {
$errorMessage .= "<li>No Last Name required!</li>";
}
if(empty($email)) {
$email = ''; // no longer required
}
if(empty($phone)) {
$errorMessage .= "<li>Phone number required!</li>";
} else {
$phone = preg_replace('/\D/', '', $phone); // strip out all chars except numbers
if (strlen($phone) != 10)
$errorMessage .= '<li>Phone number must be exactly 10 numbers!</li>';
else
$phone = '(' . substr($phone, 0, 3) . ') ' . substr($phone, 3, 3) . '-' . substr($phone, 6, 4); // (949) 555-1234
}
if(empty($preferred)) {
$errorMessage .= "<li>No Preferred Contact!</li>";
}
if(empty($street)) {
$errorMessage .= "<li>Street required!</li>";
}
if(empty($city)) {
$errorMessage .= "<li>City required!</li>";
}
if(empty($state)) {
$errorMessage .= "<li>State required!</li>";
}
if(empty($zip)) {
$errorMessage .= "<li>Zip code required!</li>";
}
if(!is_numeric($zip) || strlen($zip) != 5) {
$errorMessage .= "<li>Zip code must be 5 numbers!</li>";
}
if(empty($property)) {
$errorMessage .= "<li>No Property Type!</li>";
}
if(empty($relationship)) {
$errorMessage .= "<li>No Property Relationship!</li>";
}
if(count($trees)==0)
$errorMessage .= '<li>No trees. Grower must register one or more trees!</li>';
if(!empty($errorMessage))
{
die($errorMessage);
}
$r = $db->startTransaction();
if (!$r->isValid())
die('Transaction could not start.');
$sql = "INSERT INTO growers (first_name, middle_name, last_name, phone, email, preferred, street, city, state, zip, tools, property_type_id, property_relationship_id, source_id, notes)
VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');";
$inputs = array($firstname, $middlename, $lastname, $phone, $email, $preferred, $street, $city, $state, $zip, $tools, $property, $relationship, $source, $notes);
$r = $db->q($sql, $inputs);
if (!$r->isValid()) {
die($db->error());
}
$errorMessage = '';
$growerID = $db->getInsertId();
foreach($trees as $tree) {
$type = $tree['type'];
if(empty($type)) {
$errorMessage .= "<li>No Tree Type!</li>";
}
$varietal = $tree['varietal'];
$quantity = $tree['quantity'];
if(empty($quantity) || !is_numeric($quantity) || $quantity < 1) {
$quantity = 1; //optional so default to 1
}
$height = $tree['height'];
if(empty($height)) {
$height = 1; //optional so default to 1
}
$months = $tree['month'];
$chemical = $tree['chemical'];
if(empty($chemical)) {
$chemical = 0;
}
if(!empty($errorMessage)) {
die($errorMessage);
}
$sql = 'INSERT INTO grower_trees (grower_id, tree_type, varietal, number, avgHeight_id, chemicaled) VALUES';
$sql .= "('%s','%s','%s','%s','%s','%s');";
$inputs = array($growerID, $type, $varietal, $quantity, $height, $chemical);
$r = $db->q($sql, $inputs);
if (!$r->isValid()) {
echo 'DB error while inserting trees: ' . $db->error() .'<br/>Attempting to rollback...';
$r = $db->rollback();
if ($r->isValid())
echo 'Rollback succeeded!';
else
echo 'Rollback failed!';
}
$treeID= $db->getInsertId();
foreach($months as $month) {
$sql = "INSERT INTO month_harvests (tree_id, month_id) VALUES ('%s','%s');";
$r = $db->q($sql, array($treeID, $month));
if (!$r->isValid()) {
echo 'DB error while inserting harvest months: ' . $db->error() . '<br/>Attempting to rollback...';
$r = $db->rollback();
if ($r->isValid())
echo 'Rollback succeeded!';
else
echo 'Rollback failed!';
}
}
}
$r = $db->commit();
if ($r->isValid()) {
if (empty($email)) // if no email supplied then we don't try to send mail
$sent = false;
else
$sent = $mail->send('Registration Confirmed', growerResponse($firstname, $lastname), $email);
echo "$firstname $lastname, Thank you for registering!";
if ($sent)
echo "<br/>An email has been sent to: $email";
} else {
echo 'Failed to commit transaction. Attempting to rollback...';
$r = $db->rollback();
if ($r->isValid())
echo 'Rollback succeeded!';
else
echo 'Rollback failed!';
}
?>