|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Sco\Admin\View; |
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Sco\Admin\Contracts\ColumnInterface; |
|
8
|
|
|
use Sco\Admin\Contracts\RepositoryInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Table |
|
11
|
|
|
{ |
|
12
|
|
|
protected $perPage = 20; |
|
13
|
|
|
|
|
14
|
|
|
protected $pageName = 'page'; |
|
15
|
|
|
|
|
16
|
|
|
protected $columns; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->columns = new Collection(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var RepositoryInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $repository; |
|
27
|
|
|
|
|
28
|
|
|
public function setColumns(array $columns) |
|
29
|
|
|
{ |
|
30
|
|
|
foreach ($columns as $column) { |
|
31
|
|
|
$this->columns->push($column); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getColumns() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->columns; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function setRepository(RepositoryInterface $repository) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->repository = $repository; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getRepository() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->repository; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param int $perPage |
|
56
|
|
|
* @param string $pageName |
|
57
|
|
|
* |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
|
|
public function paginate($perPage = 25, $pageName = 'page') |
|
61
|
|
|
{ |
|
62
|
|
|
$this->perPage = (int) $perPage; |
|
63
|
|
|
$this->pageName = $pageName; |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
|
|
public function disablePagination() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->perPage = 0; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function usePagination() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->perPage > 0; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function get() |
|
87
|
|
|
{ |
|
88
|
|
|
$repository = $this->getRepository(); |
|
89
|
|
|
//$orderBy = $this->config->get('orderBy', [$repository->getKeyName(), 'desc']); |
|
|
|
|
|
|
90
|
|
|
$query = $repository->getModel(); |
|
91
|
|
|
|
|
92
|
|
|
if ($repository->isRestorable()) { |
|
93
|
|
|
$query = $query->withTrashed(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
View Code Duplication |
if ($this->usePagination()) { |
|
|
|
|
|
|
97
|
|
|
$data = $query->paginate($this->perPage, ['*'], $this->pageName); |
|
98
|
|
|
|
|
99
|
|
|
$data->setCollection($this->parseRows($data->getCollection())); |
|
100
|
|
|
} else { |
|
101
|
|
|
$data = $this->parseRows($query->get()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $data; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
View Code Duplication |
protected function parseRows(Collection $rows) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
if ($rows) { |
|
110
|
|
|
return $rows->map(function ($row) { |
|
111
|
|
|
$newRow = $this->getColumns()->mapWithKeys(function (ColumnInterface $column) use ($row) { |
|
112
|
|
|
return [$column->getName() => $column->setModel($row)->getModelValue()]; |
|
113
|
|
|
}); |
|
114
|
|
|
|
|
115
|
|
|
// whether this row has been soft deleted |
|
116
|
|
|
if ($this->getRepository()->isRestorable()) { |
|
117
|
|
|
$newRow->put('_deleted', $row->trashed() ? 1 : 0); |
|
118
|
|
|
} |
|
119
|
|
|
return $newRow; |
|
120
|
|
|
}); |
|
121
|
|
|
} |
|
122
|
|
|
return collect(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
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.