Feat: Activities Controller totally independent

This commit is contained in:
Sallu9007
2025-03-24 00:44:40 +05:30
parent 07932d7194
commit 4352abdbd1
7 changed files with 226 additions and 176 deletions

View File

@@ -20,127 +20,7 @@ class AdminController extends Controller
// View responses submitted by users
public function viewActivitiesAttendedResponses()
{
return view('admin.activities-attended-responses');
}
public function edit($id)
{
$response = ActivitiesAttended::findOrFail($id);
return view('admin.activities-attended.edit', compact('response'));
}
public function update(Request $request, $id)
{
$response = ActivitiesAttended::findOrFail($id);
// Validate the request data
$validated = $request->validate([
'title' => 'required|string',
'organising_institute' => 'required|string',
'address' => 'required|string',
'start_date' => 'required|date',
'start_time' => 'required|date_format:H:i',
'end_date' => 'required|date',
'end_time' => 'required|date_format:H:i',
'num_days' => 'required|integer',
'activity_type' => 'required|string',
'category' => 'required|string',
'level' => 'required|string',
'proof' => 'nullable|mimes:jpg,jpeg,png,pdf,doc,docx,zip',
]);
// Combine start date and time
$startDateTime = date('Y-m-d H:i:s', strtotime("{$validated['start_date']} {$validated['start_time']}"));
$endDateTime = date('Y-m-d H:i:s', strtotime("{$validated['end_date']} {$validated['end_time']}"));
// Handle the file upload if a new file is provided
if ($request->hasFile('proof')) {
// Delete old file if exists
if ($response->proof && Storage::disk('public')->exists($response->proof)) {
Storage::disk('public')->delete($response->proof);
}
// Extract year from start_date
$year = date('Y', strtotime($validated['start_date']));
$username = $response->user->name;
$originalName = $request->file('proof')->getClientOriginalName();
$fileName = $username . '_' . $originalName;
// Create path structure: year/faculty_name
$folderPath = 'proofs/' . $year . '/' . $username;
// Store file in the specified path
$proofPath = $request->file('proof')->storeAs($folderPath, $fileName, 'public');
$response->proof = $proofPath;
}
// Update other fields
$response->title = $validated['title'];
$response->organising_institute = $validated['organising_institute'];
$response->address = $validated['address'];
$response->start_date = $startDateTime;
$response->end_date = $endDateTime;
$response->num_days = $validated['num_days'];
$response->activity_type = $validated['activity_type'];
$response->category = $validated['category'];
$response->level = $validated['level'];
$response->save();
return redirect()->route('admin.ActivitiesAttendedResponses')->with('status', 'Response updated successfully');
}
public function destroy($id)
{
$response = ActivitiesAttended::findOrFail($id);
// Delete the file if it exists
if ($response->proof && Storage::disk('public')->exists($response->proof)) {
Storage::disk('public')->delete($response->proof);
}
$response->delete();
return response()->json(['success' => 'Record deleted successfully']);
}
public function getActivitiesAttendedResponses()
{
$responses = ActivitiesAttended::with('user', 'department');
return DataTables::of($responses)
->addColumn('user_name', function ($response) {
return $response->user->name ?? 'Unknown';
})
->addColumn('department_name', function ($response) {
return $response->department->name ?? 'Unknown';
})
->addColumn('start_date', function ($response) {
return \Carbon\Carbon::parse($response->start_date)->format('d-m-Y');
})
->addColumn('start_time', function ($response) {
return \Carbon\Carbon::parse($response->start_date)->format('h:i A');
})
->addColumn('end_date', function ($response) {
return \Carbon\Carbon::parse($response->end_date)->format('d-m-Y');
})
->addColumn('end_time', function ($response) {
return \Carbon\Carbon::parse($response->end_date)->format('h:i A');
})
->addColumn('action', function ($response) {
$viewButton = $response->proof
? '<a href="' . asset('storage/' . $response->proof) . '" target="_blank" class="btn btn-sm btn-primary mr-1">View</a>'
: 'No Proof';
$editButton = '<a href="' . route('admin.ActivitiesAttended.edit', $response->id) . '" class="btn btn-sm btn-info mx-1">Edit</a>';
$deleteButton = '<button type="button" class="btn btn-sm btn-danger delete-btn" data-id="' . $response->id . '">Delete</button>';
return $viewButton . ' ' . $editButton . ' ' . $deleteButton;
})
->rawColumns(['action'])
->make(true);
return view('activities-attended.index');
}
}