1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Display; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Sco\Admin\Contracts\Display\ColumnInterface; |
9
|
|
|
use Sco\Admin\Display\Concerns\WithPagination; |
10
|
|
|
|
11
|
|
|
class Table extends Display |
12
|
|
|
{ |
13
|
|
|
use WithPagination; |
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
|
|
|
public function initialize() |
27
|
|
|
{ |
28
|
|
|
parent::initialize(); |
29
|
|
|
|
30
|
|
|
$orderBy = request()->query('orderBy', ''); |
31
|
|
|
$sortedBy = request()->query('sortedBy', ''); |
32
|
|
|
|
33
|
|
|
if (! empty($orderBy) && ! empty($sortedBy)) { |
34
|
|
|
$this->orderBy($orderBy, $sortedBy); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param array $columns |
40
|
|
|
* |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
|
|
public function setColumns(array $columns) |
44
|
|
|
{ |
45
|
|
|
foreach ($columns as $column) { |
46
|
|
|
$this->columns->push($column); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return \Illuminate\Support\Collection |
54
|
|
|
*/ |
55
|
|
|
public function getColumns() |
56
|
|
|
{ |
57
|
|
|
return $this->columns; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function get() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if ($this->isPagination()) { |
63
|
|
|
$data = $this->paginate(); |
64
|
|
|
|
65
|
|
|
return $data->setCollection($this->parseRows($data->getCollection())); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->parseRows($this->getQuery()->get()); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function toArray() |
72
|
|
|
{ |
73
|
|
|
return parent::toArray() + [ |
74
|
|
|
'columns' => $this->getColumns(), |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function parseRows(Collection $rows) |
79
|
|
|
{ |
80
|
|
|
return $rows->map(function (Model $row) { |
81
|
|
|
$newRow = $this->getColumns()->mapWithKeys(function ( |
82
|
|
|
ColumnInterface $column |
83
|
|
|
) use ($row) { |
84
|
|
|
return [ |
85
|
|
|
$column->getName() => $column->setModel($row)->getValue(), |
86
|
|
|
]; |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
// whether this row has been soft deleted |
90
|
|
|
if ($this->getRepository()->isRestorable()) { |
91
|
|
|
$newRow->put('_deleted', $row->trashed() ? 1 : 0); |
92
|
|
|
} |
93
|
|
|
$newRow->put('_primary', $row->getKey()); |
94
|
|
|
|
95
|
|
|
return $newRow; |
96
|
|
|
}); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.