-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-entry.php
More file actions
214 lines (186 loc) · 8.58 KB
/
Copy pathprocess-entry.php
File metadata and controls
214 lines (186 loc) · 8.58 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
// ✅ process-entry.php
// Handles: form-manual-daypass.php, form-manual-extended.php, form-manual-faculty.php
// Inserts into: extended_logs
// If request_id passed: updates gate_pass_requests status → 'Approved'
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once 'db_connect.php';
// ── Staff login check ─────────────────────────────────────────
if (!isset($_SESSION['staff_user'])) {
header('Location: login.php');
exit();
}
// ── Only POST ─────────────────────────────────────────────────
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: manual-entry-choice.php');
exit();
}
// ── Collect inputs ────────────────────────────────────────────
$pass_type = trim($_POST['pass_type'] ?? ''); // DP, EX, FV
$serial_no = trim($_POST['serial_no'] ?? '');
$action = trim($_POST['action'] ?? 'save'); // save | print
$request_id = intval($_POST['request_id'] ?? 0);
// Student fields
$student_name = strtoupper(trim($_POST['student_name'] ?? ''));
$roll_no = trim($_POST['roll_no'] ?? '');
$room_no = trim($_POST['room_no'] ?? '') ?: null;
$academic_year = trim($_POST['academic_year'] ?? '') ?: null;
$phone_no = trim($_POST['phone_no'] ?? '');
$parent_contact = trim($_POST['parent_contact'] ?? '') ?: null;
$parent_incharge_name= trim($_POST['parent_incharge_name']?? '') ?: null;
$teacher_name = trim($_POST['teacher_name'] ?? '');
$dp_name = trim($_POST['dp_name'] ?? '') ?: null;
$reason = trim($_POST['reason'] ?? '');
// class_name: manual override if dropdown was 'other'
$class_name = trim($_POST['class_select'] ?? $_POST['class_name'] ?? '');
if ($class_name === 'other' || $class_name === '') {
$class_name = strtoupper(trim($_POST['manual_class'] ?? ''));
}
// district: manual override
$district = trim($_POST['dist_select'] ?? $_POST['district'] ?? '');
if ($district === 'other' || $district === '') {
$district = trim($_POST['manual_dist'] ?? '');
}
// Hostel/block: from POST (locked fields) or session fallback
$hostel_type = trim($_POST['hostel_type'] ?? $_SESSION['hostel_type'] ?? '');
$block_name = trim($_POST['block_name'] ?? $_SESSION['block_name'] ?? '');
// Date/Time
$out_date = trim($_POST['out_date'] ?? date('Y-m-d'));
$out_time = trim($_POST['out_time'] ?? date('H:i:s'));
$expected_return_date= trim($_POST['expected_return_date']?? '') ?: null;
// Supervisor = logged-in staff
$supervisor_name = $_SESSION['staff_user'] ?? '';
// FV special: faculty visit, visit_date used as out_date
if ($pass_type === 'FV') {
$out_date = trim($_POST['visit_date'] ?? date('Y-m-d'));
// out_time from form (faculty left time), in_time = now (when they arrived)
// Actually FV form: in_time (arrival) = locked now, out_time = when they leave (future)
$in_time_fv = trim($_POST['in_time'] ?? date('H:i:s'));
$out_time_fv = trim($_POST['out_time'] ?? '');
// We'll store: out_time = arrival (in_time field locked), expected logic varies
// For FV, out_time = current (when they arrived), out_time field = departure time
$out_time = $in_time_fv; // arrival time
}
// ── Validate required fields ──────────────────────────────────
$errors = [];
if (empty($serial_no)) $errors[] = 'Serial number missing.';
if (empty($pass_type) || !in_array($pass_type, ['DP', 'EX', 'FV'])) $errors[] = 'Invalid pass type.';
if ($pass_type !== 'FV' && empty($student_name)) $errors[] = 'Student name required.';
if (empty($hostel_type)) $errors[] = 'Hostel type required.';
if (empty($block_name)) $errors[] = 'Block name required.';
if (!empty($errors)) {
$_SESSION['error'] = implode(' | ', $errors);
header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? 'manual-entry-choice.php'));
exit();
}
// ── File Upload ───────────────────────────────────────────────
$application_file = null;
// 1. Existing file from student request
if (!empty($_POST['existing_application_file'])) {
$application_file = trim($_POST['existing_application_file']);
}
// 2. New file upload (overrides existing)
if (isset($_FILES['application_file']) && $_FILES['application_file']['error'] === UPLOAD_ERR_OK) {
$upload_dir = 'uploads/application/' . date('Y-m-d') . '/';
if (!is_dir($upload_dir)) mkdir($upload_dir, 0755, true);
$tmp = $_FILES['application_file']['tmp_name'];
$orig_name = $_FILES['application_file']['name'];
$ext = strtolower(pathinfo($orig_name, PATHINFO_EXTENSION));
$allowed = ['pdf','jpg','jpeg','png'];
if (in_array($ext, $allowed) && $_FILES['application_file']['size'] <= 5 * 1024 * 1024) {
$dest = $upload_dir . $serial_no . '.' . $ext;
if (move_uploaded_file($tmp, $dest)) {
$application_file = $dest;
}
}
}
// 3. Webcam image (base64)
if (!empty($_POST['webcam_image']) && empty($application_file)) {
$base64 = $_POST['webcam_image'];
if (strpos($base64, 'data:image') === 0) {
$img_data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64));
$upload_dir = 'uploads/application/' . date('Y-m-d') . '/';
if (!is_dir($upload_dir)) mkdir($upload_dir, 0755, true);
$cam_path = $upload_dir . $serial_no . '_cam_' . time() . '.jpg';
if (file_put_contents($cam_path, $img_data)) {
$application_file = $cam_path;
}
}
}
// ── INSERT into extended_logs ─────────────────────────────────
$sql = "INSERT INTO extended_logs (
serial_no, student_name, roll_no, room_no, class_name, academic_year,
block_name, hostel_type,
out_date, out_time, district, phone_no,
parent_name, parent_phone,
expected_return_date, reason, teacher_name,
application_file, supervisor_name, pass_type, status
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'Out')";
$stmt = mysqli_prepare($conn, $sql);
if (!$stmt) {
$_SESSION['error'] = 'DB Error: ' . mysqli_error($conn);
header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? 'manual-entry-choice.php'));
exit();
}
mysqli_stmt_bind_param($stmt, 'ssssssssssssssssssss',
$serial_no,
$student_name,
$roll_no,
$room_no,
$class_name,
$academic_year,
$block_name,
$hostel_type,
$out_date,
$out_time,
$district,
$phone_no,
$parent_incharge_name, // parent_name column
$parent_contact, // parent_phone column
$expected_return_date,
$reason,
$teacher_name,
$application_file,
$supervisor_name,
$pass_type
);
if (!mysqli_stmt_execute($stmt)) {
$err = mysqli_stmt_error($stmt);
mysqli_stmt_close($stmt);
$_SESSION['error'] = 'Save failed: ' . $err;
header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? 'manual-entry-choice.php'));
exit();
}
$new_log_id = mysqli_insert_id($conn);
mysqli_stmt_close($stmt);
// ── If came from student request, mark as Approved ─────────────
if ($request_id > 0) {
$upd = mysqli_prepare($conn,
"UPDATE gate_pass_requests
SET status='Approved', approved_by=?, approved_at=NOW(), out_date=?, out_time=?
WHERE id=? AND status='Pending'"
);
$approved_by = $_SESSION['staff_user'];
mysqli_stmt_bind_param($upd, 'sssi', $approved_by, $out_date, $out_time, $request_id);
mysqli_stmt_execute($upd);
mysqli_stmt_close($upd);
// Clear session approve_request data
unset($_SESSION['approve_request']);
}
// ── Redirect ──────────────────────────────────────────────────
if ($action === 'print') {
// Redirect to gatepass print/download
header('Location: download-gatepass.php?id=' . $new_log_id);
} else {
$_SESSION['success'] = "Record saved! Serial: $serial_no";
// Back to staff dashboard / pending requests
if ($request_id > 0) {
header('Location: staff-pending-requests.php?approved=1');
} else {
header('Location: manual-entry-choice.php?saved=1');
}
}
exit();
?>