chore: display news data

feat: filter news data
This commit is contained in:
myuqinurrohman 2025-03-25 10:34:23 +07:00
parent 96643c30de
commit 3c1212d2e9
15 changed files with 311 additions and 131 deletions

View File

@ -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 '<span class="badge bg-primary" style="display:inline-block;font-size:0.8rem;">Berita</span>';
}else{
return '<span class="badge bg-success" style="display:inline-block;font-size:0.8rem;">Pengumuman</span>';
return '<span class="badge bg-info" style="display:inline-block;font-size:0.8rem;">Pengumuman</span>';
}
})
->addColumn('action', function ($data) {

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -0,0 +1,15 @@
<?php
namespace App\Models\Legacy;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class OldNews extends Model
{
use SoftDeletes;
protected $connection = 'legacy';
protected $table = 'news';
}

View File

@ -3,9 +3,11 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class News extends Model
{
use SoftDeletes;
protected $table = 'news';
protected $fillable = [

View File

@ -16,13 +16,15 @@ public function up(): void
$table->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();
});
}

View File

@ -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);

View File

@ -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,
]);
}
}
}

View File

@ -12,13 +12,10 @@
<div class="card-head">
<h5 class="card-title">{{$title}}</h5>
</div>
@foreach ($errors->all() as $item)
{{$item}}
@endforeach
<form action="{{route('dashboard.news.store')}}" method="post" enctype="multipart/form-data">
<form action="{{route('dashboard.news.store')}}" method="post" enctype="multipart/form-data" id="myForm">
@csrf
<div class="row g-4">
<div class="col-lg-6">
<div class="col-lg-12">
<div class="form-group">
<label class="form-label" for="title">Judul<span class="required-input">*</span></label>
<div class="form-control-wrap">
@ -64,7 +61,7 @@
</div>
<div class="col-lg-6">
<div class="form-group">
<label class="form-label" for="image">Gambar<span class="required-input">*</span></label>
<label class="form-label" for="image">Gambar</label>
<div class="form-control-wrap">
<input type="file" class="form-control @error('image') border-danger @enderror" id="image" name="image" accept="image/*">
</div>
@ -85,7 +82,7 @@
<hr>
<div class="form-group d-flex justify-content-end">
<a href="{{route('dashboard.news.index')}}" class="btn btn-white btn-dim btn-outline-light mx-2"><span>Kembali</span></a>
<button type="submit" class="btn btn-md btn-primary">Tambahkan</button>
<button type="button" class="btn btn-md btn-primary" id="submitButton">Tambahkan</button>
</div>
</div>
</div>

View File

@ -12,14 +12,11 @@
<div class="card-head">
<h5 class="card-title">{{$title}}</h5>
</div>
@foreach ($errors->all() as $item)
{{$item}}
@endforeach
<form action="{{route('dashboard.news.update', ['id' => encrypt_id($news->id)])}}" method="post" enctype="multipart/form-data">
<form action="{{route('dashboard.news.update', ['id' => encrypt_id($news->id)])}}" method="post" enctype="multipart/form-data" id="myForm">
@csrf
@method('put')
<div class="row g-4">
<div class="col-lg-6">
<div class="col-lg-12">
<div class="form-group">
<label class="form-label" for="title">Judul<span class="required-input">*</span></label>
<div class="form-control-wrap">
@ -65,7 +62,7 @@
</div>
<div class="col-lg-6">
<div class="form-group">
<label class="form-label" for="image">Gambar<span class="required-input">*</span></label>
<label class="form-label" for="image">Gambar</label>
<div class="form-control-wrap">
<input type="file" class="form-control @error('image') border-danger @enderror" id="image" name="image" accept="image/*">
</div>
@ -85,8 +82,12 @@
<div class="col-12">
<hr>
<div class="form-group d-flex justify-content-end">
@if (request()->query('show'))
<a href="{{route('dashboard.news.show', ['id' => encrypt_id($news->id)])}}" class="btn btn-white btn-dim btn-outline-light mx-2"><span>Kembali</span></a>
@else
<a href="{{route('dashboard.news.index')}}" class="btn btn-white btn-dim btn-outline-light mx-2"><span>Kembali</span></a>
<button type="submit" class="btn btn-md btn-primary">Simpan</button>
@endif
<button type="button" class="btn btn-md btn-primary" id="submitButton">Simpan</button>
</div>
</div>
</div>

