Actions.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. *
  43. * @return $this
  44. */
  45. public function append($action)
  46. {
  47. $this->prepareAction($action);
  48. array_push($this->appends, $action);
  49. return $this;
  50. }
  51. /**
  52. * @param string|Renderable|\Dcat\Admin\Actions\Action|\Illuminate\Contracts\Support\Htmlable $action
  53. *
  54. * @return $this
  55. */
  56. public function prepend($action)
  57. {
  58. $this->prepareAction($action);
  59. array_unshift($this->prepends, $action);
  60. return $this;
  61. }
  62. public function getKey()
  63. {
  64. return $this->row->{$this->getParent()->getKeyName()};
  65. }
  66. public function disableQuickEdit(bool $value = true)
  67. {
  68. $this->actions['quickEdit'] = ! $value;
  69. return $this;
  70. }
  71. public function disableEdit(bool $value = true)
  72. {
  73. $this->actions['edit'] = ! $value;
  74. return $this;
  75. }
  76. public function disableDelete(bool $value = true)
  77. {
  78. $this->actions['delete'] = ! $value;
  79. return $this;
  80. }
  81. public function render()
  82. {
  83. $this->prependDefaultActions();
  84. $toString = [Helper::class, 'render'];
  85. $prepends = array_map($toString, $this->prepends);
  86. $appends = array_map($toString, $this->appends);
  87. return implode('', array_merge($prepends, $appends));
  88. }
  89. protected function prepareAction($action)
  90. {
  91. if ($action instanceof RowAction) {
  92. $action->setParent($this);
  93. $action->setRow($this->row);
  94. }
  95. }
  96. protected function prependDefaultActions()
  97. {
  98. foreach ($this->actions as $action => $enable) {
  99. if (! $enable) {
  100. continue;
  101. }
  102. $action = new $this->defaultActions[$action]();
  103. $this->prepareAction($action);
  104. $this->prepend($action);
  105. }
  106. }
  107. public function getParent()
  108. {
  109. return $this->parent;
  110. }
  111. public function setParent(Tree $tree)
  112. {
  113. $this->parent = $tree;
  114. }
  115. public function getRow()
  116. {
  117. return $this->row;
  118. }
  119. public function setRow($row)
  120. {
  121. $this->row = $row;
  122. }
  123. }