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\Common\Utils\Text; |
14
|
|
|
use Slick\Filter\StaticFilter; |
15
|
|
|
use Slick\Http\PhpEnvironment\Request; |
16
|
|
|
use Slick\Mvc\ControllerInterface; |
17
|
|
|
use Slick\Mvc\Service\Entity\EntityListingService; |
18
|
|
|
use Slick\Mvc\Service\Entity\QueryFilter\SearchFilter; |
19
|
|
|
use Slick\Mvc\Utils\Pagination; |
20
|
|
|
use Slick\Orm\Orm; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Entity Listing Methods |
24
|
|
|
* |
25
|
|
|
* @package Slick\Mvc\Controller |
26
|
|
|
* @author Filipe Silva <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
trait EntityListingMethods |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $rowsPerPage = 12; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Pagination |
38
|
|
|
*/ |
39
|
|
|
protected $pagination; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var EntityListingService |
43
|
|
|
*/ |
44
|
|
|
protected $listingService; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string[] |
48
|
|
|
*/ |
49
|
|
|
protected $searchFields; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Handle the request to display a list of entities |
53
|
|
|
*/ |
54
|
2 |
|
public function index() |
55
|
|
|
{ |
56
|
2 |
|
$this->getListingService() |
57
|
2 |
|
->setPagination($this->getPagination()) |
58
|
2 |
|
->getFilters()->add($this->getSearchFilter()); |
59
|
2 |
|
$this->set( |
60
|
|
|
[ |
61
|
2 |
|
$this->getEntityNamePlural() => $this->getListingService() |
62
|
2 |
|
->getList(), |
63
|
2 |
|
'pagination' => $this->getListingService()->getPagination() |
64
|
1 |
|
] |
65
|
1 |
|
); |
66
|
2 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get pagination for roes per page property |
70
|
|
|
* |
71
|
|
|
* @return Pagination |
72
|
|
|
*/ |
73
|
4 |
|
protected function getPagination() |
74
|
|
|
{ |
75
|
4 |
|
if (null == $this->pagination) { |
76
|
4 |
|
$this->pagination = new Pagination( |
77
|
|
|
[ |
78
|
4 |
|
'rowsPerPage' => $this->rowsPerPage, |
79
|
4 |
|
'request' => $this->getRequest() |
80
|
2 |
|
] |
81
|
2 |
|
); |
82
|
2 |
|
} |
83
|
4 |
|
return $this->pagination; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the entity listing service |
88
|
|
|
* |
89
|
|
|
* @return EntityListingService |
90
|
|
|
*/ |
91
|
4 |
|
protected function getListingService() |
92
|
|
|
{ |
93
|
4 |
|
if (null == $this->listingService) { |
94
|
2 |
|
$this->listingService = new EntityListingService( |
95
|
2 |
|
$this->entityClassName |
|
|
|
|
96
|
1 |
|
); |
97
|
1 |
|
} |
98
|
4 |
|
return $this->listingService; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get search filter |
103
|
|
|
* |
104
|
|
|
* @return SearchFilter |
105
|
|
|
*/ |
106
|
4 |
|
protected function getSearchFilter() |
107
|
|
|
{ |
108
|
4 |
|
$pattern = $this->getRequest()->getQuery('pattern', null); |
|
|
|
|
109
|
4 |
|
$pattern = StaticFilter::filter('text', $pattern); |
110
|
4 |
|
$this->set('pattern', $pattern); |
111
|
|
|
|
112
|
4 |
|
return new SearchFilter(['pattern' => $pattern]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the plural name of the entity |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
4 |
|
protected function getEntityNamePlural() |
121
|
|
|
{ |
122
|
4 |
|
$names = explode('\\', $this->entityClassName); |
123
|
4 |
|
$name = end($names); |
124
|
4 |
|
$nameParts = Text::camelCaseToSeparator($name, '#'); |
125
|
4 |
|
$nameParts = explode('#', $nameParts); |
126
|
4 |
|
$lastPart = array_pop($nameParts); |
127
|
4 |
|
$lastPart = ucfirst(Text::plural(strtolower($lastPart))); |
128
|
4 |
|
array_push($nameParts, $lastPart); |
129
|
4 |
|
return lcfirst(implode('', $nameParts)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Gets updated HTTP request |
134
|
|
|
* |
135
|
|
|
* @return ServerRequestInterface|Request |
136
|
|
|
*/ |
137
|
|
|
abstract public function getRequest(); |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Sets a value to be used by render |
141
|
|
|
* |
142
|
|
|
* The key argument can be an associative array with values to be set |
143
|
|
|
* or a string naming the passed value. If an array is given then the |
144
|
|
|
* value will be ignored. |
145
|
|
|
* |
146
|
|
|
* Those values must be set in the request attributes so they can be used |
147
|
|
|
* latter by any other middle ware in the stack. |
148
|
|
|
* |
149
|
|
|
* @param string|array $key |
150
|
|
|
* @param mixed $value |
151
|
|
|
* |
152
|
|
|
* @return ControllerInterface |
153
|
|
|
*/ |
154
|
|
|
abstract public function set($key, $value = null); |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Gets the entity FQ class name |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
abstract protected function getEntityClassName(); |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the fields list to use on search filter |
165
|
|
|
* |
166
|
|
|
* @return array|\string[] |
167
|
|
|
*/ |
168
|
2 |
|
protected function getSearchFields() |
169
|
|
|
{ |
170
|
2 |
|
if (null == $this->searchFields) { |
171
|
2 |
|
$descriptor = $this->getListingService() |
172
|
2 |
|
->getRepository() |
173
|
2 |
|
->getEntityDescriptor() |
174
|
1 |
|
; |
175
|
2 |
|
$field = $descriptor->getDisplayFiled(); |
176
|
2 |
|
$this->searchFields = [ |
177
|
2 |
|
$descriptor->getTableName().'.'.$field->getField() |
178
|
1 |
|
]; |
179
|
1 |
|
} |
180
|
2 |
|
return $this->searchFields; |
181
|
|
|
} |
182
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: