2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
namespace App\Http\Controllers;
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2017-10-25 22:35:58 -07:00
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2024-08-19 05:19:59 -07:00
|
|
|
use App\Models\Actionlog;
|
2021-06-10 13:15:52 -07:00
|
|
|
use App\Models\Asset;
|
2016-03-25 01:18:05 -07:00
|
|
|
use App\Models\Location;
|
2021-04-23 14:26:57 -07:00
|
|
|
use App\Models\User;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2024-02-20 09:27:56 -08:00
|
|
|
use Illuminate\Http\Request;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-07-04 14:37:58 -07:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use \Illuminate\Contracts\View\View;
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* This controller handles all actions related to Locations for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
class LocationsController extends Controller
|
|
|
|
{
|
2016-03-25 17:20:28 -07:00
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the locations listing, which is generated in getDatatable.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see LocationsController::getDatatable() method that generates the JSON response
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function index() : View
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Grab all the locations
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('view', Location::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Show the page
|
2018-12-12 18:23:39 -08:00
|
|
|
return view('locations/index');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Returns a form view used to create a new location.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see LocationsController::postCreate() method that validates and stores the data
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function create() : View
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('create', Location::class);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('locations/edit')
|
2017-10-28 11:17:52 -07:00
|
|
|
->with('item', new Location);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Validates and stores a new location.
|
|
|
|
*
|
|
|
|
* @todo Check if a Form Request would work better here.
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see LocationsController::getCreate() method that makes the form
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param ImageUploadRequest $request
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function store(ImageUploadRequest $request) : RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('create', Location::class);
|
2016-03-25 17:38:17 -07:00
|
|
|
$location = new Location();
|
2021-06-10 13:15:52 -07:00
|
|
|
$location->name = $request->input('name');
|
|
|
|
$location->parent_id = $request->input('parent_id', null);
|
|
|
|
$location->currency = $request->input('currency', '$');
|
|
|
|
$location->address = $request->input('address');
|
|
|
|
$location->address2 = $request->input('address2');
|
|
|
|
$location->city = $request->input('city');
|
|
|
|
$location->state = $request->input('state');
|
|
|
|
$location->country = $request->input('country');
|
|
|
|
$location->zip = $request->input('zip');
|
|
|
|
$location->ldap_ou = $request->input('ldap_ou');
|
|
|
|
$location->manager_id = $request->input('manager_id');
|
2024-07-04 14:37:58 -07:00
|
|
|
$location->user_id = auth()->id();
|
2023-07-10 11:44:21 -07:00
|
|
|
$location->phone = request('phone');
|
|
|
|
$location->fax = request('fax');
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2020-08-24 18:32:40 -07:00
|
|
|
$location = $request->handleImages($location);
|
2017-10-25 22:35:58 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($location->save()) {
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->route('locations.index')->with('success', trans('admin/locations/message.create.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($location->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Makes a form view to edit location information.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see LocationsController::postCreate() method that validates and stores
|
|
|
|
* @param int $locationId
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function edit($locationId = null) : View | RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-07-18 03:43:45 -07:00
|
|
|
$this->authorize('update', Location::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Check if the location exists
|
Partialize forms (#2884)
* Consolidate edit form elements into reusable partials.
This is a large code change that doesn't do much immediately. It
refactors all of the various edit.blade.php files to reference
standardized partials, so that they all reference the same base html
layout. This has the side effect of moving everything to the new fancy
"required" indicators, and making things look consistent.
In addition, I've gone ahead and renamed a few database fields. We had
Assetmodel::modelno and Consumable::model_no, I've renamed both to
model_number. We had items using ::note and ::notes, I've standardized
on ::notes. Component used total_qty where consumables and accessories
used qty, so I've moved everything to qty (And fixed a few bugs in the
helper file in the process.
TODO includes looking at how/where to place the modal javascripts to
allow for on the fly creation from all places, rather than just the
asset page.
Rename assetmodel::modelno to model_number for clarity and consistency
Rename consumable::model_no to model_number for clarity and consistency
Rename assetmodel::note to notes for clarity and consistency
Port asset and assetmodel to new partials layout. Adapt all code to the renamed model_number and notes database changes. Fix some stying.
* Share a settings variable with all views.
* Allow editing the per_page setting. We showed the value, but we never showed it on the edit page..
* use snipeSettings in all views instead of the long ugly path.
* War on partials. Centralize all bootstrap table javascript
* Use model_number instead of modelno in importer
* Codacy fix.
* More unification/deduplication. Create an edit form template layout that we use as the base for all edit forms. This gives the same interface for editing everything and makes the edit.blade.* files much easier to read.
* Use a ViewComposer instead of sharing the variable directly. Fixes artisan optimize trying to hit the db--which ruins new installs
* Fix DB seeder.
* Base sql dump and csv's to import data from for tests.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* A few things to make acceptance tests work. Add a name to the companies table, and make the locations table have the correct name
* Use a .env.tests file for testing functional and unit to allow a separate database.
* Add functional tests for compoents, groups, and licenses.
* Now that the config is in the functional.yml, this just confuses things.
* Start some functional tests for creating items.
* Add functional tests for all create methods. Still need to do tests for edits, deletes, and lots of other things
* Improvements to functional tests.
Use the built in DB seeding mechanism instead of doing it ourselves.
Break the tests into multiple units, rather than testing everything in
each function.
* Some improvements to acceptance tests.
Make sure we're only looking at the "trs" within the bootstrap table.
Creation of assets is now tested at the functional level (and is faster)
so ignore it here.
I'm testing acceptance tests with the
IMPORT_{ASSETS,ACCESSORIES,CONSUMABLES}.csv in the tests/_data folder
imported.
* update db dump
* Update tests to new reality
* env for the test setup
* only load the database at beginning of tests, not between each Functional test.
* Fix a miss from renaming note to notes.
* Set Termination date when creating an asset. It was only set on edit.
* Rename serial_number to serial in components for consistency.
* Update validation rules to match limits in database. Currently we just accepted the values and they were truncated when adding to DB.
* Much more detailed functional testing of creating items. This checks to make sure all values on form have been successfully persisted to database.
2016-11-16 16:56:57 -08:00
|
|
|
if (is_null($item = Location::find($locationId))) {
|
2017-10-15 23:32:40 -07:00
|
|
|
return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2019-10-02 03:56:56 -07:00
|
|
|
return view('locations/edit', compact('item'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Validates and stores updated location data from edit form.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see LocationsController::getEdit() method that makes the form view
|
|
|
|
* @param ImageUploadRequest $request
|
|
|
|
* @param int $locationId
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function update(ImageUploadRequest $request, $locationId = null) : RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-07-18 03:43:45 -07:00
|
|
|
$this->authorize('update', Location::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Check if the location exists
|
2016-03-25 17:38:17 -07:00
|
|
|
if (is_null($location = Location::find($locationId))) {
|
2017-10-15 23:32:40 -07:00
|
|
|
return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the location data
|
2021-06-10 13:15:52 -07:00
|
|
|
$location->name = $request->input('name');
|
|
|
|
$location->parent_id = $request->input('parent_id', null);
|
|
|
|
$location->currency = $request->input('currency', '$');
|
|
|
|
$location->address = $request->input('address');
|
|
|
|
$location->address2 = $request->input('address2');
|
|
|
|
$location->city = $request->input('city');
|
|
|
|
$location->state = $request->input('state');
|
|
|
|
$location->country = $request->input('country');
|
|
|
|
$location->zip = $request->input('zip');
|
2023-07-10 11:44:21 -07:00
|
|
|
$location->phone = request('phone');
|
|
|
|
$location->fax = request('fax');
|
2021-06-10 13:15:52 -07:00
|
|
|
$location->ldap_ou = $request->input('ldap_ou');
|
|
|
|
$location->manager_id = $request->input('manager_id');
|
2017-10-25 22:35:58 -07:00
|
|
|
|
2020-08-24 18:32:40 -07:00
|
|
|
$location = $request->handleImages($location);
|
2017-10-25 22:35:58 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($location->save()) {
|
2021-06-10 13:15:52 -07:00
|
|
|
return redirect()->route('locations.index')->with('success', trans('admin/locations/message.update.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withInput()->withInput()->withErrors($location->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Validates and deletes selected location.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $locationId
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function destroy($locationId) : RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('delete', Location::class);
|
2016-03-25 17:38:17 -07:00
|
|
|
if (is_null($location = Location::find($locationId))) {
|
2024-02-13 12:23:32 -08:00
|
|
|
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2020-05-23 10:36:02 -07:00
|
|
|
if ($location->users()->count() > 0) {
|
2017-02-22 22:49:19 -08:00
|
|
|
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_users'));
|
2020-05-23 10:36:02 -07:00
|
|
|
} elseif ($location->children()->count() > 0) {
|
2017-02-22 22:49:19 -08:00
|
|
|
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_child_loc'));
|
2020-05-23 10:36:02 -07:00
|
|
|
} elseif ($location->assets()->count() > 0) {
|
2017-02-22 22:49:19 -08:00
|
|
|
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_assets'));
|
2020-05-23 10:36:02 -07:00
|
|
|
} elseif ($location->assignedassets()->count() > 0) {
|
2017-02-22 22:49:19 -08:00
|
|
|
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.assoc_assets'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
|
2018-09-29 21:33:52 -07:00
|
|
|
if ($location->image) {
|
2021-06-10 13:15:52 -07:00
|
|
|
try {
|
2018-09-29 21:33:52 -07:00
|
|
|
Storage::disk('public')->delete('locations/'.$location->image);
|
|
|
|
} catch (\Exception $e) {
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::error($e);
|
2018-09-29 21:33:52 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-24 19:35:26 -07:00
|
|
|
$location->delete();
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-07-24 19:35:26 -07:00
|
|
|
return redirect()->to(route('locations.index'))->with('success', trans('admin/locations/message.delete.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-10 13:15:52 -07:00
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the locations detail page.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $id
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function show($id = null) : View | RedirectResponse
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2024-08-19 05:19:59 -07:00
|
|
|
$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);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if (isset($location->id)) {
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('locations/view', compact('location'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2020-04-07 17:26:56 -07:00
|
|
|
return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2019-03-09 00:34:47 -08:00
|
|
|
|
2024-07-04 14:37:58 -07:00
|
|
|
public function print_assigned($id) : View | RedirectResponse
|
2021-06-10 13:15:52 -07:00
|
|
|
{
|
2019-03-09 00:34:47 -08:00
|
|
|
|
2022-06-03 16:56:15 -07:00
|
|
|
if ($location = Location::where('id', $id)->first()) {
|
|
|
|
$parent = Location::where('id', $location->parent_id)->first();
|
|
|
|
$manager = User::where('id', $location->manager_id)->first();
|
|
|
|
$users = User::where('location_id', $id)->with('company', 'department', 'location')->get();
|
|
|
|
$assets = Asset::where('assigned_to', $id)->where('assigned_type', Location::class)->with('model', 'model.category')->get();
|
|
|
|
return view('locations/print')->with('assets', $assets)->with('users', $users)->with('location', $location)->with('parent', $parent)->with('manager', $manager);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist'));
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-03-09 00:34:47 -08:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2023-03-01 13:51:35 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view that presents a form to clone a location.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2023-03-01 14:03:33 -08:00
|
|
|
* @param int $locationId
|
2023-03-01 13:51:35 -08:00
|
|
|
* @since [v6.0.14]
|
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function getClone($locationId = null) : View | RedirectResponse
|
2023-03-01 13:51:35 -08:00
|
|
|
{
|
2023-03-01 14:01:40 -08:00
|
|
|
$this->authorize('create', Location::class);
|
|
|
|
|
2023-03-01 13:51:35 -08:00
|
|
|
// Check if the asset exists
|
2023-03-01 14:01:40 -08:00
|
|
|
if (is_null($location_to_clone = Location::find($locationId))) {
|
2023-03-01 13:51:35 -08:00
|
|
|
// Redirect to the asset management page
|
|
|
|
return redirect()->route('licenses.index')->with('error', trans('admin/locations/message.does_not_exist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$location = clone $location_to_clone;
|
2023-03-01 14:03:33 -08:00
|
|
|
|
|
|
|
// unset these values
|
2023-03-01 13:51:35 -08:00
|
|
|
$location->id = null;
|
|
|
|
$location->image = null;
|
|
|
|
|
|
|
|
return view('locations/edit')
|
|
|
|
->with('item', $location);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-19 05:19:59 -07:00
|
|
|
/**
|
|
|
|
* 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'));
|
|
|
|
|
|
|
|
}
|
2024-07-04 14:37:58 -07:00
|
|
|
public function print_all_assigned($id) : View | RedirectResponse
|
2019-03-09 00:34:47 -08:00
|
|
|
{
|
2022-06-03 16:56:15 -07:00
|
|
|
if ($location = Location::where('id', $id)->first()) {
|
|
|
|
$parent = Location::where('id', $location->parent_id)->first();
|
|
|
|
$manager = User::where('id', $location->manager_id)->first();
|
|
|
|
$users = User::where('location_id', $id)->with('company', 'department', 'location')->get();
|
|
|
|
$assets = Asset::where('location_id', $id)->with('model', 'model.category')->get();
|
|
|
|
return view('locations/print')->with('assets', $assets)->with('users', $users)->with('location', $location)->with('parent', $parent)->with('manager', $manager);
|
|
|
|
|
|
|
|
}
|
|
|
|
return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist'));
|
2024-02-20 09:27:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view that allows the user to bulk delete locations
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v6.3.1]
|
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function postBulkDelete(Request $request) : View | RedirectResponse
|
2024-02-20 09:27:56 -08:00
|
|
|
{
|
|
|
|
$locations_raw_array = $request->input('ids');
|
|
|
|
|
|
|
|
// Make sure some IDs have been selected
|
|
|
|
if ((is_array($locations_raw_array)) && (count($locations_raw_array) > 0)) {
|
2024-02-20 14:25:00 -08:00
|
|
|
$locations = Location::whereIn('id', $locations_raw_array)
|
|
|
|
->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')->get();
|
2024-02-20 09:27:56 -08:00
|
|
|
|
|
|
|
$valid_count = 0;
|
|
|
|
foreach ($locations as $location) {
|
|
|
|
if ($location->isDeletable()) {
|
|
|
|
$valid_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return view('locations/bulk-delete', compact('locations'))->with('valid_count', $valid_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('error', 'You must select at least one model to edit.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that locations can be deleted and deletes them if they can
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v6.3.1]
|
2024-07-04 14:37:58 -07:00
|
|
|
|
2024-02-20 09:27:56 -08:00
|
|
|
*/
|
2024-07-04 14:37:58 -07:00
|
|
|
public function postBulkDeleteStore(Request $request) : RedirectResponse
|
|
|
|
{
|
2024-02-20 09:27:56 -08:00
|
|
|
$locations_raw_array = $request->input('ids');
|
|
|
|
|
|
|
|
if ((is_array($locations_raw_array)) && (count($locations_raw_array) > 0)) {
|
2024-03-19 15:18:18 -07:00
|
|
|
$locations = Location::whereIn('id', $locations_raw_array)
|
|
|
|
->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')->get();
|
2024-02-20 09:27:56 -08:00
|
|
|
|
|
|
|
$success_count = 0;
|
|
|
|
$error_count = 0;
|
|
|
|
|
|
|
|
foreach ($locations as $location) {
|
|
|
|
|
|
|
|
// Can we delete this location?
|
|
|
|
if ($location->isDeletable()) {
|
|
|
|
$location->delete();
|
|
|
|
$success_count++;
|
|
|
|
} else {
|
|
|
|
$error_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 04:38:15 -07:00
|
|
|
Log::debug('Success count: '.$success_count);
|
|
|
|
Log::debug('Error count: '.$error_count);
|
2024-02-20 09:27:56 -08:00
|
|
|
// Complete success
|
|
|
|
if ($success_count == count($locations_raw_array)) {
|
|
|
|
return redirect()
|
|
|
|
->route('locations.index')
|
|
|
|
->with('success', trans_choice('general.bulk.delete.success', $success_count,
|
|
|
|
['object_type' => trans_choice('general.location_plural', $success_count), 'count' => $success_count]
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Partial success
|
|
|
|
if ($error_count > 0) {
|
|
|
|
return redirect()
|
|
|
|
->route('locations.index')
|
2024-03-19 14:32:21 -07:00
|
|
|
->with('warning', trans('general.bulk.delete.partial',
|
2024-02-20 09:27:56 -08:00
|
|
|
['success' => $success_count, 'error' => $error_count, 'object_type' => trans('general.locations')]
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 16:56:15 -07:00
|
|
|
|
|
|
|
|
2024-02-20 09:27:56 -08:00
|
|
|
// Nothing was selected - return to the index
|
|
|
|
return redirect()
|
|
|
|
->route('locations.index')
|
|
|
|
->with('error', trans('general.bulk.nothing_selected',
|
|
|
|
['object_type' => trans('general.locations')]
|
|
|
|
));
|
2019-03-09 00:34:47 -08:00
|
|
|
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|