RowAction.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* +-----------------------------------------------------------------------------------------------------------------------
* 重写 laravel-admin Actions\RowAction 类
* +-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Rewrite\Actions
* @package App\Admin\Rewrite\Actions
* @author Richer <yangzi1028@163.com>
* @date 2020年11月12日13:49:31
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Rewrite\Actions;
use Encore\Admin\Grid\Column;
use Illuminate\Http\Request;
abstract class RowAction extends GridAction
{
/**
* @var \Illuminate\Database\Eloquent\Model
*/
protected $row;
/**
* @var Column
*/
protected $column;
/**
* @var string
*/
public $selectorPrefix = '.grid-row-action-';
/**
* @var string
*/
protected $class = 'btn btn-success';
/**
* @var bool
*/
protected $asColumn = false;
/**
* Get primary key value of current row.
*
* @return mixed
*/
protected function getKey()
{
return $this->row->getKey();
}
/**
* Set row model.
*
* @param mixed $key
*
* @return \Illuminate\Database\Eloquent\Model|mixed
*/
public function row($key = null)
{
if (func_num_args() == 0) {
return $this->row;
}
return $this->row->getAttribute($key);
}
/**
* Set row model.
*
* @param \Illuminate\Database\Eloquent\Model $row
*
* @return \Encore\Admin\Actions\RowAction
*/
public function setRow($row)
{
$this->row = $row;
return $this;
}
public function getRow()
{
return $this->row;
}
/**
* @param Column $column
*
* @return $this
*/
public function setColumn(Column $column)
{
$this->column = $column;
return $this;
}
/**
* Show this action as a column.
*
* @return $this
*/
public function asColumn()
{
$this->asColumn = true;
return $this;
}
/**
* @return string
*/
public function href()
{
}
/**
* @param Request $request
*
* @return mixed
*/
public function retrieveModel(Request $request)
{
if (!$key = $request->get('_key')) {
return false;
}
$modelClass = str_replace('_', '\\', $request->get('_model'));
if ($this->modelUseSoftDeletes($modelClass)) {
return $modelClass::withTrashed()->findOrFail($key);
}
return $modelClass::findOrFail($key);
}
// public function display($value)
// {
// }
/**
* @return string
*/
protected function getElementClass()
{
return ltrim($this->selector($this->selectorPrefix), '.') . $this->addElementClass();
}
/**
* @return string
*/
public function addElementClass()
{
return " ". $this->class;
}
/**
* Render row action.
*
* @return string
*/
public function render()
{
if ($href = $this->href()) {
return "<a href='{$href}'>{$this->name()}</a>";
}
$this->addScript();
$attributes = $this->formatAttributes();
return sprintf(
"<a data-_key='%s' href='javascript:void(0);' class='%s' {$attributes}>%s</a>",
$this->getKey(),
$this->getElementClass(),
$this->asColumn ? $this->display($this->row($this->column->getName())) : $this->name()
);
}
}