CasperSecurity
<?php
namespace App\Http\Livewire\Projects;
use App\Models\ProjectDocument;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
class ProjectDocumentsLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $page_name = "Project Document";
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $project_doc_name, $created_by, $modified_by, $status;
public $modelId;
public $showmodal = false;
public $successmsg = "The project document has been saved successfully.";
public $updatemsg = "The project document has been updated successfully.";
public $deletemsg = "The project document has been deleted successfully.";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules = [
'project_doc_name' => 'required',
];
public $messages = [
'project_doc_name.required' => 'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$this->resetValidation();
$this->showmodal = true;
}
public function closemodalclick()
{
$this->resetValidation();
$this->showmodal = false;
}
public function updateclick($id)
{
$this->modelId = $id;
$this->loadData($this->modelId);
$this->showmodal = true;
}
public function deleteclick($id)
{
$this->modelId = $id;
$this->dispatchBrowserEvent('confirmdelete', [
'message' => 'Are you sure want to delete this data ?',
'funcname' => 'delete'
]);
}
public function cleanVar()
{
$this->modelId = null;
$this->project_doc_name = null;
$this->status = null;
$this->showmodal = false;
}
public function loadData($id)
{
$response = ProjectDocument::find($id);
if ($response) {
$this->project_doc_name = $response['project_doc_name'];
$this->status = $response['status'];
}
}
public function save()
{
$this->validate($this->rules, $this->messages);
$response = ProjectDocument::create([
'project_doc_name' => $this->project_doc_name,
'created_by' => Auth::user()->id,
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->successmsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
public function update()
{
$this->validate($this->rules, $this->messages);
$response = ProjectDocument::find($this->modelId);
if ($response) {
$response->update([
'project_doc_name' => $this->project_doc_name,
'modified_by' => Auth::user()->id,
'status' => $this->status ? '1' : '0',
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function delete()
{
$response = ProjectDocument::find($this->modelId);
if ($response) {
if ($response->delete()) {
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->deletemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
}
}
public function read()
{
$projectDocument = ProjectDocument::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $projectDocument;
}
public function render()
{
$dataTables = [];
$dataTables = $this->read();
foreach ($dataTables as $key=>$data){
$user = User::find($data->created_by);
$dataTables[$key]['user_name'] = $user->name;
}
return view('livewire.projects.project-documents-livewire', compact('dataTables'));
}
}