| Current Path : /home/emeraadmin/public_html/pages/taskprovider/ |
| Current File : /home/emeraadmin/public_html/pages/taskprovider/add_tasks.php |
<?php
// schedule.php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once '../../Classes/Database.php';
require_once '../../Classes/Schedules.php';
require_once '../../Classes/Task.php';
require_once '../../Service/ScheduleService.php';
require_once '../../Service/TaskService.php';
require_once '../../Service/ServiceService.php';
$serviceService = new ServiceService();
$services = $serviceService->getAllServicesForDropdown();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Read the raw POST data
$input = file_get_contents('php://input');
$data = json_decode($input, true); // Decode the JSON data into an associative array
// Extract schedule name and tasks
$scheduleName = isset($data['scheduleName']) ? trim($data['scheduleName']) : '';
$currentUserId = isset($data['currentUserId']) ? trim($data['currentUserId']) : '';
$tasks = isset($data['tasks']) ? $data['tasks'] : [];
$now = date('Y-m-d H:i:s');
try {
// Create a new schedule
$schedule = new Schedules('', $scheduleName, $currentUserId, false, $now, $now);
$scheduleService = new ScheduleService();
$schedule_id = $scheduleService->addScheduleRetuenlastinsertedid($schedule);
// Check if schedule creation was successful
if (!$schedule_id) {
echo json_encode(array('error' => 'Failed to add schedule.'));
exit;
}
// Loop through tasks array and add each task
foreach ($tasks as $taskData) {
$serviceId = isset($taskData['service']) ? trim($taskData['service']) : '';
$frequency = isset($taskData['frequency']) ? trim($taskData['frequency']) : '';
$dates = isset($taskData['dates']) ? $taskData['dates'] : [];
// Validate and format dates and times
$formattedDates = [];
foreach ($dates as $date) {
$formattedDates[] = [
'date' => $date['date'],
'time' => $date['time']
];
}
// Create a new task instance with the formatted dates and times
$task = new Task('', $schedule_id, $serviceId, $frequency, json_encode($formattedDates), $currentUserId, $currentUserId, $now, $now, false, null, false, false, false, '', null,0);
$taskService = new TaskService();
$task_id = $taskService->addTask($task);
// Check if task creation was successful
if (!$task_id) {
echo json_encode(array('error' => 'Failed to add task.'));
exit;
}
}
// If everything is successful, return a success response
echo json_encode(array('success' => true, 'message' => 'Tasks added successfully.', 'schedule_id' => $schedule_id));
} catch (Exception $e) {
// Handle any exceptions and return an error response
echo json_encode(array('error' => $e->getMessage()));
}
} else {
// If the request method is not POST, return an error response
echo json_encode(array('error' => 'Invalid request method.'));
}
?>