Selector.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Grid;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Collection;
  7. class Selector
  8. {
  9. /**
  10. * @var Grid
  11. */
  12. protected $grid;
  13. /**
  14. * @var Request
  15. */
  16. protected $request;
  17. /**
  18. * @var array|Collection
  19. */
  20. protected $selectors = [];
  21. /**
  22. * @var array
  23. */
  24. protected $selected;
  25. /**
  26. * @var string
  27. */
  28. protected $queryKey;
  29. /**
  30. * Selector constructor.
  31. */
  32. public function __construct(Grid $grid)
  33. {
  34. $this->grid = $grid;
  35. $this->request = request();
  36. $this->selectors = new Collection();
  37. $this->queryKey = $grid->getName().'_selector';
  38. }
  39. /**
  40. * @param string $column
  41. * @param string|array $label
  42. * @param array|\Closure $options
  43. * @param null|\Closure $query
  44. *
  45. * @return $this
  46. */
  47. public function select(string $column, $label, $options = [], ?\Closure $query = null)
  48. {
  49. return $this->addSelector($column, $label, $options, $query);
  50. }
  51. /**
  52. * @param string $column
  53. * @param string $label
  54. * @param array $options
  55. * @param null|\Closure $query
  56. *
  57. * @return $this
  58. */
  59. public function selectOne(string $column, $label, $options = [], ?\Closure $query = null)
  60. {
  61. return $this->addSelector($column, $label, $options, $query, 'one');
  62. }
  63. /**
  64. * @param string $column
  65. * @param string $label
  66. * @param array $options
  67. * @param null $query
  68. * @param string $type
  69. *
  70. * @return $this
  71. */
  72. protected function addSelector(string $column, $label, $options = [], ?\Closure $query = null, $type = 'many')
  73. {
  74. if (is_array($label)) {
  75. if ($options instanceof \Closure) {
  76. $query = $options;
  77. }
  78. $options = $label;
  79. $label = admin_trans_field($column);
  80. }
  81. $this->selectors[$column] = compact(
  82. 'label', 'options', 'type', 'query'
  83. );
  84. return $this;
  85. }
  86. /**
  87. * Get all selectors.
  88. *
  89. * @return array|Collection
  90. */
  91. public function getSelectors()
  92. {
  93. return $this->selectors;
  94. }
  95. /**
  96. * @return array
  97. */
  98. public function parseSelected()
  99. {
  100. if (! is_null($this->selected)) {
  101. return $this->selected;
  102. }
  103. $selected = $this->request->input($this->queryKey, []);
  104. if (! is_array($selected)) {
  105. return [];
  106. }
  107. $selected = array_filter($selected, function ($value) {
  108. return !is_null($value);
  109. });
  110. foreach ($selected as &$value) {
  111. $value = explode(',', $value);
  112. }
  113. return $this->selected = $selected;
  114. }
  115. /**
  116. * @param string $column
  117. * @param mixed $value
  118. * @param bool $add
  119. *
  120. * @return string
  121. */
  122. public function url($column, $value = null, $add = false)
  123. {
  124. $query = $this->request->query();
  125. $selected = $this->parseSelected();
  126. $options = Arr::get($selected, $column, []);
  127. if (is_null($value)) {
  128. Arr::forget($query, "{$this->queryKey}.{$column}");
  129. return $this->request->fullUrlWithQuery($query);
  130. }
  131. if (in_array($value, $options)) {
  132. array_delete($options, $value);
  133. } else {
  134. if ($add) {
  135. $options = [];
  136. }
  137. array_push($options, $value);
  138. }
  139. if (! empty($options)) {
  140. Arr::set($query, "{$this->queryKey}.{$column}", implode(',', $options));
  141. } else {
  142. Arr::forget($query, "{$this->queryKey}.{$column}");
  143. }
  144. return $this->request->fullUrlWithQuery($query);
  145. }
  146. /**
  147. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  148. */
  149. public function render()
  150. {
  151. return view('admin::grid.selector', [
  152. 'selector' => $this,
  153. 'values' => $this->selectors,
  154. 'selected' => $this->parseSelected(),
  155. ]);
  156. }
  157. }