View File

@ -16,18 +16,11 @@
<a href="#" class="btn btn-icon btn-trigger toggle-expand me-n1" data-target="pageMenu"><em class="icon ni ni-more-v"></em></a>
<div class="toggle-expand-content" data-content="pageMenu">
<ul class="nk-block-tools g-3">
{{-- <li>
<li>
<div class="drodown">
<a href="#" class="dropdown-toggle btn btn-white btn-dim btn-outline-light" data-bs-toggle="dropdown"><em class="d-none d-sm-inline icon ni ni-calender-date"></em><span><span class="d-none d-md-inline">Last</span> 30 Days</span><em class="dd-indc icon ni ni-chevron-right"></em></a>
<div class="dropdown-menu dropdown-menu-end">
<ul class="link-list-opt no-bdr">
<li><a href="#"><span>Last 30 Days</span></a></li>
<li><a href="#"><span>Last 6 Months</span></a></li>
<li><a href="#"><span>Last 1 Years</span></a></li>
</ul>
</div>
<a href="javascript: void(0);" class="btn btn-white btn-dim btn-outline-light" data-bs-toggle="modal" data-bs-target="#modalFilter"><em class="d-none d-sm-inline icon ni ni-filter"></em><span><span class="d-none d-md-inline">Filter</a>
</div>
</li> --}}
</li>
<li class="nk-block-tools-opt"><a href="{{route('dashboard.news.create')}}" class="btn btn-primary"><em class="icon ni ni-plus"></em><span>Tambah</span></a></li>
</ul>
</div>
@ -42,12 +35,12 @@
<table class="table table-striped table-bordered nowrap" id="newsTable" border="1">
<thead>
<tr>
<th style="width: 10% !important;">No</th>
<th>No</th>
<th>Judul</th>
<th>Link</th>
<th>Kategori</th>
<th>Tag</th>
<th style="width: 10% !important;"></th>
<th></th>
</tr>
</thead>
</table>
@ -55,6 +48,56 @@
</div>
</div>
</div>
<div class="modal fade" id="modalFilter">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Filter</h5>
<a href="javascript:void(0);" class="close" data-bs-dismiss="modal" aria-label="Close">
<em class="icon ni ni-cross"></em>
</a>
</div>
<form action="{{route('dashboard.news.filter')}}" class="form-validate is-alter" method="post">
@csrf
<div class="modal-body">
<div class="form-group">
<label class="form-label">Kategori</label>
<div class="form-control-wrap">
<select class="form-select js-select2" name="category">
<option value="all">Semua Kategori</option>
<option value="news">Berita</option>
<option value="announcement">Pengumuman</option>
</select>
</div>
</div>
<div class="form-group">
<label class="form-label" for="year">Tahun</label>
<div class="form-control-wrap">
<input type="number" class="form-control" id="year" placeholder="Data berita pada tahun" name="year">
</div>
</div>
<div class="form-group">
<label class="form-label" for="month">Bulan</label>
<div class="form-control-wrap">
<input type="month" class="form-control" id="month" placeholder="Data berita pada bulan dan tahun" name="month">
</div>
</div>
<div class="form-group">
<label class="form-label" for="date">Tanggal</label>
<div class="form-control-wrap">
<input type="date" class="form-control" id="date" placeholder="Data berita pada bulan dan tahun" name="date">
</div>
</div>
</div>
<div class="modal-footer bg-light">
<span class="sub-text">
<button type="submit" class="btn btn-sm btn-primary">Filter</button>
</span>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>

View File

