1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Slim\Slim; |
4
|
|
|
|
5
|
|
|
class Xhgui_Controller_Custom extends Xhgui_Controller |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Xhgui_Searcher_Interface |
9
|
|
|
*/ |
10
|
|
|
protected $searcher; |
11
|
|
|
|
12
|
|
|
public function __construct(Slim $app, Xhgui_Searcher_Interface $searcher) |
13
|
|
|
{ |
14
|
|
|
parent::__construct($app); |
15
|
|
|
$this->searcher = $searcher; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function get() |
19
|
|
|
{ |
20
|
|
|
$this->_template = 'custom/create.twig'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function help() |
24
|
|
|
{ |
25
|
|
|
$request = $this->app->request(); |
26
|
|
|
if ($request->get('id')) { |
27
|
|
|
$res = $this->searcher->get($request->get('id')); |
28
|
|
|
} else { |
29
|
|
|
$res = $this->searcher->latest(); |
30
|
|
|
} |
31
|
|
|
$this->_template = 'custom/help.twig'; |
32
|
|
|
$this->set(array( |
33
|
|
|
'data' => print_r($res->toArray(), 1) |
34
|
|
|
)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function query() |
38
|
|
|
{ |
39
|
|
|
$request = $this->app->request(); |
40
|
|
|
$response = $this->app->response(); |
41
|
|
|
$response['Content-Type'] = 'application/json'; |
42
|
|
|
|
43
|
|
|
$query = json_decode($request->post('query'), true); |
44
|
|
|
$error = array(); |
45
|
|
|
if (null === $query) { |
46
|
|
|
$error['query'] = json_last_error(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$retrieve = json_decode($request->post('retrieve'), true); |
50
|
|
|
if (null === $retrieve) { |
51
|
|
|
$error['retrieve'] = json_last_error(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (count($error) > 0) { |
55
|
|
|
$json = json_encode(array('error' => $error)); |
56
|
|
|
return $response->body($json); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$perPage = $this->app->config('page.limit'); |
60
|
|
|
|
61
|
|
|
$res = $this->searcher->query($query, $perPage, $retrieve); |
62
|
|
|
|
63
|
|
|
return $response->body(json_encode($res)); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.