Completed
Push — master ( 7761d4...144654 )
by Song
02:35
created

Action::setUpScript()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid;
4
5
use Encore\Admin\Admin;
6
7
class Action
8
{
9
    const EDIT = 'edit';
10
    const DELETE = 'delete';
11
12
    /**
13
     * Default actions.
14
     *
15
     * @var array
16
     */
17
    protected $defaultActions = [self::EDIT, self::DELETE];
18
19
    /**
20
     * Custom actions.
21
     *
22
     * @var array
23
     */
24
    protected $customActions = [];
25
26
    /**
27
     * Views of default actions.
28
     *
29
     * @var array
30
     */
31
    protected $defaultActionViews = [
32
        self::EDIT   => '<a href="/{path}/{id}/edit"><i class="fa fa-edit"></i></a> ',
33
        self::DELETE => '<a href="javascript:void(0);" data-id="{id}" class="_delete"><i class="fa fa-trash"></i></a> ',
34
    ];
35
36
    /**
37
     * @var Row
38
     */
39
    protected $row;
40
41
    /**
42
     * @var string
43
     */
44
    protected $path = '';
45
46
    /**
47
     * Generate a new Action instance.
48
     *
49
     * @param string $actions
50
     */
51
    public function __construct($actions = 'edit|delete')
52
    {
53
        $actions = explode('|', $actions);
54
55
        $this->defaultActions = array_intersect($actions, $this->defaultActions);
56
    }
57
58
    /**
59
     * Set row for action.
60
     *
61
     * @param Row $row
62
     */
63
    public function setRow(Row $row)
64
    {
65
        $this->row = $row;
66
    }
67
68
    /**
69
     * @param callable $callback
70
     */
71
    public function add(\Closure $callback)
72
    {
73
        $this->customActions[] = $callback($this->row);
74
    }
75
76
    /**
77
     * Set up script for action.
78
     */
79
    protected function setUpScript()
80
    {
81
        $this->path = app('router')->current()->getPath();
82
83
        $confirm = trans('admin::lang.delete_confirm');
84
        $token = csrf_token();
85
        $script = <<<SCRIPT
86
87
$('._delete').click(function() {
88
    var id = $(this).data('id');
89
    if(confirm("{$confirm}")) {
90
        $.post('/{$this->path}/' + id, {_method:'delete','_token':'{$token}'}, function(data){
91
92
            if (typeof data === 'object') {
93
                if (data.status) {
94
                    noty({
95
                        text: "<strong>Succeeded!</strong><br/>"+data.message,
96
                        type:'success',
97
                        timeout: 3000
98
                    });
99
                } else {
100
                    noty({
101
                        text: "<strong>Failed!</strong><br/>"+data.message,
102
                        type:'error',
103
                        timeout: 3000
104
                    });
105
                }
106
            }
107
108
            $.pjax.reload('#pjax-container');
109
        });
110
    }
111
});
112
113
SCRIPT;
114
115
        Admin::script($script);
116
    }
117
118
    /**
119
     * Render actions.
120
     *
121
     * @return string
122
     */
123
    public function render()
124
    {
125
        $this->setUpScript();
126
127
        $actionEntities = $this->customActions;
128
129
        foreach ($this->defaultActions as $action) {
130
            $actionEntities[] = str_replace(
131
                ['{path}', '{id}'],
132
                [$this->path, $this->row->id()],
133
                $this->defaultActionViews[$action]
134
            );
135
        }
136
137
        return implode(' ', $actionEntities);
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function __toString()
144
    {
145
        return $this->render();
146
    }
147
}
148