Skip to content

Commit

Permalink
Pagination, Download Document .Zip
Browse files Browse the repository at this point in the history
  • Loading branch information
lechiluan committed Apr 14, 2023
1 parent 46d3a4c commit 1c27df7
Show file tree
Hide file tree
Showing 12 changed files with 1,111 additions and 662 deletions.
15 changes: 14 additions & 1 deletion QAC_Staff.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class="rounded-circle me-2"

// Get the staffs data from the database where section is Session department_id when i save in login page
$id = $_SESSION['department_id'];
$sql = "SELECT * FROM Staff WHERE DepartmentID = '$id'";
$sql = "SELECT Staff.*, Role.RoleName, Department.DepartmentName FROM Staff JOIN Role ON Staff.RoleID = Role.RoleID JOIN Department ON Staff.DepartmentID = Department.DepartmentID WHERE Staff.DepartmentID = '$id'";
$result = mysqli_query($conn, $sql);
$num = mysqli_num_rows($result);
?>
Expand All @@ -102,6 +102,8 @@ class="rounded-circle me-2"
<th>ID</th>
<th>Full Name</th>
<th>Email</th>
<th>Role</th>
<th>Department</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_assoc($result)) { ?>
Expand All @@ -110,6 +112,17 @@ class="rounded-circle me-2"
<td><?php echo $row['StaffID']; ?></td>
<td><?php echo $row['FullName']; ?></td>
<td><?php echo $row['Email']; ?></td>
<?php if ($row['RoleID'] == '1') { ?>
<td><span class="badge bg-danger rounded-pill"><?= $row['RoleName']; ?></span>
</td>
<?php } else if ($row['RoleID'] == '2') { ?>
<td><span class="badge bg-warning rounded-pill"><?= $row['RoleName']; ?></span>
</td>
<?php } else if ($row['RoleID'] == '3') { ?>
<td><span class="badge bg-success rounded-pill"><?= $row['RoleName']; ?></span>
</td>
<?php } ?>
<td><?php echo $row['DepartmentName']; ?></td>
</tr>

</tbody>
Expand Down
330 changes: 157 additions & 173 deletions QAC_Topic.php

Large diffs are not rendered by default.

597 changes: 451 additions & 146 deletions QAC_Topic_Detail.php

Large diffs are not rendered by default.

520 changes: 292 additions & 228 deletions Staff.php

Large diffs are not rendered by default.

58 changes: 39 additions & 19 deletions Staff_MyPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@ class="rounded-circle me-2"
<!-- content -->
<div class="my-3">
<p><?php echo $row['Content']; ?></p>
<?php
// Check if a document exists in the database and display a download button if it does
$sql_download = "SELECT * FROM Document WHERE Document.IdeaID = " . $row['IdeaID'];
$result_download = mysqli_query($conn, $sql_download);
if (mysqli_num_rows($result_download) > 0) {
// Build a zip file containing all the documents for this idea
$zip = new ZipArchive();
$zipName = "documents-" . $row['IdeaID'] . ".zip";
$zipPath = "uploads/" . $zipName;
if ($zip->open($zipPath, ZipArchive::CREATE) !== TRUE) {
echo "Error: Could not create zip file";
}
while ($row_download = mysqli_fetch_assoc($result_download)) {
$filePath = $row_download['DocumentPath'];
$fileName = basename($filePath);
$zip->addFile($filePath, $fileName);
}
$zip->close();
?>
<a href="<?php echo $zipPath; ?>" download="<?php echo $zipName; ?>"
class="btn btn-primary">Download Documents</a>
<?php
}
?>
</div>
<!-- like and dislike buttons -->
<div class="d-flex justify-content-between align-items-center">
Expand Down Expand Up @@ -292,7 +316,7 @@ class="rounded-circle me-2"
<?php } ?>
<!-- Comment form -->
<div class="mt-3">
<form action="Staff.php" method="post">
<form action="Staff_MyPost.php" method="post">
<!-- Comment content input -->
<input type="hidden" name="ideaID" value="<?php echo $row['IdeaID']; ?>">
<div class="form-floating mb-3">
Expand Down Expand Up @@ -334,7 +358,7 @@ class="rounded-circle me-2"

