136 lines
3.5 KiB
PHP
136 lines
3.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Livewire\Users;
|
||
|
|
|
||
|
|
use App\Models\User;
|
||
|
|
use Livewire\Component;
|
||
|
|
use Livewire\WithPagination;
|
||
|
|
|
||
|
|
class UsersList extends Component
|
||
|
|
{
|
||
|
|
use WithPagination;
|
||
|
|
|
||
|
|
public $showCreateModal = false;
|
||
|
|
public $showEditModal = false;
|
||
|
|
public $showDeleteModal = false;
|
||
|
|
|
||
|
|
public $editingUser = null;
|
||
|
|
public $deletingUser = null;
|
||
|
|
|
||
|
|
// Form fields
|
||
|
|
public $name = '';
|
||
|
|
public $email = '';
|
||
|
|
public $password = '';
|
||
|
|
public $password_confirmation = '';
|
||
|
|
|
||
|
|
protected $rules = [
|
||
|
|
'name' => 'required|string|max:255',
|
||
|
|
'email' => 'required|email|unique:users,email',
|
||
|
|
'password' => 'required|min:8|confirmed',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function render()
|
||
|
|
{
|
||
|
|
$users = User::where('planner_id', auth()->user()->planner_id)
|
||
|
|
->orderBy('name')
|
||
|
|
->paginate(10);
|
||
|
|
|
||
|
|
return view('livewire.users.users-list', [
|
||
|
|
'users' => $users
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function create()
|
||
|
|
{
|
||
|
|
$this->reset(['name', 'email', 'password', 'password_confirmation']);
|
||
|
|
$this->resetValidation();
|
||
|
|
$this->showCreateModal = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store()
|
||
|
|
{
|
||
|
|
$this->validate();
|
||
|
|
|
||
|
|
User::create([
|
||
|
|
'name' => $this->name,
|
||
|
|
'email' => $this->email,
|
||
|
|
'password' => bcrypt($this->password),
|
||
|
|
'planner_id' => auth()->user()->planner_id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->showCreateModal = false;
|
||
|
|
$this->reset(['name', 'email', 'password', 'password_confirmation']);
|
||
|
|
|
||
|
|
session()->flash('success', 'User created successfully.');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function edit(User $user)
|
||
|
|
{
|
||
|
|
$this->editingUser = $user;
|
||
|
|
$this->name = $user->name;
|
||
|
|
$this->email = $user->email;
|
||
|
|
$this->password = '';
|
||
|
|
$this->password_confirmation = '';
|
||
|
|
$this->resetValidation();
|
||
|
|
$this->showEditModal = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update()
|
||
|
|
{
|
||
|
|
$rules = [
|
||
|
|
'name' => 'required|string|max:255',
|
||
|
|
'email' => 'required|email|unique:users,email,' . $this->editingUser->id,
|
||
|
|
];
|
||
|
|
|
||
|
|
if ($this->password) {
|
||
|
|
$rules['password'] = 'min:8|confirmed';
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->validate($rules);
|
||
|
|
|
||
|
|
$this->editingUser->update([
|
||
|
|
'name' => $this->name,
|
||
|
|
'email' => $this->email,
|
||
|
|
]);
|
||
|
|
|
||
|
|
if ($this->password) {
|
||
|
|
$this->editingUser->update([
|
||
|
|
'password' => bcrypt($this->password)
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->showEditModal = false;
|
||
|
|
$this->reset(['name', 'email', 'password', 'password_confirmation', 'editingUser']);
|
||
|
|
|
||
|
|
session()->flash('success', 'User updated successfully.');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function confirmDelete(User $user)
|
||
|
|
{
|
||
|
|
$this->deletingUser = $user;
|
||
|
|
$this->showDeleteModal = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function delete()
|
||
|
|
{
|
||
|
|
if ($this->deletingUser->id === auth()->id()) {
|
||
|
|
session()->flash('error', 'You cannot delete your own account.');
|
||
|
|
$this->showDeleteModal = false;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->deletingUser->delete();
|
||
|
|
$this->showDeleteModal = false;
|
||
|
|
$this->deletingUser = null;
|
||
|
|
|
||
|
|
session()->flash('success', 'User deleted successfully.');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function cancel()
|
||
|
|
{
|
||
|
|
$this->showCreateModal = false;
|
||
|
|
$this->showEditModal = false;
|
||
|
|
$this->showDeleteModal = false;
|
||
|
|
$this->reset(['name', 'email', 'password', 'password_confirmation', 'editingUser', 'deletingUser']);
|
||
|
|
}
|
||
|
|
}
|