File: /home/bdedition/public_html/core/app/Http/Controllers/SiteController.php
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
use App\Models\News;
use App\Models\Page;
use App\Models\Category;
use App\Models\Frontend;
use App\Models\Language;
use App\Constants\Status;
use Illuminate\Http\Request;
use App\Models\SupportTicket;
use App\Models\SupportMessage;
use App\Models\AdminNotification;
use Illuminate\Support\Facades\Cookie;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class SiteController extends Controller
{
public function index()
{
$pageTitle = 'Home';
$sections = Page::where('tempname', activeTemplate())->where('slug', '/')->first();
$seoContents = $sections->seo_content;
$seoImage = @$seoContents->image ? getImage(getFilePath('seo') . '/' . @$seoContents->image, getFileSize('seo')) : null;
$latestNews = $this->newsQuery()->latest()->limit(5)->get(['id', 'title', 'slug', 'category_id']);
$latestNewsBanner = $this->newsQuery(orderBy: 'desc')->take(10)->get(['id', 'title', 'image', 'short_description', 'slug', 'category_id']);
$trending = $this->newsQuery('trending', 'desc')->skip(1)->take(3)->get();
$lastMustRead = $this->newsQuery('mustRead', 'desc')->first();
$bannerLatestNews = $this->newsQuery(orderBy: 'desc')->take(4)->get();
$bannerLastTrending = $this->newsQuery('trending', 'desc')->first();
$bannerMustRead = $this->newsQuery('mustRead', 'desc')->take(4)->get();
$firstFourCategories = $this->categoryQuery()->take(4)->with('news')->get();
$fourVideos = $this->newsQuery('video', 'desc')->take(4)->get(['id', 'video_link', 'have_video', 'slug', 'category_id']);
$trendingNews = $this->newsQuery('trending', 'desc')->take(8)->get();
$moreTrendingNews = $this->newsQuery('trending', 'desc')->skip(8)->take(5)->get();
$mostPopular = $this->newsQuery()->orderByDesc('views')->take(5)->get(['id', 'title', 'slug', 'category_id']);
$photos = $this->newsQuery(orderBy: 'desc')->take(5)->get(['id', 'title', 'image', 'slug', 'category_id']);
$categories = $this->categoryQuery()->get();
$afterFourCategories = false;
if (count($categories) > 4) {
$afterFourCategories = $this->categoryQuery()->skip(4)->take(count($categories) - 4)->with('news')->get();
}
return view('Template::home', compact('pageTitle', 'sections', 'seoContents', 'seoImage', 'latestNews', 'latestNewsBanner', 'trending', 'lastMustRead', 'bannerLatestNews', 'bannerMustRead', 'bannerLastTrending', 'firstFourCategories', 'fourVideos', 'trendingNews', 'moreTrendingNews', 'mostPopular', 'photos', 'afterFourCategories', 'categories'));
}
public function categoryDetails($slug)
{
$id = Category::where('slug', $slug)->firstOrFail()->id;
$category = Category::active()->where('id', $id)->with('news')->firstOrFail();
$firstCategoryNews = $this->newsQuery(orderBy: 'desc')->where('category_id', $category->id)->first();
$latestNews = $this->newsQuery(orderBy: 'desc')->take(6)->get(['id', 'title', 'image', 'slug','category_id']);
$removeDuplicate = @$latestNews->first()->id;
$pageTitle = "$category->name";
$seoContents = $category->seoContent;
$seoImage = getImage(getFilePath('seo') . '/' . @$category->seo_content->image, getFileSize('seo'));
return view('Template::category.details', compact('category', 'pageTitle', 'firstCategoryNews', 'latestNews', 'removeDuplicate', 'seoContents', 'seoImage'));
}
public function loadMore(Request $request)
{
$data = $this->newsQuery(orderBy: 'desc')->where('category_id', $request->catId);
if ($request->id > 0) {
$data = $data->where('id', '<', $request->id);
} else {
$data = $data->where('id', '!=', $request->remove);
}
$data = $data->take(8)->get(['id', 'title', 'image', 'slug']);
$next = $this->newsQuery(orderBy: 'desc')->where('id', '<', @$data->last()->id)->take(8)->count();
return view('Template::partials.load_more', compact('next', 'data'));
}
public function newsDetails($category, $newsId)
{
$pageTitle = __('বিস্তারিত');
$news = $this->newsQuery()->where('id', $newsId)->firstOrFail();
$news->increment('views');
$catInfo = Category::where('slug', $category)->firstOrFail();
$column = ['id', 'title', 'image', 'slug', 'category_id'];
$topNews = $this->newsQuery()->where('id', '<>', $news->id)->orderByDesc('views')->take(6)->get($column);
$trendingNews = $this->newsQuery('trending', 'desc')->where('id', '<>', $news->id)->take(4)->get($column);
$latestNews = $this->newsQuery(orderBy: 'desc')->where('id', '<>', $news->id)->take(6)->get($column);
$seoContents['keywords'] = $news->tags ?? [];
$seoContents['social_title'] = $news->title;
$seoContents['description'] = strLimit(strip_tags($news->short_description), 150);
$seoContents['social_description'] = strLimit(strip_tags($news->short_description), 150);
$seoImage = getImage(getFilePath('news') . '/' . @$news->image, getFileSize('news'));
$seoContents = json_decode(json_encode($seoContents));
return view('Template::news.details', compact('news', 'pageTitle', 'topNews', 'trendingNews', 'latestNews', 'seoImage', 'seoContents','catInfo'));
}
public function newsPrint($category, $newsId)
{
$pageTitle = __('Details');
$news = $this->newsQuery()->where('id', $newsId)->firstOrFail();
$news->increment('views');
$catInfo = Category::where('slug', $category)->firstOrFail();
$column = ['id', 'title', 'image', 'slug', 'category_id'];
$topNews = $this->newsQuery()->where('id', '<>', $news->id)->orderByDesc('views')->take(6)->get($column);
$trendingNews = $this->newsQuery('trending', 'desc')->where('id', '<>', $news->id)->take(4)->get($column);
$latestNews = $this->newsQuery(orderBy: 'desc')->where('id', '<>', $news->id)->take(6)->get($column);
$seoContents['keywords'] = $news->tags ?? [];
$seoContents['social_title'] = $news->title;
$seoContents['description'] = strLimit(strip_tags($news->short_description), 150);
$seoContents['social_description'] = strLimit(strip_tags($news->short_description), 150);
$seoImage = getImage(getFilePath('news') . '/' . @$news->image, getFileSize('news'));
$seoContents = json_decode(json_encode($seoContents));
$data = "Title: {$news->title}, \nNews Url: " . route('news.details', ['category' => $news->category->slug, 'news' => $news->id]) . ",\nTime: {$news->created_at}";
$qrCode = QrCode::format('svg') // Change to SVG
->size(100)
->encoding('UTF-8')
->generate($data);
return view('Template::news.print', compact('news', 'pageTitle', 'topNews', 'trendingNews', 'latestNews', 'seoImage', 'seoContents', 'catInfo', 'qrCode'));
}
// public function newsDetails($slug)
// {
// $pageTitle = __('বিস্তারিত');
// $news = $this->newsQuery()->where('slug', $slug)->firstOrFail();
// $news->increment('views');
// $column = ['id', 'title', 'image', 'slug'];
// $topNews = $this->newsQuery()->where('id', '<>', $news->id)->orderByDesc('views')->take(6)->get($column);
// $trendingNews = $this->newsQuery('trending', 'desc')->where('id', '<>', $news->id)->take(4)->get($column);
// $latestNews = $this->newsQuery(orderBy: 'desc')->where('id', '<>', $news->id)->take(6)->get($column);
// $seoContents['keywords'] = $news->tags ?? [];
// $seoContents['social_title'] = $news->title;
// $seoContents['description'] = strLimit(strip_tags($news->short_description), 150);
// $seoContents['social_description'] = strLimit(strip_tags($news->short_description), 150);
// $seoImage = getImage(getFilePath('news') . '/' . @$news->image, getFileSize('news'));
// $seoContents = json_decode(json_encode($seoContents));
// return view('Template::news.details', compact('news', 'pageTitle', 'topNews', 'trendingNews', 'latestNews', 'seoImage', 'seoContents'));
// }
public function search(Request $request)
{
if (!$request->search) {
return back();
}
$searchInput = $request->search;
$search = $this->newsQuery(orderBy: 'desc')->searchable(['tags', 'short_description', 'title', 'category:name'])
->paginate(getPaginate());
$pageTitle = "Search result for $request->search";
return view('Template::news.search', compact('pageTitle', 'search'));
}
public function videos()
{
$videos = $this->newsQuery('video', 'desc')->paginate(getPaginate());
$pageTitle = 'Videos';
return view('Template::videos', compact('pageTitle', 'videos'));
}
public function photos()
{
$photos = $this->newsQuery(orderBy: 'desc')->paginate(getPaginate());
$pageTitle = 'Photos';
return view('Template::photos', compact('pageTitle', 'photos'));
}
public function archived(Request $request)
{
$search = $this->newsQuery(orderBy: 'desc')->whereDate('created_at', Carbon::parse($request->date))->paginate(getPaginate());
$pageTitle = "News search for $request->date";
return view('Template::news.search', compact('pageTitle', 'search'));
}
public function newsQuery($scope = null, $orderBy = null)
{
$news = News::active()->approved();
if ($scope) {
$news = $news->$scope();
}
if ($orderBy) {
$news = $news->orderBy('id', $orderBy);
}
return $news;
}
public function categoryQuery()
{
return Category::active()->orderBy('id')->whereHas('news', function ($news) {
$news->active()->approved();
});
}
public function pages($slug)
{
$page = Page::where('tempname', activeTemplate())->where('slug', $slug)->firstOrFail();
$pageTitle = $page->name;
$sections = $page->secs;
$seoContents = $page->seo_content;
$seoImage = @$seoContents->image ? getImage(getFilePath('seo') . '/' . @$seoContents->image, getFileSize('seo')) : null;
return view('Template::pages', compact('pageTitle', 'sections', 'seoContents', 'seoImage'));
}
public function contact()
{
$pageTitle = "Contact Us";
$user = auth()->user();
$sections = Page::where('tempname', activeTemplate())->where('slug', 'contact')->first();
$seoContents = $sections->seo_content;
$seoImage = @$seoContents->image ? getImage(getFilePath('seo') . '/' . @$seoContents->image, getFileSize('seo')) : null;
return view('Template::contact', compact('pageTitle', 'user', 'sections', 'seoContents', 'seoImage'));
}
public function contactSubmit(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'subject' => 'required|string|max:255',
'message' => 'required',
]);
$request->session()->regenerateToken();
if (!verifyCaptcha()) {
$notify[] = ['error', 'Invalid captcha provided'];
return back()->withNotify($notify);
}
$random = getNumber();
$ticket = new SupportTicket();
$ticket->user_id = auth()->id() ?? 0;
$ticket->name = $request->name;
$ticket->email = $request->email;
$ticket->priority = Status::PRIORITY_MEDIUM;
$ticket->ticket = $random;
$ticket->subject = $request->subject;
$ticket->last_reply = Carbon::now();
$ticket->status = Status::TICKET_OPEN;
$ticket->save();
$adminNotification = new AdminNotification();
$adminNotification->user_id = auth()->user() ? auth()->user()->id : 0;
$adminNotification->title = 'A new contact message has been submitted';
$adminNotification->click_url = urlPath('admin.ticket.view', $ticket->id);
$adminNotification->save();
$message = new SupportMessage();
$message->support_ticket_id = $ticket->id;
$message->message = $request->message;
$message->save();
$notify[] = ['success', 'Ticket created successfully!'];
return to_route('ticket.view', [$ticket->ticket])->withNotify($notify);
}
public function policyPages($slug)
{
$policy = Frontend::where('slug', $slug)->where('data_keys', 'policy_pages.element')->firstOrFail();
$pageTitle = $policy->data_values->title;
$seoContents = $policy->seo_content;
$seoImage = @$seoContents->image ? frontendImage('policy_pages', $seoContents->image, getFileSize('seo'), true) : null;
return view('Template::policy', compact('policy', 'pageTitle', 'seoContents', 'seoImage'));
}
public function governingBody()
{
$pageTitle = "Governance";
return view('Template::governingBody', compact('pageTitle'));
}
public function changeLanguage($lang = null)
{
$language = Language::where('code', $lang)->first();
if (!$language) $lang = 'en';
session()->put('lang', $lang);
return back();
}
public function cookieAccept()
{
Cookie::queue('gdpr_cookie', gs('site_name'), 43200);
}
public function cookiePolicy()
{
$cookieContent = Frontend::where('data_keys', 'cookie.data')->first();
abort_if($cookieContent->data_values->status != Status::ENABLE, 404);
$pageTitle = 'Cookie Policy';
$cookie = Frontend::where('data_keys', 'cookie.data')->first();
return view('Template::cookie', compact('pageTitle', 'cookie'));
}
public function placeholderImage($size = null)
{
$imgWidth = explode('x', $size)[0];
$imgHeight = explode('x', $size)[1];
$text = $imgWidth . '×' . $imgHeight;
$fontFile = realpath('assets/font/solaimanLipi_bold.ttf');
$fontSize = round(($imgWidth - 50) / 8);
if ($fontSize <= 9) {
$fontSize = 9;
}
if ($imgHeight < 100 && $fontSize > 30) {
$fontSize = 30;
}
$image = imagecreatetruecolor($imgWidth, $imgHeight);
$colorFill = imagecolorallocate($image, 100, 100, 100);
$bgFill = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgFill);
$textBox = imagettfbbox($fontSize, 0, $fontFile, $text);
$textWidth = abs($textBox[4] - $textBox[0]);
$textHeight = abs($textBox[5] - $textBox[1]);
$textX = ($imgWidth - $textWidth) / 2;
$textY = ($imgHeight + $textHeight) / 2;
header('Content-Type: image/jpeg');
imagettftext($image, $fontSize, 0, $textX, $textY, $colorFill, $fontFile, $text);
imagejpeg($image);
imagedestroy($image);
}
public function maintenance()
{
$pageTitle = 'Maintenance Mode';
if (gs('maintenance_mode') == Status::DISABLE) {
return to_route('home');
}
$maintenance = Frontend::where('data_keys', 'maintenance.data')->first();
return view('Template::maintenance', compact('pageTitle', 'maintenance'));
}
}