Skip to content

Commit

Permalink
completed Delete User modal changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jhuebel committed Jul 9, 2019
1 parent 8e3d7e4 commit 9bcd194
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 28 deletions.
98 changes: 72 additions & 26 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,47 +242,93 @@ public function destroy(Request $request)
$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!');
if ($request->user_clients == $id || $request->user_tasks == $id || $request->user_leads == $id) {
Session()->flash('flash_message_warning', '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 ($user->tasks()->count() > 0) {
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_assigned_id = $request->user_tasks;
$task->save();
}
}
}

// are we keeping her tasks?
if ('' === $request->user_tasks) {
// just delete all the tasks related to this user
$user->tasks()->delete();
// clean up tasks created but not assigned to the user
$tasks = Task::where('user_created_id', $id)->get();
if ('' == $request->user_tasks) {
foreach ($tasks as $task) {
$task->user_created_id = $task->user_assigned_id;
$task->save();
}
} else {
// move all clients to new user
foreach ($user->tasks() as $task) {
$task->user_id = $request->user_tasks;
foreach ($tasks as $task) {
$task->user_created_id = $request->user_tasks;
$task->save();
}
}

// refresh the user
$user->refresh();

// are we keeping her leads?
if ('' === $request->user_leads) {
// just delete all the leads related to this user
$user->leads()->delete();
if ($user->leads()->count() > 0) {
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();
}
}
}

// clean up leads created but not assigned to the user
$leads = Lead::where('user_created_id', $id)->get();
if ('' == $request->user_leads) {
foreach ($leads as $lead) {
$lead->user_created_id = $lead->user_assigned_id;
$lead->save();
}
} else {
// move all clients to new user
foreach ($user->leads() as $lead) {
$lead->user_assigned_id = $request->user_leads;
foreach ($leads as $lead) {
$lead->user_created_id = $request->user_leads;
$lead->save();
}
}

// refresh the user
$user->refresh();

// are we keeping her clients?
if ($user->clients()->count() > 0) {
if ('' == $request->user_clients) {
// just delete all the clients related to this user
foreach ($user->clients as $client) {
// clean out all remaining client tasks and leads
$client->tasks()->delete();
$client->leads()->delete();
$client->delete();
}
} else {
// move all clients to new user
foreach ($user->clients as $client) {
$client->user_id = $request->user_clients;
$client->save();
}
}
}

// refresh the user one more time
$user->refresh();

$user->delete();
Session()->flash('flash_message', 'User successfully deleted');
}
Expand Down
4 changes: 3 additions & 1 deletion resources/views/layouts/master.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ function postRead(id) {
<script type="text/javascript" src="{{ URL::asset('js/jquery.dataTables.min.js') }}"></script>
<script type="text/javascript" src="{{ URL::asset('js/jasny-bootstrap.min.js') }}"></script>
<script type="text/javascript" src="{{ URL::asset('js/jquery.caret.min.js') }}"></script>
<script type="text/javascript" src="{{ URL::asset('js/jquery.atwho.min.js') }}"></script>
<script type="text/javascript" src="{{ URL::asset('js/jquery.atwho.min.js') }}"></script>

@stack('scripts')

</body>

</html>
6 changes: 5 additions & 1 deletion resources/views/users/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<!--HANDLE TASKS-->
<div class="form-group">
{{ Form::label('user_clients', __('Choose a new user to assign the clients')) }}
{{ Form::label('user_clients', __('Choose a new user to assign the clients')) }} <span class="glyphicon glyphicon-exclamation-sign text-danger" data-toggle="tooltip" title="Deleting all clients also deletes ALL tasks and leads assigned to that client, regardless of the user they are assigned to."></span>
{{ Form::select('user_clients', $users, null, ['class' => 'form-control', 'placeholder' => 'Delete All Clients']) }}
</div>

Expand Down Expand Up @@ -89,5 +89,9 @@
});
});
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
</script>
@endpush

0 comments on commit 9bcd194

Please sign in to comment.