2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
use App\Helpers\Helper;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Input;
|
|
|
|
use Lang;
|
|
|
|
use App\Models\Location;
|
2016-12-19 11:04:28 -08:00
|
|
|
use phpDocumentor\Reflection\Types\Array_;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Redirect;
|
|
|
|
use App\Models\Setting;
|
2016-04-19 20:23:04 -07:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Asset;
|
2016-03-25 01:18:05 -07:00
|
|
|
use DB;
|
|
|
|
use Str;
|
|
|
|
use Validator;
|
|
|
|
use View;
|
|
|
|
use Auth;
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2017-10-25 22:35:58 -07:00
|
|
|
use Image;
|
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
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
|
|
|
/**
|
|
|
|
* 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
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2016-12-15 17:12:22 -08:00
|
|
|
public function index()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-03-25 17:20:28 -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
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2016-12-15 17:12:22 -08:00
|
|
|
public function create()
|
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
|
|
|
$locations = Location::orderBy('name', 'ASC')->get();
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-03-25 17:38:17 -07:00
|
|
|
$location_options_array = Location::getLocationHierarchy($locations);
|
|
|
|
$location_options = Location::flattenLocationsArray($location_options_array);
|
2016-03-25 01:18:05 -07:00
|
|
|
$location_options = array('' => 'Top Level') + $location_options;
|
|
|
|
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('locations/edit')
|
2016-12-19 22:00:50 -08:00
|
|
|
->with('location_options', $location_options)
|
2017-10-28 11:17:52 -07:00
|
|
|
->with('item', new Location);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-03-25 17:20:28 -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]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2017-10-25 22:35:58 -07:00
|
|
|
public function store(ImageUploadRequest $request)
|
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();
|
2017-10-25 22:35:58 -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');
|
2016-12-19 22:00:50 -08:00
|
|
|
$location->user_id = Auth::id();
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2017-10-25 22:35:58 -07:00
|
|
|
if ($request->file('image')) {
|
|
|
|
$image = $request->file('image');
|
|
|
|
$file_name = str_random(25).".".$image->getClientOriginalExtension();
|
|
|
|
$path = public_path('uploads/locations/'.$file_name);
|
2019-05-24 19:11:08 -07:00
|
|
|
Image::make($image->getRealPath())->resize(800, null, function ($constraint) {
|
2017-10-25 22:35:58 -07:00
|
|
|
$constraint->aspectRatio();
|
|
|
|
$constraint->upsize();
|
|
|
|
})->save($path);
|
|
|
|
$location->image = $file_name;
|
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($location->save()) {
|
2016-12-15 17:12:22 -08:00
|
|
|
return redirect()->route("locations.index")->with('success', trans('admin/locations/message.create.success'));
|
2016-03-25 01:18:05 -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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-03-25 17:20:28 -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
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2016-12-15 17:12:22 -08:00
|
|
|
public function edit($locationId = null)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Show the page
|
2016-03-25 17:38:17 -07:00
|
|
|
$locations = Location::orderBy('name', 'ASC')->get();
|
|
|
|
$location_options_array = Location::getLocationHierarchy($locations);
|
|
|
|
$location_options = Location::flattenLocationsArray($location_options_array);
|
2016-03-25 01:18:05 -07:00
|
|
|
$location_options = array('' => 'Top Level') + $location_options;
|
|
|
|
|
2017-05-22 17:27:00 -07:00
|
|
|
return view('locations/edit', compact('item'))
|
2018-07-16 14:22:25 -07:00
|
|
|
->with('location_options', $location_options);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-03-25 17:20:28 -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 int $locationId
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2017-10-25 22:35:58 -07:00
|
|
|
public function update(ImageUploadRequest $request, $locationId = null)
|
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
|
2017-10-25 22:35:58 -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');
|
|
|
|
|
2017-11-07 11:28:13 -08:00
|
|
|
$old_image = $location->image;
|
|
|
|
|
|
|
|
// Set the model's image property to null if the image is being deleted
|
|
|
|
if ($request->input('image_delete') == 1) {
|
|
|
|
$location->image = null;
|
|
|
|
}
|
|
|
|
|
2017-10-25 22:35:58 -07:00
|
|
|
if ($request->file('image')) {
|
|
|
|
$image = $request->file('image');
|
2017-11-07 11:28:13 -08:00
|
|
|
$file_name = $location->id.'-'.str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension();
|
|
|
|
|
|
|
|
if ($image->getClientOriginalExtension()!='svg') {
|
2019-05-24 19:11:08 -07:00
|
|
|
Image::make($image->getRealPath())->resize(800, null, function ($constraint) {
|
2017-11-07 11:28:13 -08:00
|
|
|
$constraint->aspectRatio();
|
|
|
|
$constraint->upsize();
|
|
|
|
})->save(app('locations_upload_path').$file_name);
|
|
|
|
} else {
|
|
|
|
$image->move(app('locations_upload_path'), $file_name);
|
|
|
|
}
|
2017-10-25 22:35:58 -07:00
|
|
|
$location->image = $file_name;
|
2017-11-07 11:28:13 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((($request->file('image')) && (isset($old_image)) && ($old_image!='')) || ($request->input('image_delete') == 1)) {
|
|
|
|
try {
|
|
|
|
unlink(app('locations_upload_path').$old_image);
|
|
|
|
} catch (\Exception $e) {
|
2019-03-13 12:22:12 -07:00
|
|
|
\Log::info($e);
|
2017-11-07 11:28:13 -08:00
|
|
|
}
|
2017-10-25 22:35:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($location->save()) {
|
2016-12-19 22:00:50 -08:00
|
|
|
return redirect()->route("locations.index")->with('success', trans('admin/locations/message.update.success'));
|
2016-03-25 01:18:05 -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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-25 17:20:28 -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
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2016-12-15 17:12:22 -08:00
|
|
|
public function destroy($locationId)
|
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))) {
|
2017-02-22 22:49:19 -08:00
|
|
|
return redirect()->to(route('locations.index'))->with('error', trans('admin/locations/message.not_found'));
|
2016-03-25 01:18:05 -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'));
|
2017-10-25 22:35:58 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
} elseif ($location->childLocations->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'));
|
2017-10-25 22:35:58 -07:00
|
|
|
|
2016-03-25 01:18:05 -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'));
|
2017-10-25 22:35:58 -07:00
|
|
|
|
2016-03-25 01:18:05 -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'));
|
2017-10-25 22:35:58 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
|
|
|
$location->delete();
|
2017-02-22 22:49:19 -08:00
|
|
|
return redirect()->to(route('locations.index'))->with('success', trans('admin/locations/message.delete.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-03-25 17:20:28 -07:00
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the locations detail page.
|
2016-03-25 01:18:05 -07:00
|
|
|
*
|
2016-03-25 17:20:28 -07:00
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $locationId
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2016-12-15 17:12:22 -08:00
|
|
|
public function show($locationId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-03-25 17:38:17 -07:00
|
|
|
$location = Location::find($locationId);
|
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
|
|
|
}
|
|
|
|
|
2017-10-25 22:35:58 -07:00
|
|
|
return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist', compact('id')));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|