diff --git a/app/Helpers/CustomHelper.php b/app/Helpers/CustomHelper.php index e1265a1..82262ed 100644 --- a/app/Helpers/CustomHelper.php +++ b/app/Helpers/CustomHelper.php @@ -2,6 +2,7 @@ use Hashids\Hashids; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\File; use Illuminate\Support\Str; // Template @@ -11,9 +12,9 @@ // } if (! function_exists('store_file')) { - function store_file($file, $path, $disk = 'public') + function store_file($file = null, $path, $disk = 'public') { - if (isset($file)) { + if ($file && $file instanceof \Illuminate\Http\UploadedFile) { $randomFilename = Str::random(35).time().'.'.$file->getClientOriginalExtension(); $originalFileName = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)).'.'.$file->getClientOriginalExtension(); @@ -34,6 +35,19 @@ function store_file($file, $path, $disk = 'public') } } +if (! function_exists('delete_file')) { + function delete_file($path) + { + if (File::exists(storage_path($path))) { + File::delete(storage_path($path)); + + return true; + } else { + return false; + } + } +} + if (!function_exists('get_role_name')) { function get_role_name($role = null) { diff --git a/app/Http/Controllers/Dashboard/ClassificationController.php b/app/Http/Controllers/Dashboard/ClassificationController.php index 6b0434a..669898c 100644 --- a/app/Http/Controllers/Dashboard/ClassificationController.php +++ b/app/Http/Controllers/Dashboard/ClassificationController.php @@ -33,12 +33,12 @@ public function store(ClassificationRequest $request){ if($createdClassification){ return redirect()->back()->with('success-status', 'Berhasil menambahkan klasifikasi.'); }else{ - return redirect()->back()->with('failed-status', 'Gagal menambahkan klasifikasi'); + return redirect()->back()->with('failed-status', 'Gagal menambahkan klasifikasi.'); } } public function edit($id){ - $classification = Classification::find(decrypt_id($id)); + $classification = Classification::findOrFail(decrypt_id($id)); return view('dashboard.classifications.edit', [ 'title' => 'Ubah Klasifikasi', @@ -51,13 +51,13 @@ public function update(ClassificationRequest $request, $id){ $validatedData['slug'] = Str::slug($validatedData['name']); $validatedData['enhancer_id'] = Auth::id(); - $classification = Classification::find(decrypt_id($id)); + $classification = Classification::findOrFail(decrypt_id($id)); $updatedClassification = $classification->update($validatedData); if($updatedClassification){ return redirect()->back()->with('success-status', 'Berhasil mengubah klasifikasi.'); }else{ - return redirect()->back()->with('failed-status', 'Gagal mengubah klasifikasi'); + return redirect()->back()->with('failed-status', 'Gagal mengubah klasifikasi.'); } } @@ -76,7 +76,7 @@ public function destroy($id){ $classification->delete(); - return response()->json(['message' => 'Berhasil menghapus klasifikasi']); + return response()->json(['message' => 'Berhasil menghapus klasifikasi.']); } catch (\Exception $e) { return response()->json(['message' => 'Gagal menghapus klasifikasi.'], 500); } diff --git a/app/Http/Controllers/Dashboard/DatatableController.php b/app/Http/Controllers/Dashboard/DatatableController.php index 77ec068..c9ecdfe 100644 --- a/app/Http/Controllers/Dashboard/DatatableController.php +++ b/app/Http/Controllers/Dashboard/DatatableController.php @@ -5,6 +5,7 @@ use App\Http\Controllers\Controller; use App\Models\Classification; use App\Models\GovernmentAgency; +use App\Models\News; use App\Models\SubClassification; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; @@ -185,4 +186,64 @@ public function governmentAgency(Request $request){ return response()->json(['error' => 'Unauthorized'], 401); } + + public function news(Request $request){ + if ($request->ajax()) { + $data = News::all(); + + return DataTables::of($data) + ->addIndexColumn() + ->addColumn('title', function ($data) { + return shorten_text($data->title, 50); + }) + ->addColumn('link', function ($data) { + if ($data->link != null) { + return ''.$data->link.''; + }else{ + return 'Tidak ada link'; + } + }) + ->addColumn('category', function ($data) { + if ($data->category == '0') { + return 'Berita'; + }else{ + return 'Pengumuman'; + } + }) + ->addColumn('action', function ($data) { + $showUrl = route('dashboard.news.show', ['id' => encrypt_id($data->id)]); + + $editUrl = route('dashboard.news.edit', ['id' => encrypt_id($data->id)]); + + $deleteUrl = route('dashboard.news.destroy', ['id' => encrypt_id($data->id)]); + + $dropdown = ''; + + return $dropdown; + }) + + ->rawColumns(['link', 'category', 'action']) + ->make(true); + } + + return response()->json(['error' => 'Unauthorized'], 401); + + } } diff --git a/app/Http/Controllers/Dashboard/GovernmentAgencyController.php b/app/Http/Controllers/Dashboard/GovernmentAgencyController.php index aeb495a..0f43b03 100644 --- a/app/Http/Controllers/Dashboard/GovernmentAgencyController.php +++ b/app/Http/Controllers/Dashboard/GovernmentAgencyController.php @@ -38,7 +38,7 @@ public function store(GovernmentAgencyRequest $request){ } public function edit($id){ - $governmentAgency = GovernmentAgency::find(decrypt_id($id)); + $governmentAgency = GovernmentAgency::findOrFail(decrypt_id($id)); return view('dashboard.government-agencies.edit',[ 'title' => 'Ubah OPD', diff --git a/app/Http/Controllers/Dashboard/NewsController.php b/app/Http/Controllers/Dashboard/NewsController.php new file mode 100644 index 0000000..75a5df2 --- /dev/null +++ b/app/Http/Controllers/Dashboard/NewsController.php @@ -0,0 +1,97 @@ + 'Berita' + ]); + } + + public function create(){ + return view('dashboard.news.create', [ + 'title' => 'Tambah Berita' + ]); + } + + public function store(NewsRequest $request){ + $validatedData = $request->validated(); + $validatedData['image'] = store_file($validatedData['image'], '/uploads/news', 'public')['randomFileName']; + $validatedData['slug'] = Str::slug($validatedData['title']); + $validatedData['author_id'] = Auth::id(); + + + $createdNews = News::create($validatedData); + + if($createdNews){ + return redirect()->back()->with('success-status', 'Berhasil menambahkan berita.'); + }else{ + return redirect()->back()->with('failed-status', 'Gagal menambahkan berita.'); + } + } + + public function edit($id){ + $news = News::findOrFail(decrypt_id($id)); + + return view('dashboard.news.edit', [ + 'title' => 'Ubah Berita', + 'news' => $news + ]); + } + + public function update(NewsRequest $request, $id){ + $validatedData = $request->validated(); + + $news = News::findOrFail(decrypt_id($id)); + + if($request->has('image')){ + delete_file('app/public/uploads/news/'.$news->image); + $validatedData['image'] = store_file($validatedData['image'], '/uploads/news', 'public')['randomFileName']; + }else{ + $validatedData['image'] = $news->image; + } + + $validatedData['slug'] = Str::slug($validatedData['title']); + $validatedData['author_id'] = Auth::id(); + + $news = News::findOrFail(decrypt_id($id)); + $updatedNews = $news->update($validatedData); + + if($updatedNews){ + return redirect()->back()->with('success-status', 'Berhasil mengubah berita.'); + }else{ + return redirect()->back()->with('failed-status', 'Gagal mengubah berita.'); + } + } + + public function show($id){ + $news = News::findOrFail(decrypt_id($id)); + + return view('dashboard.news.show', [ + 'title' => 'Detail Berita', + 'news' => $news + ]); + } + + public function destroy($id){ + try { + $news = News::findOrFail(decrypt_id($id)); + delete_file('app/public/uploads/news/'.$news->image); + $news->delete(); + + + return response()->json(['message' => 'Berhasil menghapus berita.']); + } catch (\Exception $e) { + return response()->json(['message' => 'Gagal menghapus berita.'], 500); + } + } +} diff --git a/app/Http/Controllers/Dashboard/SubClassificationController.php b/app/Http/Controllers/Dashboard/SubClassificationController.php index 06bbce8..7424434 100644 --- a/app/Http/Controllers/Dashboard/SubClassificationController.php +++ b/app/Http/Controllers/Dashboard/SubClassificationController.php @@ -50,12 +50,12 @@ public function store(SubClassificationRequest $request){ if($createdSubClassification){ return redirect()->back()->with('success-status', 'Berhasil menambahkan sub klasifikasi.'); }else{ - return redirect()->back()->with('failed-status', 'Gagal menambahkan sub klasifikasi'); + return redirect()->back()->with('failed-status', 'Gagal menambahkan sub klasifikasi.'); } } public function edit($id){ - $subClassification = SubClassification::find(decrypt_id($id)); + $subClassification = SubClassification::findOrFail(decrypt_id($id)); return view('dashboard.sub-classifications.edit', [ 'title' => 'Ubah Sub Klasifikasi', @@ -69,13 +69,13 @@ public function update(SubClassificationRequest $request, $id){ $validatedData['slug'] = Str::slug($validatedData['name']); $validatedData['enhancer_id'] = Auth::id(); - $subClassification = SubClassification::find(decrypt_id($id)); + $subClassification = SubClassification::findOrFail(decrypt_id($id)); $updatedSubClassification = $subClassification->update($validatedData); if($updatedSubClassification){ return redirect()->back()->with('success-status', 'Berhasil mengubah sub klasifikasi.'); }else{ - return redirect()->back()->with('failed-status', 'Gagal mengubah sub klasifikasi'); + return redirect()->back()->with('failed-status', 'Gagal mengubah sub klasifikasi.'); } } @@ -94,7 +94,7 @@ public function destroy($id){ $subClassification->delete(); - return response()->json(['message' => 'Berhasil menghapus sub klasifikasi']); + return response()->json(['message' => 'Berhasil menghapus sub klasifikasi.']); } catch (\Exception $e) { return response()->json(['message' => 'Gagal menghapus sub klasifikasi.'], 500); } diff --git a/app/Http/Requests/NewsRequest.php b/app/Http/Requests/NewsRequest.php new file mode 100644 index 0000000..484a127 --- /dev/null +++ b/app/Http/Requests/NewsRequest.php @@ -0,0 +1,58 @@ +|string> + */ + public function rules(): array + { + $rules = [ + 'title' => ['required' , 'max:200'], + 'content' => ['required'], + 'link' => ['nullable', 'url'], + 'tag' => ['nullable'], + 'category' => ['required', 'in:0,1'], + ]; + + if($this->isMethod('post')){ + $rules['image'] = ['required', 'image', 'mimes:png,jpg,jpeg', 'max:4096']; + }else if($this->isMethod('put')){ + $rules['image'] = ['nullable', 'image', 'mimes:png,jpg,jpeg', 'max:4096']; + } + + return $rules; + } + + public function messages(): array + { + return [ + 'title.required' => 'Judul harus diisi.', + 'title.max' => 'Judul tidak boleh lebih dari 200 karakter.', + 'content.required' => 'Konten harus diisi.', + 'link.url' => 'Harus link valid.', + 'category.required' => 'Kategori harus dipilih.', + 'category.in' => 'Kategori harus bernilai berita atau pengumuman.', + + 'image.required' => 'Gambar wajib diunggah.', + 'image.image' => 'File yang diunggah harus berupa gambar.', + 'image.mimes' => 'Format gambar yang diperbolehkan adalah PNG, JPG, atau JPEG.', + 'image.max' => 'Ukuran gambar tidak boleh lebih dari 4MB.', + ]; + } +} diff --git a/app/Models/News.php b/app/Models/News.php new file mode 100644 index 0000000..4434aa0 --- /dev/null +++ b/app/Models/News.php @@ -0,0 +1,21 @@ +id(); $table->string('name'); - $table->string('slug'); + $table->string('slug')->unique();; $table->text('description')->nullable(); $table->enum('status', ['0', '1'])->default('0'); $table->unsignedBigInteger('enhancer_id'); diff --git a/database/migrations/2025_02_25_085404_create_sub_classifications_table.php b/database/migrations/2025_02_25_085404_create_sub_classifications_table.php index 5e6fad8..5b95f80 100644 --- a/database/migrations/2025_02_25_085404_create_sub_classifications_table.php +++ b/database/migrations/2025_02_25_085404_create_sub_classifications_table.php @@ -14,7 +14,7 @@ public function up(): void Schema::create('sub_classifications', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->string('slug'); + $table->string('slug')->unique();; $table->text('description')->nullable(); $table->enum('status', ['0', '1'])->default('0'); $table->unsignedBigInteger('classification_id'); diff --git a/database/migrations/2025_02_27_014325_create_government_agencies_table.php b/database/migrations/2025_02_27_014325_create_government_agencies_table.php index 6c4461e..a50722a 100644 --- a/database/migrations/2025_02_27_014325_create_government_agencies_table.php +++ b/database/migrations/2025_02_27_014325_create_government_agencies_table.php @@ -14,7 +14,7 @@ public function up(): void Schema::create('government_agencies', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->string('slug'); + $table->string('slug')->unique();; $table->string('short_name')->nullable(); $table->text('location')->nullable(); $table->text('description')->nullable(); diff --git a/database/migrations/2025_02_27_030638_create_news_table.php b/database/migrations/2025_02_27_030638_create_news_table.php new file mode 100644 index 0000000..0728a2a --- /dev/null +++ b/database/migrations/2025_02_27_030638_create_news_table.php @@ -0,0 +1,36 @@ +id(); + $table->string('title'); + $table->string('slug')->unique(); + $table->text('content'); + $table->string('image'); + $table->string('link')->nullable(); + $table->string('tag')->nullable(); + $table->enum('category', ['0', '1'])->default('0'); + $table->unsignedBigInteger('author_id'); + $table->foreign('author_id')->references('id')->on('users'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('news'); + } +}; diff --git a/database/seeders/NewsSeeder.php b/database/seeders/NewsSeeder.php new file mode 100644 index 0000000..20a174f --- /dev/null +++ b/database/seeders/NewsSeeder.php @@ -0,0 +1,17 @@ +
-
{!!old('description')!!}
- +
diff --git a/resources/views/dashboard/classifications/edit.blade.php b/resources/views/dashboard/classifications/edit.blade.php index ad450fe..d200a26 100644 --- a/resources/views/dashboard/classifications/edit.blade.php +++ b/resources/views/dashboard/classifications/edit.blade.php @@ -42,8 +42,7 @@
-
{!! $classification->description !!}
- +
diff --git a/resources/views/dashboard/government-agencies/create.blade.php b/resources/views/dashboard/government-agencies/create.blade.php index c712a25..2953a87 100644 --- a/resources/views/dashboard/government-agencies/create.blade.php +++ b/resources/views/dashboard/government-agencies/create.blade.php @@ -52,8 +52,7 @@
-
{!!old('description')!!}
- +
diff --git a/resources/views/dashboard/government-agencies/edit.blade.php b/resources/views/dashboard/government-agencies/edit.blade.php index 87619f0..bd6b81e 100644 --- a/resources/views/dashboard/government-agencies/edit.blade.php +++ b/resources/views/dashboard/government-agencies/edit.blade.php @@ -53,8 +53,7 @@
-
{!!$governmentAgency->description!!}
- +
diff --git a/resources/views/dashboard/news/create.blade.php b/resources/views/dashboard/news/create.blade.php new file mode 100644 index 0000000..fa1780d --- /dev/null +++ b/resources/views/dashboard/news/create.blade.php @@ -0,0 +1,101 @@ +@include('partials.dashboard.head') +@include('partials.dashboard.sidebar', ['page' => 'news']) +@include('partials.dashboard.header') + +
+
+
+
+
+
+
+
+
{{$title}}
+
+ @foreach ($errors->all() as $item) + {{$item}} + @endforeach +
+ @csrf +
+
+
+ +
+ +
+ @error('title') + {{$message}} + @enderror +
+
+
+
+ +
+ +
+ @error('link') + {{$message}} + @enderror +
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+ @error('tag') + {{$message}} + @enderror +
+
+
+
+ +
+ +
+ @error('image') + {{$message}} + @enderror +
+
+
+
+ +
+ +
+
+
+
+
+
+ Kembali + +
+
+
+
+
+
+
+
+
+
+
+ +@include('partials.dashboard.footer', ['rich_editor' => true]) diff --git a/resources/views/dashboard/news/edit.blade.php b/resources/views/dashboard/news/edit.blade.php new file mode 100644 index 0000000..8b27e94 --- /dev/null +++ b/resources/views/dashboard/news/edit.blade.php @@ -0,0 +1,102 @@ +@include('partials.dashboard.head') +@include('partials.dashboard.sidebar', ['page' => 'news']) +@include('partials.dashboard.header') + +
+
+
+
+
+
+
+
+
{{$title}}
+
+ @foreach ($errors->all() as $item) + {{$item}} + @endforeach +
+ @csrf + @method('put') +
+
+
+ +
+ +
+ @error('title') + {{$message}} + @enderror +
+
+
+
+ +
+ +
+ @error('link') + {{$message}} + @enderror +
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+ @error('tag') + {{$message}} + @enderror +
+
+
+
+ +
+ +
+ @error('image') + {{$message}} + @enderror +
+
+
+
+ +
+ +
+
+
+
+
+
+ Kembali + +
+
+
+
+
+
+
+
+
+
+
+ +@include('partials.dashboard.footer', ['rich_editor' => true]) diff --git a/resources/views/dashboard/news/index.blade.php b/resources/views/dashboard/news/index.blade.php new file mode 100644 index 0000000..4671b2d --- /dev/null +++ b/resources/views/dashboard/news/index.blade.php @@ -0,0 +1,63 @@ +@include('partials.dashboard.head') +@include('partials.dashboard.sidebar', ['page' => 'news']) +@include('partials.dashboard.header') + +
+
+
+
+
+
+
+

