Added location restore

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-08-19 13:19:59 +01:00
parent eb8d43a804
commit 229d8b9bf5
4 changed files with 144 additions and 40 deletions

View file

@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Http\Requests\ImageUploadRequest;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Location;
use App\Models\User;
@ -193,7 +194,13 @@ class LocationsController extends Controller
*/
public function show($id = null) : View | RedirectResponse
{
$location = Location::find($id);
$location = Location::withCount('assignedAssets as assigned_assets_count')
->withCount('assets as assets_count')
->withCount('rtd_assets as rtd_assets_count')
->withCount('children as children_count')
->withCount('users as users_count')
->withTrashed()
->find($id);
if (isset($location->id)) {
return view('locations/view', compact('location'));
@ -249,6 +256,41 @@ class LocationsController extends Controller
}
/**
* Restore a given Asset Model (mark as un-deleted)
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0]
* @param int $id
*/
public function postRestore($id) : RedirectResponse
{
$this->authorize('create', Location::class);
if ($location = Location::withTrashed()->find($id)) {
if ($location->deleted_at == '') {
return redirect()->back()->with('error', trans('general.not_deleted', ['item_type' => trans('general.location')]));
}
if ($location->restore()) {
$logaction = new Actionlog();
$logaction->item_type = Location::class;
$logaction->item_id = $location->id;
$logaction->created_at = date('Y-m-d H:i:s');
$logaction->user_id = auth()->id();
$logaction->logaction('restore');
return redirect()->route('locations.index')->with('success', trans('admin/locations/message.restore.success'));
}
// Check validation
return redirect()->back()->with('error', trans('general.could_not_restore', ['item_type' => trans('general.location'), 'error' => $location->getErrors()->first()]));
}
return redirect()->back()->with('error', trans('admin/models/message.does_not_exist'));
}
public function print_all_assigned($id) : View | RedirectResponse
{
if ($location = Location::where('id', $id)->first()) {

View file

@ -20,6 +20,11 @@ return array(
'success' => 'Location updated successfully.'
),
'restore' => array(
'error' => 'Location was not restored, please try again',
'success' => 'Location restored successfully.'
),
'delete' => array(
'confirm' => 'Are you sure you wish to delete this location?',
'error' => 'There was an issue deleting the location. Please try again.',

View file

@ -9,6 +9,10 @@
@parent
@stop
@section('header_right')
<a href="{{ route('locations.index') }}" class="btn btn-primary" style="margin-right: 10px;">
{{ trans('general.back') }}</a>
@endsection
{{-- Page content --}}
@section('content')
@ -382,22 +386,64 @@
<div class="col-md-3">
@can('update', $location)
<div class="col-md-12">
<a href="{{ route('locations.edit', ['location' => $location->id]) }}" style="width: 100%;" class="btn btn-sm btn-primary pull-left">{{ trans('admin/locations/table.update') }} </a>
<a href="{{ route('locations.edit', ['location' => $location->id]) }}" style="width: 100%;" class="btn btn-sm btn-warning btn-social">
<x-icon type="edit" />
{{ trans('admin/locations/table.update') }}
</a>
</div>
@endcan
<div class="col-md-12" style="padding-top: 5px;">
<a href="{{ route('locations.print_assigned', ['locationId' => $location->id]) }}" style="width: 100%;" class="btn btn-sm btn-default pull-left">{{ trans('admin/locations/table.print_assigned') }} </a>
<a href="{{ route('locations.print_assigned', ['locationId' => $location->id]) }}" style="width: 100%;" class="btn btn-sm btn-primary btn-social hidden-print">
<x-icon type="print" />
{{ trans('admin/locations/table.print_assigned') }}
</a>
</div>
<div class="col-md-12" style="padding-top: 5px; padding-bottom: 20px;">
<a href="{{ route('locations.print_all_assigned', ['locationId' => $location->id]) }}" style="width: 100%;" class="btn btn-sm btn-default pull-left">{{ trans('admin/locations/table.print_all_assigned') }} </a>
<a href="{{ route('locations.print_all_assigned', ['locationId' => $location->id]) }}" style="width: 100%;" class="btn btn-sm btn-primary btn-social hidden-print">
<x-icon type="print" />
{{ trans('admin/locations/table.print_all_assigned') }}
</a>
</div>
@can('delete', $location)
<div class="col-md-12 hidden-print" style="padding-top: 10px;">
@if ($location->deleted_at=='')
@if ($location->isDeletable())
<button class="btn btn-sm btn-block btn-danger btn-social delete-location" data-toggle="modal" data-title="{{ trans('general.delete') }}" data-content="{{ trans('general.sure_to_delete_var', ['item' => $location->name]) }}" data-target="#dataConfirmModal">
<x-icon type="delete" />
{{ trans('general.delete') }}
</button>
@else
<a href="#" class="btn btn-block btn-sm btn-danger btn-social hidden-print disabled" data-tooltip="true" data-placement="top" data-title="{{ trans('general.cannot_be_deleted') }}">
<x-icon type="delete" />
{{ trans('general.delete') }}
</a>
@endif
@else
<form method="POST" action="{{ route('locations.restore', ['location' => $location->id]) }}">
@csrf
<button class="btn btn-sm btn-block btn-warning btn-social delete-asset">
<x-icon type="restore" />
{{ trans('general.restore') }}
</button>
</form>
@endif
</div>
@endcan
@if ($location->image!='')
<div class="col-md-12 text-center" style="padding-bottom: 20px;">
<img src="{{ Storage::disk('public')->url('locations/'.e($location->image)) }}" class="img-responsive img-thumbnail" style="width:100%" alt="{{ $location->name }}">
</div>
@endif
<div class="col-md-12">
<ul class="list-unstyled" style="line-height: 25px; padding-bottom: 20px;">
@if ($location->address!='')
@ -426,21 +472,27 @@
</div>
@endif
</div>
</div>
</div>
</div>
</div>
</div>
@stop
@section('moar_scripts')
<script>
$('#dataConfirmModal').on('show.bs.modal', function (event) {
var content = $(event.relatedTarget).data('content');
var title = $(event.relatedTarget).data('title');
$(this).find(".modal-body").text(content);
$(this).find(".modal-header").text(title);
});
</script>
@include ('partials.bootstrap-table', [
'exportFile' => 'locations-export',
'search' => true
])
'exportFile' => 'locations-export',
'search' => true
])
@stop

View file

@ -66,6 +66,11 @@ Route::group(['middleware' => 'auth'], function () {
[LocationsController::class, 'postBulkDeleteStore']
)->name('locations.bulkdelete.store');
Route::post(
'{location}/restore',
[LocationsController::class, 'postRestore']
)->name('locations.restore');
Route::get('{locationId}/clone',
[LocationsController::class, 'getClone']