1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Services; |
4
|
|
|
|
5
|
|
|
use Akibatech\Crud\Traits\Crudable; |
6
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\View; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CrudTable |
12
|
|
|
* |
13
|
|
|
* @package Akibatech\Crud\Services |
14
|
|
|
*/ |
15
|
|
|
class CrudTable |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CrudFields |
19
|
|
|
*/ |
20
|
|
|
protected $fields; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var CrudManager |
24
|
|
|
*/ |
25
|
|
|
protected $manager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Collection |
29
|
|
|
*/ |
30
|
|
|
protected $entries; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var LengthAwarePaginator |
34
|
|
|
*/ |
35
|
|
|
protected $paginator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $class; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* CrudTable constructor. |
44
|
|
|
* |
45
|
|
|
* @param Crudable|string |
46
|
|
|
*/ |
47
|
|
|
public function __construct($crudable) |
48
|
|
|
{ |
49
|
|
|
if (is_string($crudable)) |
50
|
|
|
{ |
51
|
|
|
$this->class = $crudable; |
52
|
|
|
$crudable = new $crudable; |
53
|
|
|
} |
54
|
|
|
else |
55
|
|
|
{ |
56
|
|
|
if (array_key_exists(Crudable::class, class_uses($crudable, true))) |
57
|
|
|
{ |
58
|
|
|
$this->class = get_class($crudable); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->fields = $crudable->getCrudFields(); |
63
|
|
|
$this->manager = $crudable->getCrudManager(); |
64
|
|
|
|
65
|
|
|
$this->hydrateEntries(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param void |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
|
|
protected function hydrateEntries() |
73
|
|
|
{ |
74
|
|
|
$class = $this->class; |
75
|
|
|
|
76
|
|
|
$this->paginator = $class::latest()->paginate($this->manager->getPerPage()); |
77
|
|
|
$this->entries = $this->paginator->getCollection(); |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param void |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getClass() |
87
|
|
|
{ |
88
|
|
|
return $this->class; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param void |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function table() |
96
|
|
|
{ |
97
|
|
|
$view = View::make('crud::table')->with([ |
98
|
|
|
'create_url' => $this->manager->getActionRoute('create'), |
99
|
|
|
'title' => trans('crud::table.title', ['name' => $this->manager->getPluralizedName()]), |
100
|
|
|
'count' => $this->paginator->total(), |
101
|
|
|
'is_empty' => $this->paginator->total() === 0, |
102
|
|
|
'pagination' => $this->paginator, |
103
|
|
|
'entries' => $this->entries->all(), |
104
|
|
|
'columns' => $this->fields->columns() |
105
|
|
|
]); |
106
|
|
|
|
107
|
|
|
return $view->render(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param void |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function __toString() |
115
|
|
|
{ |
116
|
|
|
return $this->table(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|