diff --git a/app/Http/Controllers/Dashboard/DatatableController.php b/app/Http/Controllers/Dashboard/DatatableController.php index a159831..0ed67d5 100644 --- a/app/Http/Controllers/Dashboard/DatatableController.php +++ b/app/Http/Controllers/Dashboard/DatatableController.php @@ -26,6 +26,7 @@ use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use SebastianBergmann\Type\TrueType; use Yajra\DataTables\DataTables; @@ -409,12 +410,43 @@ public function governmentAgency(Request $request){ public function news(Request $request){ if ($request->ajax()) { - $data = News::all(); + + + $category = $request->input('category', 'all'); + $year = $request->input('year', null); + $month = $request->input('month', null); + $date = $request->input('date', null); + + $query = News::query(); + + if($category !== 'all'){ + if($category === 'news'){ + $query->where('category', '0'); + }else if($category === 'announcement'){ + $query->where('category', '1'); + } + } + + if ($year) { + $query->whereYear('created_at', $year); + } + + if ($month) { + $monthYear = Carbon::parse($month); + $query->whereYear('created_at', $monthYear->year) + ->whereMonth('created_at', $monthYear->month); + } + + if ($date) { + $query->whereDate('created_at', $date); + } + + $data = $query->get(); return DataTables::of($data) ->addIndexColumn() ->addColumn('title', function ($data) { - return shorten_text($data->title, 50); + return $data->title; }) ->addColumn('link', function ($data) { if ($data->link != null) { @@ -424,10 +456,10 @@ public function news(Request $request){ } }) ->addColumn('category', function ($data) { - if ($data->category == '0') { + if ($data->category === '0') { return 'Berita'; }else{ - return 'Pengumuman'; + return 'Pengumuman'; } }) ->addColumn('action', function ($data) { diff --git a/app/Http/Controllers/Dashboard/NewsController.php b/app/Http/Controllers/Dashboard/NewsController.php index 75a5df2..31de6cb 100644 --- a/app/Http/Controllers/Dashboard/NewsController.php +++ b/app/Http/Controllers/Dashboard/NewsController.php @@ -11,10 +11,15 @@ class NewsController extends Controller { - public function index(){ + public function index(Request $request){ return view('dashboard.news.index', [ - 'title' => 'Berita' + 'title' => 'Berita', + 'category' => $request->input('category', null), + 'year' => $request->input('year', null), + 'month' => $request->input('month', null), + 'date' => $request->input('date', null), ]); + } public function create(){ @@ -25,15 +30,18 @@ public function create(){ 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(); + if($request->has('image')){ + $validatedData['image'] = store_file($validatedData['image'], '/uploads/news', 'public')['randomFileName']; + } + $createdNews = News::create($validatedData); if($createdNews){ - return redirect()->back()->with('success-status', 'Berhasil menambahkan berita.'); + return redirect()->route('dashboard.news.index')->with('success-status', 'Berhasil menambahkan berita.'); }else{ return redirect()->back()->with('failed-status', 'Gagal menambahkan berita.'); } @@ -94,4 +102,15 @@ public function destroy($id){ return response()->json(['message' => 'Gagal menghapus berita.'], 500); } } + + public function filter(Request $request){ + + $filteredRequest = $request->except('_token'); + + $filterField = array_filter($filteredRequest, function ($value) { + return filled($value); + }); + + return redirect()->route('dashboard.news.index', $filterField); + } } diff --git a/app/Http/Requests/NewsRequest.php b/app/Http/Requests/NewsRequest.php index 484a127..713edfa 100644 --- a/app/Http/Requests/NewsRequest.php +++ b/app/Http/Requests/NewsRequest.php @@ -28,14 +28,9 @@ public function rules(): array 'link' => ['nullable', 'url'], 'tag' => ['nullable'], 'category' => ['required', 'in:0,1'], + 'image' => 'nullable', 'image', 'mimes:png,jpg,jpeg', 'max:4096' ]; - 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; } diff --git a/app/Models/Legacy/OldNews.php b/app/Models/Legacy/OldNews.php new file mode 100644 index 0000000..ca95e0c --- /dev/null +++ b/app/Models/Legacy/OldNews.php @@ -0,0 +1,15 @@ +string('title'); $table->string('slug')->unique(); $table->text('content'); - $table->string('image'); + $table->string('image')->nullable(); $table->string('link')->nullable(); $table->string('tag')->nullable(); $table->enum('category', ['0', '1'])->default('0'); + $table->integer('views')->default(0); $table->unsignedBigInteger('author_id'); $table->foreign('author_id')->references('id')->on('users'); $table->timestamps(); + $table->softDeletes(); }); } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index aa05bd6..182fcb0 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -20,6 +20,7 @@ public function run(): void $this->call(SubClassificationSeeder::class); $this->call(LocationSeeder::class); $this->call(SubLocationSeeder::class); + $this->call(NewsSeeder::class); $this->call(CompanySeeder::class); $this->call(MediaSeeder::class); $this->call(JournalistSeeder::class); diff --git a/database/seeders/NewsSeeder.php b/database/seeders/NewsSeeder.php index 20a174f..b9c3ec7 100644 --- a/database/seeders/NewsSeeder.php +++ b/database/seeders/NewsSeeder.php @@ -2,8 +2,11 @@ namespace Database\Seeders; +use App\Models\Legacy\OldNews; +use App\Models\News; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; +use Illuminate\Support\Str; class NewsSeeder extends Seeder { @@ -12,6 +15,24 @@ class NewsSeeder extends Seeder */ public function run(): void { - // + $legacyNews = OldNews::all(); + + foreach($legacyNews as $item){ + News::create([ + 'id' => $item->id, + 'title' => $item->title, + 'slug' => Str::slug($item->title), + 'content' => $item->content, + 'category' => $item->category, + 'link' => $item->link, + 'image' => $item->image, + 'tag' => $item->tag, + 'created_at' => $item->created_at, + 'updated_at' => $item->updated_at, + 'deleted_at' => $item->deleted_at, + 'author_id' => $item->enhancer, + ]); + + } } } diff --git a/resources/views/dashboard/news/create.blade.php b/resources/views/dashboard/news/create.blade.php index fa1780d..0edebcf 100644 --- a/resources/views/dashboard/news/create.blade.php +++ b/resources/views/dashboard/news/create.blade.php @@ -12,13 +12,10 @@