2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Image;
|
|
|
|
use Input;
|
|
|
|
use Lang;
|
|
|
|
use App\Models\AssetModel;
|
|
|
|
use Redirect;
|
|
|
|
use Auth;
|
|
|
|
use DB;
|
|
|
|
use Str;
|
|
|
|
use Validator;
|
|
|
|
use View;
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\Company;
|
|
|
|
use Config;
|
|
|
|
use App\Helpers\Helper;
|
2016-12-15 15:56:52 -08:00
|
|
|
use Illuminate\Http\Request;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:21:09 -07:00
|
|
|
* This class controls all actions related to asset models for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2016-03-25 01:18:05 -07:00
|
|
|
*/
|
|
|
|
class AssetModelsController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the accessories listing, which is generated in getDatatable.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @see AssetModelsController::getDatatable() method that generates the JSON response
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-12-15 06:11:03 -08:00
|
|
|
public function index()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Show the page
|
|
|
|
return View::make('models/index');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view containing the asset model creation form.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-12-15 06:11:03 -08:00
|
|
|
public function create()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Show the page
|
|
|
|
return View::make('models/edit')
|
2016-12-19 22:00:50 -08:00
|
|
|
->with('category_list', Helper::categoryList('asset'))
|
|
|
|
->with('depreciation_list', Helper::depreciationList())
|
|
|
|
->with('manufacturer_list', Helper::manufacturerList())
|
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
|
|
|
->with('item', new AssetModel);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate and process the new Asset Model data.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-12-15 15:56:52 -08:00
|
|
|
public function store(Request $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
// Create a new asset model
|
|
|
|
$model = new AssetModel;
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
if ($request->input('depreciation_id') == '') {
|
2016-03-25 01:18:05 -07:00
|
|
|
$model->depreciation_id = 0;
|
|
|
|
} else {
|
2016-12-19 22:00:50 -08:00
|
|
|
$model->depreciation_id = $request->input('depreciation_id');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
if ($request->input('eol') == '') {
|
2016-03-25 01:18:05 -07:00
|
|
|
$model->eol = 0;
|
|
|
|
} else {
|
2016-12-19 22:00:50 -08:00
|
|
|
$model->eol = $request->input('eol');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-09-15 19:58:27 -07:00
|
|
|
// Save the model data
|
2016-12-19 22:00:50 -08:00
|
|
|
$model->name = $request->input('name');
|
|
|
|
$model->model_number = $request->input('model_number');
|
|
|
|
$model->manufacturer_id = $request->input('manufacturer_id');
|
|
|
|
$model->category_id = $request->input('category_id');
|
|
|
|
$model->notes = $request->input('notes');
|
|
|
|
$model->user_id = Auth::id();
|
2016-09-15 19:58:27 -07:00
|
|
|
$model->requestable = Input::has('requestable');
|
|
|
|
|
2016-12-15 15:56:52 -08:00
|
|
|
if ($request->input('custom_fieldset')!='') {
|
|
|
|
$model->fieldset_id = e($request->input('custom_fieldset'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Input::file('image')) {
|
|
|
|
$image = Input::file('image');
|
|
|
|
$file_name = str_random(25).".".$image->getClientOriginalExtension();
|
|
|
|
$path = public_path('uploads/models/'.$file_name);
|
2016-06-22 12:27:41 -07:00
|
|
|
Image::make($image->getRealPath())->resize(500, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
$constraint->upsize();
|
|
|
|
})->save($path);
|
2016-03-25 01:18:05 -07:00
|
|
|
$model->image = $file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Was it created?
|
|
|
|
if ($model->save()) {
|
|
|
|
// Redirect to the new model page
|
2016-12-15 06:11:03 -08:00
|
|
|
return redirect()->route("models.index")->with('success', trans('admin/models/message.create.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
return redirect()->back()->withInput()->withErrors($model->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-19 22:00:50 -08:00
|
|
|
* Validates and stores new Asset Model data created from the
|
|
|
|
* modal form on the Asset Creation view.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v2.0]
|
|
|
|
* @param Request $request
|
|
|
|
* @return String JSON
|
|
|
|
*/
|
2016-12-15 15:56:52 -08:00
|
|
|
public function apiStore(Request $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
//COPYPASTA!!!! FIXME
|
2016-03-25 19:26:22 -07:00
|
|
|
$model = new AssetModel;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
$settings=Input::all();
|
2016-09-27 06:53:59 -07:00
|
|
|
$settings['eol']= null;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
$model->name=$request->input('name');
|
|
|
|
$model->manufacturer_id = $request->input('manufacturer_id');
|
|
|
|
$model->category_id = $request->input('category_id');
|
|
|
|
$model->model_number = $request->input('model_number');
|
|
|
|
$model->user_id = Auth::id();
|
|
|
|
$model->notes = $request->input('notes');
|
2016-09-27 06:53:59 -07:00
|
|
|
$model->eol= null;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-12-15 15:56:52 -08:00
|
|
|
if ($request->input('fieldset_id')=='') {
|
2016-04-23 03:12:31 -07:00
|
|
|
$model->fieldset_id = null;
|
|
|
|
} else {
|
2016-12-15 15:56:52 -08:00
|
|
|
$model->fieldset_id = e($request->input('fieldset_id'));
|
2016-04-23 03:12:31 -07:00
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($model->save()) {
|
|
|
|
return JsonResponse::create($model);
|
|
|
|
} else {
|
|
|
|
return JsonResponse::create(["error" => "Failed validation: ".print_r($model->getErrors()->all('<li>:message</li>'), true)], 500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view containing the asset model edit form.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-12-15 06:11:03 -08:00
|
|
|
public function edit($modelId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Check if the model 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 = AssetModel::find($modelId))) {
|
2016-03-25 01:18:05 -07:00
|
|
|
// Redirect to the model management page
|
2016-12-19 11:04:28 -08:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
$view = View::make('models/edit', compact('item'));
|
2016-12-19 22:00:50 -08:00
|
|
|
$view->with('category_list', Helper::categoryList('asset'));
|
|
|
|
$view->with('depreciation_list', Helper::depreciationList());
|
|
|
|
$view->with('manufacturer_list', Helper::manufacturerList());
|
2016-03-25 01:18:05 -07:00
|
|
|
return $view;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates and processes form data from the edit
|
|
|
|
* Asset Model form based on the model ID passed.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-12-15 15:56:52 -08:00
|
|
|
public function update(Request $request, $modelId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Check if the model exists
|
|
|
|
if (is_null($model = AssetModel::find($modelId))) {
|
|
|
|
// Redirect to the models management page
|
2016-12-19 11:04:28 -08:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
if ($request->input('depreciation_id') == '') {
|
2016-03-25 01:18:05 -07:00
|
|
|
$model->depreciation_id = 0;
|
|
|
|
} else {
|
2016-12-19 22:00:50 -08:00
|
|
|
$model->depreciation_id = $request->input('depreciation_id');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
if ($request->input('eol') == '') {
|
2016-09-27 06:53:59 -07:00
|
|
|
$model->eol = null;
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
2016-12-19 22:00:50 -08:00
|
|
|
$model->eol = $request->input('eol');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-15 15:56:52 -08:00
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
$model->name = $request->input('name');
|
|
|
|
$model->model_number = $request->input('model_number');
|
|
|
|
$model->manufacturer_id = $request->input('manufacturer_id');
|
|
|
|
$model->category_id = $request->input('category_id');
|
|
|
|
$model->notes = $request->input('notes');
|
2016-09-15 19:58:27 -07:00
|
|
|
|
|
|
|
$model->requestable = Input::has('requestable');
|
|
|
|
|
2016-12-15 15:56:52 -08:00
|
|
|
if ($request->input('custom_fieldset')=='') {
|
2016-03-25 01:18:05 -07:00
|
|
|
$model->fieldset_id = null;
|
|
|
|
} else {
|
2016-12-19 22:00:50 -08:00
|
|
|
$model->fieldset_id = $request->input('custom_fieldset');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Input::file('image')) {
|
|
|
|
$image = Input::file('image');
|
|
|
|
$file_name = str_random(25).".".$image->getClientOriginalExtension();
|
|
|
|
$path = public_path('uploads/models/'.$file_name);
|
|
|
|
Image::make($image->getRealPath())->resize(300, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
$constraint->upsize();
|
|
|
|
})->save($path);
|
|
|
|
$model->image = $file_name;
|
|
|
|
}
|
|
|
|
|
2016-12-15 15:56:52 -08:00
|
|
|
if ($request->input('image_delete') == 1 && Input::file('image') == "") {
|
2016-03-25 01:18:05 -07:00
|
|
|
$model->image = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($model->save()) {
|
2016-12-15 06:11:03 -08:00
|
|
|
return redirect()->route("models.index")->with('success', trans('admin/models/message.update.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
return redirect()->back()->withInput()->withErrors($model->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate and delete the given Asset Model. An Asset Model
|
|
|
|
* cannot be deleted if there are associated assets.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
2016-12-15 06:11:03 -08:00
|
|
|
public function destroy($modelId)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Check if the model exists
|
|
|
|
if (is_null($model = AssetModel::find($modelId))) {
|
2016-12-19 11:04:28 -08:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
if ($model->assets()->count() > 0) {
|
2016-03-25 01:18:05 -07:00
|
|
|
// Throw an error that this model is associated with assets
|
2016-12-15 06:11:03 -08:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.assoc_users'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
// Delete the model
|
|
|
|
$model->delete();
|
|
|
|
|
|
|
|
// Redirect to the models management page
|
|
|
|
return redirect()->route('models.index')->with('success', trans('admin/models/message.delete.success'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore a given Asset Model (mark as un-deleted)
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return Redirect
|
|
|
|
*/
|
|
|
|
public function getRestore($modelId = null)
|
|
|
|
{
|
|
|
|
|
|
|
|
// Get user information
|
|
|
|
$model = AssetModel::withTrashed()->find($modelId);
|
|
|
|
|
|
|
|
if (isset($model->id)) {
|
|
|
|
|
|
|
|
// Restore the model
|
|
|
|
$model->restore();
|
|
|
|
|
|
|
|
// Prepare the success message
|
2016-04-07 13:39:35 -07:00
|
|
|
$success = trans('admin/models/message.restore.success');
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
// Redirect back
|
2016-12-15 06:11:03 -08:00
|
|
|
return redirect()->route('models.index')->with('success', $success);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
return redirect()->back()->with('error', trans('admin/models/message.not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the model information to present to the model view page
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-12-15 06:11:03 -08:00
|
|
|
public function show($modelId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
$model = AssetModel::withTrashed()->find($modelId);
|
|
|
|
|
|
|
|
if (isset($model->id)) {
|
2016-12-19 22:00:50 -08:00
|
|
|
return View::make('models/view', compact('model'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
// Prepare the error message
|
|
|
|
$error = trans('admin/models/message.does_not_exist', compact('id'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
// Redirect to the user management page
|
|
|
|
return redirect()->route('models.index')->with('error', $error);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the clone page to clone a model
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
public function getClone($modelId = null)
|
|
|
|
{
|
|
|
|
// Check if the model exists
|
|
|
|
if (is_null($model_to_clone = AssetModel::find($modelId))) {
|
2016-12-19 11:04:28 -08:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$model = clone $model_to_clone;
|
|
|
|
$model->id = null;
|
|
|
|
|
|
|
|
// Show the page
|
|
|
|
$view = View::make('models/edit');
|
2016-12-19 22:00:50 -08:00
|
|
|
$view->with('category_list', Helper::categoryList('asset'));
|
|
|
|
$view->with('depreciation_list', Helper::depreciationList());
|
|
|
|
$view->with('manufacturer_list', Helper::manufacturerList());
|
2016-11-29 06:16:52 -08:00
|
|
|
$view->with('item', $model);
|
2016-03-25 01:18:05 -07:00
|
|
|
$view->with('clone_model', $model_to_clone);
|
|
|
|
return $view;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the custom fields form
|
|
|
|
*
|
|
|
|
* @author [B. Wetherington] [<uberbrady@gmail.com>]
|
|
|
|
* @since [v2.0]
|
|
|
|
* @param int $modelId
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
public function getCustomFields($modelId)
|
|
|
|
{
|
2016-11-29 06:06:52 -08:00
|
|
|
$model = AssetModel::find($modelId);
|
2016-03-25 01:18:05 -07:00
|
|
|
return View::make("models.custom_fields_form")->with("model", $model);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the JSON response to populate the data tables on the
|
|
|
|
* Asset Model listing page.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v2.0]
|
|
|
|
* @param string $status
|
|
|
|
* @return String JSON
|
|
|
|
*/
|
|
|
|
|
2016-12-15 15:56:52 -08:00
|
|
|
public function getDatatable(Request $request, $status = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-10-28 14:15:13 -07:00
|
|
|
$models = AssetModel::with('category', 'assets', 'depreciation', 'manufacturer');
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
switch ($status) {
|
|
|
|
case 'Deleted':
|
|
|
|
$models->withTrashed()->Deleted();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Input::has('search')) {
|
2016-12-15 15:56:52 -08:00
|
|
|
$models = $models->TextSearch($request->input('search'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-12-19 22:00:50 -08:00
|
|
|
$offset = request('offset', 0);
|
|
|
|
$limit = request('limit', 50);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
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
|
|
|
$allowed_columns = ['id','name','model_number'];
|
2016-12-15 15:56:52 -08:00
|
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
|
|
|
$sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at';
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
$models = $models->orderBy($sort, $order);
|
|
|
|
|
|
|
|
$modelCount = $models->count();
|
|
|
|
$models = $models->skip($offset)->take($limit)->get();
|
|
|
|
|
|
|
|
$rows = array();
|
|
|
|
|
|
|
|
foreach ($models as $model) {
|
2016-12-19 22:00:50 -08:00
|
|
|
$actions = '<div style="white-space: nowrap;">';
|
2016-03-25 01:18:05 -07:00
|
|
|
if ($model->deleted_at == '') {
|
2016-12-19 22:00:50 -08:00
|
|
|
$actions .= Helper::generateDatatableButton('clone', route('clone/model', $model->id));
|
|
|
|
$actions .= Helper::generateDatatableButton('edit', route('models.edit', $model->id));
|
|
|
|
$actions .= Helper::generateDatatableButton(
|
|
|
|
'delete',
|
|
|
|
route('models.destroy', $model->id),
|
|
|
|
trans('admin/models/message.delete.confirm'),
|
|
|
|
$model->name
|
|
|
|
);
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
2016-12-19 22:00:50 -08:00
|
|
|
$actions .= Helper::generateDatatableButton('restore', route('restore/model', $model->id));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$rows[] = array(
|
2016-12-19 22:00:50 -08:00
|
|
|
'id' => $model->id,
|
2016-12-19 11:04:28 -08:00
|
|
|
'manufacturer' => (string)link_to_route('manufacturers.show', $model->manufacturer->name, ['manufacturer' => $model->manufacturer->id]),
|
2016-12-15 06:11:03 -08:00
|
|
|
'name' => (string)link_to_route('models.show',$model->name, ['model' => $model->id]),
|
2016-12-19 11:04:28 -08:00
|
|
|
'image' => ($model->image!='') ? '<img src="'.url('/').'/uploads/models/'.$model->image.'" height=50 width=50>' : '',
|
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
|
|
|
'modelnumber' => $model->model_number,
|
2016-03-25 01:18:05 -07:00
|
|
|
'numassets' => $model->assets->count(),
|
2016-11-29 06:06:52 -08:00
|
|
|
'depreciation' => (($model->depreciation) && ($model->depreciation->id > 0)) ? $model->depreciation->name.' ('.$model->depreciation->months.')' : trans('general.no_depreciation'),
|
2016-12-19 11:04:28 -08:00
|
|
|
'category' => ($model->category) ? (string)link_to_route('categories.show', $model->category->name, ['category' => $model->category->id]) : '',
|
2016-04-07 13:39:35 -07:00
|
|
|
'eol' => ($model->eol) ? $model->eol.' '.trans('general.months') : '',
|
2016-11-29 06:06:52 -08:00
|
|
|
'note' => $model->getNote(),
|
2016-12-19 11:04:28 -08:00
|
|
|
'fieldset' => ($model->fieldset) ? (string)link_to_route('custom_fields/model', $model->fieldset->name, ['model' => $model->fieldset->id]) : '',
|
2016-03-25 01:18:05 -07:00
|
|
|
'actions' => $actions
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = array('total' => $modelCount, 'rows' => $rows);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-12-19 11:04:28 -08:00
|
|
|
* Get the asset information to present to the model view detail page
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v2.0]
|
|
|
|
* @param Request $request
|
|
|
|
* @param $modelID
|
|
|
|
* @return String JSON
|
|
|
|
* @internal param int $modelId
|
|
|
|
*/
|
|
|
|
public function getDataView(Request $request, $modelID)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-10-28 14:15:13 -07:00
|
|
|
$assets = Asset::where('model_id', '=', $modelID)->with('company', 'assetstatus');
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if (Input::has('search')) {
|
2016-12-15 15:56:52 -08:00
|
|
|
$assets = $assets->TextSearch(e($request->input('search')));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 11:04:28 -08:00
|
|
|
$offset = request('offset',0);
|
|
|
|
$limit = request('limit', 50);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
|
|
|
|
$allowed_columns = ['name', 'serial','asset_tag'];
|
2016-12-15 15:56:52 -08:00
|
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
|
|
|
$sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at';
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
$assets = $assets->orderBy($sort, $order);
|
|
|
|
|
|
|
|
$assetsCount = $assets->count();
|
|
|
|
$assets = $assets->skip($offset)->take($limit)->get();
|
|
|
|
|
|
|
|
$rows = array();
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($assets as $asset) {
|
|
|
|
$actions = '';
|
|
|
|
|
|
|
|
if ($asset->assetstatus) {
|
|
|
|
if ($asset->assetstatus->deployable != 0) {
|
|
|
|
if (($asset->assigned_to !='') && ($asset->assigned_to > 0)) {
|
2016-12-19 22:00:50 -08:00
|
|
|
$actions = Helper::generateDatatableButton('checkin', route('checkin/hardware', $asset->id));
|
2016-03-25 01:18:05 -07:00
|
|
|
} else {
|
2016-12-19 22:00:50 -08:00
|
|
|
$actions = Helper::generateDatatableButton('checkout', route('checkout/hardware', $asset->id));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows[] = array(
|
|
|
|
'id' => $asset->id,
|
2016-12-19 11:04:28 -08:00
|
|
|
'name' => (string)link_to_route('hardware.show', $asset->showAssetName(), ['asset' => $asset->id]),
|
|
|
|
'asset_tag' => (string)link_to_route('hardware.show', $asset->asset_tag, ['asset' => $asset->id]),
|
2016-03-25 01:18:05 -07:00
|
|
|
'serial' => $asset->serial,
|
2016-12-19 11:04:28 -08:00
|
|
|
'assigned_to' => ($asset->assigned_to) ? (string)link_to_route('users.show', $asset->assigneduser->fullName(), ['asset' =>$asset->assigned_to]) : '',
|
2016-03-25 01:18:05 -07:00
|
|
|
'actions' => $actions,
|
|
|
|
'companyName' => Company::getName($asset)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = array('total' => $assetsCount, 'rows' => $rows);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|