Skip to content

Commit df1332e

Browse files
authored
feat(notice): migrate notices table to app_settings key-value store (#450)
* feat(notice): migrate notices table to app_settings key-value store * fix(notice): update DeleteUnusedImages and NoticeDialog for AppSetting notice format
1 parent 2814634 commit df1332e

12 files changed

Lines changed: 451 additions & 171 deletions

File tree

‎app/Console/Commands/DeleteUnusedImages.php‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Console\Commands;
44

5+
use App\Models\AppSetting;
56
use App\Models\Blog;
67
use App\Models\ForumAnswer;
78
use App\Models\ForumPost;
@@ -33,17 +34,14 @@ public function handle(): void
3334
Blog::whereNotNull('featured_image_path')->pluck('featured_image_path')->toArray()
3435
);
3536

36-
// Notice images — use DB::table to bypass the getImageAttribute accessor,
37-
// which converts stored paths to full URLs and would cause every notice
38-
// image to be falsely flagged as unused.
39-
$this->cleanDirectory(
40-
'notices',
41-
DB::table('notices')
42-
->whereNotNull('image')
43-
->where('image', 'not like', 'http%')
44-
->pluck('image')
45-
->toArray()
46-
);
37+
// Notice images — read from app_settings
38+
$rawNotice = AppSetting::get('site_notice');
39+
$noticeData = is_array($rawNotice) ? $rawNotice : json_decode($rawNotice ?? '', true);
40+
$noticeImages = [];
41+
if (! empty($noticeData['image']) && ! str($noticeData['image'])->startsWith(['http://', 'https://'])) {
42+
$noticeImages[] = $noticeData['image'];
43+
}
44+
$this->cleanDirectory('notices', $noticeImages);
4745

4846
// Resource files (items stored under resources/)
4947
$rawItems = DB::table('resources')

‎app/Http/Controllers/Admin/NoticeController.php‎

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,74 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Http\Requests\Notice\UpdateNoticeRequest;
7-
use App\Models\Notice;
7+
use App\Models\AppSetting;
8+
use Illuminate\Support\Facades\Cache;
89
use Illuminate\Support\Facades\Storage;
910
use Inertia\Inertia;
1011

1112
class NoticeController extends Controller
1213
{
1314
public function edit()
1415
{
16+
$rawNotice = AppSetting::get('site_notice');
17+
$notice = is_array($rawNotice) ? $rawNotice : (json_decode($rawNotice ?? '', true) ?: [
18+
'title' => null,
19+
'message' => null,
20+
'image' => null,
21+
'video_url' => null,
22+
'show_button' => false,
23+
'button_title' => null,
24+
'button_link' => null,
25+
'is_active' => false,
26+
]);
27+
28+
if (! empty($notice['image'])) {
29+
$notice['image'] = str($notice['image'])->startsWith(['http://', 'https://'])
30+
? $notice['image']
31+
: Storage::url($notice['image']);
32+
}
33+
1534
return Inertia::render('admin/NoticeEdit', [
16-
'notice' => Notice::singleton(),
35+
'notice' => $notice,
1736
]);
1837
}
1938

2039
public function update(UpdateNoticeRequest $request)
2140
{
22-
$notice = Notice::singleton();
41+
$rawNotice = AppSetting::get('site_notice');
42+
$existing = is_array($rawNotice) ? $rawNotice : (json_decode($rawNotice ?? '', true) ?: []);
43+
2344
$data = $request->validated();
2445

2546
if ($request->boolean('remove_image')) {
26-
$rawImage = $notice->getRawOriginal('image');
47+
$rawImage = $existing['image'] ?? null;
2748
if ($rawImage && ! str($rawImage)->startsWith(['http://', 'https://'])) {
2849
Storage::delete($rawImage);
2950
}
30-
$data['image'] = null;
51+
$existing['image'] = null;
3152
}
3253

3354
if ($request->hasFile('image')) {
34-
$rawImage = $notice->getRawOriginal('image');
55+
$rawImage = $existing['image'] ?? null;
3556
if ($rawImage && ! str($rawImage)->startsWith(['http://', 'https://'])) {
3657
Storage::delete($rawImage);
3758
}
3859

3960
$path = $request->file('image')->store('notices');
40-
$data['image'] = $path;
41-
} else {
42-
if (! $request->boolean('remove_image')) {
43-
unset($data['image']);
44-
}
61+
$existing['image'] = $path;
4562
}
4663

47-
unset($data['remove_image']);
64+
$existing['title'] = $data['title'] ?? null;
65+
$existing['message'] = $data['message'] ?? null;
66+
$existing['video_url'] = $data['video_url'] ?? null;
67+
$existing['show_button'] = (bool) ($data['show_button'] ?? false);
68+
$existing['button_title'] = $data['button_title'] ?? null;
69+
$existing['button_link'] = $data['button_link'] ?? null;
70+
$existing['is_active'] = (bool) ($data['is_active'] ?? false);
71+
$existing['updated_at'] = now()->toDateTimeString();
4872

49-
$notice->update($data);
73+
AppSetting::set('site_notice', json_encode($existing));
74+
Cache::forget('active_notice_for_display');
5075

5176
return redirect()->route('admin.notice.edit')->with('success', 'Notice updated.');
5277
}

‎app/Http/Controllers/HomeController.php‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Models\AppSetting;
56
use App\Models\Blog;
67
use App\Models\ForumPost;
78
use App\Models\ForumVote;
8-
use App\Models\Notice;
99
use App\Models\Resource;
1010
use App\Models\ResourceVote;
1111
use App\Models\Subject;
1212
use App\Models\UserAppreciation;
1313
use Illuminate\Http\Request;
1414
use Illuminate\Support\Facades\Auth;
1515
use Illuminate\Support\Facades\Cache;
16+
use Illuminate\Support\Facades\Storage;
1617
use Inertia\Inertia;
1718

1819
class HomeController extends Controller
@@ -44,13 +45,31 @@ public function index(Request $request)
4445
->toArray();
4546
});
4647