if (mysqli_query($conn, $sql)) {
echo "<script>alert('Comment added successfully!')</script>";
echo "<script>window.location.href='Staff.php'</script>";
echo "<script>window.location.href='Staff_MyPost.php'</script>";
} else {
echo "<script>alert('Error: " . $sql . "<br>" . mysqli_error($conn) . "')</script>";
}
Expand Down Expand Up @@ -388,34 +412,29 @@ class="rounded-circle me-2"
$isAnonymous = isset($_POST['anonymous']) ? true : false;

// Check if user uploaded a file
if (isset($_FILES['file-upload']['file-upload'])) {
if (isset($_FILES['file-upload']['name']) && $_FILES['file-upload']['name'] != "") {
$fileName = $_FILES['file-upload']['name'];
$tempFile = $_FILES['file-upload']['tmp_name'];
$uploadDir = "uploads/";
$fileType = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));

$validFileTypes = array("pdf", "doc", "docx", "txt", "path", "jpg", "png", "jpeg", "gif");
$validFileTypes = array("pdf", "doc", "docx", "txt", "path", "png", "jpg", "jpeg", "gif");
// Check if file type is valid
if (!in_array($fileType, $validFileTypes)) {
echo "<script>alert('Invalid file type. Please upload a PDF, DOC, DOCX, TXT or PATH file.')</script>";
echo "<script>window.location.href = 'Staff.php'</script>";
echo "<script>window.location.href = 'Staff_MyPost.php'</script>";
exit();
}

// Check file size
if ($_FILES['file-upload']['size'] > 5000000) {
} else if ($_FILES['file-upload']['size'] > 5000000) {
echo "<script>alert('File size must be less than 5 MB.')</script>";
echo "<script>window.location.href = 'Staff.php'</script>";
echo "<script>window.location.href = 'Staff_MyPost.php'</script>";
exit();
} else {
$documentPath = $uploadDir . uniqid() . "." . $fileType;
move_uploaded_file($tempFile, $documentPath);
}

$documentPath = $uploadDir . uniqid() . "." . $fileType;
move_uploaded_file($tempFile, $documentPath);
} else {
$documentPath = "";
}


$postDate = date("Y-m-d H:i:s");
$staffID = $_SESSION["staff_id"];

Expand All @@ -431,14 +450,14 @@ class="rounded-circle me-2"

if (!$result) {
echo "<script>alert('Error while saving document path.')</script>";
echo "<script>window.location.href = 'Staff.php'</script>";
echo "<script>window.location.href = 'Staff_MyPost.php'</script>";
}
}
echo "<script>alert('Idea added successfully.')</script>";
echo "<script>window.location.href = 'Staff.php'</script>";
echo "<script>window.location.href = 'Staff_MyPost.php'</script>";
} else {
echo "<script>alert('Error while adding idea: " . mysqli_error($conn) . "')</script>";
echo "<script>window.location.href = 'Staff.php'</script>";
echo "<script>window.location.href = 'Staff_MyPost.php'</script>";
}
}
?>
Expand All @@ -457,7 +476,7 @@ class="rounded-circle me-2"
aria-label="Close"></button>
</div>
<!-- body -->
<form action="Staff.php" method="post" enctype="multipart/form-data">
<form action="Staff_MyPost.php" method="post" enctype="multipart/form-data">
<div class="modal-body">
<!-- choose topic (auto-synced) -->
<input type="hidden" name="topic-id" id="topic-id">
Expand Down Expand Up @@ -697,4 +716,5 @@ class="text-primary">terms
});
</script>
</body>

</html>
49 changes: 35 additions & 14 deletions Staff_PostIdeas.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class="d-flex align-items-center text-decoration-none text-dark"
<!-- Create Idea Form -->
<div class="bg-white p-3 text-center mb-3">
<form class="text-start" action="Staff_PostIdeas.php?topic=<?php echo $topic; ?>"
enctype="multipart/form-data"
method="post">

<input type="hidden" name="topic-id" value="<?php echo $topic; ?>">

