Filter.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 $columnName;
  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->columnName = $name;
  70. return $this;
  71. }
  72. /**
  73. * Get column name.
  74. *
  75. * @return string
  76. */
  77. public function getColumnName()
  78. {
  79. return str_replace(['.', '->'], '_', $this->getOriginalColumnName());
  80. }
  81. /**
  82. * @return mixed
  83. */
  84. public function getOriginalColumnName()
  85. {
  86. return $this->columnName ?: $this->parent->getName();
  87. }
  88. /**
  89. * @return string
  90. */
  91. public function getQueryName()
  92. {
  93. return $this->parent->grid()->getName().
  94. '_filter_'.
  95. $this->getColumnName();
  96. }
  97. /**
  98. * Get filter value of this column.
  99. *
  100. * @param string $default
  101. *
  102. * @return array|\Illuminate\Http\Request|string
  103. */
  104. public function value($default = '')
  105. {
  106. return request($this->getQueryName(), $default);
  107. }
  108. /**
  109. * @param mixed $model
  110. * @param string $query
  111. * @param mixed array $params
  112. *
  113. * @return void
  114. */
  115. protected function withQuery($model, string $query, array $params)
  116. {
  117. Helper::withQueryCondition($model, $this->getOriginalColumnName(), $query, $params);
  118. }
  119. /**
  120. * Add reset button.
  121. */
  122. protected function addResetButton()
  123. {
  124. $value = $this->value();
  125. if ($value === '' || $value === null) {
  126. return;
  127. }
  128. $style = $this->shouldDisplay() ? 'style=\'margin:3px 14px\'' : '';
  129. return $this->parent->addHeader(
  130. "&nbsp;<a class='feather icon-rotate-ccw' href='{$this->urlWithoutFilter()}' {$style}></a>"
  131. );
  132. }
  133. /**
  134. * @return string
  135. */
  136. protected function renderFormButtons()
  137. {
  138. return <<<HMLT
  139. <li class="dropdown-divider"></li>
  140. <li>
  141. <button class="btn btn-sm btn-primary column-filter-submit "><i class="feather icon-search"></i></button>&nbsp;
  142. <a href="{$this->urlWithoutFilter()}" class="btn btn-sm btn-default"><i class="feather icon-rotate-ccw"></i></a>
  143. </li>
  144. HMLT;
  145. }
  146. /**
  147. * Get form action url.
  148. *
  149. * @return string
  150. */
  151. public function formAction()
  152. {
  153. return Helper::fullUrlWithoutQuery([
  154. $this->getQueryName(),
  155. $this->getColumnName(),
  156. $this->parent->grid()->model()->getPageName(),
  157. '_pjax',
  158. ]);
  159. }
  160. /**
  161. * @return string
  162. */
  163. protected function urlWithoutFilter()
  164. {
  165. return Helper::fullUrlWithoutQuery([
  166. $this->getQueryName(),
  167. ]);
  168. }
  169. /**
  170. * @param string $key
  171. *
  172. * @return array|null|string
  173. */
  174. protected function trans($key)
  175. {
  176. return __("admin.{$key}");
  177. }
  178. /**
  179. * @param bool $value
  180. *
  181. * @return $this
  182. */
  183. public function display(bool $value)
  184. {
  185. $this->display = $value;
  186. return $this;
  187. }
  188. /**
  189. * @return $this
  190. */
  191. public function hide()
  192. {
  193. return $this->display(false);
  194. }
  195. /**
  196. * @return bool
  197. */
  198. public function shouldDisplay()
  199. {
  200. return $this->display;
  201. }
  202. /**
  203. * Add a query binding.
  204. *
  205. * @param mixed $value
  206. * @param Model $model
  207. */
  208. public function addBinding($value, Model $model)
  209. {
  210. //
  211. }
  212. /**
  213. * {@inheritdoc}
  214. */
  215. public function render()
  216. {
  217. //
  218. }
  219. /**
  220. * @param array ...$params
  221. *
  222. * @return static
  223. */
  224. public static function make(...$params)
  225. {
  226. return new static(...$params);
  227. }
  228. }