1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\Tabulator\BulkActions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
6
|
|
|
use LeKoala\Tabulator\AbstractBulkAction; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Generic bulk action that can run a callable |
10
|
|
|
*/ |
11
|
|
|
class GenericBulkAction extends AbstractBulkAction |
12
|
|
|
{ |
13
|
|
|
protected string $name = ''; |
14
|
|
|
protected string $label = ''; |
15
|
|
|
protected $callable = null; |
16
|
|
|
protected bool $xhr = true; |
17
|
|
|
|
18
|
|
|
public function __construct($name, $label, $callable = null) |
19
|
|
|
{ |
20
|
|
|
parent::__construct(); |
21
|
|
|
|
22
|
|
|
$this->name = $name; |
23
|
|
|
$this->label = $label; |
24
|
|
|
|
25
|
|
|
if ($callable) { |
26
|
|
|
$this->callable = $callable; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getI18nLabel(): string |
31
|
|
|
{ |
32
|
|
|
return $this->getLabel(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function process(HTTPRequest $request): string |
36
|
|
|
{ |
37
|
|
|
$records = $this->getRecords() ?? []; |
|
|
|
|
38
|
|
|
$i = 0; |
39
|
|
|
$success = 0; |
40
|
|
|
$errors = 0; |
41
|
|
|
if (!$this->callable) { |
42
|
|
|
return "Callbable function not defined"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
foreach ($records as $record) { |
46
|
|
|
try { |
47
|
|
|
$result = $this->callable($record, $this->tabulatorGrid); |
|
|
|
|
48
|
|
|
if ($result) { |
49
|
|
|
$success++; |
50
|
|
|
} elseif ($result === false) { |
51
|
|
|
$errors++; |
52
|
|
|
} |
53
|
|
|
} catch (\Throwable $th) { |
54
|
|
|
$errors++; |
55
|
|
|
} |
56
|
|
|
$i++; |
57
|
|
|
} |
58
|
|
|
if ($errors) { |
59
|
|
|
$result = _t(__CLASS__ . ".RECORDSPROCESSEDWITHERR", "{err} errors and {count} records processed", ["err", $errors, "count" => $i]); |
60
|
|
|
} else { |
61
|
|
|
$result = _t(__CLASS__ . ".RECORDSPROCESSED", "{count} records processed", ["count" => $i]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the value of callable |
69
|
|
|
*/ |
70
|
|
|
public function getCallable() |
71
|
|
|
{ |
72
|
|
|
return $this->callable; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the value of callable |
77
|
|
|
* |
78
|
|
|
* @param callable $callable |
79
|
|
|
*/ |
80
|
|
|
public function setCallable($callable): self |
81
|
|
|
{ |
82
|
|
|
$this->callable = $callable; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.