Header.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Encore\Admin\Grid\Tools;
  3. use Encore\Admin\Grid;
  4. use Illuminate\Contracts\Support\Htmlable;
  5. use Illuminate\Contracts\Support\Renderable;
  6. use Illuminate\Database\Query\Builder;
  7. class Header extends AbstractTool
  8. {
  9. /**
  10. * @var Builder
  11. */
  12. protected $queryBuilder;
  13. /**
  14. * Header constructor.
  15. *
  16. * @param Grid $grid
  17. */
  18. public function __construct(Grid $grid)
  19. {
  20. $this->grid = $grid;
  21. }
  22. /**
  23. * Get model query builder.
  24. *
  25. * @return \Illuminate\Database\Eloquent\Builder
  26. */
  27. public function queryBuilder()
  28. {
  29. if (!$this->queryBuilder) {
  30. $this->queryBuilder = $this->grid->model()->getQueryBuilder();
  31. }
  32. return $this->queryBuilder;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function render()
  38. {
  39. $content = call_user_func($this->grid->header(), $this->queryBuilder());
  40. if (empty($content)) {
  41. return '';
  42. }
  43. if ($content instanceof Renderable) {
  44. $content = $content->render();
  45. }
  46. if ($content instanceof Htmlable) {
  47. $content = $content->toHtml();
  48. }
  49. return <<<HTML
  50. <div class="box-header with-border clearfix">
  51. {$content}
  52. </div>
  53. HTML;
  54. }
  55. }