48+
$notice = Cache::rememberForever('active_notice_for_display', function () {
49+
$raw = AppSetting::get('site_notice');
50+
if (! $raw) {
51+
return null;
52+
}
53+
$data = is_array($raw) ? $raw : json_decode($raw, true);
54+
if (empty($data['is_active'])) {
55+
return null;
56+
}
57+
if (! empty($data['image'])) {
58+
$data['image'] = str($data['image'])->startsWith(['http://', 'https://'])
59+
? $data['image']
60+
: Storage::url($data['image']);
61+
}
62+
63+
return $data;
64+
});
65+
4766
return Inertia::render('Feed', [
4867
'feed' => $feed,
4968
'subjects' => $subjects,
5069
'course' => $course,
5170
'group' => $group,
5271
'sort' => $sort,
53-
'notice' => Notice::activeForDisplay(),
72+
'notice' => $notice,
5473
]);
5574
}
5675

‎app/Http/Requests/Notice/UpdateNoticeRequest.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ public function authorize(): bool
2323
public function rules(): array
2424
{
2525
return [
26-
'title' => ['required', 'string', 'max:255'],
27-
'message' => ['required', 'string'],
26+
'title' => ['nullable', 'string', 'max:255'],
27+
'message' => ['nullable', 'string'],
2828
'image' => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp', 'max:5120'],
2929
'remove_image' => ['nullable', 'boolean'],
30+
'video_url' => ['nullable', 'url', 'max:500'],
3031
'show_button' => ['required', 'boolean'],
3132
'button_title' => ['nullable', 'required_if:show_button,true', 'string', 'max:100'],
3233
'button_link' => ['nullable', 'required_if:show_button,true', 'string', 'max:500'],

‎app/Models/Notice.php‎

Lines changed: 0 additions & 45 deletions
This file was deleted.

‎app/Observers/NoticeObserver.php‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎app/Providers/AppServiceProvider.php‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
use App\Models\Blog;
66
use App\Models\Node;
7-
use App\Models\Notice;
87
use App\Models\Product;
98
use App\Models\Resource;
109
use App\Models\Subject;
1110
use App\Models\User;
1211
use App\Observers\BlogObserver;
1312
use App\Observers\NodeObserver;
14-
use App\Observers\NoticeObserver;
1513
use App\Observers\ProductObserver;
1614
use App\Observers\ResourceObserver;
1715
use App\Observers\SubjectObserver;
@@ -43,7 +41,6 @@ public function boot(): void
4341

4442
Blog::observe(BlogObserver::class);
4543
Node::observe(NodeObserver::class);
46-
Notice::observe(NoticeObserver::class);
4744
Product::observe(ProductObserver::class);
4845
Resource::observe(ResourceObserver::class);
4946
Subject::observe(SubjectObserver::class);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Facades\Schema;
7+
8+
return new class extends Migration
9+
{
10+
public function up(): void
11+
{
12+
if (Schema::hasTable('notices')) {
13+
$first = DB::table('notices')->first();
14+
if ($first) {
15+
DB::table('app_settings')->updateOrInsert(
16+
['key' => 'site_notice'],
17+
[
18+
'value' => json_encode([
19+
'title' => $first->title,
20+
'message' => $first->message,
21+
'image' => $first->image,
22+
'show_button' => (bool) $first->show_button,
23+
'button_title' => $first->button_title,
24+
'button_link' => $first->button_link,
25+
'is_active' => (bool) $first->is_active,
26+
]),
27+
'updated_at' => now(),
28+
]
29+
);
30+
}
31+
32+
Schema::dropIfExists('notices');
33+
}
34+
}
35+
36+
public function down(): void
37+
{
38+
Schema::create('notices', function (Blueprint $table) {
39+
$table->id();
40+
$table->string('title')->nullable();
41+
$table->text('message')->nullable();
42+
$table->string('image')->nullable();
43+
$table->boolean('show_button')->default(false);
44+
$table->string('button_title')->nullable();
45+
$table->string('button_link')->nullable();
46+
$table->boolean('is_active')->default(false);
47+
$table->timestamps();
48+
});
49+
}
50+
};

0 commit comments

Comments
 (0)