Skip to content

Commit

Permalink
first part of fix for user delete modal
Browse files Browse the repository at this point in the history
  • Loading branch information
jhuebel committed Jul 9, 2019
1 parent fa50b5a commit 8e3d7e4
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 207 deletions.
55 changes: 51 additions & 4 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(
public function index()
{
return view('users.index')
->with('users', User::select('id', 'name')->orderBy('name')->get());
->with('users', User::orderBy('name')->get()->pluck('name', 'id'));
}

public function users()
Expand All @@ -69,7 +69,7 @@ public function anyData()
return '<a href="'.route('users.edit', $user->id).'" class="btn btn-success"> Edit</a>';
})
->add_column('delete', function ($user) {
return '<button type="button" class="btn btn-danger delete_client" data-client_id="'.$user->id.'" onClick="openModal('.$user->id.')" id="myBtn">Delete</button>';
return '<button type="button" class="btn btn-danger delete_client" data-client_id="'.$user->id.'" data-toggle="modal" data-target="#myModal">Delete</button>';
})->make(true);
}

Expand Down Expand Up @@ -236,9 +236,56 @@ public function update($id, UpdateUserRequest $request)
*
* @return mixed
*/
public function destroy(Request $request, $id)
public function destroy(Request $request)
{
$this->users->destroy($request, $id);
// load the user so we can get relational data
$id = $request->id;
$user = User::with('clients', 'tasks', 'leads')->findOrFail($id);

if ($request->user_clients === $id || $request->user_tasks === $id || $request->user_leads === $id) {
Session()->flash('flash_error', 'You may not reassign clients, leads or tasks to the user you are deleting!');
} else {
// are we keeping her clients?
if ('' === $request->user_clients) {
// just delete all the clients related to this user
foreach ($user->clients as $client) {
$client->delete();
}
} else {
// move all clients to new user
foreach ($user->clients() as $client) {
$client->user_id = $request->user_clients;
$client->save();
}
}

// are we keeping her tasks?
if ('' === $request->user_tasks) {
// just delete all the tasks related to this user
$user->tasks()->delete();
} else {
// move all clients to new user
foreach ($user->tasks() as $task) {
$task->user_id = $request->user_tasks;
$task->save();
}
}

// are we keeping her leads?
if ('' === $request->user_leads) {
// just delete all the leads related to this user
$user->leads()->delete();
} else {
// move all clients to new user
foreach ($user->leads() as $lead) {
$lead->user_assigned_id = $request->user_leads;
$lead->save();
}
}

$user->delete();
Session()->flash('flash_message', 'User successfully deleted');
}

return redirect()->route('users.index');
}
Expand Down
41 changes: 6 additions & 35 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ class User extends Authenticatable

protected $primaryKey = 'id';

public function clients()
{
return $this->hasMany(Client::class, 'user_id', 'id');
}

public function tasks()
{
return $this->hasMany(Task::class, 'user_assigned_id', 'id');
}

public function leads()
{
return $this->hasMany(Lead::class, 'user_id', 'id');
return $this->hasMany(Lead::class, 'user_assigned_id', 'id');
}

public function department()
Expand All @@ -73,38 +78,4 @@ public function getNameAndDepartmentAttribute()
{
return $this->name.' '.'('.$this->department()->first()->name.')';
}

public function moveTasks($user_id)
{
$tasks = $this->tasks()->get();
foreach ($tasks as $task) {
$task->user_assigned_id = $user_id;
$task->save();
}
}

public function moveLeads($user_id)
{
$leads = $this->leads()->get();
foreach ($leads as $lead) {
$lead->user_assigned_id = $user_id;
$lead->save();
}
}

public function moveClients($user_id)
{
$clients = $this->clients()->get();
foreach ($clients as $client) {
$client->user_id = $user_id;
$client->save();
}
}

public function getAvatarattribute()
{
$setting = Setting::first();

return $this->image_path ? 'images/'.$setting->company.'/'.$this->image_path : 'images/default_avatar.jpg';
}
}
30 changes: 0 additions & 30 deletions app/Repositories/User/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,34 +120,4 @@ public function update($id, $requestData)

return $user;
}

/**
* @param $id
*
* @return mixed
*/
public function destroy($request, $id)
{
$user = User::findorFail($id);
if ($user->hasRole('super_administrator')) {
return Session()->flash('flash_message_warning', 'Not allowed to delete super admin');
}

if ('move_all_tasks' == $request->tasks && '' != $request->task_user) {
$user->moveTasks($request->task_user);
}
if ('move_all_leads' == $request->leads && '' != $request->lead_user) {
$user->moveLeads($request->lead_user);
}
if ('move_all_clients' == $request->clients && '' != $request->client_user) {
$user->moveClients($request->client_user);
}

try {
$user->delete();
Session()->flash('flash_message', 'User successfully deleted');
} catch (\Illuminate\Database\QueryException $e) {
Session()->flash('flash_message_warning', 'User can NOT have, leads, clients, or tasks assigned when deleted');
}
}
}
2 changes: 0 additions & 2 deletions app/Repositories/User/UserRepositoryContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ public function getAllUsersWithDepartments();
public function create($requestData);

public function update($id, $requestData);

public function destroy($request, $id);
}
178 changes: 42 additions & 136 deletions resources/views/users/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@stop

@section('content')

<table class="table table-striped" id="users-table">
<thead>
<tr>
Expand All @@ -17,81 +18,46 @@
</table>


<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="padding:35px 50px;">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4><span class="glyphicon glyphicon-lock"></span> Handle deletion of user</h4>
</div>
<div class="modal-body" style="padding:40px 50px;">
<form role="form">

