194 lines
5.7 KiB
PHP
194 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\News;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\View;
|
|
|
|
class HomepageController extends Controller
|
|
{
|
|
protected $yesterdayVisitor;
|
|
|
|
protected $todayVisitor;
|
|
|
|
protected $thisMonthVisitor;
|
|
|
|
protected $thisYearVisitor;
|
|
|
|
protected $totalVisitor;
|
|
|
|
public function __construct(){
|
|
$today = Carbon::today();
|
|
$yesterday = Carbon::yesterday();
|
|
$thisMonth = Carbon::now()->startOfMonth();
|
|
$thisYear = Carbon::now()->startOfYear();
|
|
|
|
$this->yesterdayVisitor = DB::table('visitors')
|
|
->whereDate('date', '=', $yesterday->toDateString())
|
|
->value('visitors');
|
|
|
|
$this->todayVisitor = DB::table('visitors')
|
|
->whereDate('date', '=', $today->toDateString())
|
|
->value('visitors');
|
|
|
|
$this->thisMonthVisitor = DB::table('visitors')
|
|
->whereBetween('date', [$thisMonth, Carbon::now()])
|
|
->sum('visitors');
|
|
|
|
$this->thisYearVisitor = DB::table('visitors')
|
|
->whereBetween('date', [$thisYear, Carbon::now()])
|
|
->sum('visitors');
|
|
|
|
$this->totalVisitor = DB::table('visitors')->sum('visitors');
|
|
|
|
View::share('yesterdayVisitor', $this->yesterdayVisitor);
|
|
View::share('todayVisitor', $this->todayVisitor);
|
|
View::share('thisMonthVisitor', $this->thisMonthVisitor);
|
|
View::share('thisYearVisitor', $this->thisYearVisitor);
|
|
View::share('totalVisitor', $this->totalVisitor);
|
|
}
|
|
|
|
public function landing(){
|
|
$data = [
|
|
'title' => 'Beranda',
|
|
'page' => 'landing',
|
|
'news' => News::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])
|
|
->where('category', '=', '0')
|
|
->latest()
|
|
->get(),
|
|
'announcements' => News::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])
|
|
->where('category', '=', '1')
|
|
->latest()
|
|
->get(),
|
|
];
|
|
|
|
return view('homepage.index', $data);
|
|
}
|
|
|
|
public function search(Request $request){
|
|
|
|
$request->validate([
|
|
'search' => 'nullable|string|max:255|regex:/^[a-zA-Z0-9\s]+$/'
|
|
], [
|
|
'search.string' => 'Pencarian harus berupa teks.',
|
|
'search.max' => 'Pencarian tidak boleh lebih dari 255 karakter.',
|
|
'search.regex' => 'Pencarian hanya boleh mengandung huruf, angka, dan spasi.'
|
|
]);
|
|
|
|
$search = trim($request->input('search'));
|
|
|
|
$data = [
|
|
'title' => 'Pencarian',
|
|
'page' => 'search',
|
|
'data' => News::query()
|
|
->when($search, function ($query, $search) {
|
|
return $query->where('title', 'LIKE', '%' . $search . '%');
|
|
})
|
|
->latest()
|
|
->simplePaginate(12)
|
|
->appends(['search' => $search]),
|
|
'search' => $search
|
|
];
|
|
|
|
return view('homepage.search', $data);
|
|
}
|
|
|
|
public function news(Request $request){
|
|
|
|
$data = [
|
|
'title' => 'Berita',
|
|
'page' => 'news',
|
|
];
|
|
|
|
$search = $request->input('search', null);
|
|
|
|
if($search){
|
|
$data['news'] = News::where('title', 'LIKE', "%{$search}%")
|
|
->where('category', '0')
|
|
->latest()
|
|
->simplePaginate(12);
|
|
}else{
|
|
$data['news'] = News::where('category', '0')->latest()->simplePaginate(12);
|
|
}
|
|
|
|
return view('homepage.news.index', $data);
|
|
}
|
|
|
|
public function readNews($slug){
|
|
$news = News::where('slug', $slug)->first();
|
|
$news->increment('views', 1);
|
|
|
|
$data = [
|
|
'title' => 'Berita',
|
|
'page' => 'news',
|
|
'news' => $news,
|
|
'popularNews' => News::where('category', '0')->orderBy('views', 'desc')->take(5)->get(),
|
|
'otherNews' => News::where('category', '0')->orderByDesc('id')->where('id', '!=', $news->id)->take(3)->get()
|
|
];
|
|
|
|
return view('homepage.news.read', $data);
|
|
|
|
}
|
|
|
|
public function announcement(Request $request){
|
|
|
|
$data = [
|
|
'title' => 'Pengumuman',
|
|
'page' => 'announcements',
|
|
];
|
|
|
|
$search = $request->input('search', null);
|
|
|
|
if($search){
|
|
$data['announcements'] = News::where('category', '1')->where('title', 'LIKE', "%{$search}%")
|
|
// ->orWhere('content', 'LIKE', "%{$search}%")
|
|
->latest()
|
|
->simplePaginate(6);
|
|
}else{
|
|
$data['announcements'] = News::where('category', '1')->latest()->simplePaginate(6);
|
|
}
|
|
|
|
return view('homepage.announcements.index', $data);
|
|
}
|
|
|
|
public function readAnnouncement($slug){
|
|
$announcement = News::where('slug', $slug)->first();
|
|
$announcement->increment('views', 1);
|
|
|
|
$data = [
|
|
'title' => 'Pengumuman',
|
|
'page' => 'announcements',
|
|
'announcement' => $announcement,
|
|
'otherAnnouncements' => News::where('category', '1')->orderByDesc('id')->where('id', '!=', $announcement->id)->take(4)->get()
|
|
];
|
|
|
|
return view('homepage.announcements.read', $data);
|
|
|
|
}
|
|
|
|
public function aboutUs(){
|
|
$data = [
|
|
'title' => 'Tentang Kami',
|
|
'page' => 'about_us'
|
|
];
|
|
|
|
return view('homepage.about-us.index', $data);
|
|
}
|
|
|
|
public function contact(){
|
|
$data = [
|
|
'title' => 'Kontak',
|
|
'page' => 'contacts'
|
|
];
|
|
|
|
return view('homepage.contacts.index', $data);
|
|
}
|
|
|
|
public function customError($code){
|
|
abort($code);
|
|
}
|
|
}
|