@ -7,96 +7,119 @@
<div class="nk-content-inner">
<div class="nk-content-body">
<div class="nk-block nk-block-lg">
<div class="card card-bordered card-preview">
<div class="card-inner">
<div class="card-head">
<h5 class="card-title">{{$title}}</h5>
</div>
@foreach ($errors->all() as $item)
{{$item}}
@endforeach
<form action="{{route('dashboard.news.update', ['id' => encrypt_id($news->id)])}}" method="post" enctype="multipart/form-data">
@csrf
@method('put')
<div class="row g-4">
<div class="col-lg-6">
<div class="form-group">
<label class="form-label" for="title">Judul</label>
<div class="form-control-wrap">
<input type="text" class="form-control @error('title') border-danger @enderror" id="title" name="title" placeholder="Masukan judul" value="{{$news->title}}" readonly>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label class="form-label" for="link">Link</label>
<div class="form-control-wrap">
<input type="text" class="form-control @error('link') border-danger @enderror" id="link" name="link" placeholder="Masukan link" value="{{$news->link}}" readonly>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label class="form-label" for="link">Kategori</label>
<div class="form-control-wrap">
<input type="text" class="form-control @error('link') border-danger @enderror" id="link" name="link" placeholder="Masukan link" value="{{$news->category === '0' ? 'Berita' : 'Pengumuman'}}" readonly>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label class="form-label" for="tag">Tag</label>
<div class="form-control-wrap">
<input type="text" class="form-control @error('tag') border-danger @enderror" id="tag" name="tag" placeholder="Masukan tag" value="{{$news->tag}}" readonly>
</div>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label class="form-label" for="image">Gambar<span class="required-input">*</span></label>
<div class="form-control-wrap">
<a href="javascript:void(0);" data-bs-toggle="modal" data-bs-target="#modalDefault" class="btn btn-info">Lihat gambar</a>
</div>
@error('image')
<span class="validation-error">{{$message}}</span>
@enderror
</div>
</div>
<div class="col-lg-12">
<div class="form-group">
<label class="form-label" for="phone-no-1">Konten<span class="required-input">*</span></label>
<div class="form-control-wrap">
<div class="news-content">{!! $news->content !!}</div>
</div>
</div>
</div>
<div class="col-12">
<hr>
<div class="form-group d-flex justify-content-end">
<a href="{{route('dashboard.news.index')}}" class="btn btn-white btn-dim btn-outline-light mx-2"><span>Kembali</span></a>
<a href="{{route('dashboard.news.edit', ['id' => encrypt_id($news->id)])}}" class="btn btn-warning"><span>Ubah</span></a>
</div>
</div>
<div class="modal fade" tabindex="-1" id="modalDefault">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Preview</h5>
<a href="#" class="close" data-bs-dismiss="modal" aria-label="Close">
<em class="icon ni ni-cross"></em>
</a>
<div class="card">
<div class="card-aside-wrap">
<div class="card-content">
<ul class="nav nav-tabs nav-tabs-mb-icon nav-tabs-card">
<li class="nav-item">
<a class="nav-link text-primary" href="#"><em class="icon ni ni-info"></em><span>Detail Berita</span></a>
</li>
</ul><!-- .nav-tabs -->
<div class="card-inner">
<div class="nk-block">
{{-- <div class="nk-block-head">
<h5 class="title">Personal Information</h5>
<p>Basic info, like your name and address, that you use on Nio Platform.</p>
</div> --}}
<div class="profile-ud-list">
<div class="profile-ud-item">
<div class="profile-ud wider">
<span class="profile-ud-label">Judul</span>
<span class="profile-ud-value">{{$news->title}}</span>
</div>
<div class="modal-body">
<img src="{{asset('storage/uploads/news/'. $news->image)}}" alt="preview">
</div>
{{-- <div class="modal-footer bg-light">
<span class="sub-text">Modal Footer Text</span>
</div> --}}
</div>
</div>
</div>
<div class="profile-ud-item">
<div class="profile-ud wider">
<span class="profile-ud-label">Kategori</span>
@if ($news->category === '0')
<span class="profile-ud-value"><span class="badge bg-primary">Berita</span></span>
@elseif ($news->category === '1')
<span class="profile-ud-value"><span class="badge bg-info">Pengumuman</span></span>
@else
<span class="profile-ud-value"><span class="badge bg-secondary">Tidak Diketahui</span></span>
@endif
</div>
</div>
<div class="profile-ud-item">
<div class="profile-ud wider">
<span class="profile-ud-label">Link</span>
<span class="profile-ud-value">
@if (filled($news->link))
<a href="{{$news->link}}">{{$news->link}}</a>
@else
{{'-'}}
@endif
</span>
</div>
</div>
<div class="profile-ud-item">
<div class="profile-ud wider">
<span class="profile-ud-label">Gambar</span>
<span class="profile-ud-value">
@if (filled($news->image))
<a href="javascript:void(0);" data-bs-toggle="modal" data-bs-target="#modalDefault" class="btn btn-sm btn-info">Lihat gambar</a>
@else
Tidak ada gambar
@endif
</span>
</div>
</div>
<div class="profile-ud-item">
<div class="profile-ud wider">
<span class="profile-ud-label">Dilihat</span>
<span class="profile-ud-value">
{{$news->viewers ?? '-'}}
</span>
</div>
</div>
<div class="profile-ud-item">
<div class="profile-ud wider">
<span class="profile-ud-label">Dibuat Pada</span>
<span class="profile-ud-value">{{idn_date($news->created_at, 'l, d F Y')}}</span>
</div>
</div>
</div><!-- .profile-ud-list -->
</div><!-- .nk-block -->
{{-- <div class="nk-divider divider md"></div> --}}
<div class="nk-block">
<div class="nk-block-head nk-block-head-sm nk-block-between">
<span class="profile-ud-label">Konten</span>
</div><!-- .nk-block-head -->
<div class="bq-note">
<div class="bq-note-item">
<div class="bq-note-text">
{!!$news->content ?? '-' !!}
</div>
</div><!-- .bq-note-item -->
</div><!-- .bq-note -->
</div><!-- .nk-block -->
<hr>
<div class="nk-block d-flex justify-content-end">
<a href="{{route('dashboard.news.index')}}" class="btn btn-white btn-dim btn-outline-light mx-2"><span>Kembali</span></a>
@if (!filled($news->deleted_at))
<a href="{{route('dashboard.news.edit', ['id' => encrypt_id($news->id), 'show' => true])}}" class="btn btn-warning"><span>Ubah</span></a>
@endif
</div><!-- .nk-block -->
</div><!-- .card-inner -->
</div><!-- .card-content -->
</div><!-- .card-aside-wrap -->
</div>
<div class="modal fade" tabindex="-1" id="modalDefault">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Preview</h5>
<a href="#" class="close" data-bs-dismiss="modal" aria-label="Close">
<em class="icon ni ni-cross"></em>
</a>
</div>
</form>
<div class="modal-body">
<img src="{{asset('storage/uploads/news/'. $news->image)}}" alt="preview">
</div>
{{-- <div class="modal-footer bg-light">
<span class="sub-text">Modal Footer Text</span>
</div> --}}
</div>
</div>
</div>
</div>
@ -105,4 +128,4 @@
</div>
</div>
<!-- content @e -->
@include('partials.dashboard.footer', ['rich_editor' => true])
@include('partials.dashboard.footer', ['rich_editor' => false])

