1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable; |
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
7
|
|
|
use Illuminate\Support\Fluent; |
8
|
|
|
|
9
|
|
|
class Button extends Fluent implements Arrayable |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Flag to check if user is authorized to use the button. |
13
|
|
|
* |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
protected $authorized = true; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Make a button if condition is true. |
20
|
|
|
* |
21
|
|
|
* @param bool|callable $condition |
22
|
|
|
* @param string|array $options |
23
|
|
|
* @return Button |
24
|
|
|
*/ |
25
|
|
|
public static function makeIf($condition, $options) |
26
|
|
|
{ |
27
|
|
|
if (value($condition)) { |
28
|
|
|
return static::make($options); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return static::make()->authorized(false); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Make a new button instance. |
36
|
|
|
* |
37
|
|
|
* @param string|array $options |
38
|
|
|
* @return Button |
39
|
|
|
*/ |
40
|
|
|
public static function make($options = []) |
41
|
|
|
{ |
42
|
|
|
if (is_string($options)) { |
43
|
|
|
return new static(['extend' => $options]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return new static($options); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set authotization status of the button. |
51
|
|
|
* |
52
|
|
|
* @param bool|callable $bool |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function authorized($bool) |
56
|
|
|
{ |
57
|
|
|
$this->authorized = value($bool); |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Make a button if condition is true. |
64
|
|
|
* |
65
|
|
|
* @param string $permission |
66
|
|
|
* @param string|array $options |
67
|
|
|
* @param Authorizable|null $user |
68
|
|
|
* @return Button |
69
|
|
|
*/ |
70
|
|
|
public static function makeIfCan($permission, $options, Authorizable $user = null) |
71
|
|
|
{ |
72
|
|
|
if (is_null($user)) { |
73
|
|
|
$user = auth()->user(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($user->can($permission)) { |
77
|
|
|
return static::make($options); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return static::make()->authorized(false); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set extend option value. |
85
|
|
|
* |
86
|
|
|
* @param string $value |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function extend($value) |
90
|
|
|
{ |
91
|
|
|
$this->attributes['extend'] = $value; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set editor option value. |
98
|
|
|
* |
99
|
|
|
* @param string $value |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function editor($value) |
103
|
|
|
{ |
104
|
|
|
$this->attributes['editor'] = $value; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set className option value. |
111
|
|
|
* |
112
|
|
|
* @param string $value |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function className($value) |
116
|
|
|
{ |
117
|
|
|
$this->attributes['className'] = $value; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set text option value. |
124
|
|
|
* |
125
|
|
|
* @param string $value |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function text($value) |
129
|
|
|
{ |
130
|
|
|
$this->attributes['text'] = $value; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Convert the Fluent instance to an array. |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function toArray() |
141
|
|
|
{ |
142
|
|
|
if ($this->authorized) { |
143
|
|
|
return parent::toArray(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return []; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|