| Current Path : /home/emeraadmin/public_html/pages/emeraadmin/ |
| Current File : /home/emeraadmin/public_html/pages/emeraadmin/viewtask.php |
<?php
include('head.php');
// Include database connection and TaskService class
require_once '../../Classes/Database.php';
require_once '../../Service/TaskService.php';
require_once '../../Classes/System.php';
$System = new System();
// Create TaskService instance
$taskService = new TaskService();
// Check if task ID is provided in URL
if (isset($_GET['id'])) {
// Fetch task details based on ID
$taskId = $System->decryptData($_GET['id']);
// show error if the provided id is not valid with Swal alert
if (!$taskId) {
echo '<script>
Swal.fire({
title: "Invalid task ID",
text: "The URL parameter is invalid. Please go back and try again.",
icon: "error",
confirmButtonText: "Ok"
}).then(() => {
window.location.href = "tasks.php";
});
</script>';
}
$task = $taskService->getTaskById($taskId);
// show error if the task is not found with Swal alert
if (!$task) {
exit();
}
} else {
echo '<p>No task ID provided.</p>';
}
$subtasks = $taskService->getSubtasksByTaskId($taskId);
$subtaskDates = '';
for ($i = 0; $i < count($subtasks); $i++) {
$subtaskDates .= '"' . $subtasks[$i]->subtask_date . '"';
if ($i < count($subtasks) - 1) {
$subtaskDates .= ',';
}
}
?>
<div class="main-content">
<div class="container-fluid">
<div class="page-header">
<div class="row align-items-end">
<div class="col-lg-8">
<div class="page-header-title">
<i class="ik ik-box bg-blue"></i>
<div class="d-inline">
<h5>View Task</h5>
<span>View details of a task</span>
</div>
</div>
</div>
<div class="col-lg-4">
<nav class="breadcrumb-container" aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item">
<a href="../../index.html"><i class="ik ik-home"></i></a>
</li>
<li class="breadcrumb-item">
<a href="#">Tasks</a>
</li>
<li class="breadcrumb-item active" aria-current="page">View Task</li>
</ol>
</nav>
</div>
</div>
</div>
<!-- New row for buttons and actions -->
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<div class="card-header-left">
<a href="javascript:history.back()" class="btn btn-outline-primary">Back</a>
</div>
<div class="card-header-right">
<?php
if (!$task->is_assigned) {
//echo '<a href="viewcompany.php?id=' . $System->encryptData($task->assigned_to) . '" class="btn btn-primary">View assigned company</a>';
echo '<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#AssignTaskModal">Assign to a Subcontractor</button>';
}
if (!$task->is_assigned && !$task->isPublic) {
echo ' <button type="button" class="btn btn-success" data-toggle="modal" name="makePublic" id="makePublic">Make Public </button>';
}
?>
</div>
</div>
</div>
</div>
</div>
<!-- Modal to assign task to a company -->
<div class="modal fade" id="AssignTaskModal" role="dialog" aria-labelledby="exampleModalCenterLabel"
aria-hidden="true">
<div class="modal-dialog modal-lg mt-0 mb-0" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterLabel">Assign Task to Company</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<form id="tasksForm">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<div class="col-sm-9">
<input type="hidden" id="schedule-id" name="schedule-id"
class="form-control"
value="<?php echo $taskId; ?>" readonly>
</div>
</div>
<div class="form-group row">
<label for="companyId" class="col-sm-3 col-form-label">Subcontractor</label>
<div class="col-sm-9">
<select id="companyId" name="companyId" class="form-control select2">
<!-- PHP code to fetch and display companies -->
<?php
// Fetch company list from the database
$companies = $taskService->getAllCompanies();
if ($companies) {
foreach ($companies as $company) {
echo '<option value="' . $company->id . '">' . $company->first_name . " " . $company->last_name . '</option>';
}
} else {
echo '<option value="">No companies available</option>';
}
?>
</select>
</div>
</div>
</div>
</div>
</form>
<div id="message" class="mt-3"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" form="tasksForm" id="assignTask" class="btn btn-primary">Assign Task
</button>
</div>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="AddSubtaskModal" tabindex="-1" role="dialog" aria-labelledby="AddSubtaskModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="AddSubtaskModalLabel">Add Subtask</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form id="addSubtaskForm">
<div class="modal-body">
<input type="hidden" name="task_id" value="<?php echo $taskId; ?>">
<input type="hidden" class="form-control" id="service_name" name="service_name"
value="<?php echo $task->service_id; ?>">
<div class="form-group">
<label for="subtask_date">Subtask Date</label>
<input type="date" class="form-control" id="subtask_date" name="subtask_date" required>
</div>
<div class="form-group">
<label for="subtask_time">Subtask Time</label>
<input type="time" class="form-control" id="subtask_time" name="subtask_time" required>
<!-- <label for="subtask_time">Subtask Time</label>-->
<!-- <input type="time" class="form-control" id="subtask_time" name="subtask_time">-->
</div>
<!-- Add more fields as needed -->
<script>
//Disable all dates before today
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
today = yyyy + '-' + mm + '-' + dd;
document.getElementById("subtask_date").setAttribute("min", today);
</script>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
<script>
var addSubtaskForm = document.getElementById('addSubtaskForm');
if (addSubtaskForm) {
addSubtaskForm.addEventListener('submit', function (e) {
e.preventDefault();
var formData = new FormData(addSubtaskForm);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'add_subtask.php', true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText); // Log the response
if (xhr.responseText === 'Subtask added successfully!') {
Swal.fire({
title: 'Subtask added successfully.',
icon: 'success',
confirmButtonText: 'Ok'
});
// Optionally reload the page after a short delay
setTimeout(() => {
window.location.reload();
}, 2000);
} else if (xhr.responseText === 'error') {
Swal.fire({
title: 'An error occurred',
text: 'Please try again.',
icon: 'error',
confirmButtonText: 'Ok'
});
} else if (xhr.responseText === 'invalid_data') {
Swal.fire({
title: 'Invalid data',
text: 'Please check the data and try again.',
icon: 'error',
confirmButtonText: 'Ok'
});
} else if (xhr.responseText === 'invalid_request') {
Swal.fire({
title: 'Invalid request',
text: 'Please check the request and try again.',
icon: 'error',
confirmButtonText: 'Ok'
});
}
}
};
xhr.send(formData);
});
} else {
console.error('Add subtask form not found.');
}
</script>
</div>
</div>
</div>
<div class="row">
<div class="col-xl-6 col-md-6">
<div class="card ">
<div class="card-body">
<div class="row align-items-center mb-30">
<div class="col-md-7">
<table>
<tbody>
<tr>
<td><i class="ik ik-layers icon-large"></i></td>
<td><h3 class="mb-0 fw-700 text-black"><?php echo $task->service_name; ?> </h3>
</td>
</tr>
<tr>
<td><i class="ik ik-map-pin icon-large"></i></td>
<td><h5 class="mb-0 fw-500 text-black"><?php echo $task->region; ?> </h5></td>
</tr>
<tr>
<td><i class="ik ik-calendar icon-large"></i></td>
<td><h5 class="mb-0 fw-500 text-black"><?php echo $task->schedule_name; ?> </h5>
</td>
</tr>
<tr>
<td><i class="ik ik-clock icon-large"></i></td>
<td><h5 class="mb-0 fw-500 text-black"><?php echo $task->frequency; ?> </h5>
</td>
</tr>
<tr>
<td><i class="ik ik-check-circle icon-large"></i></td>
<td>
<h2><?php echo($task->finished ? '<span class="badge badge-success">Task completed</span>' : '<span class="badge badge-danger">Not Completed</span>'); ?></h2>
</td>
</tr>
<!-- is public or not-->
<tr>
<td><i class="ik ik-eye icon-large"></i></td>
<td>
<h2><?php echo($task->isPublic ? '<span class="badge badge-yellow">Public</span>' : '<span class="badge badge-success">Not Public</span>'); ?></h2>
</td>
</tr>
<tr>
<td><i class="ik ik-user-check icon-large"></i></td>
<td>
<h2><?php echo($task->is_assigned ? '<span class="badge badge-success">Assigned</span>' : '<span class="badge badge-danger">Not Assigned</span>'); ?></h2>
</td>
</tr>
<?php if ($task->is_assigned) : ?>
<tr>
<td><i class="ik ik-user icon-large"></i></td>
<td><h6>Task Assigned To <?php echo $task->assigned_to_name; ?></h6></td>
</tr>
<?php endif; ?>
</tbody>
</table>
<style>
table {
width: 100%;
border-collapse: collapse;
}
td {
padding: 10px 15px;
vertical-align: middle;
}
.icon-large {
font-size: 24px;
color: #333;
}
.mb-0 {
margin-bottom: 0;
}
.fw-700 {
font-weight: 700;
}
.fw-500 {
font-weight: 500;
}
.text-black {
color: #000;
}
.badge-success {
background-color: #28a745;
color: #fff;
padding: 5px 10px;
border-radius: 5px;
}
.badge-danger {
background-color: #dc3545;
color: #fff;
padding: 5px 10px;
border-radius: 5px;
}
</style>
</div>
<div class="col-md-4">
<?php
// Assuming $task->dates is a JSON string
$datesJson = $task->dates;
// Decode the JSON string to an array
$datesArray = json_decode($datesJson, true);
// Extract the dates and format them for JavaScript
$formattedDates = array_map(function ($dateEntry) {
return '"' . $dateEntry['date'] . '"';
}, $datesArray);
$jsDates = implode(",", $formattedDates); // Convert the array to a comma-separated string
?>
<div id="datepicker"></div>
<script>
flatpickr("#datepicker", {
enable: [<?php echo $jsDates; ?>], // Array of selectable dates
dateFormat: "Y-m-d", // Date format
inline: true, // Show calendar inline
defaultDate: today, // Highlighted date
disableMobile: "false"
});
</script>
</div>
</div>
</div>
</div>
</div>
<!-- Form to assign task to a company -->
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h3>Subtasks</h3>
<div class="card-header-right">
<?php if ($task->is_assigned == 0) {
echo '<button type="button" class="btn btn-success" data-toggle="modal" data-target="#AddSubtaskModal">Add Subtask </button>';
}
?>
<!--button to ignore selected subtasks-->
<button type="button" class="btn btn-danger" id="ignoreSubtasks">Ignore Selected Subtasks
</button>
<script>
//Show ignoreSubtasks if any subtask is selected
var ignoreSubtasks = document.getElementById('ignoreSubtasks');
var checkboxes = document.querySelectorAll('.select_all_child');
checkboxes.forEach(function (checkbox) {
checkbox.addEventListener('change', function () {
var checked = document.querySelectorAll('.select_all_child:checked');
if (checked.length > 0) {
ignoreSubtasks.style.display = 'block';
} else {
ignoreSubtasks.style.display = 'none';
}
});
});
ignoreSubtasks.addEventListener('click', function () {
var checked = document.querySelectorAll('.select_all_child:checked');
var subtaskIds = [];
checked.forEach(function (checkbox) {
// Assuming the subtask ID is in the text content and needs to be trimmed
var subtaskId = checkbox.parentElement.nextElementSibling.textContent.trim();
// Remove the first two characters
subtaskId = subtaskId.substring(6);
subtaskIds.push(subtaskId);
});
console.log(subtaskIds);
// check if any subtask is selected
if (subtaskIds.length === 0) {
Swal.fire({
title: 'No subtasks selected',
text: 'Please select one or more subtasks to ignore.',
icon: 'warning',
confirmButtonText: 'Ok'
});
return;
}
else {
Swal.fire({
title: 'Are you sure?',
text: "Do you want to ignore the selected subtasks?",
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, ignore them!',
cancelButtonText: 'No, cancel'
}).then((result) => {
if (result.isConfirmed) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'ignore_subtasks.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
if (response.status === 'success') {
Swal.fire({
title: 'Subtasks ignored successfully.',
icon: 'success',
confirmButtonText: 'Ok'
});
// Optionally reload the page after a short delay
setTimeout(() => {
window.location.reload();
}, 2000);
} else {
let errorMessage = response.message || 'An error occurred. Please try again.';
Swal.fire({
title: 'Error',
text: errorMessage,
icon: 'error',
confirmButtonText: 'Ok'
});
}
}
};
xhr.send('subtaskIds=' + JSON.stringify(subtaskIds));
}
});
}
});
</script>
</div>
</div>
<div class="card-body">
<!-- PHP code to fetch and display subtasks -->
<?php
// Fetch and display subtasks associated with the task
// Display subtasks
if ($subtasks) {
?>
<table class="table" id="advanced_table">
<thead>
<tr>
<th>#</th>
<th>Id</th>
<th>Date</th>
<th>Updated At</th>
<th>Assigned</th>
<th>Completed</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach ($subtasks as $subtask) {
?>
<tr>
<?php if (!$subtask->completed && !$subtask->ignored) {
echo '<td><input type="checkbox" class="select_all_child" id="selectall_<?php echo strtotime($date); ?>"></td> ';
}
else {
echo '<td></td>';
}
?>
<td> ST-000<?php echo $subtask->id; ?></td>
<td><?php echo $subtask->subtask_date; ?></td>
<td><?php echo date('F j, Y, g:i a', strtotime($subtask->updated_at)); ?></td>
<td><?php echo($subtask->assigned ? '<span class="badge badge-success">Assigned</span>' : '<span class="badge badge-danger">Not Assigned</span>'); ?></td>
<td>
<?php
if ($subtask->ignored) {
echo '<span class="badge badge-warning">Ignored</span>';
} else {
echo $subtask->completed
? '<span class="badge badge-success">Completed</span>'
: '<span class="badge badge-danger">Not Completed</span>';
}
?>
</td>
<td>
<a href="viewsubtask.php?id=<?php echo $System->encryptData($subtask->id); ?>"
class="btn btn-primary">View</a>
<!-- if subtask is not assigned to any company then delete button will be shown -->
<?php
if ($subtask->assigned == 0 && $task->is_assigned == 0) {
echo '<button type="button" class="btn btn-danger delete-subtask" data-subtask-id="' . $subtask->id . '">Delete</button>';
}
?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
} else {
echo '<p>No subtasks found for this task.</p>';
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
var assignTaskButton = document.getElementById('assignTask');
if (assignTaskButton) {
assignTaskButton.addEventListener('click', function () {
console.log('Assign task button found.');
var companyId = document.getElementById('companyId').value;
if (companyId) {
Swal.fire({
title: 'Are you sure?',
text: "Do you want to assign this task to the selected company?",
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, assign it!',
cancelButtonText: 'No, cancel'
}).then((result) => {
if (result.isConfirmed) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'assigntask.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText); // Log the response
if (xhr.responseText === 'success') {
Swal.fire({
title: 'Task assigned successfully.',
icon: 'success',
confirmButtonText: 'Ok'
});
// Optionally reload the page after a short delay
setTimeout(() => {
window.location.reload();
}, 2000);
} else if (xhr.responseText === 'error') {
Swal.fire({
title: 'An error occurred',
text: 'Please try again.',
icon: 'error',
confirmButtonText: 'Ok'
});
} else if (xhr.responseText === 'task_not_found') {
Swal.fire({
title: 'Task not found',
text: 'Please check the task ID.',
icon: 'error',
confirmButtonText: 'Ok'
});
} else if (xhr.responseText === 'invalid_data') {
Swal.fire({
title: 'Invalid data',
text: 'Please check the data and try again.',
icon: 'error',
confirmButtonText: 'Ok'
});
} else if (xhr.responseText === 'invalid_request') {
Swal.fire({
title: 'Invalid request',
text: 'Please check the request and try again.',
icon: 'error',
confirmButtonText: 'Ok'
});
}
}
};
var taskId = '<?php echo htmlspecialchars($taskId); ?>';
xhr.send('taskId=' + taskId + '&companyId=' + companyId);
}
});
}
});
} else {
console.error('Assign task button not found.');
}
// make public button
var makePublicButton = document.getElementById('makePublic');
if (makePublicButton) {
makePublicButton.addEventListener('click', function () {
console.log('Make public button found.');
Swal.fire({
title: 'Are you sure?',
text: "Do you want to make this task public?",
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, make it public!',
cancelButtonText: 'No, cancel'
}).then((result) => {
if (result.isConfirmed) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'makepublic.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText); // Log the response
if (xhr.responseText === 'success') {
Swal.fire({
title: 'Task made public successfully.',
icon: 'success',
confirmButtonText: 'Ok'
});
// Optionally reload the page after a short delay
setTimeout(() => {
window.location.reload();
}, 2000);
} else if (xhr.responseText === 'error') {
Swal.fire({
title: 'An error occurred',
text: 'Please try again.',
icon: 'error',
confirmButtonText: 'Ok'
});
} else if (xhr.responseText === 'task_not_found') {
Swal.fire({
title: 'Task not found',
text: 'Please check the task ID.',
icon: 'error',
confirmButtonText: 'Ok'
});
} else if (xhr.responseText === 'invalid_data') {
Swal.fire({
title: 'Invalid data',
text: 'Please check the data and try again.',
icon: 'error',
confirmButtonText: 'Ok'
});
} else if (xhr.responseText === 'invalid_request') {
Swal.fire({
title: 'Invalid request',
text: 'Please check the request and try again.',
icon: 'error',
confirmButtonText: 'Ok'
});
}
}
};
var taskId = '<?php echo htmlspecialchars($taskId); ?>';
xhr.send('taskId=' + taskId);
}
});
});
} else {
console.error('Make public button not found.');
}
document.addEventListener('DOMContentLoaded', function () {
// Find all delete buttons and attach click event
var deleteButtons = document.querySelectorAll('.delete-subtask');
deleteButtons.forEach(function (button) {
button.addEventListener('click', function () {
var subtaskId = button.getAttribute('data-subtask-id');
// Show confirmation dialog using SweetAlert
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
// If confirmed, make AJAX request to delete_subtask.php
var xhr = new XMLHttpRequest();
xhr.open('POST', 'delete_subtask.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText); // Log the response for debugging
if (xhr.responseText === 'success') {
Swal.fire({
title: 'Deleted!',
text: 'Your subtask has been deleted.',
icon: 'success'
}).then(() => {
window.location.reload(); // Reload the page after deletion
});
} else {
Swal.fire({
title: 'Error!',
text: 'Failed to delete subtask.',
icon: 'error'
});
}
}
};
xhr.send('subtaskId=' + subtaskId); // Send subtask ID to delete_subtask.php
}
});
});
});
});
</script>
<?php include('footer.php'); ?>