Files
ahrommag/app/Livewire/Home/DetailPage.php
2025-11-16 12:43:07 +03:30

234 lines
7.4 KiB
PHP

<?php
namespace App\Livewire\Home;
use App\Models\Ads;
use App\Models\Blog;
use App\Models\Page;
use App\Models\Comment;
use Livewire\Component;
use Livewire\Attributes\On;
use Livewire\Attributes\Rule;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Session;
use Illuminate\Support\Facades\Cookie;
class DetailPage extends Component
{
public $blog;
public $slug;
public $blog_id;
public $like_status;
public $comments;
public $comment_name = 'کاربر';
#[Rule('required')]
public $comment_message;
public $comment_name_reply = 'کاربر';
public $comment_message_reply;
public $lastNews;
public $lastNews2;
public $specialNews;
public $questions = [];
public $breadcrumbs = [];
public $allComments = [];
#[Rule('required')]
public $image_url;
public $cookie;
public $pendingComments = [];
public $ads = [];
public $ip = '';
public $adscount = [];
public function mount($slug)
{
$this->blog = Blog::with('questions') ->where('slug', $slug) ->where('status', 1) ->first();
if (is_null($this->blog)) {
$softDeletedBlog = Blog::withTrashed()->where('slug', $slug)->whereNotNull('deleted_at')->first();
if (!is_null($softDeletedBlog)) {
return abort(410);
}
return abort(404);
}
$this->blog->increment('views');
$this->slug = $slug;
$this->blog_id = $this->blog->id;
$this->questions = $this->blog->questions;
$allBlogs = Blog::where('status', 1)->orderBy('published_at', 'desc')->get();
$this->lastNews = $allBlogs->whereNull('notBlog')->take(5);
$this->lastNews2 = $allBlogs->where('notBlog', 1)->take(5);
$this->specialNews = $allBlogs->whereNull('notBlog')->where('chosen', 1)->take(5);
$this->loadAds($slug);
}
protected function loadAds($currentBlogSlug)
{
$allAds = Ads::where('status', 1)
->whereJsonContains('pages', (string)1)
->orderBy('orders', 'desc')
->get();
$sidebarAds = $allAds->filter(function($ad) {
return collect([9, 10])->contains(function($subcat) use ($ad) {
return in_array($subcat, $ad->subcategories ?? []);
});
});
$contentAds = $allAds->filter(function($ad) {
return collect([13, 14])->contains(function($subcat) use ($ad) {
return in_array($subcat, $ad->subcategories ?? []);
});
});
$sidebarAds = $this->filterUnauthorizedAds($sidebarAds, $currentBlogSlug);
$contentAds = $this->filterUnauthorizedAds($contentAds, $currentBlogSlug);
$this->ads = [
'sidebar' => $sidebarAds->first(),
'content' => $contentAds->take(5)
];
$this->adscount = $contentAds->count() + ($sidebarAds->count() > 0 ? 1 : 0);
}
protected function filterUnauthorizedAds($ads, $currentBlogSlug)
{
return $ads->reject(function ($ad) use ($currentBlogSlug) {
if (empty($ad->unauthorized)) {
return false;
}
$unauthorizedSlugs = $ad->unauthorized;
if (is_array($unauthorizedSlugs)) {
return collect($unauthorizedSlugs)->contains(function ($item) use ($currentBlogSlug) {
return (is_array($item) && isset($item['unauthorize']) && $item['unauthorize'] === $currentBlogSlug) ||
($item === $currentBlogSlug);
});
}
return is_string($unauthorizedSlugs) && $unauthorizedSlugs === $currentBlogSlug;
});
}
public function comment()
{
$this->validate();
$comment = Comment::create([
'name' => $this->comment_name,
'message' => $this->comment_message,
'blog_id' => $this->blog_id,
'ip' => request()->ip(),
'user_agent' => request()->userAgent(),
'image_url' => $this->image_url
]);
$this->reset(['comment_message', 'comment_name']);
$this->dispatch('reset');
$this->storeCommentInCookie($comment->toArray());
}
public function reply($id, $parent_id)
{
if ($this->comment_message_reply) {
$comment = Comment::create([
'name' => $this->comment_name_reply,
'message' => $this->comment_message_reply,
'blog_id' => $this->blog_id,
'ip' => request()->ip(),
'user_agent' => request()->userAgent(),
'parent_id' => $id,
'type' => 2,
'image_url' => $this->image_url
]);
Comment::where('id', $id)->increment('reply');
$this->reset(['comment_message_reply', 'comment_name_reply']);
$this->dispatch('reset');
$this->storeCommentInCookie($comment->toArray());
}
}
private function storeCommentInCookie($comment)
{
$comments = json_decode(Cookie::get('pending_comments', '[]'), true);
$comments[] = $comment;
Cookie::queue('pending_comments', json_encode($comments), 60 * 24 * 7);
}
public $loadedimage;
public $loadedtitle;
public $loadedslug;
public $loadedpreview;
public $loadedContent;
public $isLoading = false;
#[On('loadBlogContent')]
public function loadBlogContent($slug)
{
$this->isLoading = true;
$blog = Blog::where('slug', $slug)->first();
if ($blog) {
$this->loadedContent = $blog->subject;
$this->loadedslug = $slug;
$this->loadedtitle = $blog->subject;
$this->loadedimage = $blog->image;
$this->loadedpreview = $blog->preview;
$this->dispatch('blogContentLoaded', [
'slug' => $slug,
'title' => $blog->subject,
'image' => $blog->image,
'preview' => $blog->preview,
]);
} else {
$this->loadedContent = 'محتوا یافت نشد.';
}
$this->isLoading = false;
}
#[Layout('components.layouts.app')]
public function render()
{
$approvedComments = Comment::with('blog')
->where('blog_id', $this->blog_id)
->where('status', 1)
->where('type', 0)
->whereHas('blog', fn($q) => $q->where('status', 1))
->get();
$this->pendingComments = collect(json_decode(Cookie::get('pending_comments', '[]'), true))
->filter(fn($comment) => $this->blog->id == $comment['blog_id'])
->each(function ($comment) {
if (isset($comment['id']) && $check = Comment::find($comment['id'])) {
if ($check->status == 1 || $check->status == 2) {
Cookie::queue(Cookie::forget('pending_comments'));
}
}
});
$this->comments = $approvedComments
->merge($this->pendingComments->map(function ($comment) {
$model = new Comment();
$model->forceFill($comment + ['id' => $comment['id'] ?? 'temp_' . uniqid()]);
return $model;
}))
->sortBy('id')
->values()
->toArray();
$this->like_status = session()->get('blog-' . $this->blog_id);
return view('livewire.home.detail-page');
}
}