|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/mvc package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Mvc\Controller; |
|
11
|
|
|
|
|
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
13
|
|
|
use Slick\Filter\StaticFilter; |
|
14
|
|
|
use Slick\Http\PhpEnvironment\Request; |
|
15
|
|
|
use Slick\Mvc\Service\Entity\EntityListingService; |
|
16
|
|
|
use Slick\Mvc\Service\Entity\QueryFilter\SearchFilter; |
|
17
|
|
|
use Slick\Mvc\Utils\Pagination; |
|
18
|
|
|
use Slick\Orm\Repository\EntityRepository; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Entity Listing Methods |
|
22
|
|
|
* |
|
23
|
|
|
* @package Slick\Mvc\Controller |
|
24
|
|
|
* @author Filipe Silva <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
trait EntityListingMethods |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $rowsPerPage = 12; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Pagination |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $pagination; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var EntityListingService |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $listingService; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string[] |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $searchFields; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Handle the request to display a list of entities |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function index() |
|
53
|
|
|
{ |
|
54
|
2 |
|
$this->getListingService() |
|
55
|
2 |
|
->setPagination($this->getPagination()) |
|
56
|
2 |
|
->getFilters()->add($this->getSearchFilter()); |
|
57
|
2 |
|
$this->set( |
|
|
|
|
|
|
58
|
|
|
[ |
|
59
|
2 |
|
$this->getEntityNamePlural() => $this->getListingService() |
|
60
|
2 |
|
->getList(), |
|
61
|
2 |
|
'pagination' => $this->getListingService()->getPagination() |
|
62
|
2 |
|
] |
|
63
|
2 |
|
); |
|
64
|
2 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get pagination for roes per page property |
|
68
|
|
|
* |
|
69
|
|
|
* @return Pagination |
|
70
|
|
|
*/ |
|
71
|
4 |
|
protected function getPagination() |
|
72
|
|
|
{ |
|
73
|
4 |
|
if (null == $this->pagination) { |
|
74
|
4 |
|
$this->pagination = new Pagination( |
|
75
|
|
|
[ |
|
76
|
4 |
|
'rowsPerPage' => $this->rowsPerPage, |
|
77
|
4 |
|
'request' => $this->getRequest() |
|
78
|
4 |
|
] |
|
79
|
4 |
|
); |
|
80
|
4 |
|
} |
|
81
|
4 |
|
return $this->pagination; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the entity listing service |
|
86
|
|
|
* |
|
87
|
|
|
* @return EntityListingService |
|
88
|
|
|
*/ |
|
89
|
4 |
|
protected function getListingService() |
|
90
|
|
|
{ |
|
91
|
4 |
|
if (null == $this->listingService) { |
|
92
|
2 |
|
$this->listingService = new EntityListingService( |
|
93
|
2 |
|
$this->getEntityClassName() |
|
|
|
|
|
|
94
|
2 |
|
); |
|
95
|
2 |
|
} |
|
96
|
4 |
|
return $this->listingService; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get search filter |
|
101
|
|
|
* |
|
102
|
|
|
* @return SearchFilter |
|
103
|
|
|
*/ |
|
104
|
4 |
|
protected function getSearchFilter() |
|
105
|
|
|
{ |
|
106
|
4 |
|
$pattern = $this->getRequest()->getQuery('pattern', null); |
|
|
|
|
|
|
107
|
4 |
|
$pattern = StaticFilter::filter('text', $pattern); |
|
108
|
4 |
|
$this->set('pattern', $pattern); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
4 |
|
return new SearchFilter(['pattern' => $pattern]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the fields list to use on search filter |
|
115
|
|
|
* |
|
116
|
|
|
* @return array|\string[] |
|
117
|
|
|
*/ |
|
118
|
2 |
|
protected function getSearchFields() |
|
119
|
|
|
{ |
|
120
|
2 |
|
if (null == $this->searchFields) { |
|
121
|
2 |
|
$field = $this->getEntityDescriptor()->getDisplayFiled(); |
|
122
|
2 |
|
$this->searchFields = [ |
|
123
|
2 |
|
$this->getEntityDescriptor() |
|
124
|
2 |
|
->getTableName().'.'.$field->getField() |
|
125
|
2 |
|
]; |
|
126
|
2 |
|
} |
|
127
|
2 |
|
return $this->searchFields; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Returns the query order by clause |
|
132
|
|
|
* |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function getOrder() |
|
136
|
|
|
{ |
|
137
|
|
|
/** @var EntityRepository $repo */ |
|
138
|
|
|
$repo = $this->getRepository(); |
|
|
|
|
|
|
139
|
|
|
$table = $repo->getEntityDescriptor()->getTableName(); |
|
140
|
|
|
$pmk = $repo->getEntityDescriptor()->getPrimaryKey()->getField(); |
|
141
|
|
|
return "{$table}.{$pmk} DESC"; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Gets updated HTTP request |
|
146
|
|
|
* |
|
147
|
|
|
* @return ServerRequestInterface|Request |
|
148
|
|
|
*/ |
|
149
|
|
|
abstract public function getRequest(); |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get the current entity descriptor |
|
153
|
|
|
* |
|
154
|
|
|
* @return \Slick\Orm\Descriptor\EntityDescriptorInterface |
|
155
|
|
|
*/ |
|
156
|
|
|
abstract protected function getEntityDescriptor(); |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get the plural name of the entity |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
abstract protected function getEntityNamePlural(); |
|
164
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.