CasperSecurity
<?php
namespace App\Http\Livewire\Settings;
use Livewire\Component;
use App\Models\ApprovalRule;
use App\Models\Module;
use App\Models\Feature;
use App\Models\User;
use App\Models\Designation;
use App\Models\Team;
use Illuminate\Support\Str;
use Livewire\WithPagination;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
class ApprovalRuleLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $module_id, $feature_id, $designation_id, $team_id, $approval_type, $no_of_approvers, $created_by, $modified_by, $status;
public $modelid;
public $showmodal = false;
public $successmsg = "The approval rule has been saved successfully.";
public $updatemsg = "The approval rule has been updated successfully.";
public $deletemsg = "The approval rule has been deleted successfully.";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
public $userArray = [], $designationArray = [], $teamArray = [];
public $userDepartment = [];
protected $listeners = ['delete'];
public $rules = [
'module_id' => 'required',
'feature_id' => 'required',
'designation_id' => 'required',
'team_id' => 'required',
'approval_type' => 'required',
'no_of_approvers' => 'required',
];
public $messages = [
'module_id.required' => 'This field is required',
'feature_id.required' => 'This field is required',
'designation_id.required' => 'This field is required',
'team_id.required' => 'This field is required',
'approval_type.required' => 'This field is required',
'no_of_approvers.required' => 'This field is required',
];
public function updatingSearch()
{
$this->resetPage();
}
public function showmodalclick()
{
$this->cleanVar();
$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->feature_id = null;
$this->designation_id = null;
$this->team_id = null;
$this->module_id = null;
$this->feature_id = null;
$this->approval_type = null;
$this->no_of_approvers = null;
$this->showmodal = false;
$this->userDepartment = [];
$this->userArray = [];
$this->designationArray = [];
}
public function loadData($id)
{
$response = ApprovalRule::find($id);
if ($response) {
$this->module_id = $response->module_id;
$this->feature_id = $response->feature_id;
$this->userArray = json_decode($response->user_id, true);
$this->designationArray = json_decode($response->designation_id, true);
$this->teamArray = $response->team_id ? json_decode($response->team_id, true) : [];
$this->approval_type = $response->approval_type;
$this->no_of_approvers = $response->no_of_approvers;
$this->created_by = $response->created_by;
$this->modified_by = $response->modified_by;
$this->status = $response->status;
}
foreach ($this->userArray as $key => $user) {
$team = DB::table('role_user')->where('user_id', $user)->where('team_id', '!=', null)->get();
if (sizeof($team) > 0) {
$tempArray = [];
foreach ($team as $tm) {
$tempArray[] = Team::select('id', 'display_name')->find($tm->team_id);
}
$this->userDepartment[$key] = $tempArray;
}
}
}
public function validateField()
{
$this->validate([
'module_id' => 'required',
'feature_id' => 'required',
'approval_type' => 'required',
'no_of_approvers' => 'required',
], [
'module_id.required' => 'Module ID is required',
'feature_id.required' => 'Feature ID is required',
'approval_type.required' => 'Approval type is required',
'no_of_approvers.required' => 'Number of approvers is required',
]);
$prevrules = [];
$prevmessages = [];
for ($i = 0; $i < $this->no_of_approvers; $i++) {
$prevrules["userArray.$i"] = 'required';
$prevrules["designationArray.$i"] = 'required';
$prevmessages += [
"userArray.$i.required" => 'User field for approver ' . ($i + 1) . ' is required.',
"designationArray.$i.required" => 'Designation field for approver ' . ($i + 1) . ' is required.',
];
}
$this->validate($prevrules, $prevmessages);
}
public function saveApprovalRule()
{
$this->validateField();
$response = ApprovalRule::create([
'module_id' => $this->module_id,
'feature_id' => $this->feature_id,
'user_id' => json_encode($this->userArray),
'designation_id' => json_encode($this->designationArray),
'team_id' => json_encode($this->teamArray),
'approval_type' => $this->approval_type,
'no_of_approvers' => $this->no_of_approvers,
'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 updateApprovalRule()
{
$this->validateField();
$response = ApprovalRule::find($this->modelid);
if ($response) {
$response->update([
'module_id' => $this->module_id,
'feature_id' => $this->feature_id,
'user_id' => json_encode($this->userArray),
'designation_id' => json_encode($this->designationArray),
'team_id' => json_encode($this->teamArray),
'approval_type' => $this->approval_type,
'no_of_approvers' => $this->no_of_approvers,
'modified_by' => Auth::user()->id,
'status' => $this->status,
]);
if ($response) {
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => $this->updatemsg
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => $this->errormsg
]);
}
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => 'Approval Rule not found'
]);
}
}
public function delete()
{
$response = ApprovalRule::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()
{
$approvalRule = ApprovalRule::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $approvalRule;
}
public function userDetailSet()
{
if (sizeof($this->userArray) > 0) {
foreach ($this->userArray as $key => $user) {
$userData = User::find($this->userArray[$key]);
$this->designationArray[$key] = $userData->designation_id;
$team = DB::table('role_user')
->where('user_id', $this->userArray[$key])->where('team_id', '!=', null)->first();
if ($team) {
$this->teamArray[$key] = $team->team_id;
}
}
}
}
public function setEmployee($keyval)
{
if ($keyval != "") {
$usData = $this->userArray[$keyval];
if ($usData) {
$roleUser = DB::table('role_user')->where('user_id', $usData)->where('team_id', '!=', null)->get();
if ($roleUser) {
$tempArray = [];
foreach ($roleUser as $dept) {
$tempArray[] = Team::select('id', 'display_name')->find($dept->team_id);
}
$this->userDepartment[$keyval] = $tempArray;
}
}
}
}
public function render()
{
$dataTables = $this->read();
if ($dataTables && sizeof($dataTables) > 0) {
foreach ($dataTables as $key => $data) {
$module = Module::find($data->module_id);
$dataTables[$key]['module_name'] = $module ? $module->module_name : 'N/A';
$feature = Feature::find($data->feature_id);
$dataTables[$key]['feature_name'] = $feature ? $feature->feature_name : 'N/A';
}
}
$modules = Module::all();
$features = [];
if ($this->module_id) {
$features = Feature::where('module_id', $this->module_id)->get();
}
$users = User::where('id', '>', 1)->get();
$designations = Designation::all();
$teams = Team::all();
$this->userDetailSet();
return view('livewire.settings.approval-rule-livewire', compact('dataTables', 'modules', 'features', 'users', 'designations', 'teams'));
}
}