Files
productalert/app/Http/Controllers/Front/HomeController.php

217 lines
8.4 KiB
PHP

<?php
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\CountryLocale;
use App\Models\Post;
use Artesaos\SEOTools\Facades\JsonLdMulti;
use Artesaos\SEOTools\Facades\OpenGraph;
use Artesaos\SEOTools\Facades\SEOMeta;
use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index(Request $request)
{
SEOTools::metatags();
SEOTools::twitter();
SEOTools::opengraph();
SEOTools::jsonLd();
SEOTools::setTitle('Top Product Reviews, Deals & New Launches');
SEOTools::setDescription('Explore ProductAlert for in-depth product reviews and incredible deals. We cover Beauty, Tech, Home Appliances, Health & Fitness, Parenting, and more.');
$country = strtolower($request->session()->get('country'));
return redirect()->route('home.country', ['country' => $country]);
}
public function country(Request $request, $country)
{
$country_locale = CountryLocale::where('slug', $country)->first();
if (! is_null($country_locale)) {
$request->session()->put('view_country_locale', $country_locale);
$featured_posts = Post::select('posts.*')
->join('post_categories', 'posts.id', '=', 'post_categories.post_id')
->join('categories', 'post_categories.category_id', '=', 'categories.id')
->whereNotNull('post_categories.id')
->whereNotNull('categories.id')
->where('categories.country_locale_slug', $country_locale->slug)
->where('posts.status', 'publish')
->orderBy('posts.publish_date', 'desc')
->take(3)
->get();
$latest_posts = Post::select('posts.*')
->join('post_categories', 'posts.id', '=', 'post_categories.post_id')
->join('categories', 'post_categories.category_id', '=', 'categories.id')
->whereNotNull('post_categories.id')
->whereNotNull('categories.id')
->where('categories.country_locale_slug', $country_locale->slug)
->whereNotIn('posts.id', $featured_posts->pluck('id')->toArray())
->where('posts.status', 'publish')
->orderBy('posts.publish_date', 'desc')
->distinct()
->take(10)
->get();
if ($latest_posts->count() <= 0) {
SEOMeta::setRobots('noindex');
}
SEOTools::metatags();
SEOTools::twitter();
SEOTools::opengraph();
SEOTools::jsonLd();
$country_name = get_country_name_by_iso($country_locale->country_iso);
SEOTools::setTitle("Your {$country_name} Guide to Product Reviews & Top Deals");
SEOTools::setDescription("Discover trusted product reviews and unbeatable deals at ProductAlert {$country_name}, your local guide to smart shopping.");
return view('front.country', compact('country_locale', 'featured_posts', 'latest_posts')
);
}
return redirect()->route('home.country', ['country' => config('platform.general.fallback_country_slug')]);
}
public function countryCategory(Request $request, $country, $category)
{
$country_locale = CountryLocale::where('slug', $country)->first();
if (is_null($country_locale)) {
abort(404);
}
$category = Category::where('slug', $category)->where('enabled', true)->first();
if (is_null($category)) {
abort(404);
}
$request->session()->put('view_country_locale', $country_locale);
$latest_posts = Post::with('post_categories')->select('posts.*')
->join('post_categories', 'posts.id', '=', 'post_categories.post_id')
->join('categories', 'post_categories.category_id', '=', 'categories.id')
->whereNotNull('post_categories.id')
->whereNotNull('categories.id')
->where('categories.country_locale_slug', $country_locale->slug)
->where('categories.id', $category->id)
->where('posts.status', 'publish')
->orderBy('posts.publish_date', 'desc')
->distinct()
->paginate(15);
if ($latest_posts->count() <= 0) {
SEOMeta::setRobots('noindex');
}
SEOTools::metatags();
SEOTools::twitter();
SEOTools::opengraph();
SEOTools::jsonLd();
$this_month = now()->format('F Y');
$country_name = get_country_name_by_iso($country_locale->country_iso);
//dd($category->type);
if ($category->type == 'review') {
SEOTools::setTitle("Top {$category->name} Reviews for {$this_month} in {$country_name}");
} elseif ($category->type == 'deals') {
SEOTools::setTitle("{$this_month} Latest Deals, Coupon Codes & Vouchers for {$country_name}");
} elseif ($category->type == 'launch') {
SEOTools::setTitle("New {$this_month} Product Launches in {$country_name}");
}
$category_name = strtolower($category->name);
SEOTools::setDescription($category->description);
return view('front.country_category', compact('country_locale', 'category', 'latest_posts'));
}
public function all(Request $request, $country)
{
$country_locale = CountryLocale::where('slug', $country)->first();
$request->session()->put('view_country_locale', $country_locale);
$latest_posts = Post::with('post_categories')->select('posts.*')
->join('post_categories', 'posts.id', '=', 'post_categories.post_id')
->join('categories', 'post_categories.category_id', '=', 'categories.id')
->whereNotNull('post_categories.id')
->whereNotNull('categories.id')
->where('categories.country_locale_slug', $country_locale->slug)
->where('posts.status', 'publish')
->orderBy('posts.publish_date', 'desc')
->distinct()
->paginate(15);
if ($latest_posts->count() <= 0) {
SEOMeta::setRobots('noindex');
}
SEOTools::metatags();
SEOTools::twitter();
SEOTools::opengraph();
SEOTools::jsonLd();
$country_name = get_country_name_by_iso($country_locale->country_iso);
SEOTools::setTitle("Find Product Reviews and Best Deals for {$country_name}");
SEOTools::setDescription("Discover the latest product reviews and unbeatable deals at ProductAlert, your guide to shopping in {$country_name}. Stay on top of fresh product updates.");
return view('front.country_all', compact('country_locale', 'latest_posts'));
}
public function post(Request $request, $country, $post_slug)
{
$post = Post::where('slug', $post_slug)->where('status', 'publish')->first();
if (! is_null($post)) {
$request->session()->put('view_country_locale', $post->post_category->category->country_locale);
SEOMeta::setTitle($post->title);
SEOMeta::setDescription($post->excerpt);
SEOMeta::addMeta('article:published_time', $post->publish_date, 'property');
SEOMeta::addMeta('article:section', $post->post_category->category->name, 'property');
OpenGraph::setDescription($post->excerpt);
OpenGraph::setTitle($post->title);
OpenGraph::setUrl(url()->current());
OpenGraph::addProperty('type', 'article');
OpenGraph::addProperty('locale', $post->post_category->category->country_locale->i18n);
OpenGraph::addImage($post->featured_image);
$jsonld_multi = JsonLdMulti::newJsonLd();
$jsonld_multi->setTitle($post->title)
->setDescription($post->excerpt)
->setType('Article')
->addImage($post->featured_image)
->addValue('author', $post->author->name)
->addValue('datePublished', $post->publish_date->format('Y-m-d'))
->addValue('dateCreated', $post->publish_date->format('Y-m-d'))
->addValue('dateModified', $post->updated_at->format('Y-m-d'))
->addValue('description', $post->excerpt)
->addValue('articleBody', trim(preg_replace('/\s\s+/', ' ', strip_tags($post->html_body))));
return view('front.post', compact('post'));
}
abort(404);
}
}