1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Services; |
4
|
|
|
|
5
|
|
|
use Akibatech\Crud\Traits\Crudable; |
6
|
|
|
use Illuminate\Contracts\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 string |
34
|
|
|
*/ |
35
|
|
|
protected $class; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* CrudTable constructor. |
39
|
|
|
* |
40
|
|
|
* @param Crudable|string |
41
|
|
|
*/ |
42
|
|
|
public function __construct($crudable) |
43
|
|
|
{ |
44
|
|
|
if (is_string($crudable)) |
45
|
|
|
{ |
46
|
|
|
$this->class = $crudable; |
47
|
|
|
$crudable = new $crudable; |
48
|
|
|
} |
49
|
|
|
else |
50
|
|
|
{ |
51
|
|
|
if (array_key_exists(Crudable::class, class_uses($crudable, true))) |
52
|
|
|
{ |
53
|
|
|
$this->class = get_class($crudable); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->fields = $crudable->getCrudFields(); |
58
|
|
|
$this->manager = $crudable->getCrudManager(); |
59
|
|
|
|
60
|
|
|
$this->hydrateEntries(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param void |
65
|
|
|
* @return self |
66
|
|
|
*/ |
67
|
|
|
protected function hydrateEntries() |
68
|
|
|
{ |
69
|
|
|
$class = $this->class; |
70
|
|
|
$entries = $class::paginate($this->manager->getPerPage()); |
71
|
|
|
$this->entries = $entries->getCollection(); |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param void |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getClass() |
81
|
|
|
{ |
82
|
|
|
return $this->class; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param void |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function table() |
90
|
|
|
{ |
91
|
|
|
$view = View::make('crud::table')->with([ |
92
|
|
|
'create_url' => $this->manager->getActionRoute('create'), |
93
|
|
|
'title' => trans('crud::table.title', ['name' => $this->manager->getPluralizedName()]), |
94
|
|
|
'count' => $this->entries->count(), |
95
|
|
|
'is_empty' => $this->entries->isEmpty(), |
96
|
|
|
'entries' => $this->entries->all(), |
97
|
|
|
'columns' => $this->fields->columns() |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
return $view->render(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param void |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function __toString() |
108
|
|
|
{ |
109
|
|
|
return $this->table(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|