Completed
Push — master ( 65fa37...c7bb09 )
by Song
02:44
created

BatchActions::addActionScripts()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Tools;
4
5
use Encore\Admin\Admin;
6
use Illuminate\Support\Collection;
7
8
class BatchActions extends AbstractTool
9
{
10
    /**
11
     * @var Collection
12
     */
13
    protected $actions;
14
15
    /**
16
     * @var bool
17
     */
18
    protected $enableDelete = true;
19
20
    /**
21
     * @var bool
22
     */
23
    private $holdAll = false;
24
25
    /**
26
     * BatchActions constructor.
27
     */
28
    public function __construct()
29
    {
30
        $this->actions = new Collection();
31
32
        $this->appendDefaultAction();
33
    }
34
35
    /**
36
     * Append default action(batch delete action).
37
     *
38
     * return void
39
     */
40
    protected function appendDefaultAction()
41
    {
42
        $this->add(new BatchDelete(trans('admin.batch_delete')));
43
    }
44
45
    /**
46
     * Disable delete.
47
     *
48
     * @return $this
49
     */
50
    public function disableDelete(bool $disable = true)
51
    {
52
        $this->enableDelete = !$disable;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Disable delete And Hode SelectAll Checkbox.
59
     *
60
     * @return $this
61
     */
62
    public function disableDeleteAndHodeSelectAll()
63
    {
64
        $this->enableDelete = false;
65
66
        $this->holdAll = true;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Add a batch action.
73
     *
74
     * @param $title
75
     * @param BatchAction|null $action
76
     *
77
     * @return $this
78
     */
79
    public function add($title, BatchAction $action = null)
80
    {
81
        $id = $this->actions->count();
82
83
        if (func_num_args() == 1) {
84
            $action = $title;
85
        } elseif (func_num_args() == 2) {
86
            $action->setTitle($title);
0 ignored issues
show
Bug introduced by
It seems like $action is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
87
        }
88
89
        if (method_exists($action, 'setId')) {
90
            $action->setId($id);
91
        }
92
93
        $this->actions->push($action);
94
95
        return $this;
96
    }
97
98
    /**
99
     * Setup scripts of batch actions.
100
     *
101
     * @return void
102
     */
103
    protected function addActionScripts()
104
    {
105
        $this->actions->each(function ($action) {
106
            $action->setGrid($this->grid);
107
108
            if (method_exists($action, 'script')) {
109
                Admin::script($action->script());
110
            }
111
        });
112
    }
113
114
    /**
115
     * Render BatchActions button groups.
116
     *
117
     * @return string
118
     */
119
    public function render()
120
    {
121
        if (!$this->enableDelete) {
122
            $this->actions->shift();
123
        }
124
125
        $this->addActionScripts();
126
127
        return Admin::component('admin::grid.batch-actions', [
128
            'all'     => $this->grid->getSelectAllName(),
129
            'row'     => $this->grid->getGridRowName(),
130
            'actions' => $this->actions,
131
            'holdAll' => $this->holdAll,
132
        ]);
133
    }
134
}
135