KUJUNTI.ID MINISH3LL
Path : /var/www/html/phkaynews-v2/app/Http/Livewire/Admin/
(S)h3ll Cr3at0r :
F!le Upl0ad :

B-Con CMD Config cPanel C-Rdp D-Log Info Jump Mass Ransom Symlink vHost Zone-H

Current File : /var/www/html/phkaynews-v2/app/Http/Livewire/Admin/PostManage.php


<?php

namespace App\Http\Livewire\Admin;

use App\Models\Category;
use App\Models\Post;
use Carbon\Carbon;
use Livewire\Component;
use Jantinnerezo\LivewireAlert\LivewireAlert;
use Livewire\WithPagination;
use Livewire\WithFileUploads;

class PostManage extends Component
{
    use LivewireAlert;
    use WithFileUploads;
    use WithPagination;
    protected $paginationTheme = 'bootstrap';

    public $title, $keyword, $description, $feature_image, $category_ids, $public = true;
    public $no_image = 'backend/images/no-thumbnail.jpg', $old_image, $update_image;
    public $s, $frm = false, $edit = false, $item, $per_page, $trashes = false, $total_trash, $trash_all = false, $restore = false;
    public $readyToLoadPost = false;
    public $sortField = 'created_at', $sortDirection = 'DESC';
    public $limited = 10, $show_limit = [10, 25, 50, 100];
    public $s_date, $e_date, $date, $_title, $user_name, $filter = false, $post;

    protected $queryString = [
        'sortField' => ['except' => 'created_at'],
        'sortDirection' => ['except' => 'DESC', 'as' => 'direction'],
        'limited' => ['except' => 10],
        'date' => ['except' => ''],
        '_title' => ['except' => ''],
        'user_name' => ['except' => ''],
        'trashes' => ['except' => false],
        'post' => ['except' => ''],
    ];
    protected $listeners = [
        'confirmed',
        'cancelled',
        "startDate" => 'getSelectedStartDate',
        "endDate" => 'getSelectedEndDate',
    ];

    public function mount()
    {
        $this->total_trash = Post::onlyTrashed()->count();
        if (!empty($this->post)) {
            $this->editItem($this->post);
        }
        if (!empty($this->date)) {
            $this->_filter();
        }
    }

    public function updatingS(): void
    {
        $this->gotoPage(1);
    }
    public function updatingTrashes(): void
    {
        $this->gotoPage(1);
        $this->filter = false;
        if (!empty($this->date)) {
            if (is_array($this->date)) {
                $this->date = [];
                $this->reset([
                    's_date',
                    'e_date',
                    '_title',
                    'user_name'
                ]);
            } else {
                $this->date = null;
            }
        }
    }

    public function getSelectedStartDate($d)
    {
        $this->s_date = $d;
    }
    public function getSelectedEndDate($d)
    {
        $this->e_date = $d;
    }

    public function loadingPost()
    {
        $this->readyToLoadPost = true;
    }

    public function render()
    {
        if ($this->trashes) {
            $posts = Post::search('title', $this->s)->onlyTrashed()->orderBy($this->sortField, $this->sortDirection)->paginate($this->limited);
        } else {
            if ($this->s_date != null) {
                $this->date = [$this->s_date, $this->e_date];
            }
            if ($this->s != null) {
                $posts = Post::search('title', $this->s)->orderBy($this->sortField, $this->sortDirection)->paginate($this->limited);
            } else {
                $posts = Post::filterDate($this->date, $this->_title, $this->user_name)->orderBy($this->sortField, $this->sortDirection)->paginate($this->limited);
            }
        }
        if (count($posts) > $this->limited) {
            $this->per_page = $posts->perPage();
        }
        return view('livewire.admin.post-manage', [
            'posts' => $this->readyToLoadPost ? $posts : [],
            'categories' => Category::all()
        ])->layout('layouts.admin');
    }

    public function sortBy($field)
    {
        if ($this->sortField === $field) {
            $this->sortDirection = $this->sortDirection === 'ASC' ? 'DESC' : 'ASC';
        } else {
            $this->sortDirection = 'ASC';
        }
        $this->sortField = $field;
    }

    public function _filter()
    {
        $this->s = '';
        $this->filter = !$this->filter;
        if ($this->filter == false) {
            if (is_array($this->date)) {
                $this->date = [];
            } else {
                $this->date = null;
            }
        }
    }

