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; namespace App\Http\Controllers;
use App\Http\Requests\ImageUploadRequest; use App\Http\Requests\ImageUploadRequest;
use App\Models\Actionlog;
use App\Models\Asset; use App\Models\Asset;
use App\Models\Location; use App\Models\Location;
use App\Models\User; use App\Models\User;
@ -193,7 +194,13 @@ class LocationsController extends Controller
*/ */
public function show($id = null) : View | RedirectResponse 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)) { if (isset($location->id)) {
return view('locations/view', compact('location')); 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 public function print_all_assigned($id) : View | RedirectResponse
{ {
if ($location = Location::where('id', $id)->first()) { if ($location = Location::where('id', $id)->first()) {

View file

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

View file

@ -9,6 +9,10 @@
@parent @parent
@stop @stop
@section('header_right')
<a href="{{ route('locations.index') }}" class="btn btn-primary" style="margin-right: 10px;">
{{ trans('general.back') }}</a>
@endsection
{{-- Page content --}} {{-- Page content --}}
@section('content') @section('content')
@ -382,65 +386,113 @@
<div class="col-md-3"> <div class="col-md-3">
@can('update', $location)
<div class="col-md-12"> <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> </div>
@endcan
<div class="col-md-12" style="padding-top: 5px;"> <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>
<div class="col-md-12" style="padding-top: 5px; padding-bottom: 20px;"> <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> </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!='') @if ($location->image!='')
<div class="col-md-12 text-center" style="padding-bottom: 20px;"> <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 }}"> <img src="{{ Storage::disk('public')->url('locations/'.e($location->image)) }}" class="img-responsive img-thumbnail" style="width:100%" alt="{{ $location->name }}">
</div> </div>
@endif @endif
<div class="col-md-12">
<div class="col-md-12">
<ul class="list-unstyled" style="line-height: 25px; padding-bottom: 20px;"> <ul class="list-unstyled" style="line-height: 25px; padding-bottom: 20px;">
@if ($location->address!='') @if ($location->address!='')
<li>{{ $location->address }}</li> <li>{{ $location->address }}</li>
@endif @endif
@if ($location->address2!='') @if ($location->address2!='')
<li>{{ $location->address2 }}</li> <li>{{ $location->address2 }}</li>
@endif @endif
@if (($location->city!='') || ($location->state!='') || ($location->zip!='')) @if (($location->city!='') || ($location->state!='') || ($location->zip!=''))
<li>{{ $location->city }} {{ $location->state }} {{ $location->zip }}</li> <li>{{ $location->city }} {{ $location->state }} {{ $location->zip }}</li>
@endif @endif
@if ($location->manager) @if ($location->manager)
<li>{{ trans('admin/users/table.manager') }}: {!! $location->manager->present()->nameUrl() !!}</li> <li>{{ trans('admin/users/table.manager') }}: {!! $location->manager->present()->nameUrl() !!}</li>
@endif @endif
@if ($location->parent) @if ($location->parent)
<li>{{ trans('admin/locations/table.parent') }}: {!! $location->parent->present()->nameUrl() !!}</li> <li>{{ trans('admin/locations/table.parent') }}: {!! $location->parent->present()->nameUrl() !!}</li>
@endif @endif
@if ($location->ldap_ou) @if ($location->ldap_ou)
<li>{{ trans('admin/locations/table.ldap_ou') }}: {{ $location->ldap_ou }}</li> <li>{{ trans('admin/locations/table.ldap_ou') }}: {{ $location->ldap_ou }}</li>
@endif @endif
</ul> </ul>
@if (($location->state!='') && ($location->country!='') && (config('services.google.maps_api_key'))) @if (($location->state!='') && ($location->country!='') && (config('services.google.maps_api_key')))
<div class="col-md-12 text-center"> <div class="col-md-12 text-center">
<img src="https://maps.googleapis.com/maps/api/staticmap?markers={{ urlencode($location->address.','.$location->city.' '.$location->state.' '.$location->country.' '.$location->zip) }}&size=700x500&maptype=roadmap&key={{ config('services.google.maps_api_key') }}" class="img-thumbnail" style="width:100%" alt="Map"> <img src="https://maps.googleapis.com/maps/api/staticmap?markers={{ urlencode($location->address.','.$location->city.' '.$location->state.' '.$location->country.' '.$location->zip) }}&size=700x500&maptype=roadmap&key={{ config('services.google.maps_api_key') }}" class="img-thumbnail" style="width:100%" alt="Map">
</div> </div>
@endif @endif
</div>
</div>
</div>
</div> </div>
</div>
</div>
@stop @stop
@section('moar_scripts') @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', [ @include ('partials.bootstrap-table', [
'exportFile' => 'locations-export', 'exportFile' => 'locations-export',
'search' => true 'search' => true
]) ])
@stop @stop

View file

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