View File

@ -254,7 +254,16 @@
$('#newsTable').DataTable({
processing: true,
serverSide: true,
ajax: '{{ route('dashboard.news.data') }}',
ajax: {
url: '{{ route('dashboard.news.data') }}',
type: 'GET',
data: function(d) {
d.category = '{{ $category }}';
d.year = '{{ $year }}';
d.month = '{{ $month }}';
d.date = '{{ $date }}';
}
},
columns: [
{ data: 'DT_RowIndex', name: 'DT_RowIndex', orderable: false, searchable: false },
{ data: 'title', name: 'title' },
@ -265,6 +274,23 @@
{ data: 'created_at', name: 'created_at', visible: false },
{ data: 'id', name: 'id', orderable: true, searchable: false, visible: false },
],
columnDefs: [
{ targets: 0, width: 'auto' },
// { targets: 2, width: '10%' },
{ targets: 3, width: '10%' },
{ targets: 4, width: '12%' },
{ targets: 5, width: '7%' },
{
targets: [1,2],
createdCell: function(td, cellData, rowData, row, col) {
$(td).css({
'word-wrap': 'break-word',
'white-space': 'normal',
'overflow-wrap': 'break-word'
});
}
}
],
language: datatableLanguage,
autoWidth: false,
order: [[6, 'desc']],

View File

@ -101,6 +101,8 @@
@include('partials.app.sweetalert')
{{-- {{dd(request()->all())}} --}}
@if (isset($page))
@if ($page === 'create_issue_management')
<script src="{{asset('assets/dashboard/js/issue-managements/create.js')}}"></script>

View File

@ -147,6 +147,7 @@
Route::prefix('news')->name('news.')->group(function(){
Route::controller(NewsController::class)->group(function(){
Route::get('/', 'index')->name('index');
Route::post('/filter', 'filter')->name('filter');
Route::get('/{id}/show', 'show')->name('show');
Route::middleware('role:1')->group(function(){