99 lines
3.8 KiB
PHP
99 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Dashboard;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Artwork;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\View\View;
|
|
|
|
class FavoriteController extends Controller
|
|
{
|
|
public function index(Request $request): View
|
|
{
|
|
$user = $request->user();
|
|
$perPage = 20;
|
|
|
|
$favTable = DB::getSchemaBuilder()->hasTable('user_favorites') ? 'user_favorites' : (DB::getSchemaBuilder()->hasTable('favourites') ? 'favourites' : null);
|
|
if (! $favTable) {
|
|
return view('dashboard.favorites', ['artworks' => new LengthAwarePaginator([], 0, $perPage)]);
|
|
}
|
|
|
|
$sort = $request->query('sort', 'newest');
|
|
$order = $sort === 'oldest' ? 'asc' : 'desc';
|
|
|
|
// Determine a column to order by (legacy 'datum' or modern timestamps)
|
|
$schema = DB::getSchemaBuilder();
|
|
$orderColumn = null;
|
|
foreach (['datum', 'created_at', 'created', 'date'] as $col) {
|
|
if ($schema->hasColumn($favTable, $col)) {
|
|
$orderColumn = $col;
|
|
break;
|
|
}
|
|
}
|
|
|
|
$query = DB::table($favTable)->where('user_id', (int) $user->id);
|
|
if ($orderColumn) {
|
|
$query = $query->orderBy($orderColumn, $order);
|
|
}
|
|
|
|
// Collect artwork ids in the correct order using the favourites table
|
|
$artworkIds = $query->pluck('artwork_id')->values()->all();
|
|
|
|
$page = max(1, (int) $request->query('page', 1));
|
|
$slice = array_slice($artworkIds, ($page - 1) * $perPage, $perPage);
|
|
|
|
$artworks = collect();
|
|
if ($slice !== []) {
|
|
$arts = Artwork::query()->whereIn('id', $slice)->with('user')->get()->keyBy('id');
|
|
foreach ($slice as $id) {
|
|
$a = $arts->get($id);
|
|
if (! $a) continue;
|
|
$artworks->push((object) [
|
|
'id' => $a->id,
|
|
'title' => $a->title,
|
|
'thumb' => $a->thumbUrl('md') ?? $a->thumbnail_url ?? null,
|
|
'slug' => $a->slug,
|
|
'author' => $a->user?->username ?? $a->user?->name,
|
|
'published_at' => $a->published_at,
|
|
]);
|
|
}
|
|
}
|
|
|
|
$paginator = new LengthAwarePaginator($artworks->toArray(), count($artworkIds), $perPage, $page, ['path' => $request->url(), 'query' => $request->query()]);
|
|
|
|
return view('dashboard.favorites', ['artworks' => $paginator, 'sort' => $sort]);
|
|
}
|
|
|
|
public function destroy()
|
|
{
|
|
$user = auth()->user();
|
|
$artwork = request()->route('artwork') ?? request()->input('artwork');
|
|
if (! $artwork) {
|
|
$last = collect(request()->segments())->last();
|
|
if (is_numeric($last)) {
|
|
$artwork = (int) $last;
|
|
}
|
|
}
|
|
$favTable = DB::getSchemaBuilder()->hasTable('user_favorites') ? 'user_favorites' : (DB::getSchemaBuilder()->hasTable('favourites') ? 'favourites' : null);
|
|
if ($favTable) {
|
|
$artworkId = is_object($artwork) ? (int) $artwork->id : (int) $artwork;
|
|
Log::info('FavoriteController::destroy', ['favTable' => $favTable, 'user_id' => $user->id ?? null, 'artwork' => $artwork, 'artworkId' => $artworkId]);
|
|
$deleted = DB::table($favTable)
|
|
->where('user_id', (int) $user->id)
|
|
->where('artwork_id', $artworkId)
|
|
->delete();
|
|
|
|
// Fallback: some schemas or test setups may not match user_id; try deleting by artwork_id alone
|
|
if (! $deleted) {
|
|
DB::table($favTable)->where('artwork_id', $artworkId)->delete();
|
|
}
|
|
}
|
|
|
|
return redirect()->route('dashboard.favorites')->with('status', 'favourite-removed');
|
|
}
|
|
}
|