| Current Path : /home/emeraadmin/public_html/test/emeraltd_prod/Service/ |
| Current File : /home/emeraadmin/public_html/test/emeraltd_prod/Service/SubtaskService.php |
<?php
require_once __DIR__ . '/../Classes/Database.php';
require_once __DIR__ . '/../Classes/Subtask.php';
class SubtaskService
{
private $db;
public function __construct()
{
$this->db = new Database();
}
public function getSubtaskById($subtaskId)
{
$query = '
SELECT
s.*,
t.service_id,
srv.name as service_name,
srv.region as service_region
FROM subtasks s
LEFT JOIN tasks t ON s.task_id = t.id
LEFT JOIN services srv ON t.service_id = srv.id
WHERE s.id = :id
';
$this->db->query($query);
$this->db->bind(':id', $subtaskId);
return $this->db->single();
}
public function getSubtaskDetailsForWorkForm($id)
{
$query = '
SELECT
s.*,
t.service_id,
srv.name as service_name,
srv.region as service_region
FROM subtasks s
LEFT JOIN tasks t ON s.task_id = t.id
LEFT JOIN services srv ON t.service_id = srv.id
WHERE s.id = :id
';
$this->db->query($query);
$this->db->bind(':id', $id);
return $this->db->single();
}
public function updateSubtaskStatusAfterMessage(int $subtask_id, string $assigned_to_Mobile, string $assigned_Message_Rsponse)
{
try {
$this->db->query('UPDATE subtasks SET assigned = true, assigned_to_Mobile = :assigned_to_Mobile, assigned_Message_Rsponse = :assigned_Message_Rsponse WHERE id = :id');
$this->db->bind(':id', $subtask_id);
$this->db->bind(':assigned_to_Mobile', $assigned_to_Mobile);
$this->db->bind(':assigned_Message_Rsponse', $assigned_Message_Rsponse);
$result = $this->db->execute();
if (!$result) {
throw new Exception('Database execution failed.');
}
return $result;
} catch (Exception $e) {
error_log('Error in updateSubtaskStatusAfterMessage: ' . $e->getMessage());
throw $e;
}
}
public function addSubtask(Subtask $subtask)
{
try {
$this->db->query('INSERT INTO subtasks (task_id, service_id, subtask_date, subtask_time, created_at, updated_at) VALUES (:task_id, :service_id, :subtask_date, :subtask_time, :created_at, :updated_at)');
$this->db->bind(':task_id', $subtask->task_id);
$this->db->bind(':service_id', $subtask->service_id);
$this->db->bind(':subtask_date', $subtask->subtask_date);
$this->db->bind(':subtask_time', $subtask->subtask_time);
$this->db->bind(':created_at', $subtask->created_at);
$this->db->bind(':updated_at', $subtask->updated_at);
$result = $this->db->execute();
if (!$result) {
throw new Exception('Database execution failed.');
}
return $result;
} catch (Exception $e) {
error_log('Error in addSubtask: ' . $e->getMessage());
throw $e;
}
}
public function deleteSubtaskById(int $subtaskId)
{
$this->db->query('DELETE FROM subtasks WHERE id = :id');
$this->db->bind(':id', $subtaskId);
return $this->db->execute();
}
public function getSubtaskLocation(string $subtaskId)
{
$this->db->query('SELECT latitude, longitude FROM form_submissions WHERE subtask_id = :subtask_id');
$this->db->bind(':subtask_id', $subtaskId);
return $this->db->single();
}
public function getSubtasksByDayForAssignedUser(int $assignedUserId)
{
//for now select all subtasks
$this->db->query('SELECT * FROM subtasks');
$subtasks = $this->db->resultSet();
$subtasksByDay = [];
foreach ($subtasks as $subtask) {
$subtaskDate = $subtask->subtask_date;
$subtaskDate = date('Y-m-d', strtotime($subtaskDate));
$subtasksByDay[$subtaskDate][] = $subtask;
}
return $subtasksByDay;
}
public function getAllSubtasksForTask($task_id)
{
try {
// Adjusted SQL query to include service_name and region from services table
$query = "
SELECT
subtasks.*,
srv.name AS service_name,
srv.region AS region
FROM subtasks
INNER JOIN tasks ON subtasks.task_id = tasks.id
LEFT JOIN services srv ON tasks.service_id = srv.id
WHERE subtasks.task_id = :task_id
";
$this->db->query($query);
$this->db->bind(':task_id', $task_id);
$subtasks = $this->db->resultSet();
return $subtasks;
} catch (PDOException $e) {
// Handle database errors appropriately (log, throw further, etc.)
error_log('Database error: ' . $e->getMessage());
return []; // Return empty array or handle error as needed
}
}
public function markSubtaskAsComplete($subtaskId)
{
try {
$this->db->query('UPDATE subtasks SET completed = true, completed_at = NOW(),submitted_by_admin = true WHERE id = :id');
$this->db->bind(':id', $subtaskId);
$result = $this->db->execute();
if (!$result) {
throw new Exception('Database execution failed.');
}
return $result;
} catch (Exception $e) {
error_log('Error in markSubtaskAsComplete: ' . $e->getMessage());
throw $e;
}
}
public function ignoreSubtask($subtaskId)
{
try {
$this->db->query('UPDATE subtasks SET ignored = true WHERE id = :id');
$this->db->bind(':id', $subtaskId);
$result = $this->db->execute();
if (!$result) {
throw new Exception('Database execution failed.');
}
return $result;
} catch (Exception $e) {
error_log('Error in ignoreSubtask: ' . $e->getMessage());
throw $e;
}
}
public function ignoreSubtasks(array $subtaskIds)
{
try {
$placeholders = implode(',', array_fill(0, count($subtaskIds), '?'));
$query = "UPDATE subtasks SET ignored = true WHERE id IN ($placeholders)";
$this->db->query($query);
foreach ($subtaskIds as $index => $id) {
$this->db->bind(($index + 1), $id);
}
$result = $this->db->execute();
if (!$result) {
throw new Exception('Database execution failed.');
}
return $result;
} catch (Exception $e) {
error_log('Error in ignoreSubtasks: ' . $e->getMessage());
throw $e;
}
}
}