CasperSecurity
<?php
namespace App\Http\Livewire\Settings;
use Livewire\Component;
use App\Models\Team;
use App\Models\Module;
use App\Models\Role;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Feature;
use App\Models\NotificationRule;
use Illuminate\Support\Str;
use Livewire\WithPagination;
class NotificationRuleLivewire extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $search;
public $perPage = 10;
public $orderBy = 'id';
public $orderAsc = '1';
public $module_id, $feature_id, $role_id, $user_id, $notification_thru, $notification_based_on, $created_by, $modified_by, $status;
public $modelid;
public $showmodal = false;
public $successmsg = "Notification Rule has been saved successfully.";
public $updatemsg = "Notification Rule has been updated successfully.";
public $deletemsg = "Notification has been deleted successfully.";
public $errormsg = "Sorry !!! Something went wrong. Please Try Again.";
protected $listeners = ['delete'];
public $rules = [
'module_id' => 'required',
'feature_id' => 'required',
'notification_thru' => 'required',
'notification_based_on' => 'required',
];
public $messages = [
'module_id.required' => 'This field is required',
'feature_id.required' => 'This field is required',
'notification_thru.required' => 'This field is required',
'notification_based_on.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->module_id = null;
$this->feature_id = null;
$this->notification_based_on = null;
$this->notification_thru = null;
$this->role_id = null;
$this->user_id = null;
$this->showmodal = false;
}
public function loadData($id)
{
$response = NotificationRule::find($id);
if ($response) {
$this->module_id = $response->module_id;
$this->feature_id = $response->feature_id;
$this->notification_based_on = $response->notification_based_on;
$this->notification_thru = $response->notification_thru;
$this->role_id = $response->role_id;
$this->user_id = $response->user_id ? json_decode($response->user_id, true) : null;
}
}
public function updatedNotificationBasedOn($val)
{
if ($val === "Role") {
$this->user_id = null;
} else {
$this->role_id = null;
}
}
public function saveRule()
{
$this->validate($this->rules, $this->messages);
$response = NotificationRule::create([
'module_id' => $this->module_id,
'feature_id' => $this->feature_id,
'role_id' => $this->role_id ? $this->role_id : null,
'user_id' => sizeof($this->user_id) > 0 ? json_encode($this->user_id) : null,
'notification_based_on' => $this->notification_based_on,
'notification_thru' => $this->notification_thru,
'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 updateRule()
{
$this->validate($this->rules, $this->messages);
$response = NotificationRule::find($this->modelid);
if ($response) {
$response->update([
'module_id' => $this->module_id,
'feature_id' => $this->feature_id,
'role_id' => $this->role_id ? $this->role_id : null,
'user_id' => $this->user_id != null && sizeof($this->user_id) > 0 ? json_encode($this->user_id) : null,
'notification_based_on' => $this->notification_based_on,
'notification_thru' => $this->notification_thru,
'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
]);
}
}
}
public function delete()
{
$response = NotificationRule::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()
{
$notifications = NotificationRule::search($this->search)->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc')->paginate($this->perPage);
return $notifications;
}
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';
if ($data->role_id != null) {
$role = Role::find($data->role_id);
$dataTables[$key]['display_name'] = $role ? $role->display_name : 'N/A';
} else {
$temp = [];
foreach (json_decode($data->user_id, true) as $user) {
$temp[] = User::select('name')->find($user);
}
$dataTables[$key]['user_names'] = sizeof($temp) > 0 ? $temp : [];
}
}
}
$modules = Module::all();
$features = [];
if ($this->module_id) {
$features = Feature::where('module_id', $this->module_id)->get();
}
$roles = Role::where('id', '>', 1)->get();
$users = User::where('id', '>', 1)->get();
return view('livewire.settings.notification-rule-livewire', compact('dataTables', 'modules', 'features', 'roles', 'users'));
}
}