196 lines
6.7 KiB
PHP
196 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Home;
|
|
|
|
use App\Models\Blog;
|
|
use App\Models\Comment;
|
|
use Livewire\Component;
|
|
use App\Models\Category;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Attributes\Rule;
|
|
use Livewire\Attributes\Layout;
|
|
use Illuminate\Support\Facades\Cookie;
|
|
|
|
class DetailNoBlog extends Component
|
|
{
|
|
public $categoryparentslug;
|
|
public $categoryunderparentslug;
|
|
public $slug;
|
|
|
|
public $blog;
|
|
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 $cookie;
|
|
public $pendingComments = [];
|
|
|
|
public function mount($categoryparentslug = null, $categoryunderparentslug = null, $slug = null)
|
|
{
|
|
$this->categoryparentslug = $categoryparentslug;
|
|
$this->categoryunderparentslug = $categoryunderparentslug;
|
|
$this->slug = $slug;
|
|
if (is_null($this->categoryparentslug) || is_null($this->categoryunderparentslug) || is_null($this->slug)) {
|
|
return abort('404');
|
|
}
|
|
if (Blog::where('slug', $this->slug)->first()) {
|
|
$this->blog = Blog::where('slug', $this->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);
|
|
} else {
|
|
return abort(404);
|
|
}
|
|
}
|
|
$this->blog_id = $this->blog->id;
|
|
} else {
|
|
return abort('404');
|
|
}
|
|
$this->categoryunderparentslug = Category::where('slug', $this->categoryunderparentslug)->first();
|
|
|
|
$this->blog->increment('views');
|
|
$this->slug = $slug;
|
|
$this->blog_id = $this->blog->id;
|
|
|
|
$this->lastNews = Blog::query()->where('status', 1)->where('notBlog', null)->orderBy('created_at', 'desc')->get()->take(5);
|
|
$this->lastNews2 = Blog::query()->where('status', 1)->where('notBlog', 1)->orderBy('created_at', 'desc')->get()->take(5);
|
|
$this->specialNews = Blog::query()->where('status', 1)->where('notBlog', null)->where('chosen', 1)->orderBy('created_at', 'desc')->get()->take(5);
|
|
}
|
|
|
|
#[Rule('required')]
|
|
public $image_url;
|
|
|
|
public function comment()
|
|
{
|
|
$this->validate();
|
|
$comment_id = 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->comment_message = null;
|
|
$this->comment_name = 'کاربر';
|
|
$this->dispatch('reset');
|
|
$newComment = Comment::find($comment_id->id);
|
|
$this->storeCommentInCookie($newComment->toArray());
|
|
}
|
|
|
|
public function reply($id, $parent_id)
|
|
{
|
|
if ($this->comment_message_reply) {
|
|
$comment_id = 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->comment_message_reply = null;
|
|
$this->comment_name_reply = 'کاربر';
|
|
$this->dispatch('reset');
|
|
|
|
$this->storeCommentInCookie($comment_id->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()
|
|
{
|
|
$this->comments = Comment::query()
|
|
->where('blog_id', $this->blog_id)
|
|
->where('status', 1)
|
|
->where('type', 0)
|
|
->whereHas('blog', function ($q) {
|
|
$q->where('status', 1);
|
|
})
|
|
->get();
|
|
|
|
$this->like_status = session()->get('blog-' . $this->blog_id);
|
|
|
|
$this->pendingComments = json_decode(Cookie::get('pending_comments', '[]'), true);
|
|
|
|
foreach ($this->pendingComments as $key => $comment) {
|
|
if ($this->blog->id != $comment['blog_id']) {
|
|
unset($this->pendingComments[$key]);
|
|
} else {
|
|
$checkCookie = Comment::where('id', $comment['id'])->first();
|
|
|
|
if (!is_null($checkCookie)) {
|
|
if ($checkCookie->status == 1 || $checkCookie->status == 2) {
|
|
Cookie::queue(Cookie::forget('pending_comments'));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$pendingCommentsCollection = collect($this->pendingComments)->map(function ($comment) {
|
|
if (!isset($comment['id'])) {
|
|
$comment['id'] = 'temp_' . uniqid();
|
|
}
|
|
|
|
$model = new Comment();
|
|
$model->forceFill($comment);
|
|
|
|
return $model;
|
|
});
|
|
|
|
$combinedCollection = $this->comments->merge($pendingCommentsCollection);
|
|
|
|
$flattenedArray = $combinedCollection
|
|
->map(function ($comment) {
|
|
return $comment->toArray();
|
|
})
|
|
->all();
|
|
|
|
usort($flattenedArray, function ($a, $b) {
|
|
return $a['id'] <=> $b['id'];
|
|
});
|
|
|
|
$this->comments = $flattenedArray;
|
|
|
|
return view('livewire.home.detail-no-blog');
|
|
}
|
|
}
|