Allow artworks to have one primary category and extra secondaries.

Keep artwork_category as membership, store a deterministic primary_category_id, and let upload plus studio pick up to five additional same-type categories without changing canonical URLs.
This commit is contained in:
test
2026-09-20 14:50:12 +02:00
parent 4d60a876af
commit 232880e01f
43 changed files with 2896 additions and 565 deletions
@@ -3,12 +3,14 @@
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use App\Http\Requests\Manage\ManageArtworkDestroyRequest;
use App\Http\Requests\Manage\ManageArtworkEditRequest;
use App\Http\Requests\Manage\ManageArtworkUpdateRequest;
use App\Http\Requests\Manage\ManageArtworkDestroyRequest;
use App\Models\Artwork;
use App\Services\Artworks\ArtworkCategoryService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class ManageController extends Controller
{
@@ -17,14 +19,12 @@ class ManageController extends Controller
$userId = $request->user()->id;
$perPage = 50;
$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');
$categorySub = DB::table('categories as c')
->select('c.id as category_id', 'c.name as category_name');
$query = DB::table('artworks as a')
->leftJoinSub($categorySub, 'cat', function ($join) {
$join->on('a.id', '=', 'cat.artwork_id');
$join->on('a.primary_category_id', '=', 'cat.category_id');
})
->leftJoin('artwork_stats as s', 'a.id', '=', 's.artwork_id')
->where('a.user_id', $userId)
@@ -52,8 +52,8 @@ class ManageController extends Controller
{
$artwork = $request->artwork();
$selectedCategory = DB::table('artwork_category')->where('artwork_id', (int)$id)->value('category_id');
$artwork->category = $selectedCategory;
$selectedCategory = (int) ($artwork->primary_category_id ?: DB::table('artwork_category')->where('artwork_id', (int) $id)->value('category_id'));
$artwork->category = $selectedCategory ?: null;
$categories = DB::table('categories')
->where('content_type_id', 0)
@@ -64,7 +64,7 @@ class ManageController extends Controller
return view('manage.edit', [
'artwork' => $artwork,
'categories' => $categories,
'page_title' => 'Edit Artwork: ' . ($artwork->title ?? ''),
'page_title' => 'Edit Artwork: '.($artwork->title ?? ''),
]);
}
@@ -91,14 +91,14 @@ class ManageController extends Controller
$update['fname'] = basename($attPath);
}
DB::table('artworks')->where('id', (int)$id)->update($update);
DB::table('artworks')->where('id', (int) $id)->update($update);
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'],
]);
$model = Artwork::query()->find((int) $id);
if ($model) {
app(ArtworkCategoryService::class)
->sync($model, (int) $data['section'], [], 'moderator');
}
}
return redirect()->route('manage')->with('status', 'Artwork was successfully updated.');
@@ -108,14 +108,14 @@ class ManageController extends Controller
{
$artwork = $request->artwork();
if (!empty($artwork->fname)) {
Storage::delete('public/uploads/attachments/' . $artwork->fname);
if (! empty($artwork->fname)) {
Storage::delete('public/uploads/attachments/'.$artwork->fname);
}
if (!empty($artwork->picture)) {
Storage::delete('public/uploads/artworks/' . $artwork->picture);
if (! empty($artwork->picture)) {
Storage::delete('public/uploads/artworks/'.$artwork->picture);
}
DB::table('artworks')->where('id', (int)$id)->delete();
DB::table('artworks')->where('id', (int) $id)->delete();
return redirect()->route('manage')->with('status', 'Artwork deleted.');
}