1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\GridView\Columns; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Itstructure\GridView\Filters\StubFilter; |
7
|
|
|
use Itstructure\GridView\Actions\{BaseAction, View, Edit, Delete}; |
8
|
|
|
use Itstructure\GridView\Traits\Configurable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ActionColumn. |
12
|
|
|
* @package Itstructure\GridView\Columns |
13
|
|
|
*/ |
14
|
|
|
class ActionColumn extends BaseColumn |
15
|
|
|
{ |
16
|
|
|
use Configurable; |
17
|
|
|
|
18
|
|
|
const |
19
|
|
|
ACTION_VIEW = 'view', |
20
|
|
|
ACTION_EDIT = 'edit', |
21
|
|
|
ACTION_DELETE = 'delete', |
22
|
|
|
|
23
|
|
|
ACTION_DEFINITIONS = [ |
24
|
|
|
self::ACTION_VIEW => View::class, |
25
|
|
|
self::ACTION_EDIT => Edit::class, |
26
|
|
|
self::ACTION_DELETE => Delete::class, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $actionTypes; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var BaseAction[] |
36
|
|
|
*/ |
37
|
|
|
protected $actionObjects = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ActionColumn constructor. |
41
|
|
|
* @param array $config |
42
|
|
|
*/ |
43
|
|
|
public function __construct(array $config) |
44
|
|
|
{ |
45
|
|
|
$this->loadConfig($config); |
46
|
|
|
$this->buildActionButtons(); |
47
|
|
|
|
48
|
|
|
$this->filter = new StubFilter(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function buildActionButtons() |
52
|
|
|
{ |
53
|
|
|
foreach ($this->actionTypes as $key => $type) { |
54
|
|
|
if (is_string($key) && in_array($key, array_keys(self::ACTION_DEFINITIONS))) { |
55
|
|
|
if ($type instanceof Closure) { |
56
|
|
|
$class = self::ACTION_DEFINITIONS[$key]; |
57
|
|
|
$this->fillActionObjects(new $class(['url' => $type])); |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (is_string($type) && in_array($type, array_keys(self::ACTION_DEFINITIONS))) { |
62
|
|
|
$class = self::ACTION_DEFINITIONS[$type]; |
63
|
|
|
$this->fillActionObjects(new $class); |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (is_numeric($key)) { |
69
|
|
|
if (is_string($type) && in_array($type, array_keys(self::ACTION_DEFINITIONS))) { |
70
|
|
|
$class = self::ACTION_DEFINITIONS[$type]; |
71
|
|
|
$this->fillActionObjects(new $class); |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (is_array($type) && isset($type['class']) && class_exists($type['class'])) { |
76
|
|
|
$this->fillActionObjects(new $type['class']($type)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getLabel(): string |
86
|
|
|
{ |
87
|
|
|
return $this->label ?? trans('grid_view::grid.actions'); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $row |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function render($row) |
95
|
|
|
{ |
96
|
|
|
$value = ''; |
97
|
|
|
$bootstrapColWidth = floor(12 / count($this->actionObjects)); |
98
|
|
|
foreach ($this->actionObjects as $actionObj) { |
99
|
|
|
$value .= $actionObj->render($row, $bootstrapColWidth); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return '<div class="row">' . $value . '</div>'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param BaseAction $actionObject |
107
|
|
|
*/ |
108
|
|
|
protected function fillActionObjects(BaseAction $actionObject): void |
109
|
|
|
{ |
110
|
|
|
$this->actionObjects = array_merge($this->actionObjects, [$actionObject]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|