1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Folk\Controllers; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
6
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
7
|
|
|
use Folk\Admin; |
8
|
|
|
use Folk\Entities\EntityInterface; |
9
|
|
|
use Zend\Diactoros\Response\RedirectResponse; |
10
|
|
|
use Folk\SearchQuery; |
11
|
|
|
|
12
|
|
|
class SearchEntity extends Entity |
13
|
|
|
{ |
14
|
|
|
public function html(Request $request, Response $response, Admin $app, EntityInterface $entity) |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$search = new SearchQuery($request->getQueryParams()); |
17
|
|
|
$items = $this->search($entity, $search); |
18
|
|
|
|
19
|
|
|
//Redirect to edit element if it's only one result |
20
|
|
|
if (count($items) === 1 && !empty($search->getQuery())) { |
21
|
|
|
return new RedirectResponse($app->getRoute('read', [ |
22
|
|
|
'entity' => $entity->getName(), |
23
|
|
|
'id' => key($items), |
24
|
|
|
])); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
//Load the values |
28
|
|
|
$row = $entity->getScheme($app['builder']); |
29
|
|
|
|
30
|
|
|
//Remove non-listable elements |
31
|
|
|
$removedKeys = []; |
32
|
|
|
|
33
|
|
|
foreach ($row as $key => $value) { |
34
|
|
|
if ($value->get('list') === false) { |
35
|
|
|
$removedKeys[] = $key; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
foreach ($removedKeys as $key) { |
40
|
|
|
unset($row[$key]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
//populate the data |
44
|
|
|
$rows = []; |
45
|
|
|
|
46
|
|
|
foreach ($items as $id => &$item) { |
47
|
|
|
$rows[$id] = clone $row; |
48
|
|
|
$rows[$id]->val($item); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (count($items) === 50) { |
52
|
|
|
$search->setPage($search->getPage() + 1); |
53
|
|
|
} else { |
54
|
|
|
$search->setPage(null); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
//List all results |
58
|
|
|
return $app['templates']->render('pages/search', [ |
59
|
|
|
'rows' => $rows, |
60
|
|
|
'entity' => $entity, |
61
|
|
|
'search' => $search, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function json(Request $request, Response $response, Admin $app) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$entity = $app->getEntity($request->getAttribute('entity')); |
68
|
|
|
$search = new SearchQuery($request->getQueryParams()); |
69
|
|
|
|
70
|
|
|
$json = []; |
71
|
|
|
|
72
|
|
|
foreach ($this->search($entity, $search) as $id => $item) { |
|
|
|
|
73
|
|
|
$json[$id] = $entity->getLabel($id, $item); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return json_encode($json); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Execute the search in the entity. |
81
|
|
|
* |
82
|
|
|
* @param EntityInterface $entity |
83
|
|
|
* @param SearchQuery $search |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
private function search(EntityInterface $entity, SearchQuery $search) |
88
|
|
|
{ |
89
|
|
|
if ($search->getPage() === null) { |
90
|
|
|
$search->setPage(1); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (($id = $search->getId())) { |
94
|
|
|
if ($items = $entity->read($id)) { |
95
|
|
|
return [$id => $items]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return []; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $entity->search($search); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.