ÿØÿà JFIF H H ÿÛ C GIF89;
| System: Linux srv913213 5.15.0-190-generic #200-Ubuntu SMP Fri Aug 7 15:06:04 UTC 2026 x86_64 Current Path : /var/www/yapwebtv.com/Modules_old/Api/Http/Controllers/ |
| Current File : /var/www/yapwebtv.com/Modules_old/Api/Http/Controllers/HomeController.php |
<?php
namespace Modules\Api\Http\Controllers;
use App\Enums\VisitorPageType;
use App\Reaction;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Config;
use Modules\Post\Entities\Category;
use Modules\Post\Entities\Comment;
use Validator;
use DB;
use Modules\Post\Entities\Post;
use Sentinel;
use Carbon\Carbon;
use URL;
use App\Traits\ApiReturnFormat;
use Modules\Appearance\Entities\ThemeSection;
use LaravelLocalization;
use App\VisitorTracker;
class HomeController extends Controller
{
use ApiReturnFormat;
public function homeContent(Request $request)
{
$primarySection = ThemeSection::where('is_primary', 1)->first();
if ($primarySection->status == 1):
$primarySectionPosts = Post::with(['category', 'image', 'user'])
->where('visibility', 1)
->where('status', 1)
->where('slider', '!=', 1)
->where('language', LaravelLocalization::setLocale() ?? settingHelper('default_language'))
->orderBY('id', 'desc')
->when(Sentinel::check() == false, function ($query) {
$query->where('auth_required', 0);
})
->limit(10)->get();
else:
$primarySectionPosts = [];
endif;
$sliderPosts = Post::with(['category', 'image', 'user'])
->where('visibility', 1)
->where('slider', 1)
->where('status', 1)
->where('language', LaravelLocalization::setLocale() ?? settingHelper('default_language'))
->when(Sentinel::check() == false, function ($query) {
$query->where('auth_required', 0);
})
->orderBY('id', 'desc')
->limit(5)->get();
$categorySections = ThemeSection::with('ad')
->with(['category'])
->where('is_primary', '<>', 1)->orderBy('order', 'ASC')
->where(function ($query) {
$query->where('language', LaravelLocalization::setLocale() ?? settingHelper('default_language'))->orWhere('language', null);
})
->get();
$categorySections->each(function ($section) {
$section->load('post');
});
$video_posts = Post::with('category', 'image', 'user')
->where('post_type', 'video')
->where('visibility', 1)
->where('status', 1)
->when(Sentinel::check() == false, function ($query) {
$query->where('auth_required', 0);
})
->orderBy('id', 'desc')
->where('language', LaravelLocalization::setLocale() ?? settingHelper('default_language'))
->limit(8)
->get();
$latest_posts = Post::with('category', 'image', 'user')
->where('visibility', 1)
->where('status', 1)
->when(Sentinel::check() == false, function ($query) {
$query->where('auth_required', 0);
})
->orderBy('id', 'desc')
->where('language', LaravelLocalization::setLocale() ?? settingHelper('default_language'))
->limit(6)
->get();
$totalPostCount = Post::where('visibility', 1)
->where('status', 1)
->when(Sentinel::check() == false, function ($query) {
$query->where('auth_required', 0);
})
->orderBy('id', 'desc')
->where('language', LaravelLocalization::setLocale() ?? settingHelper('default_language'))
->count();
$tracker = new VisitorTracker();
$tracker->page_type = \App\Enums\VisitorPageType::HomePage;
$tracker->url = \Request::url();
$tracker->source_url = \url()->previous();
$tracker->ip = \Request()->ip();
$tracker->agent_browser = UserAgentBrowser(\Request()->header('User-Agent'));
$tracker->save();
$homeContent['primarySection'] = $primarySection;
$homeContent['primarySectionPosts'] = $primarySectionPosts;
$homeContent['categorySections'] = $categorySections;
$homeContent['sliderPosts'] = $sliderPosts;
$homeContent['video_posts'] = $video_posts;
$homeContent['latest_posts'] = $latest_posts;
$homeContent['totalPostCount'] = $totalPostCount;
return $this->responseWithSuccess('Successfully data found', $homeContent);
}
public function homeContents(){
$language = Config::get('app.locale') ?? settingHelper('default_language');
$home_content['latest_posts'] = $this->imageUrlset($this->commentsCount($this->dateToHuman($this->latestPosts($language))));
$home_content['top_stories'] = $this->imageUrlset($this->commentsCount($this->dateToHuman($this->popularPost($language))));
$home_content['featured_posts'] = $this->featuredCategoryPost($language);
$home_content['trending_posts'] = $this->trendingPosts($language);
$home_content['tags'] = $this->tags();
foreach ($home_content['tags'] as $tag):
unset($tag->tags_count);
endforeach;
return $this->responseWithSuccess('Successfully data found', $home_content, 200);
}
private function popularPost($language) {
return Post::with('image','category:id,category_name,slug','user:id,first_name,last_name')
->where('visibility', 1)
->where('status', 1)
->where('language',$language)
->when(Sentinel::check() == false, function ($query) {
$query->where('auth_required', 0);
})
->select('id','category_id','image_id','user_id','title','slug','post_type','created_at')
->orderBy('total_hit','DESC')
->take(10)
->get();
}
private function latestPosts($language){
return Post::with('image','category:id,category_name,slug','user:id,first_name,last_name')
->where('visibility', 1)
->where('status', 1)
->where('language',$language)
->when(Sentinel::check() == false, function ($query) {
$query->where('auth_required', 0);
})
->select('id','category_id','image_id','user_id','title','slug','post_type','created_at')
->orderBy('id', 'desc')
->take(10)
->get();
}
private function featuredCategoryPost($language){
$categories = Category::where('is_featured', 1)->select('id','category_name','slug')->get();
foreach ($categories as $category):
$response[] =
array(
'id' => $category->id,
'category_name' => $category->category_name,
'slug' => $category->slug,
'posts' => $this->imageUrlset($this->commentsCount($this->dateToHuman($this->getFeaturedPosts($category->id, $language))))
);
endforeach;
if (!isset($response)):
$response[] = array();
endif;
return $response;
}
private function getFeaturedPosts($category, $language){
return Post::with('image','user:id,first_name,last_name')
->where('category_id', $category)
->where('visibility', 1)
->where('status', 1)
->where('language',$language)
->when(Sentinel::check() == false, function ($query) {
$query->where('auth_required', 0);
})
->select('id','category_id','image_id','user_id','title','slug','post_type','created_at')
->orderBy('id', 'desc')
->take(10)
->get();
}
private function trendingPosts($language){
$hitPosts = VisitorTracker::with('posts:id,category_id,image_id,user_id,title,slug,post_type,language,created_at','posts.image','posts.category:id,category_name,slug','posts.user:id,first_name,last_name')
->select(DB::raw('count(*) as hitsCount, slug'))->where('page_type',VisitorPageType::PostDetailPage)
->groupBy('slug')->orderBy('hitsCount', 'desc')
->where('created_at', '>=', Carbon::now()->subDay(7))
->take(10)
->get();
foreach ($hitPosts as $hitPost):
if (isset($hitPost['posts']->created_at)):
$hitPost['posts']->created = Carbon::parse($hitPost['posts']->created_at)->diffForHumans();
endif;
if (isset($hitPost['posts']->id)):
$hitPost['posts']->commentsCount = Comment::where('post_id', $hitPost['posts']->id)->where('comment_id', '=' , null)->count();
endif;
if (isset($hitPost['posts']->image)) {
if ($hitPost['posts']->image->disk == 's3') {
$s3Link = "https://s3." . Config::get('filesystems.disks.s3.region') . ".amazonaws.com/" . Config::get('filesystems.disks.s3.bucket') . "/";
$hitPost['posts']->image->original_image = $s3Link . $hitPost['posts']->image->original_image;
$hitPost['posts']->image->og_image = $s3Link . $hitPost['posts']->image->og_image;
$hitPost['posts']->image->big_image = $s3Link . $hitPost['posts']->image->big_image;
$hitPost['posts']->image->big_image_two = $s3Link . $hitPost['posts']->image->big_image_two;
$hitPost['posts']->image->medium_image = $s3Link . $hitPost['posts']->image->medium_image;
$hitPost['posts']->image->medium_image_two = $s3Link . $hitPost['posts']->image->medium_image_two;
$hitPost['posts']->image->medium_image_three = $s3Link . $hitPost['posts']->image->medium_image_three;
$hitPost['posts']->image->small_image = $s3Link . $hitPost['posts']->image->small_image;
$hitPost['posts']->image->thumbnail = $s3Link . $hitPost['posts']->image->thumbnail;
} else {
$hitPost['posts']->image->original_image = static_asset($hitPost['posts']->image->original_image);
$hitPost['posts']->image->og_image = static_asset($hitPost['posts']->image->og_image);
$hitPost['posts']->image->big_image = static_asset($hitPost['posts']->image->big_image);
$hitPost['posts']->image->big_image_two = static_asset($hitPost['posts']->image->big_image_two);
$hitPost['posts']->image->medium_image = static_asset($hitPost['posts']->image->medium_image);
$hitPost['posts']->image->medium_image_two = static_asset($hitPost['posts']->image->medium_image_two);
$hitPost['posts']->image->medium_image_three = static_asset($hitPost['posts']->image->medium_image_three);
$hitPost['posts']->image->small_image = static_asset($hitPost['posts']->image->small_image);
$hitPost['posts']->image->thumbnail = static_asset($hitPost['posts']->image->thumbnail);
}
unset($hitPost['posts']->image->id);
unset($hitPost['posts']->image->disk);
unset($hitPost['posts']->image->created_at);
unset($hitPost['posts']->image->updated_at);
}
endforeach;
$trending_post = array();
foreach($hitPosts as $key => $hitpost):
$trending_post[] = $hitpost->posts;
endforeach;
return $trending_post;
}
protected function tags(){
return VisitorTracker::select(DB::raw('count(*) as tags_count, slug'))->where('page_type',VisitorPageType::PostByTagsPage)
->groupBy('slug')->orderBy('tags_count', 'desc')->take(6)
->get();
}
protected function commentsCount($posts){
foreach ($posts as $post):
$post->commentsCount = Comment::where('post_id', $post->id)->where('comment_id', '=' , null)->count();
endforeach;
return $posts;
}
// protected function reactionCount($posts){
// foreach ($posts as $post):
// $post->reactionsCount = Reaction::where('post_id', $post->id)->count();
// endforeach;
// }
protected function dateToHuman($posts){
foreach ($posts as $post):
if (isset($post->created_at)):
$post->created = Carbon::parse($post->created_at)->diffForHumans();
endif;
endforeach;
return $posts;
}
protected function imageUrlset($posts)
{
foreach ($posts as $post) {
if (isset($post->image)) {
if ($post->image->disk == 's3') {
$s3Link = "https://s3." . Config::get('filesystems.disks.s3.region') . ".amazonaws.com/" . Config::get('filesystems.disks.s3.bucket') . "/";
$post->image->original_image = $s3Link . $post->image->original_image;
$post->image->og_image = $s3Link . $post->image->og_image;
$post->image->big_image = $s3Link . $post->image->big_image;
$post->image->big_image_two = $s3Link . $post->image->big_image_two;
$post->image->medium_image = $s3Link . $post->image->medium_image;
$post->image->medium_image_two = $s3Link . $post->image->medium_image_two;
$post->image->medium_image_three = $s3Link . $post->image->medium_image_three;
$post->image->small_image = $s3Link . $post->image->small_image;
$post->image->thumbnail = $s3Link . $post->image->thumbnail;
} else {
$post->image->original_image = static_asset($post->image->original_image);
$post->image->og_image = static_asset($post->image->og_image);
$post->image->big_image = static_asset($post->image->big_image);
$post->image->big_image_two = static_asset($post->image->big_image_two);
$post->image->medium_image = static_asset($post->image->medium_image);
$post->image->medium_image_two = static_asset($post->image->medium_image_two);
$post->image->medium_image_three = static_asset($post->image->medium_image_three);
$post->image->small_image = static_asset($post->image->small_image);
$post->image->thumbnail = static_asset($post->image->thumbnail);
}
}
unset($post->image->id);
unset($post->image->disk);
unset($post->image->created_at);
unset($post->image->updated_at);
}
return $posts;
}
}