mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-24 21:24:13 -08:00
323c3807fa
* Make delete routes work. We put a little form in the modal that spoofs the delete field. * Fix route on creating a user. * Fix redundant id parameter. * Port acceptance tests to new urls. * Initial work on migrating to model based policies instead of global gates. Will allow for much more detailed permissions bits in the future. * This needs to stay for the dashboard checks. * Add user states for permissions to build tests. * Build up unit tests for gates/permissions. Move accessories/consumables/assets to policies instead of in authserviceprovider * Migrate various locations to new syntax. Update test to be more specific * Fix functional tests. Add an artisan command for installing a settings setup on travis-ci * Try a different id... Need to come up with a better way of passing the id for tests that need an existing one. * Try to fix travis * Update urls to use routes and not hardcode old paths. Also fix some migration errors found along the way.: * Add a environment for travis functional tests. * Adjust config file to make travis use it. * Use redirect()->route instead of redirect()-to * Dump all failures in the output directory if travis fails. * Cleanups and minor fixes. * Adjust the supplier modelfactory to comply with new validation restrictions. * Some test fixes. * Locales can be longer than 5 characters according to faker... fex gez_ET. Increase lenght in mysql and add a validation * Update test database dump to latest migrations. * Extend Supplier phone/fax length. This catches issues found in testing with a phone number with a five digit extension. fex (356) 654-3024 x36632 Also move away from escaping all values put into eloquent. Eloquent already uses PDO parameter binding, and this was leading to names like Mr Ryan O'Malley turning into an html escaped version of that name when stored. All values should be escaped when using {{}}, we'll just have to be more cautious when we use {!!, but I think we already are? * Remove additional escaping here, like we did in suppliers controller. * No need to eager load all of these relationships when we can call the count on the querybuilder directly * Work on controller cleanup * Always start from scrach, catches more issues this way. * Update sql dump. Remove old code from permissions test. * Generate a deletable item on demand in the test, rather than relying on one existing. I think we should probably move to mock all the database stuff at some point.. * More travis related fixes * Break script into multiple functional lines * Update all controllers to use the new helper, also cleanup syntax and docblocks along the way.
513 lines
17 KiB
PHP
Executable file
513 lines
17 KiB
PHP
Executable file
<?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;
|
|
use Illuminate\Http\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
/**
|
|
* 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>]
|
|
*/
|
|
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
|
|
*/
|
|
public function index()
|
|
{
|
|
// 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
|
|
*/
|
|
public function create()
|
|
{
|
|
// Show the page
|
|
return View::make('models/edit')
|
|
->with('category_list', Helper::categoryList('asset'))
|
|
->with('depreciation_list', Helper::depreciationList())
|
|
->with('manufacturer_list', Helper::manufacturerList())
|
|
->with('item', new AssetModel);
|
|
}
|
|
|
|
|
|
/**
|
|
* Validate and process the new Asset Model data.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v1.0]
|
|
* @return Redirect
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
|
|
// Create a new asset model
|
|
$model = new AssetModel;
|
|
|
|
if ($request->input('depreciation_id') == '') {
|
|
$model->depreciation_id = 0;
|
|
} else {
|
|
$model->depreciation_id = $request->input('depreciation_id');
|
|
}
|
|
|
|
if ($request->input('eol') == '') {
|
|
$model->eol = 0;
|
|
} else {
|
|
$model->eol = $request->input('eol');
|
|
}
|
|
|
|
// Save the model data
|
|
$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();
|
|
$model->requestable = Input::has('requestable');
|
|
|
|
if ($request->input('custom_fieldset')!='') {
|
|
$model->fieldset_id = e($request->input('custom_fieldset'));
|
|
}
|
|
|
|
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(500, null, function ($constraint) {
|
|
$constraint->aspectRatio();
|
|
$constraint->upsize();
|
|
})->save($path);
|
|
$model->image = $file_name;
|
|
}
|
|
|
|
// Was it created?
|
|
if ($model->save()) {
|
|
// Redirect to the new model page
|
|
return redirect()->route("models.index")->with('success', trans('admin/models/message.create.success'));
|
|
}
|
|
return redirect()->back()->withInput()->withErrors($model->getErrors());
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public function apiStore(Request $request)
|
|
{
|
|
//COPYPASTA!!!! FIXME
|
|
$model = new AssetModel;
|
|
|
|
$settings=Input::all();
|
|
$settings['eol']= null;
|
|
|
|
$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');
|
|
$model->eol= null;
|
|
|
|
if ($request->input('fieldset_id')=='') {
|
|
$model->fieldset_id = null;
|
|
} else {
|
|
$model->fieldset_id = e($request->input('fieldset_id'));
|
|
}
|
|
|
|
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
|
|
*/
|
|
public function edit($modelId = null)
|
|
{
|
|
// Check if the model exists
|
|
if (is_null($item = AssetModel::find($modelId))) {
|
|
// Redirect to the model management page
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
|
}
|
|
|
|
$view = View::make('models/edit', compact('item'));
|
|
$view->with('category_list', Helper::categoryList('asset'));
|
|
$view->with('depreciation_list', Helper::depreciationList());
|
|
$view->with('manufacturer_list', Helper::manufacturerList());
|
|
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
|
|
*/
|
|
public function update(Request $request, $modelId = null)
|
|
{
|
|
// Check if the model exists
|
|
if (is_null($model = AssetModel::find($modelId))) {
|
|
// Redirect to the models management page
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
|
}
|
|
|
|
if ($request->input('depreciation_id') == '') {
|
|
$model->depreciation_id = 0;
|
|
} else {
|
|
$model->depreciation_id = $request->input('depreciation_id');
|
|
}
|
|
|
|
if ($request->input('eol') == '') {
|
|
$model->eol = null;
|
|
} else {
|
|
$model->eol = $request->input('eol');
|
|
}
|
|
|
|
$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->requestable = Input::has('requestable');
|
|
|
|
if ($request->input('custom_fieldset')=='') {
|
|
$model->fieldset_id = null;
|
|
} else {
|
|
$model->fieldset_id = $request->input('custom_fieldset');
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
if ($request->input('image_delete') == 1 && Input::file('image') == "") {
|
|
$model->image = null;
|
|
}
|
|
|
|
if ($model->save()) {
|
|
return redirect()->route("models.index")->with('success', trans('admin/models/message.update.success'));
|
|
}
|
|
return redirect()->back()->withInput()->withErrors($model->getErrors());
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public function destroy($modelId)
|
|
{
|
|
// Check if the model exists
|
|
if (is_null($model = AssetModel::find($modelId))) {
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.not_found'));
|
|
}
|
|
|
|
if ($model->assets()->count() > 0) {
|
|
// Throw an error that this model is associated with assets
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.assoc_users'));
|
|
}
|
|
// Delete the model
|
|
$model->delete();
|
|
|
|
// Redirect to the models management page
|
|
return redirect()->route('models.index')->with('success', trans('admin/models/message.delete.success'));
|
|
}
|
|
|
|
|
|
/**
|
|
* 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
|
|
$success = trans('admin/models/message.restore.success');
|
|
|
|
// Redirect back
|
|
return redirect()->route('models.index')->with('success', $success);
|
|
|
|
}
|
|
return redirect()->back()->with('error', trans('admin/models/message.not_found'));
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public function show($modelId = null)
|
|
{
|
|
$model = AssetModel::withTrashed()->find($modelId);
|
|
|
|
if (isset($model->id)) {
|
|
return View::make('models/view', compact('model'));
|
|
}
|
|
// Prepare the error message
|
|
$error = trans('admin/models/message.does_not_exist', compact('id'));
|
|
|
|
// Redirect to the user management page
|
|
return redirect()->route('models.index')->with('error', $error);
|
|
}
|
|
|
|
/**
|
|
* 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))) {
|
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
|
}
|
|
|
|
$model = clone $model_to_clone;
|
|
$model->id = null;
|
|
|
|
// Show the page
|
|
$view = View::make('models/edit');
|
|
$view->with('category_list', Helper::categoryList('asset'));
|
|
$view->with('depreciation_list', Helper::depreciationList());
|
|
$view->with('manufacturer_list', Helper::manufacturerList());
|
|
$view->with('item', $model);
|
|
$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)
|
|
{
|
|
$model = AssetModel::find($modelId);
|
|
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
|
|
*/
|
|
|
|
public function getDatatable(Request $request, $status = null)
|
|
{
|
|
$models = AssetModel::with('category', 'assets', 'depreciation', 'manufacturer');
|
|
|
|
switch ($status) {
|
|
case 'Deleted':
|
|
$models->withTrashed()->Deleted();
|
|
break;
|
|
}
|
|
|
|
|
|
if (Input::has('search')) {
|
|
$models = $models->TextSearch($request->input('search'));
|
|
}
|
|
|
|
$offset = request('offset', 0);
|
|
$limit = request('limit', 50);
|
|
|
|
$allowed_columns = ['id','name','model_number'];
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
|
$sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at';
|
|
|
|
$models = $models->orderBy($sort, $order);
|
|
|
|
$modelCount = $models->count();
|
|
$models = $models->skip($offset)->take($limit)->get();
|
|
|
|
$rows = array();
|
|
|
|
foreach ($models as $model) {
|
|
$actions = '<div style="white-space: nowrap;">';
|
|
if ($model->deleted_at == '') {
|
|
$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
|
|
);
|
|
} else {
|
|
$actions .= Helper::generateDatatableButton('restore', route('restore/model', $model->id));
|
|
}
|
|
|
|
$rows[] = array(
|
|
'id' => $model->id,
|
|
'manufacturer' => (string)link_to_route('manufacturers.show', $model->manufacturer->name, ['manufacturer' => $model->manufacturer->id]),
|
|
'name' => (string)link_to_route('models.show',$model->name, ['model' => $model->id]),
|
|
'image' => ($model->image!='') ? '<img src="'.url('/').'/uploads/models/'.$model->image.'" height=50 width=50>' : '',
|
|
'modelnumber' => $model->model_number,
|
|
'numassets' => $model->assets->count(),
|
|
'depreciation' => (($model->depreciation) && ($model->depreciation->id > 0)) ? $model->depreciation->name.' ('.$model->depreciation->months.')' : trans('general.no_depreciation'),
|
|
'category' => ($model->category) ? (string)link_to_route('categories.show', $model->category->name, ['category' => $model->category->id]) : '',
|
|
'eol' => ($model->eol) ? $model->eol.' '.trans('general.months') : '',
|
|
'note' => $model->getNote(),
|
|
'fieldset' => ($model->fieldset) ? (string)link_to_route('custom_fields/model', $model->fieldset->name, ['model' => $model->fieldset->id]) : '',
|
|
'actions' => $actions
|
|
);
|
|
}
|
|
|
|
$data = array('total' => $modelCount, 'rows' => $rows);
|
|
|
|
return $data;
|
|
}
|
|
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
$assets = Asset::where('model_id', '=', $modelID)->with('company', 'assetstatus');
|
|
|
|
if (Input::has('search')) {
|
|
$assets = $assets->TextSearch(e($request->input('search')));
|
|
}
|
|
$offset = request('offset',0);
|
|
$limit = request('limit', 50);
|
|
|
|
|
|
$allowed_columns = ['name', 'serial','asset_tag'];
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
|
$sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at';
|
|
|
|
$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)) {
|
|
$actions = Helper::generateDatatableButton('checkin', route('checkin/hardware', $asset->id));
|
|
} else {
|
|
$actions = Helper::generateDatatableButton('checkout', route('checkout/hardware', $asset->id));
|
|
}
|
|
}
|
|
}
|
|
|
|
$rows[] = array(
|
|
'id' => $asset->id,
|
|
'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]),
|
|
'serial' => $asset->serial,
|
|
'assigned_to' => ($asset->assigned_to) ? (string)link_to_route('users.show', $asset->assigneduser->fullName(), ['asset' =>$asset->assigned_to]) : '',
|
|
'actions' => $actions,
|
|
'companyName' => Company::getName($asset)
|
|
);
|
|
}
|
|
|
|
$data = array('total' => $assetsCount, 'rows' => $rows);
|
|
|
|
return $data;
|
|
}
|
|
}
|