CasperSecurity
<?php
namespace App\Http\Livewire\Organisation;
use Livewire\Component;
use App\Models\User;
use App\Models\Designation;
use App\Models\EmployeeRegistration;
use App\Models\Team;
use App\Models\Role;
use App\Models\RoleUser;
use Illuminate\Support\Str;
use Livewire\WithPagination;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
class UserLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $avilemployee, $name,$employee_id, $email, $password, $password_confirmation, $tempRoleId, $roleId, $remember_token, $status, $attachedRole, $designationId;
public $modelid;
public $import_flag=false;
public $comparision_data;
public $showmodal = false;
public $selectedDepartments;
public $rules = [
'name' => 'required|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|min:8|confirmed',
'roleId' => 'required',
];
public function updateRules()
{
return [
'name' => 'required|max:255',
'email' => 'required|string|email|max:255|unique:users,email,' . $this->modelid,
'roleId' => 'required',
];
}
public function passwordUpdateRule()
{
return [
'name' => 'required|max:255',
'email' => 'required|string|email|max:255|unique:users,email,' . $this->modelid,
'designationId' => 'required',
'password' => 'required|min:8|confirmed',
'roleId' => 'required',
];
}
public $messages = [
'name.required' => 'The name field is required.',
'name.max' => 'The name may not be greater than :max characters.',
'email.required' => 'The email field is required.',
'designation_id.required' => 'The designation field is required.',
'email.string' => 'The email must be a string.',
'email.email' => 'The email must be a valid email address.',
'email.max' => 'The email may not be greater than :max characters.',
'email.unique' => 'The email has already been taken.',
'roleId.required' => 'Select role',
'password.required' => 'The password field is required.',
'password.min' => 'The password must be at least :min characters.',
'password.confirmed' => 'The password confirmation does not match.',
];
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 cleanVar()
{
$this->modelid = null;
$this->import_flag = false;
$this->comparision_data=null;
$this->name = null;
$this->password = null;
$this->password_confirmation = null;
$this->selectedDepartments = null;
$this->email = null;
$this->designationId = null;
$this->status = null;
$this->showmodal = false;
}
// public function updatedComparisionData($value){
// // dd($value);
// }
public function searchemp(){
if($this->comparision_data){
$isavail=EmployeeRegistration::where('employee_id',$this->comparision_data)
->orWhere('mobile_no',$this->comparision_data)
->orWhere('email_id',$this->comparision_data)->first();
// dd($isavail->team_id);
if($isavail){
$this->avilemployee=$isavail;
$this->name=$isavail->name;
$this->employee_id=$isavail->employee_id;
$this->email=$isavail->email_id;
$this->designationId=$isavail->designation;
$this->roleId=$isavail->role_id;
$this->selectedDepartments=$isavail->team_id;
//dd($this->selectedDepartments);
}
// comparision_data
}
else{
//dd('');
}
}
public function submitUser()
{
$this->validate($this->rules, $this->messages);
$userData = [
'name' => $this->name,
'employee_id'=>$this->employee_id,
'email' => $this->email,
'designation_id' => $this->designationId,
'password' => Hash::make($this->password),
'role_id'=> Role::find($this->roleId)->id,
'role_name'=> Role::find($this->roleId)->name
];
//dd($userData);
$response = User::create($userData);
if ($response) {
if($this->avilemployee != null){
$this->avilemployee->user_id=$response['id'];
$this->avilemployee->save();
}
$role = Role::find($this->roleId);
if ($role) {
$response->attachRole($role);
}
$checkUser = DB::table('role_user')->where('user_id', $response->id)->where('role_id', $this->roleId)->where('team_id', '=', NULL)->first();
if ($checkUser) {
$updateUser = DB::table('role_user')->update([
'team_id' => $this->selectedDepartments,
]);
} else {
$roleUser = DB::table('role_user')->where('user_id', $response->id)->where('role_id', $this->roleId)->where('team_id', $this->selectedDepartments)->first();
if ($roleUser == null) {
$newRoleUser = DB::table('role_user')->insert([
'role_id' => $this->roleId,
'user_id' => $response->id,
'user_type' => 'App\Models\User',
'team_id' => $this->selectedDepartments,
]);
}
$roleUser = null;
}
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => "New has been created",
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => "Failed to create user.",
]);
}
}
public function updateClick($id)
{
$this->modelid = $id;
$this->loadUser($id);
$this->showmodal = true;
}
public function loadUser($id)
{
$user = User::find($id);
if ($user) {
$this->name = $user->name;
$this->email = $user->email;
$this->status = $user->status;
$this->roleId = DB::table('role_user')->where('user_id', $user->id)->first()->role_id;
$this->designationId = $user->designation_id;
$roleUser = DB::table('role_user')->where('user_id',$user->id)->where('team_id', '!=', null)->first();
$this->selectedDepartments= $roleUser->team_id;
$this->tempRoleId = $this->roleId;
}
}
public function updateUser()
{
if ($this->password) {
$this->validate($this->passwordUpdateRule(), $this->messages);
} else {
$this->validate($this->updateRules(), $this->messages);
}
$user = User::find($this->modelid);
if ($user) {
if($this->avilemployee != null){
$this->avilemployee->user_id=$user['id'];
$this->avilemployee->save();
}
$role = Role::find($this->tempRoleId);
if ($role) {
$user->detachRole($role);
}
if ($this->password) {
$updated = $user->update([
'name' => $this->name,
'email' => $this->email,
'designation_id' => $this->designationId,
'password' => Hash::make($this->password),
'status' => $this->status,
]);
} else {
$updated = $user->update([
'name' => $this->name,
'email' => $this->email,
'designation_id' => $this->designationId,
'status' => $this->status,
]);
}
if ($updated) {
$role = Role::find($this->roleId);
if ($role) {
$user->attachRole($role);
}
$existRoleUser = DB::table('role_user')->where('user_id', $this->modelid)->delete();
$roleuser = DB::table('role_user')->insert([
'role_id' => $this->roleId,
'user_id' => $this->modelid,
'user_type' => 'App\Models\User',
'team_id' => $this->selectedDepartments,
]);
$this->cleanVar();
$this->dispatchBrowserEvent('closeOffCanvas');
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'success',
'message' => "User has been updated.",
]);
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => "Failed to update user.",
]);
}
} else {
$this->dispatchBrowserEvent('showsuccessmsg', [
'type' => 'error',
'message' => "User not found",
]);
}
}
public function read()
{
$users = User::search($this->search)
->whereNotIn('id', ['1'])
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')
->paginate($this->perPage);
foreach ($users as $user){
if($user->designation_id !=""){
if($user->designation){
$user->designation=$user->designation->designation_name;
}
}else{
$user->designation='NA';
}
}
return $users;
}
public function render()
{
$roles = Role::where('id', '>', 1)->get();
$designations = Designation::all();
$departments = Team::all();
$dataTable = $this->read();
return view('livewire.organisation.user-livewire', compact('dataTable', 'roles', 'designations', 'departments'));
}
}