Files
ahrommag/app/Models/IndexPage.php
2025-11-16 12:43:07 +03:30

359 lines
9.4 KiB
PHP

<?php
namespace App\Livewire\Panel;
use DOMXPath;
use DOMDocument;
use App\Models\Blog;
use App\Models\Image;
use App\Models\Heading;
use Livewire\Component;
use App\Models\Category;
use Carbon\Carbon;
use Livewire\Attributes\On;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Url;
use Livewire\WithPagination;
use Livewire\WithoutUrlPagination;
use Illuminate\Database\Eloquent\JsonEncodingException;
class IndexPage extends Component
{
use WithPagination, WithoutUrlPagination;
public $blogs = [];
public $categories;
public $headings;
public $comment_message = [];
public $currentCategory = 'all';
public function checkHeadingMount()
{
$blogs = Blog::orderBy('id', 'Desc')->get();
foreach ($blogs as $blog) {
$existingHeading = Heading::where('blog_id', $blog->id)->first();
if ($existingHeading) {
continue;
}
$htmlContent = $blog->content;
$dom = new DOMDocument();
@$dom->loadHTML(mb_convert_encoding($htmlContent, 'HTML-ENTITIES', 'UTF-8'));
$xpath = new DOMXPath($dom);
$headings = $xpath->query('//h1 | //h2 | //h3 | //h4 | //h5 | //h6');
foreach ($headings as $heading) {
$headingText = $heading->nodeValue;
$tagType = $heading->nodeName;
$nextNode = $heading->nextSibling;
while ($nextNode && !in_array($nextNode->nodeName, ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])) {
$nextNode = $nextNode->nextSibling;
}
if(strlen($headingText) > 3){
Heading::create([
'blog_id' => $blog->id,
'content' => $headingText ,
'typeTag' => $tagType,
]);
}
}
}
}
public function approveHeading($headingId)
{
$heading = Heading::find($headingId);
$heading->update(['status' => 1]);
// ...
}
public function rejectHeading($headingId)
{
$heading = Heading::find($headingId);
$heading->update(['status' => 2]);
// ...
}
public function approveHeadingexpert($headingId)
{
$heading = Heading::find($headingId);
$heading->update(['content_expert_status' => 1]);
// ...
}
public function rejectHeadingexpert($headingId)
{
$heading = Heading::find($headingId);
$heading->update(['content_expert_status' => 2]);
// ...
}
public function makeMessage($headingId, $key)
{
$heading = Heading::find($headingId);
if ($heading) {
$textmessage = $this->comment_message[$key] ?? null;
if ($textmessage) {
if (!mb_check_encoding($textmessage, 'UTF-8')) {
$textmessage = utf8_encode($textmessage);
}
$time = Carbon::now();
$headingMessageBefore = $heading->content_expert_message;
if (empty($headingMessageBefore)) {
$headingMessageBefore = [];
} elseif (!is_array($headingMessageBefore)) {
$headingMessageBefore = [$headingMessageBefore];
}
$headingMessageBefore[] = ['message' => $textmessage, 'time' => $time];
$headingMessageBefore = array_map(function ($message) {
if (!mb_check_encoding($message['message'], 'UTF-8')) {
$message['message'] = utf8_encode($message['message']);
}
return $message;
}, $headingMessageBefore);
}
}
}
public function headingStatus( $value, $value2)
{
$blog = Blog::find($value);
if(!is_null($blog)){
$blog->update([
'headingCheck' => $value2 ,
]);
}
}
public function imageStatus($value , $value2)
{
$blog = Blog::find($value);
if(!is_null($blog)){
$blog->update([
'imageCheck' => $value2 ,
]);
}
}
public $imagesblog = [];
public $wModalsOpened = [];
public $loadedBlogs = [];
public function checkImageMountForBlog($blogId, $modalid)
{
if (isset($this->loadedBlogs[$blogId])) {
$this->wModalsOpened[] = $modalid;
$this->dispatch('openthemodal', $modalid);
return;
}
$blog = Blog::find($blogId);
if (!$blog) {
return;
}
$imageName = $blog->image;
$existingImage = Image::where('blog_id', $blogId)
->where('imagePath', $imageName)
->first();
if (is_null($existingImage)) {
Image::create([
'blog_id' => $blogId,
'imagePath' => $imageName,
'status' => 0
]);
}
$content = $blog->content;
if (empty($content)) {
return;
}
$pattern = '/<img[^>]+src="([^">]+)"/i';
preg_match_all($pattern, $content, $matches);
$extractedImages = $matches[1] ?? [];
$extractedImages = array_map(function ($imageUrl) {
$parts = explode('/images/', $imageUrl);
return end($parts);
}, $extractedImages);
$existingImages = Image::where('blog_id', $blogId)->get();
$existingImagePaths = $existingImages->pluck('imagePath')->toArray();
$imagesToDelete = array_diff($existingImagePaths, $extractedImages);
if (!empty($imagesToDelete)) {
Image::where('blog_id', $blogId)
->whereIn('imagePath', $imagesToDelete)
->delete();
}
foreach ($extractedImages as $imagePath) {
$existingImage = Image::where('blog_id', $blogId)
->where('imagePath', $imagePath)
->first();
if (is_null($existingImage)) {
Image::create([
'blog_id' => $blogId,
'imagePath' => $imagePath,
'status' => 0
]);
}
}
$this->loadedBlogs[$blogId] = true;
$this->wModalsOpened[] = $modalid;
$this->imagesblog = $blog->images;
$this->dispatch('openthemodal', $modalid);
}
public function approveImage($imageId)
{
$image = Image::find($imageId);
$image->update(['status' => 1]);
}
public function rejectImage($imageId)
{
$image = Image::find($imageId);
$image->update(['status' => 2]);
}
public function delete($id)
{
$blog = Blog::where("id", $id)->first();
if($blog){
$blog->delete();
}
}
public function status($id , $likey = false)
{
$blog= Blog::find($id);
if($blog->status==1){
$blog->update(["status"=>0]);
}else{
$blog->update(["status"=>1]);
}
}
public function setCategory($category)
{
$this->currentCategory = $category;
$this->resetPage();
}
public function refactor($id)
{
$blog = Blog::withTrashed()->find($id);
if ($blog && $blog->trashed()) {
$blog->restore();
return response()->json(['message' => 'Blog restored successfully!']);
} else {
return response()->json(['message' => 'Blog not found or is not soft deleted'], 404);
}
}
public function mount(){
$this->checkHeadingMount();
}
#[Layout('components.layouts.panel.master')]
public function render()
{
$query = Blog::orderBy('created_at', 'Desc');
if ($this->currentCategory == 'publish') {
$query->where('status', 1);
} elseif ($this->currentCategory == 'draft') {
$query->where('status', 0);
} elseif ($this->currentCategory == 'justtrashedlaravel') {
$query->onlyTrashed();
} elseif ($this->currentCategory !== 'all') {
$query->whereHas('category', function ($q) {
$q->where('id', $this->currentCategory);
});
}
$paginatedBlogs = $query->paginate(10);
$this->categories = Category::all();
$headings = Heading::all();
$blogHeadingsMap = [];
foreach ($paginatedBlogs as $blog) {
if (!isset($blogHeadingsMap[$blog->id])) {
$blogHeadingsMap[$blog->id] = [
'blog' => $blog,
'headings' => [],
'images' => []
];
}
}
foreach ($headings as $head) {
if (isset($blogHeadingsMap[$head->blog_id])) {
$blogHeadingsMap[$head->blog_id]['headings'][] = $head;
}
}
$this->blogs = array_values($blogHeadingsMap);
dd($this->blogs);
return view('livewire.panel.index-page', [
'blogs' => $this->blogs,
'paginatedBlogs' => $paginatedBlogs,
]);
}
}