2018-09-20 15:33:03 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SelectController.php
|
|
|
|
*
|
|
|
|
* Controller class for select2 ajax controllers.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-02-09 00:29:04 +01:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-09-20 15:33:03 -05:00
|
|
|
*
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2018-09-20 15:33:03 -05:00
|
|
|
* @copyright 2018 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Select;
|
|
|
|
|
|
|
|
use App\Http\Controllers\PaginatedAjaxController;
|
|
|
|
use Illuminate\Contracts\Pagination\Paginator;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Http\Request;
|
2021-09-03 15:58:09 +02:00
|
|
|
use Illuminate\Support\Str;
|
2018-09-20 15:33:03 -05:00
|
|
|
|
|
|
|
abstract class SelectController extends PaginatedAjaxController
|
|
|
|
{
|
|
|
|
final protected function baseRules()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'limit' => 'int',
|
|
|
|
'page' => 'int',
|
|
|
|
'term' => 'nullable|string',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default method called by the route handler
|
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2018-09-20 15:33:03 -05:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function __invoke(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, $this->rules());
|
2018-12-16 15:18:17 -06:00
|
|
|
$limit = $request->get('limit', 50);
|
2018-09-20 15:33:03 -05:00
|
|
|
|
2021-08-28 06:46:53 -05:00
|
|
|
$query = $this->baseQuery($request)->when($request->has('id'), function ($query) {
|
|
|
|
return $query->whereKey(request('id'));
|
|
|
|
});
|
|
|
|
$query = $this->search($request->get('term'), $query, $this->searchFields($request));
|
2021-03-12 10:29:19 +01:00
|
|
|
$this->sort($request, $query);
|
2018-09-20 15:33:03 -05:00
|
|
|
$paginator = $query->simplePaginate($limit);
|
|
|
|
|
|
|
|
return $this->formatResponse($paginator);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param Paginator $paginator
|
2018-09-20 15:33:03 -05:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
protected function formatResponse($paginator)
|
|
|
|
{
|
|
|
|
return response()->json([
|
|
|
|
'results' => collect($paginator->items())->map([$this, 'formatItem']),
|
2020-09-21 14:54:51 +02:00
|
|
|
'pagination' => ['more' => $paginator->hasMorePages()],
|
2018-09-20 15:33:03 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default item formatting, should supply at least id and text keys
|
|
|
|
* Check select2 docs.
|
|
|
|
* Default implementation uses primary key and the first value in the model
|
|
|
|
* If only one value is in the model attributes, that is the id and text.
|
|
|
|
*
|
2021-09-08 23:35:56 +02:00
|
|
|
* @param Model $model
|
2018-09-20 15:33:03 -05:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function formatItem($model)
|
|
|
|
{
|
|
|
|
$attributes = collect($model->getAttributes());
|
2020-09-21 14:54:51 +02:00
|
|
|
|
2018-09-20 15:33:03 -05:00
|
|
|
return [
|
|
|
|
'id' => $attributes->count() == 1 ? $attributes->first() : $model->getKey(),
|
|
|
|
'text' => $attributes->forget($model->getKeyName())->first(),
|
|
|
|
];
|
|
|
|
}
|
2021-09-03 15:58:09 +02:00
|
|
|
|
|
|
|
protected function includeGeneral(): bool
|
|
|
|
{
|
|
|
|
if (request()->has('id') && request('id') !== 0) {
|
|
|
|
return false;
|
|
|
|
} elseif (request()->has('term') && ! Str::contains('general', strtolower(request('term')))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-20 15:33:03 -05:00
|
|
|
}
|