2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2016-12-23 17:52:00 -08:00
|
|
|
use App\Models\CustomField;
|
2016-03-25 01:18:05 -07:00
|
|
|
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;
|
2017-10-06 16:56:43 -07:00
|
|
|
use App\Http\Requests\ImageUploadRequest;
|
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>]
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return View
|
|
|
|
*/
|
2016-12-15 06:11:03 -08:00
|
|
|
public function index()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('index', AssetModel::class);
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('models/index');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('create', AssetModel::class);
|
2017-10-28 11:17:52 -07:00
|
|
|
$category_type = 'asset';
|
|
|
|
return view('models/edit')->with('category_type',$category_type)
|
2016-12-19 22:00:50 -08:00
|
|
|
->with('depreciation_list', Helper::depreciationList())
|
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
|
|
|
|
*/
|
2017-10-06 16:56:43 -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', AssetModel::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// Create a new asset model
|
|
|
|
$model = new AssetModel;
|
|
|
|
|
2016-09-15 19:58:27 -07:00
|
|
|
// Save the model data
|
2016-12-26 15:17:46 -08:00
|
|
|
$model->eol = $request->input('eol');
|
|
|
|
$model->depreciation_id = $request->input('depreciation_id');
|
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');
|
2017-09-25 11:53:10 -07:00
|
|
|
$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')) {
|
2017-10-06 16:56:43 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
$image = Input::file('image');
|
2017-11-02 08:02:51 -07:00
|
|
|
$file_name = str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension();
|
2017-11-07 11:28:13 -08:00
|
|
|
$path = app('models_upload_path');
|
2017-10-06 16:56:43 -07:00
|
|
|
|
|
|
|
if ($image->getClientOriginalExtension()!='svg') {
|
|
|
|
Image::make($image->getRealPath())->resize(500, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
$constraint->upsize();
|
|
|
|
})->save($path.'/'.$file_name);
|
|
|
|
} else {
|
|
|
|
$image->move($path, $file_name);
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
$model->image = $file_name;
|
2017-10-06 16:56:43 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Was it created?
|
|
|
|
if ($model->save()) {
|
2018-04-23 21:16:55 -07:00
|
|
|
if ($this->shouldAddDefaultValues($request->input())) {
|
|
|
|
$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'));
|
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2018-03-07 18:22:49 -08:00
|
|
|
$this->authorize('update', AssetModel::class);
|
2017-10-28 11:17:52 -07:00
|
|
|
if ($item = AssetModel::find($modelId)) {
|
|
|
|
$category_type = 'asset';
|
|
|
|
$view = View::make('models/edit', compact('item','category_type'));
|
|
|
|
$view->with('depreciation_list', Helper::depreciationList());
|
|
|
|
return $view;
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2017-10-28 11:17:52 -07:00
|
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-10-06 17:03:51 -07:00
|
|
|
public function update(ImageUploadRequest $request, $modelId = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2018-03-07 18:22:49 -08:00
|
|
|
$this->authorize('update', AssetModel::class);
|
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
|
|
|
}
|
|
|
|
|
2017-11-07 11:28:13 -08:00
|
|
|
$model->depreciation_id = $request->input('depreciation_id');
|
|
|
|
$model->eol = $request->input('eol');
|
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');
|
2017-11-07 11:28:13 -08:00
|
|
|
$model->requestable = $request->input('requestable', '0');
|
2016-09-15 19:58:27 -07:00
|
|
|
|
2018-04-23 21:16:55 -07:00
|
|
|
$this->removeCustomFieldsDefaultValues($model);
|
|
|
|
|
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');
|
2018-04-23 21:16:55 -07:00
|
|
|
|
|
|
|
if ($this->shouldAddDefaultValues($request->input())) {
|
|
|
|
$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'));
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2017-11-07 11:28:13 -08:00
|
|
|
$old_image = $model->image;
|
2017-10-28 05:46:43 -07:00
|
|
|
|
2017-11-07 11:28:13 -08:00
|
|
|
// Set the model's image property to null if the image is being deleted
|
|
|
|
if ($request->input('image_delete') == 1) {
|
|
|
|
$model->image = null;
|
|
|
|
}
|
2017-10-28 05:46:43 -07:00
|
|
|
|
2017-11-07 11:28:13 -08:00
|
|
|
if ($request->file('image')) {
|
|
|
|
$image = $request->file('image');
|
|
|
|
$file_name = $model->id.'-'.str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension();
|
2017-10-06 17:03:51 -07:00
|
|
|
|
|
|
|
if ($image->getClientOriginalExtension()!='svg') {
|
|
|
|
Image::make($image->getRealPath())->resize(500, null, function ($constraint) {
|
|
|
|
$constraint->aspectRatio();
|
|
|
|
$constraint->upsize();
|
2017-11-07 11:28:13 -08:00
|
|
|
})->save(app('models_upload_path').$file_name);
|
2017-10-06 17:03:51 -07:00
|
|
|
} else {
|
2017-11-07 11:28:13 -08:00
|
|
|
$image->move(app('models_upload_path'), $file_name);
|
2017-10-06 17:03:51 -07:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
$model->image = $file_name;
|
2017-10-06 17:03:51 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
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('models_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
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2017-11-07 11:28:13 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
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
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('delete', AssetModel::class);
|
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
|
|
|
}
|
2017-10-28 05:46:43 -07:00
|
|
|
|
|
|
|
if ($model->image) {
|
|
|
|
try {
|
|
|
|
unlink(public_path().'/uploads/models/'.$model->image);
|
|
|
|
} catch (\Exception $e) {
|
2019-03-13 12:22:12 -07:00
|
|
|
\Log::info($e);
|
2017-10-28 05:46:43 -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)
|
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('create', AssetModel::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
// 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
|
|
|
{
|
2018-01-20 04:39:31 -08:00
|
|
|
$this->authorize('view', AssetModel::class);
|
2016-03-25 01:18:05 -07:00
|
|
|
$model = AssetModel::withTrashed()->find($modelId);
|
|
|
|
|
|
|
|
if (isset($model->id)) {
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('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('depreciation_list', Helper::depreciationList());
|
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);
|
2017-06-09 16:44:03 -07:00
|
|
|
return view("models.custom_fields_form")->with("model", $model);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-08 17:48:48 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view that allows the user to bulk edit model attrbutes
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.7]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function postBulkEdit(Request $request)
|
|
|
|
{
|
2018-07-16 14:22:25 -07:00
|
|
|
|
2017-06-08 17:48:48 -07:00
|
|
|
$models_raw_array = Input::get('ids');
|
2017-10-18 08:45:05 -07:00
|
|
|
|
2017-12-12 09:10:05 -08:00
|
|
|
// Make sure some IDs have been selected
|
|
|
|
if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) {
|
|
|
|
|
|
|
|
|
2019-05-22 00:52:51 -07:00
|
|
|
$models = AssetModel::whereIn('id', $models_raw_array)->withCount('assets as assets_count')->orderBy('assets_count', 'ASC')->get();
|
2017-12-12 09:10:05 -08:00
|
|
|
|
|
|
|
// If deleting....
|
|
|
|
if ($request->input('bulk_actions')=='delete') {
|
|
|
|
$valid_count = 0;
|
|
|
|
foreach ($models as $model) {
|
|
|
|
if ($model->assets_count == 0) {
|
|
|
|
$valid_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return view('models/bulk-delete', compact('models'))->with('valid_count', $valid_count);
|
|
|
|
|
|
|
|
// Otherwise display the bulk edit screen
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$nochange = ['NC' => 'No Change'];
|
|
|
|
$fieldset_list = $nochange + Helper::customFieldsetList();
|
|
|
|
$depreciation_list = $nochange + Helper::depreciationList();
|
|
|
|
|
|
|
|
return view('models/bulk-edit', compact('models'))
|
|
|
|
->with('fieldset_list', $fieldset_list)
|
|
|
|
->with('depreciation_list', $depreciation_list);
|
|
|
|
}
|
2017-06-08 17:48:48 -07:00
|
|
|
|
2017-10-18 08:15:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('error', 'You must select at least one model to edit.');
|
2017-06-08 17:48:48 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view that allows the user to bulk edit model attrbutes
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v1.7]
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function postBulkEditSave(Request $request)
|
|
|
|
{
|
|
|
|
|
|
|
|
$models_raw_array = Input::get('ids');
|
|
|
|
$update_array = array();
|
|
|
|
|
2017-12-12 09:10:05 -08:00
|
|
|
|
2019-05-23 17:39:50 -07:00
|
|
|
if (($request->filled('manufacturer_id') && ($request->input('manufacturer_id')!='NC'))) {
|
2017-06-08 17:48:48 -07:00
|
|
|
$update_array['manufacturer_id'] = $request->input('manufacturer_id');
|
|
|
|
}
|
2019-05-23 17:39:50 -07:00
|
|
|
if (($request->filled('category_id') && ($request->input('category_id')!='NC'))) {
|
2017-06-08 17:48:48 -07:00
|
|
|
$update_array['category_id'] = $request->input('category_id');
|
|
|
|
}
|
|
|
|
if ($request->input('fieldset_id')!='NC') {
|
|
|
|
$update_array['fieldset_id'] = $request->input('fieldset_id');
|
|
|
|
}
|
|
|
|
if ($request->input('depreciation_id')!='NC') {
|
|
|
|
$update_array['depreciation_id'] = $request->input('depreciation_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (count($update_array) > 0) {
|
|
|
|
AssetModel::whereIn('id', $models_raw_array)->update($update_array);
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('success', trans('admin/models/message.bulkedit.success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('warning', trans('admin/models/message.bulkedit.error'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-12 09:10:05 -08:00
|
|
|
/**
|
|
|
|
* Validate and delete the given Asset Models. 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
|
|
|
|
*/
|
|
|
|
public function postBulkDelete(Request $request)
|
|
|
|
{
|
|
|
|
$models_raw_array = Input::get('ids');
|
|
|
|
|
|
|
|
if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) {
|
|
|
|
|
2019-05-22 00:52:51 -07:00
|
|
|
$models = AssetModel::whereIn('id', $models_raw_array)->withCount('assets as assets_count')->get();
|
2017-12-12 09:10:05 -08:00
|
|
|
|
|
|
|
$del_error_count = 0;
|
|
|
|
$del_count = 0;
|
|
|
|
|
|
|
|
foreach ($models as $model) {
|
|
|
|
\Log::debug($model->id);
|
|
|
|
|
|
|
|
if ($model->assets_count > 0) {
|
|
|
|
$del_error_count++;
|
|
|
|
} else {
|
|
|
|
$model->delete();
|
|
|
|
$del_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
\Log::debug($del_count);
|
|
|
|
\Log::debug($del_error_count);
|
|
|
|
|
|
|
|
if ($del_error_count == 0) {
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('success', trans('admin/models/message.bulkdelete.success',['success_count'=> $del_count] ));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('warning', trans('admin/models/message.bulkdelete.success_partial', ['fail_count'=>$del_error_count, 'success_count'=> $del_count]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('models.index')
|
|
|
|
->with('error', trans('admin/models/message.bulkdelete.error'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-23 21:16:55 -07:00
|
|
|
/**
|
|
|
|
* Returns true if a fieldset is set, 'add default values' is ticked and if
|
|
|
|
* any default values were entered into the form.
|
|
|
|
*
|
|
|
|
* @param array $input
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function shouldAddDefaultValues(array $input)
|
|
|
|
{
|
|
|
|
return !empty($input['add_default_values'])
|
|
|
|
&& !empty($input['default_values'])
|
|
|
|
&& !empty($input['custom_fieldset']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds default values to a model (as long as they are truthy)
|
|
|
|
*
|
|
|
|
* @param AssetModel $model
|
|
|
|
* @param array $defaultValues
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues)
|
|
|
|
{
|
|
|
|
foreach ($defaultValues as $customFieldId => $defaultValue) {
|
|
|
|
if ($defaultValue) {
|
|
|
|
$model->defaultValues()->attach($customFieldId, ['default_value' => $defaultValue]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all default values
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function removeCustomFieldsDefaultValues(AssetModel $model)
|
|
|
|
{
|
|
|
|
$model->defaultValues()->detach();
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|