Your IP : 216.73.216.86


Current Path : /home/emeraadmin/www/4d695/
Upload File :
Current File : /home/emeraadmin/www/4d695/send_task_email_to_workers.php.tar

home/emeraadmin/public_html/pages/send_task_email_to_workers.php000064400000014440151677420050021322 0ustar00<?php
// Load .env file
require_once '../vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();

// Set base URL
$base_url = $_ENV['BASE_URL'];

// Include necessary files
require_once 'emailUtils.php'; // Ensure this file contains your sendEmail function
require_once '../Service/TaskService.php'; // Adjust path as per your project structure

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents("php://input"), true);

    // Extract action, email, and task IDs from the decoded data
    $action = $data['action'] ?? null;
    $email = $data['worker_email'] ?? null;
    $taskIds = $data['task_ids'] ?? [];

    $taskService = new TaskService();
    $errors = [];
    $successes = [];

    if ($action === 'bulk') {
        $emailContent = ''; // Initialize variable to accumulate email content

        foreach ($taskIds as $taskId) {
            $task = $taskService->getTaskById($taskId);

            if ($task) {
                $taskDates = json_decode($task->dates, true);
                $taskStartDate = $taskDates[0]['date'];
                $taskEndDate = $taskDates[count($taskDates) - 1]['date'];

                // Construct task-specific content
                $taskContent = "
                    <div class='task-item'>
                        <p>Service Location: {$task->service_name}</p>
                        <p>Dates: " . date('d/m/Y', strtotime($taskStartDate)) . " to " . date('d/m/Y', strtotime($taskEndDate)) . "</p>
                        <p>Frequency: {$task->frequency}</p>
                        <a href='{$base_url}/pages/taskform.php?task_id={$task->id}&email={$email}' class='button'>Fill out the form</a>
                    </div>
                    <hr>
                ";
                $emailContent .= $taskContent; // Append task content to accumulated email content

                // Update task status in the database
                $updateResult = $taskService->updateTaskStatusAfterMessage($taskId, $email);
                if (!$updateResult) {
                    $errors[] = "Failed to update database for task ID $taskId";
                }
            } else {
                $errors[] = "Task not found for ID $taskId";
            }
        }

        if (empty($errors)) {
            $emailTemplate = "
                <!DOCTYPE html>
                <html lang='en'>
                <head>
                    <meta charset='UTF-8'>
                    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
                    <title>Service Confirmation Request</title>
                    <style>
                        body { font-family: Arial, sans-serif; background-color: #f9f9f9; color: #333; margin: 0; padding: 0; }
                        .container { width: 100%; max-width: 600px; margin: 30px auto; padding: 30px; background-color: #ffffff; border: 1px solid #e0e0e0; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); }
                        .header { text-align: center; padding-bottom: 20px; border-bottom: 1px solid #e0e0e0; }
                        .header img { max-width: 150px; height: auto; }
                        .content { line-height: 1.6; padding: 20px 0; }
                        .details { background-color: #003763; color: #ffffff; padding: 20px; border-radius: 5px; margin: 20px 0; }
                        .details p { margin: 0 0 10px; }
                        .details .button { display: inline-block; padding: 10px 20px; background-color: #ffce00; color: #003763; text-decoration: none; border-radius: 5px; text-align: center; font-weight: bold; transition: background-color 0.3s; margin-top: 10px; }
                        .details .button:hover { background-color: #e6b800; }
                        .separator { border-bottom: 1px dashed #ffffff; margin: 20px 0; }
                        .footer { margin-top: 30px; font-size: 12px; color: #777; text-align: center; border-top: 1px solid #e0e0e0; padding-top: 20px; }
                        .footer ul { list-style-type: none; padding: 0; margin: 10px 0; }
                        .footer li { margin-bottom: 5px; }
                        .footer p { margin: 5px 0; }
                    </style>
                </head>
                <body>
                    <div class='container'>
                        <div class='header'>
                            <img src='https://emerafs.com.au/wp-content/uploads/2023/07/Emera-Primary-Logo-Color-e1691736085254.png' alt='Company Logo'>
                        </div>
                        <div class='content'>
                            <p>Dear {$email},</p>
                            <p>Please find the attached details for the upcoming services:</p>
                            <div class='details'>
                                {$emailContent}
                            </div>
                        </div>
                        <div class='footer'>
                            <p>Best regards,</p>
                            <p>EMERA FACILITY SERVICE</p>
                            <hr>
                            <p><strong>Important:</strong></p>
                            <ul>
                                <li>Do not share your email with anyone.</li>
                            </ul>
                        </div>
                    </div>
                </body>
                </html>
            ";

            $result = sendEmail($email, "New Tasks Assigned", $emailTemplate);

            if (strpos($result, 'Email sent') !== false) {
                $successes[] = "Bulk email sent successfully to $email";
            } else {
                $errors[] = "Failed to send bulk email to $email: $result";
            }
        }

        echo json_encode(empty($errors) ? ['status' => 'success', 'message' => $successes] : ['status' => 'partial', 'successes' => $successes, 'errors' => $errors]);
    } else {
        echo json_encode(['status' => 'error', 'message' => "Invalid action parameter: $action"]);
    }
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request method']);
}
?>