138 lines
4.7 KiB
PHP
138 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Artwork;
|
|
use App\Models\ArtworkCategory;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ManageController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$userId = $request->user()->id;
|
|
$perPage = 50;
|
|
|
|
// Use default connection query builder and join category name to avoid Eloquent model issues
|
|
$categorySub = DB::table('artwork_category as ac')
|
|
->join('categories as c', 'ac.category_id', '=', 'c.id')
|
|
->select('ac.artwork_id', DB::raw('MIN(c.name) as category_name'))
|
|
->groupBy('ac.artwork_id');
|
|
|
|
$query = DB::table('artworks as a')
|
|
->leftJoinSub($categorySub, 'cat', function ($join) {
|
|
$join->on('a.id', '=', 'cat.artwork_id');
|
|
})
|
|
->where('a.user_id', $userId)
|
|
->select('a.*', DB::raw('cat.category_name as category_name'))
|
|
->orderByDesc('a.published_at')
|
|
->orderByDesc('a.id');
|
|
|
|
$artworks = $query->paginate($perPage);
|
|
|
|
return view('manage.index', [
|
|
'artworks' => $artworks,
|
|
'page_title' => 'Artwork Manager',
|
|
]);
|
|
}
|
|
|
|
public function edit(Request $request, $id)
|
|
{
|
|
$userId = $request->user()->id;
|
|
$artwork = DB::table('artworks')->where('id', (int)$id)->where('user_id', $userId)->first();
|
|
if (! $artwork) {
|
|
abort(404);
|
|
}
|
|
|
|
// If artworks no longer have a single `category` column, fetch pivot selection
|
|
$selectedCategory = DB::table('artwork_category')->where('artwork_id', (int)$id)->value('category_id');
|
|
$artwork->category = $selectedCategory;
|
|
|
|
$categories = DB::table('categories')
|
|
->where('content_type_id', 0)
|
|
->orderBy('id')
|
|
->select(DB::raw('id as category_id'), DB::raw('name as category_name'))
|
|
->get();
|
|
|
|
return view('manage.edit', [
|
|
'artwork' => $artwork,
|
|
'categories' => $categories,
|
|
'page_title' => 'Edit Artwork: ' . ($artwork->name ?? ''),
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$userId = $request->user()->id;
|
|
$existing = DB::table('artworks')->where('id', (int)$id)->where('user_id', $userId)->first();
|
|
|
|
if (! $existing) {
|
|
abort(404);
|
|
}
|
|
|
|
$data = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'section' => 'nullable|integer',
|
|
'description' => 'nullable|string',
|
|
'artwork' => 'nullable|file|image',
|
|
'attachment' => 'nullable|file',
|
|
]);
|
|
$update = [
|
|
'name' => $data['name'],
|
|
'description' => $data['description'] ?? $existing->description,
|
|
'updated' => now(),
|
|
];
|
|
|
|
// handle artwork image upload (replacing picture)
|
|
if ($request->hasFile('artwork')) {
|
|
$file = $request->file('artwork');
|
|
$path = $file->store('public/uploads/artworks');
|
|
$filename = basename($path);
|
|
$update['picture'] = $filename;
|
|
}
|
|
|
|
// handle attachment upload (zip, etc.)
|
|
if ($request->hasFile('attachment')) {
|
|
$att = $request->file('attachment');
|
|
$attPath = $att->store('public/uploads/attachments');
|
|
$update['fname'] = basename($attPath);
|
|
}
|
|
|
|
DB::table('artworks')->where('id', (int)$id)->where('user_id', $userId)->update($update);
|
|
|
|
// Update pivot: set single category selection for this artwork
|
|
if (isset($data['section'])) {
|
|
DB::table('artwork_category')->where('artwork_id', (int)$id)->delete();
|
|
DB::table('artwork_category')->insert([
|
|
'artwork_id' => (int)$id,
|
|
'category_id' => (int)$data['section'],
|
|
]);
|
|
}
|
|
|
|
return redirect()->route('manage')->with('status', 'Artwork was successfully updated.');
|
|
}
|
|
|
|
public function destroy(Request $request, $id)
|
|
{
|
|
$userId = $request->user()->id;
|
|
$artwork = DB::table('artworks')->where('id', (int)$id)->where('user_id', $userId)->first();
|
|
if (! $artwork) {
|
|
abort(404);
|
|
}
|
|
|
|
// delete files if present (stored in new storage location)
|
|
if (!empty($artwork->fname)) {
|
|
Storage::delete('public/uploads/attachments/' . $artwork->fname);
|
|
}
|
|
if (!empty($artwork->picture)) {
|
|
Storage::delete('public/uploads/artworks/' . $artwork->picture);
|
|
}
|
|
|
|
DB::table('artworks')->where('id', (int)$id)->where('user_id', $userId)->delete();
|
|
|
|
return redirect()->route('manage')->with('status', 'Artwork deleted.');
|
|
}
|
|
}
|