-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.php
157 lines (126 loc) · 5.51 KB
/
go.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
<?php
include('shared.inc.php');
function nextScreen()
{
global $link;
if (! empty($_GET['gridid'])) {
$snippet = "WHERE `grids`.`gridid`='" . intval($_GET['gridid']) . "'";
} else {
$snippet = "ORDER BY RAND() LIMIT 0, 1";
}
$result = mysqli_query($link, "SELECT `grids`.`gridid`, `companies`.`companyid`, `companies`.`name`, `companies`.`type`, `companies`.`number`, `grids`.`document`, `grids`.`page`, `grids`.`date` AS `submissiondate` FROM `grids` LEFT JOIN `companies` ON `grids`.`companyid`=`companies`.`companyid` $snippet;");
$row = mysqli_fetch_assoc($result);
$gridid = $row['gridid'];
$_SESSION['grids'][$gridid] = $row;
$result = mysqli_query($link, "SELECT * FROM `gridrows` WHERE `gridid`='$gridid' ORDER BY `order`");
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
$_SESSION['grids'][$gridid]['rows'][$i] = $row;
$i++;
}
$result = mysqli_query($link, "SELECT SUM(IF(`confirmed`>=1,1,0)) AS `pending`, SUM(IF(`confirmed`>=3,1,0)) AS `complete`, COUNT(`gridid`) AS `total` FROM `grids`");
$_SESSION['stats'] = mysqli_fetch_assoc($result);
return $gridid;
}
$fields = array('firstname', 'middlename', 'lastname', 'nationality', 'idnumber', 'companyname', 'address1', 'address2', 'address3', 'city', 'stateprovince', 'postalcode', 'country', 'position', 'type', 'startdate', 'enddate', 'status');
$numberFields = count($fields);
$success = FALSE;
if (! empty($_POST['nodata'])) {
$result = mysqli_query($link, "UPDATE `grids` SET `confirmed`='-1' WHERE `gridid`='" . intval($_POST['gridid']) . "'");
$gridid = nextScreen();
} elseif (count($_POST)) {
$gridid = $_POST['gridid'];
$numberRows = count($_POST['rowid']);
if (! preg_match('/\d\d\d\d-\d\d-\d\d/', $_POST['submissiondate'])) {
$errors[] = 'Please fill in the submission (stamp) date.';
}
for ($i = 0; $i < $numberRows; $i++) {
if (! empty($_POST['startdate'][$i]) && ! preg_match('/\d\d\d\d-\d\d-\d\d/', $_POST['startdate'][$i]))
$errors[] = 'There is a problem with the start date in row ' . ($i + 1) . '.';
if (! empty($_POST['enddate'][$i]) && ! preg_match('/\d\d\d\d-\d\d-\d\d/', $_POST['enddate'][$i]))
$errors[] = 'There is a problem with the end date in row ' . ($i + 1) . '.';
$allEmpty = $i;
for ($j = 0; $j < $numberFields; $j++) {
if (! empty(trim($_POST[$fields[$j]][$i]))) {
$allEmpty = FALSE;
}
}
if ($allEmpty !== FALSE)
$errors[] = 'Please fill in at least one field in row ' . ($allEmpty + 1) . ($allEmpty + 1 > 1 ? ' or delete the row' : '') . '.';
}
if (! count($errors)) {
// Trying to save and everything is fine.
$result = mysqli_query($link, "UPDATE `grids` SET `date`='" . $_POST['submissiondate'] . "', `confirmed`=`confirmed`+1 WHERE `gridid`='" . intval($_POST['gridid']) . "' LIMIT 1");
// INSERT new rows
$insertSQL = "INSERT INTO `gridrows` (`gridid`, ";
for ($j = 0; $j < $numberFields; $j++) {
$insertSQL .= '`' . $fields[$j] . '`, ';
}
$insertSQL .= "`order`, `timestamp`, `createstamp`) VALUES ";
$insertSQLBody = array();
for ($i = 0; $i < $numberRows; $i++) {
if (empty($_POST['startdate'][$i]))
$_POST['startdate'][$i] = '0000-00-00';
if (empty($_POST['enddate'][$i]))
$_POST['enddate'][$i] = '0000-00-00';
$o = '';
if (empty($_POST['rowid'][$i])) {
$o .= "('" . intval($_POST['gridid']) . "', ";
for ($j = 0; $j < $numberFields; $j++) {
$o .= "'" . mysqli_real_escape_string($link, strip_tags($_POST[$fields[$j]][$i])) . "', ";
}
$o .= "'" . ($i + 1) . "', NOW(), NOW())";
}
if (strlen($o))
$insertSQLBody[] = $o;
}
if (count($insertSQLBody)) {
$result = mysqli_query($link, $insertSQL . implode(', ', $insertSQLBody));
if (mysqli_errno($link)) {
//echo "SQL Error: " . mysqli_errno($link) . ': ' . mysql_error() . "<BR>\n";
echo "SQL Error: " . mysqli_error($link) . "<BR>\n";
error_log("SQL Error: " . mysqli_error($link) . "<BR>\n");
error_log("Insert statement was: " . $insertSQL . implode(', ', $insertSQLBody) . "<BR>\n");
error_log("Dumped POST request:<BR>\n" . print_r($_POST, true));
die;
}
}
// UPDATE existing rows
for ($i = 0; $i < $numberRows; $i++) {
$updateSQLBody = '';
$updateSQL = "UPDATE `gridrows` SET ";
if (! empty($_POST['rowid'][$i])) {
for ($j = 0; $j < $numberFields; $j++) {
if ($j)
$updateSQLBody .= ', ';
$updateSQLBody .= '`' . $fields[$j] . "`='" . mysqli_real_escape_string($link, strip_tags($_POST[$fields[$j]][$i])) . "'";
}
}
if (strlen($updateSQLBody)) {
$updateSQL .= $updateSQLBody . " WHERE `rowid`='" . (int)$_POST['rowid'][$i] . "' LIMIT 1";
$result = mysqli_query($link, $updateSQL);
// echo $updateSQL . "<BR>\n";
// echo mysql_error() . "<BR>\n";
}
}
$success = TRUE;
} else {
// Trying to save but there are errors.
for ($i = 0; $i < $numberRows; $i++) {
for ($j = 0; $j < $numberFields; $j++) {
$_SESSION['grids'][$_POST['gridid']]['rows'][$i][$fields[$j]] = $_POST[$fields[$j]][$i];
}
}
$_SESSION['grids'][$_POST['gridid']]['submissiondate'] = $_POST['submissiondate'];
}
} else {
$gridid = nextScreen();
}
$grid = $_SESSION['grids'][$gridid];
$image = $grid['type'] . $grid['number'] . '-' . $grid['document'] . '-' . ($grid['page'] - 1) . '.gif';
if ($success) {
include('success.form.php');
} else {
include('go.form.php');
}
?>