mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-24 05:04:07 -08:00
Added created_by to kits
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
354b00ef15
commit
951f03094a
|
@ -23,7 +23,6 @@ class PredefinedKitsController extends Controller
|
||||||
public function index(Request $request) : JsonResponse | array
|
public function index(Request $request) : JsonResponse | array
|
||||||
{
|
{
|
||||||
$this->authorize('view', PredefinedKit::class);
|
$this->authorize('view', PredefinedKit::class);
|
||||||
$allowed_columns = ['id', 'name'];
|
|
||||||
|
|
||||||
$kits = PredefinedKit::query();
|
$kits = PredefinedKit::query();
|
||||||
|
|
||||||
|
@ -36,8 +35,25 @@ class PredefinedKitsController extends Controller
|
||||||
$limit = app('api_limit_value');
|
$limit = app('api_limit_value');
|
||||||
|
|
||||||
$order = $request->input('order') === 'desc' ? 'desc' : 'asc';
|
$order = $request->input('order') === 'desc' ? 'desc' : 'asc';
|
||||||
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'name';
|
|
||||||
$kits->orderBy($sort, $order);
|
switch ($request->input('sort')) {
|
||||||
|
case 'created_by':
|
||||||
|
$kits = $kits->OrderByCreatedBy($order);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// This array is what determines which fields should be allowed to be sorted on ON the table itself.
|
||||||
|
// These must match a column on the consumables table directly.
|
||||||
|
$allowed_columns = [
|
||||||
|
'id',
|
||||||
|
'name',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
];
|
||||||
|
|
||||||
|
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
|
||||||
|
$kits = $kits->orderBy($sort, $order);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
$total = $kits->count();
|
$total = $kits->count();
|
||||||
$kits = $kits->skip($offset)->take($limit)->get();
|
$kits = $kits->skip($offset)->take($limit)->get();
|
||||||
|
|
|
@ -55,6 +55,7 @@ class PredefinedKitsController extends Controller
|
||||||
// Create a new Predefined Kit
|
// Create a new Predefined Kit
|
||||||
$kit = new PredefinedKit;
|
$kit = new PredefinedKit;
|
||||||
$kit->name = $request->input('name');
|
$kit->name = $request->input('name');
|
||||||
|
$kit->created_by = auth()->id();
|
||||||
|
|
||||||
if (! $kit->save()) {
|
if (! $kit->save()) {
|
||||||
return redirect()->back()->withInput()->withErrors($kit->getErrors());
|
return redirect()->back()->withInput()->withErrors($kit->getErrors());
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Http\Transformers;
|
namespace App\Http\Transformers;
|
||||||
|
|
||||||
|
use App\Helpers\Helper;
|
||||||
use App\Models\PredefinedKit;
|
use App\Models\PredefinedKit;
|
||||||
use App\Models\SnipeModel;
|
use App\Models\SnipeModel;
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
|
@ -30,6 +31,12 @@ class PredefinedKitsTransformer
|
||||||
$array = [
|
$array = [
|
||||||
'id' => (int) $kit->id,
|
'id' => (int) $kit->id,
|
||||||
'name' => e($kit->name),
|
'name' => e($kit->name),
|
||||||
|
'created_by' => ($kit->adminuser) ? [
|
||||||
|
'id' => (int) $kit->adminuser->id,
|
||||||
|
'name'=> e($kit->adminuser->present()->fullName()),
|
||||||
|
] : null,
|
||||||
|
'created_at' => Helper::getFormattedDateObject($kit->created_at, 'datetime'),
|
||||||
|
'updated_at' => Helper::getFormattedDateObject($kit->updated_at, 'datetime'),
|
||||||
];
|
];
|
||||||
|
|
||||||
$permissions_array['available_actions'] = [
|
$permissions_array['available_actions'] = [
|
||||||
|
|
|
@ -135,6 +135,13 @@ class PredefinedKit extends SnipeModel
|
||||||
*/
|
*/
|
||||||
protected $searchableRelations = [];
|
protected $searchableRelations = [];
|
||||||
|
|
||||||
|
|
||||||
|
public function adminuser()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(\App\Models\User::class, 'created_by');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Establishes the kits -> models relationship
|
* Establishes the kits -> models relationship
|
||||||
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
* @return \Illuminate\Database\Eloquent\Relations\Relation
|
||||||
|
@ -181,4 +188,9 @@ class PredefinedKit extends SnipeModel
|
||||||
* BEGIN QUERY SCOPES
|
* BEGIN QUERY SCOPES
|
||||||
* -----------------------------------------------
|
* -----------------------------------------------
|
||||||
**/
|
**/
|
||||||
|
|
||||||
|
public function scopeOrderByCreatedBy($query, $order)
|
||||||
|
{
|
||||||
|
return $query->leftJoin('users as admin_sort', 'kits.created_by', '=', 'admin_sort.id')->select('kits.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue