Actions.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Dcat\Admin\Tree;
  3. use Dcat\Admin\Support\Helper;
  4. use Dcat\Admin\Tree;
  5. use Illuminate\Contracts\Support\Renderable;
  6. class Actions implements Renderable
  7. {
  8. /**
  9. * @var Tree
  10. */
  11. protected $parent;
  12. /**
  13. * @var \Illuminate\Database\Eloquent\Model
  14. */
  15. public $row;
  16. /**
  17. * @var array
  18. */
  19. protected $appends = [];
  20. /**
  21. * @var array
  22. */
  23. protected $prepends = [];
  24. /**
  25. * @var array
  26. */
  27. protected $actions = [
  28. 'delete' => true,
  29. 'quickEdit' => true,
  30. 'edit' => false,
  31. ];
  32. /**
  33. * @var array
  34. */
  35. protected $defaultActions = [
  36. 'edit' => Tree\Actions\Edit::class,
  37. 'quickEdit' => Tree\Actions\QuickEdit::class,
  38. 'delete' => Tree\Actions\Delete::class,
  39. ];
  40. /**
  41. * @param string|Renderable|\Dcat\Admin\Actions\Action|\Illuminate\Contracts\Support\Htmlable $action
  42. * @return $this
  43. */
  44. public function append($action)
  45. {
  46. $this->prepareAction($action);
  47. array_push($this->appends, $action);
  48. return $this;
  49. }
  50. /**
  51. * @param string|Renderable|\Dcat\Admin\Actions\Action|\Illuminate\Contracts\Support\Htmlable $action
  52. * @return $this
  53. */
  54. public function prepend($action)
  55. {
  56. $this->prepareAction($action);
  57. array_unshift($this->prepends, $action);
  58. return $this;
  59. }
  60. public function getKey()
  61. {
  62. return $this->row->{$this->parent()->getKeyName()};
  63. }
  64. public function quickEdit(bool $value = true)
  65. {
  66. $this->actions['quickEdit'] = $value;
  67. return $this;
  68. }
  69. public function disableQuickEdit(bool $value = true)
  70. {
  71. return $this->quickEdit(! $value);
  72. }
  73. public function edit(bool $value = true)
  74. {
  75. $this->actions['edit'] = $value;
  76. return $this;
  77. }
  78. public function disableEdit(bool $value = true)
  79. {
  80. return $this->edit(! $value);
  81. }
  82. public function delete(bool $value = true)
  83. {
  84. $this->actions['delete'] = $value;
  85. return $this;
  86. }
  87. public function disableDelete(bool $value = true)
  88. {
  89. return $this->delete(! $value);
  90. }
  91. public function render()
  92. {
  93. $this->prependDefaultActions();
  94. $toString = [Helper::class, 'render'];
  95. $prepends = array_map($toString, $this->prepends);
  96. $appends = array_map($toString, $this->appends);
  97. return implode('', array_merge($prepends, $appends));
  98. }
  99. protected function prepareAction($action)
  100. {
  101. if ($action instanceof RowAction) {
  102. $action->setParent($this);
  103. $action->setRow($this->row);
  104. }
  105. }
  106. protected function prependDefaultActions()
  107. {
  108. foreach ($this->actions as $action => $enable) {
  109. if (! $enable) {
  110. continue;
  111. }
  112. $action = new $this->defaultActions[$action]();
  113. $this->prepareAction($action);
  114. $this->prepend($action);
  115. }
  116. }
  117. public function parent()
  118. {
  119. return $this->parent;
  120. }
  121. public function setParent(Tree $tree)
  122. {
  123. $this->parent = $tree;
  124. }
  125. public function getRow()
  126. {
  127. return $this->row;
  128. }
  129. public function setRow($row)
  130. {
  131. $this->row = $row;
  132. }
  133. }