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 $queryName;
  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->queryName = $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',
  83. 'options',
  84. 'type',
  85. 'query'
  86. );
  87. return $this;
  88. }
  89. /**
  90. * Get all selectors.
  91. *
  92. * @return array|Collection
  93. */
  94. public function getSelectors()
  95. {
  96. return $this->selectors;
  97. }
  98. /**
  99. * @return array
  100. */
  101. public function parseSelected()
  102. {
  103. if (!is_null($this->selected)) {
  104. return $this->selected;
  105. }
  106. $selected = $this->request->get($this->queryName, []);
  107. if (!is_array($selected)) {
  108. return [];
  109. }
  110. $selected = array_filter($selected, function ($value) {
  111. return !is_null($value);
  112. });
  113. foreach ($selected as &$value) {
  114. $value = explode(',', $value);
  115. }
  116. return $this->selected = $selected;
  117. }
  118. /**
  119. * @param string $column
  120. * @param mixed $value
  121. * @param bool $add
  122. *
  123. * @return string
  124. */
  125. public function url($column, $value = null, $add = false)
  126. {
  127. $query = $this->request->query();
  128. $selected = $this->parseSelected();
  129. $options = Arr::get($selected, $column, []);
  130. if (is_null($value)) {
  131. Arr::forget($query, "{$this->queryName}.{$column}");
  132. return $this->request->fullUrlWithQuery($query);
  133. }
  134. if (in_array($value, $options)) {
  135. array_delete($options, $value);
  136. } else {
  137. if ($add) {
  138. $options = [];
  139. }
  140. array_push($options, $value);
  141. }
  142. if (!empty($options)) {
  143. Arr::set($query, "{$this->queryName}.{$column}", implode(',', $options));
  144. } else {
  145. Arr::forget($query, "{$this->queryName}.{$column}");
  146. }
  147. return $this->request->fullUrlWithQuery($query);
  148. }
  149. /**
  150. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  151. */
  152. public function render()
  153. {
  154. return view('admin::grid.selector', [
  155. 'self' => $this,
  156. 'selected' => $this->parseSelected(),
  157. ]);
  158. }
  159. }