|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sco\Admin\Display; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Sco\Admin\Contracts\Display\ColumnInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Table extends Display |
|
10
|
|
|
{ |
|
11
|
|
|
protected $perPage = 20; |
|
12
|
|
|
|
|
13
|
|
|
protected $pageName = 'page'; |
|
14
|
|
|
|
|
15
|
|
|
protected $columns; |
|
16
|
|
|
|
|
17
|
|
|
protected $type = 'table'; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
parent::__construct(); |
|
22
|
|
|
|
|
23
|
|
|
$this->columns = new Collection(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param array $columns |
|
28
|
|
|
* |
|
29
|
|
|
* @return $this |
|
30
|
|
|
*/ |
|
31
|
|
|
public function setColumns(array $columns) |
|
32
|
|
|
{ |
|
33
|
|
|
foreach ($columns as $column) { |
|
34
|
|
|
$this->columns->push($column); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return \Illuminate\Support\Collection |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getColumns() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->columns; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param int $perPage |
|
50
|
|
|
* @param string $pageName |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
|
|
public function paginate($perPage = 25, $pageName = 'page') |
|
55
|
|
|
{ |
|
56
|
|
|
$this->perPage = (int) $perPage; |
|
57
|
|
|
$this->pageName = $pageName; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return $this |
|
64
|
|
|
*/ |
|
65
|
|
|
public function disablePagination() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->perPage = 0; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
public function usePagination() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->perPage > 0; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function get() |
|
81
|
|
|
{ |
|
82
|
|
|
$builder = $this->getQuery(); |
|
83
|
|
|
|
|
84
|
|
|
if ($this->usePagination()) { |
|
85
|
|
|
$data = $builder->paginate($this->perPage, ['*'], $this->pageName) |
|
86
|
|
|
->appends(request()->except($this->pageName)); |
|
87
|
|
|
|
|
88
|
|
|
$data->setCollection($this->parseRows($data->getCollection())); |
|
89
|
|
|
} else { |
|
90
|
|
|
$data = $this->parseRows($builder->get()); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $data; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function toArray() |
|
97
|
|
|
{ |
|
98
|
|
|
return parent::toArray() + [ |
|
99
|
|
|
'columns' => $this->getColumns(), |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function parseRows(Collection $rows) |
|
104
|
|
|
{ |
|
105
|
|
|
return $rows->map(function (Model $row) { |
|
106
|
|
|
$newRow = $this->getColumns()->mapWithKeys(function ( |
|
107
|
|
|
ColumnInterface $column |
|
108
|
|
|
) use ($row) { |
|
109
|
|
|
return [ |
|
110
|
|
|
$column->getName() => $column->setModel($row)->getValue(), |
|
111
|
|
|
]; |
|
112
|
|
|
}); |
|
113
|
|
|
|
|
114
|
|
|
// whether this row has been soft deleted |
|
115
|
|
|
if ($this->getRepository()->isRestorable()) { |
|
116
|
|
|
$newRow->put('_deleted', $row->trashed() ? 1 : 0); |
|
117
|
|
|
} |
|
118
|
|
|
$newRow->put('_primary', $row->getKey()); |
|
119
|
|
|
|
|
120
|
|
|
return $newRow; |
|
121
|
|
|
}); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.