Repository navigation
Expand file tree
/
Copy pathdelete_week.php
More file actions
107 lines (86 loc) · 3.46 KB
/
Copy pathdelete_week.php
File metadata and controls
107 lines (86 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
require_once 'includes/session_config.php';
include 'conn.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'User not logged in']);
exit();
}
// Get JSON data from request
$json_data = file_get_contents('php://input');
$data = json_decode($json_data, true);
if (!$data) {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Invalid data format']);
exit();
}
// Check if we have the required parameters
if (!isset($data['weekId']) || !isset($data['projectId'])) {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Missing required parameters']);
exit();
}
$week_id = intval($data['weekId']);
$project_id = intval($data['projectId']);
$user_id = $_SESSION['user_id'];
// Debug information
error_log("Attempting to delete week ID: " . $week_id . " for project ID: " . $project_id);
// Verify the project belongs to this client
$verify_sql = "SELECT id FROM projects WHERE id = ? AND user_id = ?";
$verify_stmt = $conn->prepare($verify_sql);
$verify_stmt->bind_param("ii", $project_id, $user_id);
$verify_stmt->execute();
$verify_result = $verify_stmt->get_result();
if ($verify_result->num_rows === 0) {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Project not found or not authorized']);
exit();
}
// Start transaction
$conn->begin_transaction();
try {
// First check if the week exists
$check_week_sql = "SELECT id FROM project_weeks WHERE id = ? AND project_id = ?";
$check_week_stmt = $conn->prepare($check_week_sql);
$check_week_stmt->bind_param("ii", $week_id, $project_id);
$check_week_stmt->execute();
$check_week_result = $check_week_stmt->get_result();
if ($check_week_result->num_rows === 0) {
throw new Exception("Week not found in this project");
}
// First delete all tasks in this week
$delete_tasks_sql = "DELETE FROM project_tasks WHERE week_id = ?";
$delete_tasks_stmt = $conn->prepare($delete_tasks_sql);
$delete_tasks_stmt->bind_param("i", $week_id);
$delete_tasks_result = $delete_tasks_stmt->execute();
if (!$delete_tasks_result) {
throw new Exception("Error deleting tasks: " . $conn->error);
}
// Then delete the week
$delete_week_sql = "DELETE FROM project_weeks WHERE id = ?";
$delete_week_stmt = $conn->prepare($delete_week_sql);
$delete_week_stmt->bind_param("i", $week_id);
$delete_week_result = $delete_week_stmt->execute();
if (!$delete_week_result) {
throw new Exception("Error deleting week: " . $conn->error);
}
// Commit transaction
$conn->commit();
header('Content-Type: application/json');
echo json_encode(['success' => true]);
} catch (Exception $e) {
// Rollback transaction on error
$conn->rollback();
// Log the error for debugging
error_log("Week deletion error: " . $e->getMessage());
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Error deleting week: ' . $e->getMessage()]);
}
// Close statements
if (isset($verify_stmt)) $verify_stmt->close();
if (isset($check_week_stmt)) $check_week_stmt->close();
if (isset($delete_tasks_stmt)) $delete_tasks_stmt->close();
if (isset($delete_week_stmt)) $delete_week_stmt->close();
$conn->close();
?>