    public function pkmDate($date = null)
    {
        if (!empty($this->s_date) || !empty($this->e_date)) {
            $this->reset([
                's_date',
                'e_date',
                '_title',
                'user_name'
            ]);
        }
        switch ($date) {
            case 'today':
                $this->date = Carbon::now()->format('Y-m-d');
                break;
            case 'yesterday':
                $this->date = Carbon::yesterday()->format('Y-m-d');
                $this->gotoPage(1);
                break;
            case 'thisweek':
                $this->date = [Carbon::now()->subWeek()->format('Y-m-d'), Carbon::now()->format('Y-m-d')];
                $this->gotoPage(1);
                break;
            case 'lastweek':
                $this->date = [Carbon::now()->subWeek()->subWeeks(1)->format('Y-m-d'), Carbon::now()->subWeek()->format('Y-m-d')];
                $this->gotoPage(1);
                break;
            case 'thismonth':
                $this->date = [Carbon::now()->subMonth()->format('Y-m-d'), Carbon::now()->format('Y-m-d')];
                $this->gotoPage(1);
                break;
            case 'lastmonth':
                $this->date = [Carbon::now()->subMonth()->subMonths(1)->format('Y-m-d'), Carbon::now()->subMonth()->format('Y-m-d')];
                $this->gotoPage(1);
                break;
            default:
                $this->date = null;
                $this->gotoPage(1);
                break;
        }
    }

    public function submit()
    {
        if ($this->edit) {
            $this->validate([
                'title' => 'required',
                'keyword' => 'required'
            ]);
            if (!empty($this->update_image)) {
                $this->validate([
                    'update_image' => 'image|max:1024'
                ]);
                $old_img = public_path($this->old_image); //is get path old image 
                if (file_exists($old_img)) {
                    unlink($old_img);
                }
                $this->old_image = '';
                $update_image = $this->update_image->store('thumbnails');
            }
            if (!empty($this->feature_image)) {
                $update_image = $this->feature_image->store('thumbnails');
            }
            $this->item->update([
                'title' => $this->title,
                'keyword' => $this->keyword,
                'description' => $this->description,
                'public' => $this->public,
                'feature_image' => empty($this->old_image) ? $update_image : $this->old_image
            ]);
            $this->item->categories()->sync($this->category_ids);
            $action = 'update';
        } else {

            $this->validate([
                'title' => 'required|min:15',
                'keyword' => 'required',
                'feature_image' => 'image|max:1024'
            ]);
            if (empty($this->category_ids)) {
                return $this->alert('error', 'Your item require categories', [
                    'position' =>  'center',
                    'toast' =>  true,
                    'showCancelButton' =>  false,
                    'showConfirmButton' =>  false,
                    'timerProgressBar' => true,
                ]);
            }
            $file = $this->feature_image->store('thumbnails');
            $post = Post::create([
                'title' => $this->title,
                'keyword' => $this->keyword,
                'description' => $this->description,
                'feature_image' => $file,
                'public' => $this->public,
                'user_id' => auth()->user()->id
            ]);
            $post->categories()->attach($this->category_ids);
            $action = 'create';
            $this->gotoPage(1);
        }
        $this->formOpen();
        $this->alert('success', 'Your item has been ' . $action . ' successfully.', [
            'position' =>  'top',
            'toast' =>  true,
            'showCancelButton' =>  false,
            'showConfirmButton' =>  false,
            'timerProgressBar' => true,
        ]);
    }

    public function formOpen()
    {
        $this->frm = !$this->frm;
        if ($this->frm == false) {
            $this->reset([
                'title',
                'keyword',
                'public',
            ]);
            if (!empty($this->old_image)) {
                $this->old_image = $this->no_image;
            }
            $this->resetValidation();
            $this->dispatchBrowserEvent('descriptionEvent', ['description' => '']);
            $this->dispatchBrowserEvent('categoryEvent', ['category_ids' => null]);
            $this->post = null;
            $this->loadingPost();
        }
        if ($this->edit) {
            $this->edit = false;
        }
    }

    public function editItem($id)
    {
        $this->formOpen();
        $post = Post::findOrFail($id);
        $this->post = $post->id;
        $this->edit = !$this->edit;
        $this->item = $post;
        $this->old_image = $post->feature_image;
        $this->title = $post->title;
        $this->keyword = $post->keyword;
        $this->public = $post->public;
        $this->description = $post->description;
        $this->category_ids = $post->post_categories->pluck('category_id')->toArray();
        $this->dispatchBrowserEvent('descriptionEvent', ['description' => $this->description]);
        $this->dispatchBrowserEvent('categoryEvent', ['category_ids' => $this->category_ids]);
    }

    public function changePublish($id)
    {
        $post = Post::findOrFail($id);
        $post->update([
            'public' => !$post->public
        ]);
        if ($post->public) {
            $status = 'On';
        } else {
            $status = 'Off';
        }
        $this->alert('success', 'Your Post has been publish ' . $status . ' successfully.', [
            'position' =>  'top',
            'toast' =>  true,
            'showCancelButton' =>  false,
            'showConfirmButton' =>  false,
            'timerProgressBar' => true,
        ]);
    }

