-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_project.php
More file actions
202 lines (183 loc) · 9.56 KB
/
Copy pathadd_project.php
File metadata and controls
202 lines (183 loc) · 9.56 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
<?php
session_start();
$manager = $_SESSION['user']['role'];
if (!isset($_SESSION['user']) || $manager !== 'Manager') {
header('Location: login.php');
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
require 'db.php.inc';
$errors = [];
$data = $_POST;
$uploaded_file_paths = [];
$upload_dir = __DIR__ . '/documents/';
if (empty($data['project_id']) || !preg_match('/^[A-Z]{4}-\d{5}$/', $data['project_id'])) {
$errors['project_id'] = "Project ID must start with 4 uppercase letters, followed by a dash (-) and 5 digits.";
} else {
$checkStmt = $pdo->prepare("SELECT COUNT(*) FROM projects WHERE project_id = :project_id");
$checkStmt->execute([':project_id' => $data['project_id']]);
if ($checkStmt->fetchColumn()) {
$errors['project_id'] = "Project ID already exists. Please use a different Project ID.";
}
}
if (empty($data['total_budget']) || $data['total_budget'] <= 0) {
$errors['total_budget'] = "Budget must be a positive numeric value.";
}
if (!empty($data['start_date']) && !empty($data['end_date'])) {
if ($data['end_date'] <= $data['start_date']) {
$errors['dates'] = "End Date must be later than Start Date.";
}
} else {
$errors['dates'] = "Start Date and End Date are required.";
}
$uploaded_files = $_FILES['documents'] ?? null;
$file_titles = $_POST['doc_titles'] ?? [];
if ($uploaded_files && count($uploaded_files['name']) > 0) {
for ($i = 0; $i < count($uploaded_files['name']); $i++) {
if (!empty($uploaded_files['name'][$i])) {
$file_name = $uploaded_files['name'][$i];
$file_size = $uploaded_files['size'][$i];
$file_tmp = $uploaded_files['tmp_name'][$i];
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
if (!in_array($file_ext, ['pdf', 'docx', 'png', 'jpg'])) {
$errors['documents'] = 'Invalid file type. Accepted types are PDF, DOCX, PNG, and JPG.';
} elseif ($file_size > 2 * 1024 * 1024) {
$errors['documents'] = 'File size cannot exceed 2MB.';
} else {
$unique_file_name = uniqid() . '-' . $file_name;
$file_path = $upload_dir . $unique_file_name;
if (move_uploaded_file($file_tmp, $file_path)) {
$uploaded_file_paths[] = [
'path' => $file_path,
'title' => $file_titles[$i] ?? "Document " . ($i + 1)
];
} else {
$errors['documents'] = 'Failed to upload file. Please ensure the uploads directory is writable.';
}
}
}
}
if (count($uploaded_file_paths) > 3) {
$errors['documents'] = 'You can upload a maximum of three files.';
}
}
if (empty($errors)) {
try {
$pdo->beginTransaction();
$stmt = $pdo->prepare("
INSERT INTO projects (project_id, title, description, customer_name, budget, start_date, end_date)
VALUES (:project_id, :title, :description, :customer_name, :budget, :start_date, :end_date)
");
$stmt->execute([
':project_id' => $data['project_id'],
':title' => $data['project_title'],
':description' => $data['project_description'],
':customer_name' => $data['customer_name'],
':budget' => $data['total_budget'],
':start_date' => $data['start_date'],
':end_date' => $data['end_date']
]);
if (!empty($uploaded_file_paths)) {
$docStmt = $pdo->prepare("
INSERT INTO project_documents (project_id, title, file_path)
VALUES (:project_id, :title, :file_path)
");
foreach ($uploaded_file_paths as $file_data) {
$docStmt->execute([
':project_id' => $data['project_id'],
':title' => $file_data['title'],
':file_path' => $file_data['path']
]);
}
}
$pdo->commit();
$success = "Project successfully added!";
$data = [];
} catch (PDOException $e) {
$pdo->rollBack();
$errors['database'] = "Database error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Project</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<?php include 'header.php'; ?>
<main>
<?php include 'sidebar.php'; ?>
<section class="form-section1">
<h2>Add Project</h2>
<?php if (!empty($success)): ?>
<div class="success"><?= $success ?></div>
<?php endif; ?>
<form action="add_project.php" method="POST" enctype="multipart/form-data" novalidate>
<label for="project_id">Project ID</label>
<input type="text" id="project_id" name="project_id" value="<?= $data['project_id'] ?? '' ?>" pattern="[A-Z]{4}-\d{5}" title="Must start with 4 uppercase letters followed by a dash (-) and 5 digits" class="<?= isset($errors['project_id']) ? 'invalid' : '' ?>" required>
<?php if(!empty($errors['project_id'])):?>
<div class="error"><?= $errors['project_id'] ?? '' ?></div>
<?php endif ;?>
<label for="project_title">Project Title</label>
<input type="text" id="project_title" name="project_title" value="<?= $data['project_title'] ?? '' ?>" class="<?= isset($errors['project_title']) ? 'invalid' : '' ?>" required>
<?php if(!empty($errors['project_title'])):?>
<div class="error"><?= $errors['project_title'] ?? '' ?></div>
<?php endif ;?>
<label for="project_description">Project Description</label>
<textarea id="project_description" name="project_description" rows="4" class="<?= isset($errors['project_description']) ? 'invalid' : '' ?>" required><?= htmlspecialchars($data['project_description'] ?? '') ?></textarea>
<?php if(!empty($errors['project_description'])):?>
<div class="error"><?= $errors['project_description'] ?? '' ?></div>
<?php endif ;?>
<label for="customer_name">Customer Name</label>
<input type="text" id="customer_name" name="customer_name" value="<?= $data['customer_name'] ?? '' ?>" class="<?= isset($errors['customer_name']) ? 'invalid' : '' ?>" required>
<?php if(!empty($errors['customer_name'])):?>
<div class="error"><?= $errors['customer_name'] ?? '' ?></div>
<?php endif ;?>
<label for="total_budget">Total Budget</label>
<input type="number" id="total_budget" name="total_budget" min="1" value="<?= $data['total_budget'] ?? '' ?>" class="<?= isset($errors['total_budget']) ? 'invalid' : '' ?>" required>
<?php if(!empty($errors['total_budget'])):?>
<div class="error"><?= $errors['total_budget'] ?? '' ?></div>
<?php endif ;?>
<label for="start_date">Start Date</label>
<input type="date" id="start_date" name="start_date" value="<?= $data['start_date'] ?? '' ?>" class="<?= isset($errors['dates']) ? 'invalid' : '' ?>" required>
<?php if(!empty($errors['dates'])):?>
<div class="error"><?= $errors['dates'] ?? '' ?></div>
<?php endif ;?>
<label for="end_date">End Date</label>
<input type="date" id="end_date" name="end_date" value="<?= $data['end_date'] ?? '' ?>" class="<?= isset($errors['dates']) ? 'invalid' : '' ?>" required>
<?php if(!empty($errors['dates'])):?>
<div class="error"><?= $errors['dates'] ?? '' ?></div>
<?php endif ;?>
<fieldset>
<legend>Supporting Documents</legend>
<div>
<label for="doc1_title">Document 1 Title</label>
<input type="text" id="doc1_title" name="doc_titles[]" required>
<input type="file" id="doc1" name="documents[]" accept=".pdf,.docx,.png,.jpg" required>
</div>
<div>
<label for="doc2_title">Document 2 Title</label>
<input type="text" id="doc2_title" name="doc_titles[]">
<input type="file" id="doc2" name="documents[]" accept=".pdf,.docx,.png,.jpg">
</div>
<div>
<label for="doc3_title">Document 3 Title</label>
<input type="text" id="doc3_title" name="doc_titles[]">
<input type="file" id="doc3" name="documents[]" accept=".pdf,.docx,.png,.jpg">
</div>
</fieldset>
<?php if(!empty($errors['documents'])):?>
<div class="error"><?= $errors['documents'] ?? '' ?></div>
<?php endif ;?>
<button type="submit" class="btn-primary">Confirm</button>
</form>
</section>
</main>
<?php include 'footer.php'; ?>
</body>
</html>