1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sco\Admin\View; |
5
|
|
|
|
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Sco\Admin\Contracts\ColumnInterface; |
8
|
|
|
|
9
|
|
|
class Table extends View |
10
|
|
|
{ |
11
|
|
|
protected $perPage = 20; |
12
|
|
|
|
13
|
|
|
protected $pageName = 'page'; |
14
|
|
|
|
15
|
|
|
protected $columns; |
16
|
|
|
|
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
$this->columns = new Collection(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $columns |
24
|
|
|
* |
25
|
|
|
* @return $this |
26
|
|
|
*/ |
27
|
|
|
public function setColumns(array $columns) |
28
|
|
|
{ |
29
|
|
|
foreach ($columns as $column) { |
30
|
|
|
$this->columns->push($column); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getColumns() |
37
|
|
|
{ |
38
|
|
|
return $this->columns; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function paginate($perPage = 25, $pageName = 'page') |
45
|
|
|
{ |
46
|
|
|
$this->perPage = (int) $perPage; |
47
|
|
|
$this->pageName = $pageName; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function disablePagination() |
56
|
|
|
{ |
57
|
|
|
$this->perPage = 0; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function usePagination() |
66
|
|
|
{ |
67
|
|
|
return $this->perPage > 0; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function get() |
71
|
|
|
{ |
72
|
|
|
$repository = $this->getRepository(); |
73
|
|
|
//$orderBy = $this->config->get('orderBy', [$repository->getKeyName(), 'desc']); |
|
|
|
|
74
|
|
|
$query = $repository->getQuery(); |
75
|
|
|
|
76
|
|
|
if ($repository->isRestorable()) { |
77
|
|
|
$query->withTrashed(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
if ($this->usePagination()) { |
|
|
|
|
81
|
|
|
$data = $query->paginate($this->perPage, ['*'], $this->pageName); |
82
|
|
|
|
83
|
|
|
$data->setCollection($this->parseRows($data->getCollection())); |
84
|
|
|
} else { |
85
|
|
|
$data = $this->parseRows($query->get()); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $data; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function toArray() |
92
|
|
|
{ |
93
|
|
|
return parent::toArray() + [ |
94
|
|
|
'columns' => $this->getColumns(), |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
protected function parseRows(Collection $rows) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
if ($rows) { |
101
|
|
|
return $rows->map(function ($row) { |
102
|
|
|
$newRow = $this->getColumns()->mapWithKeys(function (ColumnInterface $column) use ($row) { |
103
|
|
|
return [$column->getName() => $column->setModel($row)->getModelValue()]; |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
// whether this row has been soft deleted |
107
|
|
|
if ($this->getRepository()->isRestorable()) { |
108
|
|
|
$newRow->put('_deleted', $row->trashed() ? 1 : 0); |
109
|
|
|
} |
110
|
|
|
return $newRow; |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
return collect(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.