{{$title}}

+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+ + + + + + + + + + + +
NoJudulLinkKategoriTag
+
+
+
+
+
+
+
+
+ +@include('partials.dashboard.footer' ,['datatable' => 'news']) diff --git a/resources/views/dashboard/news/show.blade.php b/resources/views/dashboard/news/show.blade.php new file mode 100644 index 0000000..59af91d --- /dev/null +++ b/resources/views/dashboard/news/show.blade.php @@ -0,0 +1,108 @@ +@include('partials.dashboard.head') +@include('partials.dashboard.sidebar', ['page' => 'news']) +@include('partials.dashboard.header') + +
+
+
+
+
+
+
+
+
{{$title}}
+
+ @foreach ($errors->all() as $item) + {{$item}} + @endforeach +
+ @csrf + @method('put') +
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ +
+ +
+
+
+
+
+ + + @error('image') + {{$message}} + @enderror +
+
+
+
+ +
+
{!! $news->content !!}
+
+
+
+
+
+
+ Kembali + Ubah +
+
+ +
+
+
+
+
+
+
+
+
+ +@include('partials.dashboard.footer', ['rich_editor' => true]) diff --git a/resources/views/dashboard/sub-classifications/create.blade.php b/resources/views/dashboard/sub-classifications/create.blade.php index faa3aac..030736b 100644 --- a/resources/views/dashboard/sub-classifications/create.blade.php +++ b/resources/views/dashboard/sub-classifications/create.blade.php @@ -19,7 +19,7 @@
- +
@error('name') {{$message}} @@ -59,8 +59,7 @@
-
{!!old('description')!!}
- +
diff --git a/resources/views/dashboard/sub-classifications/edit.blade.php b/resources/views/dashboard/sub-classifications/edit.blade.php index 17bb522..d956819 100644 --- a/resources/views/dashboard/sub-classifications/edit.blade.php +++ b/resources/views/dashboard/sub-classifications/edit.blade.php @@ -54,8 +54,7 @@
-
{!!$subClassification->description!!}
- +
diff --git a/resources/views/partials/app/datatable.blade.php b/resources/views/partials/app/datatable.blade.php index af3e27d..1760a8e 100644 --- a/resources/views/partials/app/datatable.blade.php +++ b/resources/views/partials/app/datatable.blade.php @@ -90,3 +90,28 @@ }); @endif + +@if ($datatable === 'news') + +@endif diff --git a/resources/views/partials/dashboard/footer.blade.php b/resources/views/partials/dashboard/footer.blade.php index 5a871f8..1d51b80 100644 --- a/resources/views/partials/dashboard/footer.blade.php +++ b/resources/views/partials/dashboard/footer.blade.php @@ -60,8 +60,8 @@ @endif @if (isset($rich_editor)) - - + + @endif diff --git a/resources/views/partials/dashboard/sidebar.blade.php b/resources/views/partials/dashboard/sidebar.blade.php index ea513f5..a4a5fa5 100644 --- a/resources/views/partials/dashboard/sidebar.blade.php +++ b/resources/views/partials/dashboard/sidebar.blade.php @@ -67,7 +67,7 @@
Kelola
  • - + Berita diff --git a/routes/web.php b/routes/web.php index 93b7c83..cceef5d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -4,6 +4,7 @@ use App\Http\Controllers\Dashboard\ClassificationController; use App\Http\Controllers\Dashboard\DatatableController; use App\Http\Controllers\Dashboard\GovernmentAgencyController; +use App\Http\Controllers\Dashboard\NewsController; use App\Http\Controllers\Dashboard\OverviewController; use App\Http\Controllers\Dashboard\SubClassificationController; use Illuminate\Support\Facades\Route; @@ -73,5 +74,19 @@ Route::get('/data', [DatatableController::class, 'governmentAgency'])->name('data'); }); + + Route::prefix('news')->name('news.')->group(function(){ + Route::controller(NewsController::class)->group(function(){ + Route::get('/', 'index')->name('index'); + Route::get('/create', 'create')->name('create'); + Route::post('/', 'store')->name('store'); + Route::get('/{id}/show', 'show')->name('show'); + Route::get('/{id}/edit', 'edit')->name('edit'); + Route::put('/{id}', 'update')->name('update'); + Route::delete('/{id}', 'destroy')->name('destroy'); + }); + + Route::get('/data', [DatatableController::class, 'news'])->name('data'); + }); }); });