<!--HANDLE TASKS-->
<div class="form-group">
<label for="tasks"><span class=""></span> {{ __('How to handle the user tasks?') }}</label>
<select name="handle_tasks" id="handle_tasks" class="form-control">
<option value="delete_all_tasks">{{ __('Delete all tasks') }}</option>
<option value="move_all_tasks"> {{ __('Move all tasks') }}</option>
</select>
</div>
<div class="form-group" id="assign_tasks" style="display:none">
<label for="user_tasks"><span class="glyphicon glyphicon-user"></span> {{ __('Choose a new user to assign the tasks') }}</label>
<select name="user_tasks" id="user_tasks" class="form-control">
<option value="null" disabled selected> {{ __('Select a user') }} </option>
@foreach ($users as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</select>
</div>

<!--HANDLE LEADS-->
<div class="form-group">
<label for="handle_leads"><span class=""></span> {{ __('How to handle the user leads?') }}</label>
<select name="leads" id="handle_leads" class="form-control">
<option value="delete_all_leads">{{ __('Delete all leads') }}</option>
<option value="move_all_leads"> {{ __('Move all leads') }}</option>
</select>
</div>
<div class="form-group" id="assign_leads" style="display:none">
<label for="user_leads"><span class="glyphicon glyphicon-user"></span> {{ __('Choose a new user to assign the leads') }}</label>
<select name="user_leads" id="user_leads" class="form-control">
<option value="null" disabled selected> {{ __('Select a user') }} </option>
@foreach ($users as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</select>
</div>

<!--HANDLE CLIENTS-->
<div class="form-group">
<label for="handle_clients"><span class=""></span> {{ __('How to handle the user clients?') }}</label>
<select name="clients" id="handle_clients" class="form-control">
<option value="delete_all_clients">{{ __('Delete all clients') }}</option>
<option value="move_all_clients"> {{ __('Move all clients') }}</option>
</select>
</div>
<div class="form-group" id="assign_clients" style="display:none">
<label for="user_clients"><span class="glyphicon glyphicon-user"></span> {{ __('Choose a new user to assign the clients') }}</label>
<select name="user_clients" id="user_clients" class="form-control">
<option value="null" disabled selected> {{ __('Select a user') }} </option>
@foreach ($users as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</select>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog">
{!! Form::open(['route' => ['users.destroy', 'delete'], 'method' => 'delete']) !!} <!-- and invalid ID is intentionally set here -->
{!! Form::hidden('id', '', ['id' => 'client-id']) !!}
<div class="modal-dialog" role="document">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="padding:35px 50px;">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title"><span class="glyphicon glyphicon-lock"></span> Handle deletion of user</h4>
</div>
<div class="modal-body" style="padding:40px 50px;">

<!--HANDLE TASKS-->
<div class="form-group">
{{ Form::label('user_clients', __('Choose a new user to assign the clients')) }}
{{ Form::select('user_clients', $users, null, ['class' => 'form-control', 'placeholder' => 'Delete All Clients']) }}
</div>

<!--HANDLE LEADS-->
<div class="form-group">
{{ Form::label('user_leads', __('Choose a new user to assign the leads')) }}
{{ Form::select('user_leads', $users, null, ['class' => 'form-control', 'placeholder' => 'Delete All Leads']) }}
</div>

<!--HANDLE CLIENTS-->
<div class="form-group">
{{ Form::label('user_tasks', __('Choose a new user to assign the tasks')) }}
{{ Form::select('user_tasks', $users, null, ['class' => 'form-control', 'placeholder' => 'Delete All Tasks']) }}
</div>

</div>
<div class="modal-footer">
<button type="submit" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
<button type="submit" id="confirm_delete" class="btn btn-success"><span class="glyphicon glyphicon-off"></span> Delete</button>
</div>
{!! Form::close() !!}
</div>
</form>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
<button type="submit" id="confirm_delete" class="btn btn-success"><span class="glyphicon glyphicon-off"></span> Delete</button>
</div>
</div>

</div>
</div>
</div>

@stop

Expand All @@ -117,69 +83,9 @@
});
});
function openModal(client_id) {
$("#confirm_delete").attr('delete-id', client_id);
$("#myModal").modal();
}
$("#handle_tasks").click(function () {
if($("#handle_tasks").val() == "move_all_tasks") {
$("#assign_tasks").css('display', 'block');
} else {
$("#assign_tasks").css('display', 'none');
}
});
$("#handle_clients").click(function () {
if($("#handle_clients").val() == "move_all_clients") {
$("#assign_clients").css('display', 'block');
} else {
$("#assign_clients").css('display', 'none');
}
});
$("#handle_leads").click(function () {
if($("#handle_leads").val() == "move_all_leads") {
$("#assign_leads").css('display', 'block');
} else {
$("#assign_leads").css('display', 'none');
}
});
$("#confirm_delete").click(function () {
id = $(this).attr("delete-id");
handle_leads = $("#handle_leads").val();
handle_tasks = $("#handle_tasks").val();
handle_clients = $("#handle_clients").val();
leads_user = $("#user_leads").val();
tasks_user = $("#user_tasks").val();
clients_user = $("#user_clients").val();
$.ajax({
url: "/users/" + id,
type: 'DELETE',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: {
tasks: handle_tasks,
task_user: tasks_user,
leads: handle_leads,
lead_user: leads_user,
clients: handle_clients,
client_user: clients_user,
},
complete: function (jqXHR, textStatus) {
// callback
},
success: function (data, textStatus, jqXHR) {
// success callback
},
error: function (jqXHR, textStatus, errorThrown) {
// error callback
}
$(function() {
$('#myModal').on("show.bs.modal", function (e) {
$("#client-id").val($(e.relatedTarget).data('client_id'));
});
});
Expand Down

0 comments on commit 8e3d7e4

Please sign in to comment.