CasperSecurity
<?php
namespace App\Http\Livewire\Settings;
use Livewire\Component;
use App\Models\Designation;
use Illuminate\Support\Str;
use Livewire\WithPagination;
class DesignationLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $designation_short_name, $designation_name, $designation_level, $status;
public $modelid;
public $showmodal = false;
protected $listeners = ['delete'];
public $rules = [
'designation_short_name' => 'required|max:5',
'designation_name' => 'required|max:30',
'designation_level' => 'required',
];
public $messages = [
'designation_short_name.required' => 'The designation short name is required.',
'designation_short_name.max' => 'The designation short name may not be greater than :max characters.',
'designation_name.required' => 'The designation name is required.',
'designation_name.max' => 'The designation name may not be greater than :max characters.',
'designation_level.required' => 'The designation level 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 cleanVar()
{
$this->modelid = null;
$this->designation_short_name = null;
$this->designation_name = null;
$this->designation_level = null;
$this->showmodal = false;
}
public function submitDesignation()
{
$this->validate($this->rules, $this->messages);
$response = Designation::create([
'designation_name' => $this->designation_name,
'designation_short_name' => $this->designation_short_name,
'designation_level' => $this->designation_level,
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => "New Designation Created",
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => "Failed to create designation.",
]);
}
}
public function updateClick($id){
$this->modelid = $id;
$this->loadDesignation($id);
$this->showmodal = true;
}
public function loadDesignation($id){
$designation = Designation::find($id);
if($designation){
$this->designation_name = $designation->designation_name;
$this->designation_short_name = $designation->designation_short_name;
$this->designation_level = $designation->designation_level;
$this->status = $designation->status;
}
}
public function updateDesignation()
{
$this->validate($this->rules, $this->messages);
$designation = Designation::find($this->modelid);
if ($designation) {
$updated = $designation->update([
'designation_name' => $this->designation_name,
'designation_short_name' => $this->designation_short_name,
'designation_level' => $this->designation_level,
'status' => $this->status,
]);
if ($updated) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => "Designation has been updated.",
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => "Failed to update designation.",
]);
}
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => "Designation not found",
]);
}
}
public function deleteClick($id){
$this->modelid=$id;
$this->dispatchBrowserEvent('confirmdelete',[
'message'=>'Are you sure want to delete this data ?',
'funcname'=>'delete'
]);
}
public function delete(){
$response=Designation::find($this->modelid);
if($response){
if($response->delete()){
$this->cleanVar();
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'success',
'message'=>'This designation has been deleted'
]);
}else{
$this->dispatchBrowserEvent('showsuccessmsg',[
'type'=>'error',
'message'=>'Something went wrong'
]);
}
}
}
public function read()
{
$designations = Designation::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $designations;
}
public function render()
{
$designation = $this->read();
return view('livewire.settings.designation-livewire', compact('designation'));
}
}