123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717 |
- <?php
- namespace Dcat\Admin\Grid;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Middleware\Pjax;
- use Dcat\Admin\Repositories\Repository;
- use Illuminate\Database\Eloquent\Model as EloquentModel;
- use Illuminate\Database\Eloquent\Relations\Relation;
- use Illuminate\Database\Query\Builder;
- use Illuminate\Pagination\AbstractPaginator;
- use Illuminate\Pagination\LengthAwarePaginator;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Collection;
- use Illuminate\Http\Request;
- use Illuminate\Support\Str;
- /**
- * @mixin Builder
- */
- class Model
- {
- /**
- * @var Request
- */
- protected $request;
- /**
- * @var Repository
- */
- protected $repository;
- /**
- * @var EloquentModel
- */
- protected $model;
- /**
- * Array of queries of the model.
- *
- * @var \Illuminate\Support\Collection
- */
- protected $queries;
- /**
- * Sort parameters of the model.
- *
- * @var array
- */
- protected $sort;
- /**
- * @var Collection|LengthAwarePaginator
- */
- protected $data = null;
- /*
- * 20 items per page as default.
- *
- * @var int
- */
- protected $perPage = 20;
- /**
- * @var string
- */
- protected $pageName = 'page';
- /**
- * @var int
- */
- protected $currentPage;
- /**
- * If the model use pagination.
- *
- * @var bool
- */
- protected $usePaginate = true;
- /**
- * The query string variable used to store the per-page.
- *
- * @var string
- */
- protected $perPageName = 'per_page';
- /**
- * The query string variable used to store the sort.
- *
- * @var string
- */
- protected $sortName = '_sort';
- /**
- * Collection callback.
- *
- * @var callable[]
- */
- protected $collectionCallback = [];
- /**
- * @var Grid
- */
- protected $grid;
- /**
- * @var Relation
- */
- protected $relation;
- /**
- * @var array
- */
- protected $eagerLoads = [];
- /**
- * @var array
- */
- protected $constraints = [];
- /**
- * Create a new grid model instance.
- *
- * @param Repository $repository
- * @param Request $request
- */
- public function __construct(Request $request, ?Repository $repository = null)
- {
- if ($repository) {
- $this->repository = Admin::createRepository($repository);
- }
- $this->request = $request;
- $this->queries = collect();
- }
- /**
- * @return Repository|null
- */
- public function getRepository()
- {
- return $this->repository;
- }
- /**
- * @return Collection
- */
- public function getQueries()
- {
- return $this->queries;
- }
- /**
- * @return LengthAwarePaginator|Collection
- */
- public function paginator()
- {
- return $this->get();
- }
- /**
- * Get primary key name of model.
- *
- * @return string
- */
- public function getKeyName()
- {
- return $this->grid->getKeyName();
- }
- /**
- * Enable or disable pagination.
- *
- * @param bool $use
- */
- public function usePaginate($use = true)
- {
- $this->usePaginate = $use;
- }
- /**
- * Get the query string variable used to store the per-page.
- *
- * @return string
- */
- public function getPerPageName()
- {
- return $this->perPageName;
- }
- /**
- * Set the query string variable used to store the per-page.
- *
- * @param string $name
- *
- * @return $this
- */
- public function setPerPageName($name)
- {
- $this->perPageName = $name;
- return $this;
- }
- /**
- * @param int $perPage
- */
- public function setPerPage(int $perPage)
- {
- $this->perPage = $perPage;
- return $this;
- }
- /**
- * @param string $pageName
- */
- public function setPageName(string $pageName)
- {
- $this->pageName = $pageName;
- return $this;
- }
- /**
- * @return string
- */
- public function getPageName()
- {
- return $this->pageName;
- }
- /**
- * Get the query string variable used to store the sort.
- *
- * @return string
- */
- public function getSortName()
- {
- return $this->sortName;
- }
- /**
- * Set the query string variable used to store the sort.
- *
- * @param string $name
- *
- * @return $this
- */
- public function setSortName($name)
- {
- $this->sortName = $name;
- return $this;
- }
- /**
- * Set parent grid instance.
- *
- * @param Grid $grid
- *
- * @return $this
- */
- public function setGrid(Grid $grid)
- {
- $this->grid = $grid;
- return $this;
- }
- /**
- * Get parent gird instance.
- *
- * @return Grid
- */
- public function getGrid()
- {
- return $this->grid;
- }
- /**
- * Get filter of Grid.
- *
- * @return Filter
- */
- public function getFilter()
- {
- return $this->grid->getFilter();
- }
- /**
- * Get constraints.
- *
- * @return array|bool
- */
- public function getConstraints()
- {
- return $this->constraints;
- }
- /**
- * @param array $constraints
- * @return $this
- */
- public function setConstraints(array $constraints)
- {
- $this->constraints = $constraints;
- return $this;
- }
- /**
- * Set collection callback.
- *
- * @param callable $callback
- *
- * @return $this
- */
- public function collection(callable $callback = null)
- {
- $this->collectionCallback[] = $callback;
- return $this;
- }
- /**
- * Build.
- *
- * @param bool $toArray
- *
- * @return array|Collection|mixed
- */
- public function buildData(bool $toArray = true)
- {
- if (is_null($this->data) || $this->data instanceof \Closure) {
- $this->setData($this->get());
- }
- return $toArray ? $this->data->toArray() : $this->data;
- }
- /**
- * @param Collection|\Closure|array $data
- */
- public function setData($data)
- {
- if ($this->collectionCallback) {
- foreach ($this->collectionCallback as $cb) {
- $data = call_user_func($cb, $data);
- }
- }
- if (
- ($isA = is_array($data))
- || $data instanceof Collection
- || $data instanceof \Closure
- || ($isP = $data instanceof AbstractPaginator)
- ) {
- if ($isA) {
- $data = collect($data);
- } elseif (! empty($isP)) {
- $this->model = $data;
- $this->data = $data->getCollection();
- return;
- }
- $this->data = $data;
- }
- }
- /**
- * Add conditions to grid model.
- *
- * @param array $conditions
- *
- * @return $this
- */
- public function addConditions(array $conditions)
- {
- foreach ($conditions as $condition) {
- call_user_func_array([$this, key($condition)], current($condition));
- }
- return $this;
- }
- /**
- * @throws \Exception
- *
- * @return Collection|LengthAwarePaginator
- */
- public function get()
- {
- if (
- $this->model instanceof LengthAwarePaginator
- || $this->model instanceof Collection
- ) {
- return $this->model;
- }
- $this->setSort();
- $this->setPaginate();
- if ($this->data instanceof \Closure) {
- $data = $this->data;
- $this->data = collect();
- return $this->model = $data($this);
- }
- $this->model = $this->repository->get($this);
- if (is_array($this->model)) {
- $this->model = collect($this->model);
- }
- if ($this->model instanceof Collection) {
- return $this->model;
- }
- if ($this->model instanceof LengthAwarePaginator) {
- $this->model->setPageName($this->pageName);
- $this->handleInvalidPage($this->model);
- return $this->model->getCollection();
- }
- throw new \Exception('Grid query error');
- }
- /**
- * If current page is greater than last page, then redirect to last page.
- *
- * @param LengthAwarePaginator $paginator
- *
- * @return void
- */
- protected function handleInvalidPage(LengthAwarePaginator $paginator)
- {
- if (
- $this->usePaginate
- && $paginator->lastPage()
- && $paginator->currentPage() > $paginator->lastPage()
- ) {
- $lastPageUrl = $this->request->fullUrlWithQuery([
- $paginator->getPageName() => $paginator->lastPage(),
- ]);
- Pjax::respond(redirect($lastPageUrl));
- }
- }
- /**
- * Get current page.
- *
- * @return int|null
- */
- public function getCurrentPage()
- {
- if (!$this->usePaginate) {
- return null;
- }
- return $this->currentPage ?: ($this->currentPage = ($this->request->input($this->pageName) ?: 1));
- }
- /**
- * @param int $currentPage
- */
- public function setCurrentPage(int $currentPage)
- {
- $this->currentPage = $currentPage;
- return $this;
- }
- /**
- * Get items number of per page.
- *
- * @return int|null
- */
- public function getPerPage()
- {
- if (!$this->usePaginate) {
- return null;
- }
- return $this->request->input($this->perPageName) ?: $this->perPage;
- }
- /**
- * Set the grid paginate.
- *
- * @return void
- */
- protected function setPaginate()
- {
- $paginate = $this->findQueryByMethod('paginate');
- $this->queries = $this->queries->reject(function ($query) {
- return $query['method'] == 'paginate';
- });
- if (!$this->usePaginate) {
- $query = [
- 'method' => 'get',
- 'arguments' => [],
- ];
- } else {
- $query = [
- 'method' => 'paginate',
- 'arguments' => $this->resolvePerPage($paginate),
- ];
- }
- $this->queries->push($query);
- }
- /**
- * Resolve perPage for pagination.
- *
- * @param array|null $paginate
- *
- * @return array
- */
- protected function resolvePerPage($paginate)
- {
- if ($perPage = app('request')->input($this->perPageName)) {
- if (is_array($paginate)) {
- $paginate['arguments'][0] = (int) $perPage;
- return $paginate['arguments'];
- }
- $this->perPage = (int) $perPage;
- }
- return [$this->perPage, '*', $this->pageName, $this->getCurrentPage()];
- }
- /**
- * Find query by method name.
- *
- * @param $method
- *
- * @return static
- */
- public function findQueryByMethod($method)
- {
- return $this->queries->first(function ($query) use ($method) {
- return $query['method'] == $method;
- });
- }
- /**
- * Get the grid sort.
- *
- * @return array exp: ['name', 'desc']
- */
- public function getSort()
- {
- if (empty($this->sort['column']) || empty($this->sort['type'])) {
- return [null, null];
- }
- return [$this->sort['column'], $this->sort['type']];
- }
- /**
- * Set the grid sort.
- *
- * @return void
- */
- protected function setSort()
- {
- $this->sort = $this->request->input($this->sortName, []);
- if (empty($this->sort['column']) || empty($this->sort['type'])) {
- return;
- }
- if (Str::contains($this->sort['column'], '.')) {
- $this->setRelationSort($this->sort['column']);
- } else {
- $this->resetOrderBy();
- $this->queries->push([
- 'method' => 'orderBy',
- 'arguments' => [$this->sort['column'], $this->sort['type']],
- ]);
- }
- }
- /**
- * Set relation sort.
- *
- * @param string $column
- *
- * @return void
- */
- protected function setRelationSort($column)
- {
- list($relationName, $relationColumn) = explode('.', $column);
- if ($this->queries->contains(function ($query) use ($relationName) {
- return $query['method'] == 'with' && in_array($relationName, $query['arguments']);
- })) {
- $this->queries->push([
- 'method' => 'select',
- 'arguments' => ['*'],
- ]);
- $this->resetOrderBy();
- $this->queries->push([
- 'method' => 'orderBy',
- 'arguments' => [
- $relationColumn,
- $this->sort['type'],
- ],
- ]);
- }
- }
- /**
- * Reset orderBy query.
- *
- * @return void
- */
- public function resetOrderBy()
- {
- $this->queries = $this->queries->reject(function ($query) {
- return $query['method'] == 'orderBy' || $query['method'] == 'orderByDesc';
- });
- }
- /**
- * @param string $method
- * @param array $arguments
- *
- * @return $this
- */
- public function __call($method, $arguments)
- {
- $this->queries->push([
- 'method' => $method,
- 'arguments' => $arguments,
- ]);
- return $this;
- }
- /**
- * Set the relationships that should be eager loaded.
- *
- * @param mixed $relations
- *
- * @return $this|Model
- */
- public function with($relations)
- {
- if (is_array($relations)) {
- if (Arr::isAssoc($relations)) {
- $relations = array_keys($relations);
- }
- $this->eagerLoads = array_merge($this->eagerLoads, $relations);
- }
- if (is_string($relations)) {
- if (Str::contains($relations, '.')) {
- $relations = explode('.', $relations)[0];
- }
- if (Str::contains($relations, ':')) {
- $relations = explode(':', $relations)[0];
- }
- if (in_array($relations, $this->eagerLoads)) {
- return $this;
- }
- $this->eagerLoads[] = $relations;
- }
- return $this->__call('with', (array) $relations);
- }
- /**
- * @param $key
- *
- * @return mixed
- */
- public function __get($key)
- {
- $data = $this->buildData();
- if (array_key_exists($key, $data)) {
- return $data[$key];
- }
- }
- /**
- * @return $this
- */
- public function reset()
- {
- $this->data = null;
- $this->model = null;
- $this->queries = collect();
- return $this;
- }
- }
|