SelectValues.php
2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace ACP\Editing\RequestHandler;
use AC;
use AC\ListScreenRepository\Storage;
use AC\Request;
use AC\Response;
use AC\Type\ListScreenId;
use ACP\Editing\PaginatedOptions;
use ACP\Editing\RemoteOptions;
use ACP\Editing\RequestHandler;
use ACP\Editing\Service;
use ACP\Editing\ServiceFactory;
class SelectValues implements RequestHandler
{
private $storage;
public function __construct(Storage $storage)
{
$this->storage = $storage;
}
public function handle(Request $request)
{
$response = new Response\Json();
$service = $this->get_service_from_request($request);
if ( ! $service) {
$response->error();
}
switch (true) {
case $service instanceof RemoteOptions:
$options = $service->get_remote_options(
$request->filter('item_id', null, FILTER_SANITIZE_NUMBER_INT)
);
$select = new AC\Helper\Select\Response($options, false);
break;
case $service instanceof PaginatedOptions:
$options = $service->get_paginated_options(
$request->filter('searchterm'),
$request->filter('page', 1, FILTER_SANITIZE_NUMBER_INT),
$request->filter('item_id', null, FILTER_SANITIZE_NUMBER_INT)
);
$has_more = ! $options->is_last_page();
$select = new AC\Helper\Select\Response($options, $has_more);
break;
default:
$response->error();
}
$response
->set_parameters($select())
->success();
}
private function get_service_from_request(Request $request): ?Service
{
$list_id = $request->get('layout');
if ( ! ListScreenId::is_valid_id($list_id)) {
return null;
}
$list_screen = $this->storage->find(new ListScreenId($list_id));
if ( ! $list_screen || ! $list_screen->is_user_allowed(wp_get_current_user())) {
return null;
}
$strategy = $list_screen->editing();
if ( ! $strategy) {
return null;
}
$column = $list_screen->get_column_by_name((string)$request->get('column'));
if ( ! $column) {
return null;
}
return ServiceFactory::create($column);
}
}