BatchAction.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Dcat\Admin\Grid\Tools;
  3. use Dcat\Admin\Grid;
  4. use Illuminate\Contracts\Support\Renderable;
  5. abstract class BatchAction implements Renderable
  6. {
  7. /**
  8. * @var int
  9. */
  10. protected $id;
  11. /**
  12. * @var string
  13. */
  14. protected $title;
  15. /**
  16. * @var string
  17. */
  18. protected $resource;
  19. /**
  20. * @var Grid
  21. */
  22. protected $grid;
  23. /**
  24. * @param $id
  25. */
  26. public function id($id = null)
  27. {
  28. if ($id === null) {
  29. return $this->id;
  30. }
  31. $this->id = $id;
  32. }
  33. public function title($title = null)
  34. {
  35. if ($title === null) {
  36. return $this->title;
  37. }
  38. $this->title = $title;
  39. }
  40. /**
  41. * @param Grid $grid
  42. */
  43. public function setGrid(Grid $grid)
  44. {
  45. $this->grid = $grid;
  46. $this->resource = $grid->resource();
  47. }
  48. /**
  49. * @return string
  50. */
  51. public function token()
  52. {
  53. return csrf_token();
  54. }
  55. /**
  56. * @param bool $dotPrefix
  57. *
  58. * @return string
  59. */
  60. public function elementClass()
  61. {
  62. return sprintf(
  63. '%s-%s',
  64. $this->grid->batchName(),
  65. $this->id
  66. );
  67. }
  68. /**
  69. * @return string
  70. */
  71. public function elementSelector()
  72. {
  73. return '.'.$this->elementClass();
  74. }
  75. /**
  76. * Script of batch action.
  77. *
  78. * @return string
  79. */
  80. abstract public function script();
  81. public function render()
  82. {
  83. return <<<HTML
  84. <li><a href="#" class="{$this->elementClass()}">{$this->title()}</a></li>
  85. HTML;
  86. }
  87. }