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.
314 lines
11 KiB
PHP
Executable file
314 lines
11 KiB
PHP
Executable file
<?php
|
|
namespace App\Http\Controllers;
|
|
|
|
use Input;
|
|
use Lang;
|
|
use App\Models\Statuslabel;
|
|
use App\Models\Asset;
|
|
use Redirect;
|
|
use DB;
|
|
use App\Models\Setting;
|
|
use Str;
|
|
use View;
|
|
use App\Helpers\Helper;
|
|
use Auth;
|
|
use Illuminate\Http\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
/**
|
|
* This controller handles all actions related to Status Labels for
|
|
* the Snipe-IT Asset Management application.
|
|
*
|
|
* @version v1.0
|
|
*/
|
|
class StatuslabelsController extends Controller
|
|
{
|
|
/**
|
|
* Show a list of all the statuslabels.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
|
|
public function index()
|
|
{
|
|
// Show the page
|
|
return View::make('statuslabels/index', compact('statuslabels'));
|
|
}
|
|
|
|
|
|
/**
|
|
* Show a count of assets by status label
|
|
*
|
|
* @return array
|
|
*/
|
|
|
|
public function getAssetCountByStatuslabel()
|
|
{
|
|
|
|
$statusLabels = Statuslabel::with()->get();
|
|
$labels=[];
|
|
$points=[];
|
|
$colors=[];
|
|
foreach ($statusLabels as $statusLabel) {
|
|
if ($statusLabel->assets()->count() > 0) {
|
|
$labels[]=$statusLabel->name;
|
|
$points[]=$statusLabel->assets()->whereNull('assigned_to')->count();
|
|
if ($statusLabel->color!='') {
|
|
$colors[]=$statusLabel->color;
|
|
}
|
|
}
|
|
}
|
|
$labels[]='Deployed';
|
|
$points[]=Asset::whereNotNull('assigned_to')->count();
|
|
|
|
$colors_array = array_merge($colors, Helper::chartColors());
|
|
|
|
$result= [
|
|
"labels" => $labels,
|
|
"datasets" => [ [
|
|
"data" => $points,
|
|
"backgroundColor" => $colors_array,
|
|
"hoverBackgroundColor" => $colors_array
|
|
]]
|
|
];
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Statuslabel create.
|
|
*
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function create()
|
|
{
|
|
// Show the page
|
|
$item = new Statuslabel;
|
|
$use_statuslabel_type = $item->getStatuslabelType();
|
|
$statuslabel_types = Helper::statusTypeList();
|
|
|
|
return View::make('statuslabels/edit', compact('statuslabel_types', 'item'))->with('use_statuslabel_type', $use_statuslabel_type);
|
|
}
|
|
|
|
|
|
/**
|
|
* Statuslabel create form processing.
|
|
*
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
|
|
// create a new model instance
|
|
$statusLabel = new Statuslabel();
|
|
|
|
if (!$request->has('statuslabel_types')) {
|
|
return redirect()->back()->withInput()->withErrors(['statuslabel_types' => trans('validation.statuslabel_type')]);
|
|
}
|
|
|
|
$statusType = Statuslabel::getStatuslabelTypesForDB($request->input('statuslabel_types'));
|
|
|
|
// Save the Statuslabel data
|
|
$statusLabel->name = Input::get('name');
|
|
$statusLabel->user_id = Auth::id();
|
|
$statusLabel->notes = Input::get('notes');
|
|
$statusLabel->deployable = $statusType['deployable'];
|
|
$statusLabel->pending = $statusType['pending'];
|
|
$statusLabel->archived = $statusType['archived'];
|
|
$statusLabel->color = Input::get('color');
|
|
$statusLabel->show_in_nav = Input::get('show_in_nav', 0);
|
|
|
|
|
|
// Was the asset created?
|
|
if ($statusLabel->save()) {
|
|
// Redirect to the new Statuslabel page
|
|
return redirect()->route('statuslabels.index')->with('success', trans('admin/statuslabels/message.create.success'));
|
|
}
|
|
return redirect()->back()->withInput()->withErrors($statusLabel->getErrors());
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function apiStore(Request $request)
|
|
{
|
|
$statuslabel = new Statuslabel();
|
|
if (!$request->has('statuslabel_types')) {
|
|
return JsonResponse::create(["error" => trans('validation.statuslabel_type')], 500);
|
|
}
|
|
$statustype = Statuslabel::getStatuslabelTypesForDB(Input::get('statuslabel_types'));
|
|
$statuslabel->name = Input::get('name');
|
|
$statuslabel->user_id = Auth::id();
|
|
$statuslabel->notes = '';
|
|
$statuslabel->deployable = $statustype['deployable'];
|
|
$statuslabel->pending = $statustype['pending'];
|
|
$statuslabel->archived = $statustype['archived'];
|
|
|
|
|
|
if ($statuslabel->isValid()) {
|
|
$statuslabel->save();
|
|
// Redirect to the new Statuslabel page
|
|
return JsonResponse::create($statuslabel);
|
|
}
|
|
return JsonResponse::create(["error" => $statuslabel->getErrors()->first()], 500);
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Statuslabel update.
|
|
*
|
|
* @param int $statuslabelId
|
|
* @return \Illuminate\Contracts\View\View
|
|
*/
|
|
public function edit($statuslabelId = null)
|
|
{
|
|
// Check if the Statuslabel exists
|
|
if (is_null($item = Statuslabel::find($statuslabelId))) {
|
|
// Redirect to the blogs management page
|
|
return redirect()->route('statuslabels.index')->with('error', trans('admin/statuslabels/message.does_not_exist'));
|
|
}
|
|
|
|
$use_statuslabel_type = $item->getStatuslabelType();
|
|
|
|
$statuslabel_types = array('' => trans('admin/hardware/form.select_statustype')) + array('undeployable' => trans('admin/hardware/general.undeployable')) + array('pending' => trans('admin/hardware/general.pending')) + array('archived' => trans('admin/hardware/general.archived')) + array('deployable' => trans('admin/hardware/general.deployable'));
|
|
|
|
return View::make('statuslabels/edit', compact('item', 'statuslabel_types'))->with('use_statuslabel_type', $use_statuslabel_type);
|
|
}
|
|
|
|
|
|
/**
|
|
* Statuslabel update form processing page.
|
|
*
|
|
* @param int $statuslabelId
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function update(Request $request, $statuslabelId = null)
|
|
{
|
|
// Check if the Statuslabel exists
|
|
if (is_null($statuslabel = Statuslabel::find($statuslabelId))) {
|
|
// Redirect to the blogs management page
|
|
return redirect()->route('statuslabels.index')->with('error', trans('admin/statuslabels/message.does_not_exist'));
|
|
}
|
|
|
|
if (!$request->has('statuslabel_types')) {
|
|
return redirect()->back()->withInput()->withErrors(['statuslabel_types' => trans('validation.statuslabel_type')]);
|
|
}
|
|
|
|
|
|
// Update the Statuslabel data
|
|
$statustype = Statuslabel::getStatuslabelTypesForDB(Input::get('statuslabel_types'));
|
|
$statuslabel->name = Input::get('name');
|
|
$statuslabel->notes = Input::get('notes');
|
|
$statuslabel->deployable = $statustype['deployable'];
|
|
$statuslabel->pending = $statustype['pending'];
|
|
$statuslabel->archived = $statustype['archived'];
|
|
$statuslabel->color = Input::get('color');
|
|
$statuslabel->show_in_nav = Input::get('show_in_nav',0);
|
|
|
|
|
|
// Was the asset created?
|
|
if ($statuslabel->save()) {
|
|
// Redirect to the saved Statuslabel page
|
|
return redirect()->to("admin/settings/statuslabels/")->with('success', trans('admin/statuslabels/message.update.success'));
|
|
}
|
|
return redirect()->back()->withInput()->withErrors($statuslabel->getErrors());
|
|
}
|
|
|
|
/**
|
|
* Delete the given Statuslabel.
|
|
*
|
|
* @param int $statuslabelId
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function destroy($statuslabelId)
|
|
{
|
|
// Check if the Statuslabel exists
|
|
if (is_null($statuslabel = Statuslabel::find($statuslabelId))) {
|
|
// Redirect to the blogs management page
|
|
return redirect()->route('statuslabels.index')->with('error', trans('admin/statuslabels/message.not_found'));
|
|
}
|
|
|
|
|
|
if ($statuslabel->has_assets() == 0) {
|
|
$statuslabel->delete();
|
|
// Redirect to the statuslabels management page
|
|
return redirect()->route('statuslabels.index')->with('success', trans('admin/statuslabels/message.delete.success'));
|
|
}
|
|
// Redirect to the asset management page
|
|
return redirect()->route('statuslabels.index')->with('error', trans('admin/statuslabels/message.assoc_assets'));
|
|
}
|
|
|
|
|
|
public function getDatatable()
|
|
{
|
|
$statuslabels = Statuslabel::select(array('id','name','deployable','pending','archived','color','show_in_nav'))
|
|
->whereNull('deleted_at');
|
|
|
|
if (Input::has('search')) {
|
|
$statuslabels = $statuslabels->TextSearch(e(Input::get('search')));
|
|
}
|
|
|
|
$offset = request('offset', 0);
|
|
$limit = request('limit', 50);
|
|
|
|
$allowed_columns = ['id','name'];
|
|
$order = Input::get('order') === 'asc' ? 'asc' : 'desc';
|
|
$sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
|
|
|
|
$statuslabels->orderBy($sort, $order);
|
|
|
|
$statuslabelsCount = $statuslabels->count();
|
|
$statuslabels = $statuslabels->skip($offset)->take($limit)->get();
|
|
|
|
$rows = array();
|
|
|
|
foreach ($statuslabels as $statuslabel) {
|
|
|
|
if ($statuslabel->deployable == 1) {
|
|
$label_type = trans('admin/statuslabels/table.deployable');
|
|
} elseif ($statuslabel->pending == 1) {
|
|
$label_type = trans('admin/statuslabels/table.pending');
|
|
} elseif ($statuslabel->archived == 1) {
|
|
$label_type = trans('admin/statuslabels/table.archived');
|
|
} else {
|
|
$label_type = trans('admin/statuslabels/table.undeployable');
|
|
}
|
|
$actions = '<nobr>';
|
|
$actions .= Helper::generateDatatableButton('edit', route('statuslabels.edit', $statuslabel->id));
|
|
$actions .= Helper::generateDatatableButton(
|
|
'delete',
|
|
route('statuslabels.destroy'),
|
|
true, /*enabled*/
|
|
trans('admin/statuslabels/message.delete.confirm'),
|
|
$statuslabel->name
|
|
);
|
|
$actions .= '</nobr>';
|
|
|
|
if ($statuslabel->color!='') {
|
|
$color = '<div class="pull-left" style="margin-right: 5px; height: 20px; width: 20px; background-color: '.e($statuslabel->color).'"></div>'.e($statuslabel->color);
|
|
} else {
|
|
$color = '';
|
|
}
|
|
|
|
|
|
$rows[] = array(
|
|
'id' => e($statuslabel->id),
|
|
'type' => e($label_type),
|
|
'name' => e($statuslabel->name),
|
|
'color' => $color,
|
|
'show_in_nav' => ($statuslabel->show_in_nav=='1') ? trans('general.yes') : trans('general.no'),
|
|
'actions' => $actions
|
|
);
|
|
}
|
|
|
|
$data = array('total' => $statuslabelsCount, 'rows' => $rows);
|
|
|
|
return $data;
|
|
|
|
}
|
|
}
|