153 lines
5.5 KiB
PHP
153 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Follow;
|
|
use App\Models\Notification;
|
|
use App\Models\permohonanProject;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class FollowController extends Controller
|
|
|
|
{
|
|
|
|
// public function index()
|
|
// {
|
|
// // Mengambil notifikasi dengan eager loading user dan project, diurutkan dari yang terbaru
|
|
// // $notifications = Follow::with(['user', 'project'])->orderBy('created_at', 'desc')->get();
|
|
|
|
// // Mendapatkan pengguna yang sedang login
|
|
// $user = auth()->user();
|
|
|
|
// // Mengambil notifikasi yang berkaitan dengan proyek milik pengguna yang sedang login
|
|
// // dan di-follow oleh akun lain, diurutkan dari yang terbaru
|
|
// $notifications = Follow::with(['user', 'project'])
|
|
// ->whereHas('project', function ($query) use ($user) {
|
|
// $query->where('user_id', $user->id);
|
|
// })
|
|
// ->orderBy('created_at', 'desc')
|
|
// ->paginate(10);
|
|
|
|
// // Mengirim data ke view
|
|
// return view('project.notification', compact('notifications'));
|
|
// }
|
|
|
|
public function index(Request $request)
|
|
{
|
|
// Mendapatkan pengguna yang sedang login
|
|
$user = auth()->user();
|
|
|
|
// Mengambil nilai pencarian dari input
|
|
$searchTerm = $request->input('search'); // Mengambil input pencarian
|
|
|
|
// Mengambil notifikasi yang berkaitan dengan proyek milik pengguna yang sedang login
|
|
// dan di-follow oleh akun lain, diurutkan dari yang terbaru dengan pagination
|
|
$notifications = Follow::with(['user', 'project'])
|
|
->whereHas('project', function ($query) use ($user) {
|
|
$query->where('user_id', $user->id);
|
|
})
|
|
->when($searchTerm, function ($query) use ($searchTerm) {
|
|
// Menambahkan kondisi pencarian jika ada nilai pencarian
|
|
$query->whereHas('project', function ($subQuery) use ($searchTerm) {
|
|
$subQuery->where('judul_project', 'like', '%' . $searchTerm . '%'); // atau deskripsi proyek
|
|
});
|
|
})
|
|
->orderBy('created_at', 'desc')
|
|
->paginate(10); // Mengambil 10 notifikasi per halaman
|
|
|
|
// Mengirim data ke view
|
|
return view('project.notification', compact('notifications', 'searchTerm')); // Kirim 'searchTerm' untuk mempertahankan nilai pencarian di view
|
|
}
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
{
|
|
// Validasi input
|
|
$request->validate([
|
|
'user_id' => 'required|exists:users,id',
|
|
'followed_user_id' => 'required|exists:users,id', // Pastikan ini ada
|
|
'project_id' => 'required|exists:permohonan_projects,id', // Pastikan ini ada
|
|
]);
|
|
|
|
// Periksa apakah sudah ada follow yang pending atau diterima
|
|
$existingFollow = Follow::where('user_id', $request->user_id)
|
|
->where('project_id', $request->project_id)
|
|
->first();
|
|
|
|
if ($existingFollow) {
|
|
return redirect()->back()->with('info', 'Anda sudah mengirimkan permintaan follow untuk proyek ini.');
|
|
}
|
|
|
|
// Ambil proyek berdasarkan project_id
|
|
$project = permohonanProject::find($request->project_id);
|
|
|
|
// Pastikan proyek ada
|
|
if (!$project) {
|
|
return redirect()->back()->with('error', 'Proyek tidak ditemukan.');
|
|
}
|
|
|
|
// Buat data follow baru dengan status pending
|
|
$follow = Follow::create([
|
|
'user_id' => auth()->id(), // ID pengguna yang sedang login
|
|
'followed_user_id' => $project->user->id, // ID pemilik proyek
|
|
'project_id' => $request->project_id, // ID proyek yang diikuti
|
|
'status' => 'pending', // Status awal
|
|
]);
|
|
|
|
// Kirim notifikasi ke pemilik proyek
|
|
Notification::create([
|
|
'user_id' => $project->user->id, // ID pemilik proyek
|
|
'follows_project_id' => $follow->id, // ID follow yang baru dibuat
|
|
'message' => 'Pengguna ' . auth()->user()->name . ' ingin mengikuti proyek Anda.',
|
|
]);
|
|
|
|
return redirect()->back()->with('success', 'Permintaan mengikuti proyek berhasil dikirim.');
|
|
}
|
|
|
|
// Metode untuk menerima permintaan follow
|
|
public function accept($id)
|
|
{
|
|
$follow = Follow::find($id);
|
|
if ($follow) {
|
|
$follow->status = 'accepted';
|
|
$follow->save();
|
|
return response()->json(['success' => true]);
|
|
}
|
|
return response()->json(['success' => false], 404);
|
|
}
|
|
|
|
// Metode untuk menolak permintaan follow
|
|
public function decline($id)
|
|
{
|
|
$follow = Follow::find($id);
|
|
if ($follow) {
|
|
$follow->status = 'declined';
|
|
$follow->save();
|
|
return response()->json(['success' => true]);
|
|
}
|
|
return response()->json(['success' => false], 404);
|
|
}
|
|
|
|
public function update(Request $request, Follow $follow)
|
|
{
|
|
// Validasi input
|
|
$request->validate([
|
|
'action' => 'required|in:accept,decline',
|
|
]);
|
|
|
|
// Perbarui status follow berdasarkan aksi yang dipilih
|
|
if ($request->action === 'accept') {
|
|
$follow->update(['status' => 'accepted']);
|
|
$message = 'Anda menerima permintaan untuk mengikuti proyek.';
|
|
} else {
|
|
$follow->update(['status' => 'declined']);
|
|
$message = 'Anda menolak permintaan untuk mengikuti proyek.';
|
|
}
|
|
|
|
return redirect()->back()->with('success', $message);
|
|
}
|
|
}
|