MMCT TEAM
Server IP : 217.21.85.138  /  Your IP : 216.73.216.103
Web Server : LiteSpeed
System : Linux in-mum-web906.main-hosting.eu 4.18.0-553.37.1.lve.el8.x86_64 #1 SMP Mon Feb 10 22:45:17 UTC 2025 x86_64
User : u915722082 ( 915722082)
PHP Version : 7.4.33
Disable Function : system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : OFF  |  Python : OFF
Directory (0755) :  /home/u915722082/public_html/globalmining/../pms/admin/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/u915722082/public_html/globalmining/../pms/admin/designer_task_actions.php
<?php
session_start();
header('Content-Type: application/json');
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);

if (!isset($_SESSION['admin_id'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

require_once '../config/config.php';
require_once '../config/db.php';

// Import PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$autoload_paths = [
    '../vendor/autoload.php',
    '../../vendor/autoload.php',
    '../../../vendor/autoload.php'
];

foreach ($autoload_paths as $path) {
    if (file_exists($path)) {
        require_once $path;
        break;
    }
}

$action = $_POST['action'] ?? '';
$current_user_id = $_SESSION['admin_id'];
$current_user_name = $_SESSION['user_name'] ?? 'Admin';
$current_user_role = $_SESSION['role'] ?? 'Employee';

// Only CEO and Manager can assign designer tasks
if ($current_user_role !== 'CEO' && $current_user_role !== 'Manager') {
    echo json_encode(['success' => false, 'message' => 'You do not have permission to manage designer tasks']);
    exit;
}

try {
    switch ($action) {
        case 'add':
            addDesignerTask($con, $current_user_id, $current_user_name);
            break;
            
        case 'edit':
            editDesignerTask($con, $current_user_id);
            break;
            
        case 'delete':
            deleteDesignerTask($con);
            break;
            
        default:
            throw new Exception('Invalid action');
    }
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}

function sendDesignerTaskEmail($designer_email, $designer_name, $title, $description, $priority, $end_date, $assigned_by_name) {
    if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
        error_log("PHPMailer not available");
        return false;
    }
    
    try {
        $mail = new PHPMailer(true);
        
        $mail->SMTPDebug = 0;
        $mail->isSMTP();
        $mail->Host = SMTP_HOST;
        $mail->SMTPAuth = true;
        $mail->Username = SMTP_USERNAME;
        $mail->Password = SMTP_PASSWORD;
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port = SMTP_PORT;
        $mail->CharSet = 'UTF-8';
        
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        
        $mail->setFrom(SMTP_FROM_EMAIL, SMTP_FROM_NAME);
        $mail->addAddress($designer_email, $designer_name);
        $mail->addReplyTo(SMTP_FROM_EMAIL, SMTP_FROM_NAME);
        
        $mail->isHTML(true);
        $mail->Subject = 'New Design Task Assigned - ' . $title;
        
        $formatted_end_date = $end_date ? date('F j, Y', strtotime($end_date)) : 'Not specified';
        
        $priority_color = [
            'Low' => '#95a5a6',
            'Medium' => '#3498db',
            'High' => '#f39c12',
            'Urgent' => '#e74c3c'
        ];
        
        $mail->Body = "
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset='UTF-8'>
        </head>
        <body style='font-family: Arial, sans-serif; line-height: 1.6; background: #f4f4f4; margin: 0; padding: 0;'>
            <div style='max-width: 600px; margin: 20px auto; background: white; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 10px rgba(0,0,0,0.1);'>
                <div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center;'>
                    <h1 style='margin:0; font-size: 24px;'>🎨 New Design Task Assigned</h1>
                </div>
                <div style='padding: 30px;'>
                    <h2 style='color: #333; margin-top: 0;'>Hello $designer_name,</h2>
                    <p>You have been assigned a new design task by <strong>$assigned_by_name</strong>.</p>
                    
                    <div style='background: #f8f9fa; padding: 20px; margin: 20px 0; border-left: 4px solid #667eea; border-radius: 4px;'>
                        <h3 style='color: #667eea; margin-top:0;'>Task Details:</h3>
                        <p><strong>Title:</strong> $title</p>
                        <p><strong>Description:</strong> " . ($description ?: 'No description provided') . "</p>
                        <p><strong>Priority:</strong> <span style='background: " . $priority_color[$priority] . "; color: white; padding: 4px 8px; border-radius: 4px;'>$priority</span></p>
                        <p><strong>Deadline:</strong> <span style='color: #e74c3c; font-weight: bold;'>$formatted_end_date</span></p>
                    </div>
                    
                    <p style='color: #666;'>Please start working on this task and update the status accordingly. If you have any questions, contact $assigned_by_name.</p>
                    
                    <p>Best regards,<br><strong>TDS Projects Team</strong></p>
                </div>
                <div style='text-align: center; padding: 20px; color: #666; font-size: 14px; background: #f8f9fa;'>
                    <p>&copy; " . date('Y') . " TheDotStudios. All rights reserved.</p>
                    <p>This is an automated email, please do not reply.</p>
                </div>
            </div>
        </body>
        </html>
        ";
        
        $mail->AltBody = "New Design Task Assigned\n\nHello $designer_name,\n\nTitle: $title\nPriority: $priority\nDeadline: $formatted_end_date\n\nAssigned by: $assigned_by_name";
        
        $mail->send();
        return true;
        
    } catch (Exception $e) {
        error_log("Email Error: " . $e->getMessage());
        return false;
    }
}

function addDesignerTask($con, $current_user_id, $current_user_name) {
    $user_id = intval($_POST['user_id'] ?? 0);
    $title = trim($_POST['title'] ?? '');
    $short_description = trim($_POST['short_description'] ?? '');
    $priority = $_POST['priority'] ?? 'Medium';
    $draft_date = !empty($_POST['draft_date']) ? $_POST['draft_date'] : null;
    $end_date = !empty($_POST['end_date']) ? $_POST['end_date'] : null;
    $publish_date = !empty($_POST['publish_date']) ? $_POST['publish_date'] : null;
    $link = trim($_POST['link'] ?? '');
    
    // Required fields validation
    if (empty($user_id) || empty($title) || empty($short_description) || empty($priority)) {
        throw new Exception('All required fields must be filled');
    }
    
    // Validate dates if provided
    if ($end_date && strtotime($end_date) < strtotime(date('Y-m-d'))) {
        throw new Exception('End date cannot be in the past');
    }
    
    if ($draft_date && $end_date && strtotime($draft_date) > strtotime($end_date)) {
        throw new Exception('Draft date cannot be after end date');
    }
    
    // Validate link if provided
    if (!empty($link) && !filter_var($link, FILTER_VALIDATE_URL)) {
        throw new Exception('Please enter a valid URL');
    }
    
    // Get designer details
    $user_stmt = mysqli_prepare($con, "SELECT fname, email FROM tbl_user WHERE uid = ?");
    mysqli_stmt_bind_param($user_stmt, "i", $user_id);
    mysqli_stmt_execute($user_stmt);
    $user_result = mysqli_stmt_get_result($user_stmt);
    $designer = mysqli_fetch_assoc($user_result);
    mysqli_stmt_close($user_stmt);
    
    if (!$designer) {
        throw new Exception('Designer not found');
    }
    
  // Insert designer task
    $stmt = mysqli_prepare($con, 
        "INSERT INTO tbl_designer_tasks (user_id, status, title, priority, short_description, draft_date, end_date, publish_date, link, assigned_by) 
         VALUES (?, 'ASSIGNED', ?, ?, ?, ?, ?, ?, ?, ?)"
    );
    mysqli_stmt_bind_param($stmt, "isssssssi", $user_id, $title, $priority, $short_description, $draft_date, $end_date, $publish_date, $link, $current_user_id);
    
    if (mysqli_stmt_execute($stmt)) {
        mysqli_stmt_close($stmt);
        
        $email_sent = sendDesignerTaskEmail(
            $designer['email'], 
            $designer['fname'], 
            $title, 
            $short_description, 
            $priority, 
            $end_date, 
            $current_user_name
        );
        
        if ($email_sent) {
            echo json_encode([
                'success' => true, 
                'message' => 'Designer task assigned successfully and email sent to ' . $designer['fname'] . '!'
            ]);
        } else {
            echo json_encode([
                'success' => true, 
                'message' => 'Designer task assigned successfully but email failed to send. Please notify the designer manually.'
            ]);
        }
    } else {
        mysqli_stmt_close($stmt);
        throw new Exception('Failed to assign designer task: ' . mysqli_error($con));
    }
}

function editDesignerTask($con, $current_user_id) {
    $task_id = intval($_POST['task_id'] ?? 0);
    $user_id = intval($_POST['user_id'] ?? 0);
    $title = trim($_POST['title'] ?? '');
    $short_description = trim($_POST['short_description'] ?? '');
    $priority = $_POST['priority'] ?? 'Medium';
    $draft_date = !empty($_POST['draft_date']) ? $_POST['draft_date'] : null;
    $end_date = !empty($_POST['end_date']) ? $_POST['end_date'] : null;
    $publish_date = !empty($_POST['publish_date']) ? $_POST['publish_date'] : null;
    $link = trim($_POST['link'] ?? '');
    $status = $_POST['status'] ?? 'ASSIGNED';
    
    if ($task_id <= 0 || empty($user_id) || empty($title) || empty($short_description)) {
        throw new Exception('All required fields must be filled');
    }
    
    // Validate link if provided
    if (!empty($link) && !filter_var($link, FILTER_VALIDATE_URL)) {
        throw new Exception('Please enter a valid URL');
    }
    
 $stmt = mysqli_prepare($con, 
        "UPDATE tbl_designer_tasks SET 
         user_id = ?, 
         title = ?, 
         priority = ?,
         short_description = ?,
         draft_date = ?,
         end_date = ?,
         publish_date = ?,
         link = ?,
         status = ?
         WHERE id = ?"
    );
    
    mysqli_stmt_bind_param($stmt, "issssssssi", $user_id, $title, $priority, $short_description, $draft_date, $end_date, $publish_date, $link, $status, $task_id);
    
    if (mysqli_stmt_execute($stmt)) {
        mysqli_stmt_close($stmt);
        echo json_encode(['success' => true, 'message' => 'Designer task updated successfully']);
    } else {
        mysqli_stmt_close($stmt);
        throw new Exception('Failed to update designer task: ' . mysqli_error($con));
    }
}

function deleteDesignerTask($con) {
    $task_id = intval($_POST['task_id'] ?? 0);
    
    if ($task_id <= 0) {
        throw new Exception('Invalid task ID');
    }
    
    $stmt = mysqli_prepare($con, "DELETE FROM tbl_designer_tasks WHERE id = ?");
    mysqli_stmt_bind_param($stmt, "i", $task_id);
    
    if (mysqli_stmt_execute($stmt)) {
        mysqli_stmt_close($stmt);
        echo json_encode(['success' => true, 'message' => 'Designer task deleted successfully']);
    } else {
        mysqli_stmt_close($stmt);
        throw new Exception('Failed to delete designer task');
    }
}
?>

MMCT - 2023