    public function cancelled()
    {
        $this->alert('info', 'Cancel');
    }

    public function confirmed()
    {
        if ($this->trashes) {
            if ($this->restore) {
                Post::onlyTrashed()->restore();
                $this->total_trash = 0;
                $this->trashes = false;
                $action = 'restore all';
            } else {
                if ($this->trash_all) {
                    $thumb_images = Post::onlyTrashed()->get()->pluck('feature_image');
                    foreach ($thumb_images as $image) {
                        $d_image = public_path($image);
                        if (file_exists($d_image)) {
                            unlink($d_image);
                        }
                    }
                    Post::onlyTrashed()->forceDelete();
                    $this->total_trash = 0;
                    $this->trashes = false;
                    $action = "Empty trashes";
                } else {
                    if ($this->item->forceDelete()) {
                        $old_img = public_path($this->item->feature_image); //is get path old image 
                        if (file_exists($old_img)) {
                            unlink($old_img);
                        }
                        $this->item->categories()->detach();
                        $this->total_trash -= 1;
                    }
                    $action = "delete";
                }
            }
        } else {
            $this->item->delete();
            $this->total_trash += 1;
            $action = "move to trash";
        }

        $this->alert('success', 'Your item has been ' . $action . ' successfully.', [
            'position' =>  'top',
            'toast' =>  true,
            'showCancelButton' =>  false,
            'showConfirmButton' =>  false,
            'timerProgressBar' => true,
        ]);
    }

    public function pkmRestore($id)
    {
        $post = Post::onlyTrashed()->findOrFail($id);
        $post->restore();
        $this->total_trash -= 1;
        if ($this->trash_all == 0) {
            $this->trashes = false;
        }
        $this->alert('success', 'Your item has been restore successfully.', [
            'position' =>  'top',
            'toast' =>  true,
            'showCancelButton' =>  false,
            'showConfirmButton' =>  false,
            'timerProgressBar' => true,
        ]);
    }

    public function restoreAll()
    {
        $this->restore = true;
        $this->confirm("You won't be able to revert this!", [
            'toast' => false,
            'position' => 'center',
            'showConfirmButton' => true,
            'onConfirmed' => 'confirmed',
            'onCancelled' => 'cancelled',
            'confirmButtonText' => 'Yes, restore it!',
            'showCancelButton' => !0,
            'cancelButtonText' => "No, cancel!",
            'customClass' => [
                'confirmButton' => 'btn btn-info mt-2',
                'cancelButton' => 'btn btn-danger ml-2 mt-2'
            ],
            'buttonsStyling' => false,
        ]);
    }

    public function emptyTrash()
    {
        $this->trash_all = !$this->trash_all;
        $this->confirm("You won't be able to revert this!", [
            'toast' => false,
            'position' => 'center',
            'showConfirmButton' => true,
            'onConfirmed' => 'confirmed',
            'onCancelled' => 'cancelled',
            'confirmButtonText' => 'Yes, delete it!',
            'showCancelButton' => !0,
            'cancelButtonText' => "No, cancel!",
            'customClass' => [
                'confirmButton' => 'btn btn-success mt-2',
                'cancelButton' => 'btn btn-danger ml-2 mt-2'
            ],
            'buttonsStyling' => false,
        ]);
    }

    public function deleted($id)
    {
        $this->item = Post::onlyTrashed()->findOrFail($id);
        $this->confirm("You won't be able to revert this!", [
            'toast' => false,
            'position' => 'center',
            'showConfirmButton' => true,
            'onConfirmed' => 'confirmed',
            'onCancelled' => 'cancelled',
            'confirmButtonText' => 'Yes, delete it!',
            'showCancelButton' => !0,
            'cancelButtonText' => "No, cancel!",
            'customClass' => [
                'confirmButton' => 'btn btn-success mt-2',
                'cancelButton' => 'btn btn-danger ml-2 mt-2'
            ],
            'buttonsStyling' => false,
        ]);
    }

    public function destroy(Post $post)
    {
        $this->item = $post;
        $this->confirm("You won't be able to revert this!", [
            'toast' => false,
            'position' => 'center',
            'showConfirmButton' => true,
            'onConfirmed' => 'confirmed',
            'onCancelled' => 'cancelled',
            'confirmButtonText' => 'Yes, delete it!',
            'showCancelButton' => !0,
            'cancelButtonText' => "No, cancel!",
            'customClass' => [
                'confirmButton' => 'btn btn-success mt-2',
                'cancelButton' => 'btn btn-danger ml-2 mt-2'
            ],
            'buttonsStyling' => false,
        ]);
    }
}

© KUJUNTI.ID