<!-- head -->
Expand Down Expand Up @@ -143,7 +143,7 @@ class="d-flex align-items-center text-decoration-none text-dark"
<div class="mb-3">
<!-- text -->
<label for="text" class="form-label text-primary">Your Ideas:</label>
<textarea class="form-control" id="text" rows="5" required
<textarea class="form-control" id="text" rows="5" required name="message"
placeholder="Enter your ideas here..."
></textarea>
</div>
Expand Down Expand Up @@ -188,35 +188,31 @@ class="text-primary">terms
$message = $_POST['message'];
$isAnonymous = isset($_POST['anonymous']) ? true : false;


// Check if user uploaded a file
if (isset($_FILES['file-upload']['file-upload'])) {
if (isset($_FILES['file-upload']['name']) && $_FILES['file-upload']['name'] != "") {
$fileName = $_FILES['file-upload']['name'];
$tempFile = $_FILES['file-upload']['tmp_name'];
$uploadDir = "uploads/";
$fileType = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));

$validFileTypes = array("pdf", "doc", "docx", "txt", "path", "jpg", "png", "jpeg", "gif");
$validFileTypes = array("pdf", "doc", "docx", "txt", "path", "png", "jpg", "jpeg", "gif");
// Check if file type is valid
if (!in_array($fileType, $validFileTypes)) {
echo "<script>alert('Invalid file type. Please upload a PDF, DOC, DOCX, TXT or PATH file.')</script>";
echo "<script>window.location.href = 'Staff_PostIdeas.php?topic=$topicID'</script>";
exit();
}

// Check file size
if ($_FILES['file-upload']['size'] > 5000000) {
} else if ($_FILES['file-upload']['size'] > 5000000) {
echo "<script>alert('File size must be less than 5 MB.')</script>";
echo "<script>window.location.href = 'Staff_PostIdeas.php?topic=$topicID'</script>";
exit();
} else {
$documentPath = $uploadDir . uniqid() . "." . $fileType;
move_uploaded_file($tempFile, $documentPath);
}

$documentPath = $uploadDir . uniqid() . "." . $fileType;
move_uploaded_file($tempFile, $documentPath);
} else {
$documentPath = "";
}


$postDate = date("Y-m-d H:i:s");
$staffID = $_SESSION["staff_id"];

Expand All @@ -226,7 +222,8 @@ class="text-primary">terms
if ($result) {
$ideaID = mysqli_insert_id($conn);
if (!empty($documentPath)) {
$sql = "INSERT INTO Document (DocumentPath, IdeaID) VALUES ('$documentPath', '$ideaID')";
$sql = "INSERT INTO Document (DocumentPath, IdeaID)
VALUES ('$documentPath', '$ideaID')";
$result = mysqli_query($conn, $sql);

if (!$result) {
Expand Down Expand Up @@ -372,6 +369,30 @@ class="rounded-circle me-2"
<!-- content -->
<div class="my-3">
<p><?php echo $row['Content']; ?></p>
<?php
// Check if a document exists in the database and display a download button if it does
$sql_download = "SELECT * FROM Document WHERE Document.IdeaID = " . $row['IdeaID'];
$result_download = mysqli_query($conn, $sql_download);
if (mysqli_num_rows($result_download) > 0) {
// Build a zip file containing all the documents for this idea
$zip = new ZipArchive();
$zipName = "documents-" . $row['IdeaID'] . ".zip";
$zipPath = "uploads/" . $zipName;
if ($zip->open($zipPath, ZipArchive::CREATE) !== TRUE) {
echo "Error: Could not create zip file";
}
while ($row_download = mysqli_fetch_assoc($result_download)) {
$filePath = $row_download['DocumentPath'];
$fileName = basename($filePath);
$zip->addFile($filePath, $fileName);
}
$zip->close();
?>
<a href="<?php echo $zipPath; ?>" download="<?php echo $zipName; ?>"
class="btn btn-primary">Download Documents</a>
<?php
}
?>
</div>
<!-- like and dislike buttons -->
<div class="d-flex justify-content-between align-items-center">
Expand Down
Loading

0 comments on commit 1c27df7

Please sign in to comment.