Filter.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace Dcat\Admin\Grid\Column;
  3. use Dcat\Admin\Grid\Column;
  4. use Dcat\Admin\Grid\Model;
  5. use Dcat\Admin\Support\Helper;
  6. use Illuminate\Contracts\Support\Renderable;
  7. abstract class Filter implements Renderable
  8. {
  9. /**
  10. * @var string|array
  11. */
  12. protected $class;
  13. /**
  14. * @var Column
  15. */
  16. protected $parent;
  17. /**
  18. * @var string
  19. */
  20. protected $getColumnName;
  21. /**
  22. * @var \Closure[]
  23. */
  24. protected $resolvings = [];
  25. /**
  26. * @var bool
  27. */
  28. protected $display = true;
  29. /**
  30. * @param Column $column
  31. */
  32. public function setParent(Column $column)
  33. {
  34. $this->parent = $column;
  35. $this->parent->grid()->fetching(function () {
  36. $this->addResetButton();
  37. $this->parent->grid()->model()->treeUrlWithoutQuery(
  38. $this->getQueryName()
  39. );
  40. });
  41. foreach ($this->resolvings as $closure) {
  42. $closure($this);
  43. }
  44. }
  45. /**
  46. * @return Column
  47. */
  48. public function parent()
  49. {
  50. return $this->parent;
  51. }
  52. /**
  53. * @param \Closure $callback
  54. *
  55. * @return $this
  56. */
  57. public function resolving(\Closure $callback)
  58. {
  59. $this->resolvings[] = $callback;
  60. return $this;
  61. }
  62. /**
  63. * @param string $name
  64. *
  65. * @return $this
  66. */
  67. public function setColumnName(string $name)
  68. {
  69. $this->getColumnName = $name;
  70. return $this;
  71. }
  72. /**
  73. * Get column name.
  74. *
  75. * @return string
  76. */
  77. public function getColumnName()
  78. {
  79. return $this->getColumnName ?: $this->parent->getName();
  80. }
  81. /**
  82. * @return string
  83. */
  84. public function getQueryName()
  85. {
  86. return $this->parent->grid()->getName().
  87. '_filter_'.
  88. $this->getColumnName();
  89. }
  90. /**
  91. * Get filter value of this column.
  92. *
  93. * @param string $default
  94. *
  95. * @return array|\Illuminate\Http\Request|string
  96. */
  97. public function value($default = '')
  98. {
  99. return request($this->getQueryName(), $default);
  100. }
  101. /**
  102. * Add reset button.
  103. */
  104. protected function addResetButton()
  105. {
  106. $value = $this->value();
  107. if ($value === '' || $value === null) {
  108. return;
  109. }
  110. $style = $this->shouldDisplay() ? 'style=\'margin:3px 14px\'' : '';
  111. return $this->parent->addHeader(
  112. "&nbsp;<a class='feather icon-rotate-ccw' href='{$this->urlWithoutFilter()}' {$style}></a>"
  113. );
  114. }
  115. /**
  116. * Get form action url.
  117. *
  118. * @return string
  119. */
  120. public function formAction()
  121. {
  122. return Helper::fullUrlWithoutQuery([
  123. $this->getQueryName(),
  124. $this->getColumnName(),
  125. $this->parent->grid()->model()->getPageName(),
  126. '_pjax',
  127. ]);
  128. }
  129. /**
  130. * @return string
  131. */
  132. protected function urlWithoutFilter()
  133. {
  134. $query = request()->query();
  135. unset($query[$this->getQueryName()]);
  136. return Helper::urlWithQuery(url()->current(), $query);
  137. }
  138. /**
  139. * @param string $key
  140. *
  141. * @return array|null|string
  142. */
  143. protected function trans($key)
  144. {
  145. return __("admin.{$key}");
  146. }
  147. /**
  148. * @param bool $value
  149. *
  150. * @return $this
  151. */
  152. public function display(bool $value)
  153. {
  154. $this->display = $value;
  155. return $this;
  156. }
  157. /**
  158. * @return $this
  159. */
  160. public function hide()
  161. {
  162. return $this->display(false);
  163. }
  164. /**
  165. * @return bool
  166. */
  167. public function shouldDisplay()
  168. {
  169. return $this->display;
  170. }
  171. /**
  172. * Add a query binding.
  173. *
  174. * @param mixed $value
  175. * @param Model $model
  176. */
  177. public function addBinding($value, Model $model)
  178. {
  179. //
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function render()
  185. {
  186. //
  187. }
  188. /**
  189. * @param array ...$params
  190. *
  191. * @return static
  192. */
  193. public static function make(...$params)
  194. {
  195. return new static(...$params);
  196. }
  197. }