ListScreenAdmin.php
1.75 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
<?php
namespace AC\Controller\Middleware;
use AC\Admin\Preference;
use AC\ListScreenRepository\Storage;
use AC\ListScreenTypes;
use AC\Middleware;
use AC\Request;
use AC\Type\ListScreenId;
class ListScreenAdmin implements Middleware {
const PARAM_LIST_ID = 'list_id';
const PARAM_LIST_KEY = 'list_key';
/** @var Storage */
private $storage;
/** @var Preference\ListScreen */
private $preference;
/** @var bool */
private $is_network;
public function __construct( Storage $storage, Preference\ListScreen $preference, $is_network = false ) {
$this->storage = $storage;
$this->preference = $preference;
$this->is_network = (bool) $is_network;
}
public function handle( Request $request ) {
$list_key = $request->get( 'list_screen' );
if ( ! $list_key ) {
$list_key = $this->preference->get_last_visited_list_key();
}
if ( ! $list_key || ! ListScreenTypes::instance()->get_list_screen_by_key( $list_key, $this->is_network ) ) {
$args = $this->is_network
? [ 'network_only' => true ]
: [ 'site_only' => true ];
$list_key = current( ListScreenTypes::instance()->get_list_screens( $args ) )->get_key();
}
if ( ! $list_key ) {
return;
}
$list_id = $request->get( 'layout_id' );
if ( ! ListScreenId::is_valid_id( $list_id ) ) {
$list_id = $this->preference->get_list_id( $list_key );
}
if ( ! ListScreenId::is_valid_id( $list_id ) || ! $this->storage->exists( new ListScreenId( $list_id ) ) ) {
$list_screens = $this->storage->find_all( [
Storage::KEY => $list_key,
] );
$list_id = $list_screens->count() > 0
? $list_screens->get_first()->get_id()->get_id()
: null;
}
$request->get_parameters()->merge( [
self::PARAM_LIST_ID => $list_id,
self::PARAM_LIST_KEY => $list_key,
] );
}
}