1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace XHGui\Controller; |
4
|
|
|
|
5
|
|
|
use Slim\Http\Request; |
6
|
|
|
use Slim\Http\Response; |
7
|
|
|
use Slim\Slim as App; |
8
|
|
|
use XHGui\AbstractController; |
9
|
|
|
use XHGui\Searcher\SearcherInterface; |
10
|
|
|
|
11
|
|
|
class CustomController extends AbstractController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var SearcherInterface |
15
|
|
|
*/ |
16
|
|
|
protected $searcher; |
17
|
|
|
|
18
|
|
|
public function __construct(App $app, SearcherInterface $searcher) |
19
|
|
|
{ |
20
|
|
|
parent::__construct($app); |
21
|
|
|
$this->searcher = $searcher; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function get(): void |
25
|
|
|
{ |
26
|
|
|
$this->render('custom/create.twig'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function help(Request $request): void |
30
|
|
|
{ |
31
|
|
|
if ($request->get('id')) { |
32
|
|
|
$res = $this->searcher->get($request->get('id')); |
33
|
|
|
} else { |
34
|
|
|
$res = $this->searcher->latest(); |
35
|
|
|
} |
36
|
|
|
$this->render('custom/help.twig', [ |
37
|
|
|
'data' => print_r($res->toArray(), 1), |
38
|
|
|
]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function query(Request $request, Response $response) |
42
|
|
|
{ |
43
|
|
|
$response['Content-Type'] = 'application/json'; |
44
|
|
|
|
45
|
|
|
$query = json_decode($request->post('query'), true); |
46
|
|
|
$error = []; |
47
|
|
|
if (null === $query) { |
48
|
|
|
$error['query'] = json_last_error(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$retrieve = json_decode($request->post('retrieve'), true); |
52
|
|
|
if (null === $retrieve) { |
53
|
|
|
$error['retrieve'] = json_last_error(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (count($error) > 0) { |
57
|
|
|
$json = json_encode(['error' => $error]); |
58
|
|
|
|
59
|
|
|
return $response->body($json); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$perPage = $this->config('page.limit'); |
63
|
|
|
|
64
|
|
|
$res = $this->searcher->query($query, $perPage, $retrieve); |
65
|
|
|
|
66
|
|
|
return $response->body(json_encode($res)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|