InlineValues.php
2.71 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace ACP\Editing\RequestHandler;
use AC;
use AC\Column;
use AC\ListScreenRepository\Storage;
use AC\Request;
use AC\Response;
use AC\Type\ListScreenId;
use ACP\Editing\ApplyFilter\EditValue;
use ACP\Editing\Editable;
use ACP\Editing\ListScreen;
use ACP\Editing\RequestHandler;
use ACP\Editing\Service;
use ACP\Editing\Settings;
class InlineValues implements RequestHandler {
/**
* @var Storage
*/
private $storage;
public function __construct( Storage $storage ) {
$this->storage = $storage;
}
public function handle( Request $request ) {
$response = new Response\Json();
$ids = $request->filter( 'ids', [], FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY );
if ( empty( $ids ) ) {
$response->error();
}
$list_id = $request->get( 'layout' );
if ( ! ListScreenId::is_valid_id( $list_id ) ) {
$response->error();
}
$list_screen = $this->storage->find( new ListScreenId( $list_id ) );
if ( ! $list_screen instanceof ListScreen ) {
$response->error();
}
$strategy = $list_screen->editing();
foreach ( $ids as $k => $id ) {
if ( ! $strategy->user_has_write_permission( $id ) ) {
unset( $ids[ $k ] );
}
}
$column = $list_screen->get_column_by_name( $request->get( 'column' ) );
$values = $column
? $this->get_values_by_column( $column, $ids )
: $this->get_values_by_list_screen( $list_screen, $ids );
$response
->set_parameter( 'editable_values', $values )
->success();
}
/**
* @param AC\ListScreen $list_screen
* @param array $ids
*
* @return array
*/
private function get_values_by_list_screen( AC\ListScreen $list_screen, array $ids ) {
$values = [];
foreach ( $list_screen->get_columns() as $column ) {
$values[] = $this->get_values_by_column( $column, $ids );
}
return array_merge( ...$values );
}
/**
* @param Column $column
* @param array $ids
*
* @return array
*/
private function get_values_by_column( Column $column, array $ids ) {
if ( ! $column instanceof Editable ) {
return [];
}
$setting = $column->get_setting( Settings::NAME );
if ( ! $setting instanceof Settings || ! $setting->is_active() ) {
return [];
}
$service = $column->editing();
if ( ! $service instanceof Service ) {
return [];
}
$values = [];
foreach ( $ids as $id ) {
$value = $service->get_value( $id );
// Apply Filters
$value = ( new EditValue( $id, $column ) )->apply_filters( $value );
// Not editable
if ( null === $value ) {
continue;
}
// Some non-existing values can be set to false
if ( false === $value ) {
$value = '';
}
$values[] = [
'id' => $id,
'column_name' => $column->get_name(),
'value' => $value,
];
}
return $values;
}
}