123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace Dcat\Admin\Grid\Tools;
- use Dcat\Admin\Grid;
- use Illuminate\Http\Request;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Collection;
- class Selector
- {
- /**
- * @var Grid
- */
- protected $grid;
- /**
- * @var Request
- */
- protected $request;
- /**
- * @var array|Collection
- */
- protected $selectors = [];
- /**
- * @var array
- */
- protected $selected;
- /**
- * @var string
- */
- protected $queryKey;
- /**
- * Selector constructor.
- */
- public function __construct(Grid $grid)
- {
- $this->grid = $grid;
- $this->request = request();
- $this->selectors = new Collection();
- $this->queryKey = $grid->getName().'_selector';
- }
- /**
- * @param string $column
- * @param string|array $label
- * @param array|\Closure $options
- * @param null|\Closure $query
- *
- * @return $this
- */
- public function select(string $column, $label, $options = [], ?\Closure $query = null)
- {
- return $this->addSelector($column, $label, $options, $query);
- }
- /**
- * @param string $column
- * @param string $label
- * @param array $options
- * @param null|\Closure $query
- *
- * @return $this
- */
- public function selectOne(string $column, $label, $options = [], ?\Closure $query = null)
- {
- return $this->addSelector($column, $label, $options, $query, 'one');
- }
- /**
- * @param string $column
- * @param string $label
- * @param array $options
- * @param null $query
- * @param string $type
- *
- * @return $this
- */
- protected function addSelector(string $column, $label, $options = [], ?\Closure $query = null, $type = 'many')
- {
- if (is_array($label)) {
- if ($options instanceof \Closure) {
- $query = $options;
- }
- $options = $label;
- $label = admin_trans_field($column);
- }
- $this->selectors[$column] = compact(
- 'label', 'options', 'type', 'query'
- );
- return $this;
- }
- /**
- * Get all selectors.
- *
- * @return array|Collection
- */
- public function getSelectors()
- {
- return $this->selectors;
- }
- /**
- * @return array
- */
- public function parseSelected()
- {
- if (! is_null($this->selected)) {
- return $this->selected;
- }
- $selected = $this->request->input($this->queryKey, []);
- if (! is_array($selected)) {
- return [];
- }
- $selected = array_filter($selected, function ($value) {
- return !is_null($value);
- });
- foreach ($selected as &$value) {
- $value = explode(',', $value);
- }
- return $this->selected = $selected;
- }
- /**
- * @param string $column
- * @param mixed $value
- * @param bool $add
- *
- * @return string
- */
- public function url($column, $value = null, $add = false)
- {
- $query = $this->request->query();
- $selected = $this->parseSelected();
- $options = Arr::get($selected, $column, []);
- if (is_null($value)) {
- Arr::forget($query, "{$this->queryKey}.{$column}");
- return $this->request->fullUrlWithQuery($query);
- }
- if (in_array($value, $options)) {
- array_delete($options, $value);
- } else {
- if ($add) {
- $options = [];
- }
- array_push($options, $value);
- }
- if (! empty($options)) {
- Arr::set($query, "{$this->queryKey}.{$column}", implode(',', $options));
- } else {
- Arr::forget($query, "{$this->queryKey}.{$column}");
- }
- return $this->request->fullUrlWithQuery($query);
- }
- /**
- * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
- */
- public function render()
- {
- return view('admin::grid.selector', [
- 'selector' => $this,
- 'values' => $this->selectors,
- 'selected' => $this->parseSelected(),
- ]);
- }
- }
|