Relation.php
1.78 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
<?php
namespace ACA\MetaBox\Editing\Service;
use ACA;
use ACA\MetaBox\Entity;
use ACP;
use ACP\Editing\View;
use InvalidArgumentException;
abstract class Relation implements ACP\Editing\PaginatedOptions, ACP\Editing\Service {
/**
* @var Entity\Relation
*/
protected $relation;
public function __construct( Entity\Relation $relation ) {
$this->relation = $relation;
}
public function get_view( string $context ): ?View {
$view = new ACP\Editing\View\AjaxSelect();
$view->set_multiple( true )
->set_clear_button( true );
if ( 'bulk' === $context ) {
$view->has_methods( true );
}
return $view;
}
public function update( int $id, $data ): void {
$method = $data['method'] ?? null;
if ( ! $method ) {
$relation_ids = $data && is_array( $data )
? $data
: [];
$this->replace( $id, $relation_ids );
return;
}
$relation_ids = $data['value'] ?? [];
if ( ! is_array( $relation_ids ) ) {
throw new InvalidArgumentException( 'Invalid value' );
}
switch ( $method ) {
case 'add':
foreach ( $relation_ids as $relation_id ) {
$this->relation->add_relation( $id, $relation_id );
}
break;
case 'remove':
foreach ( $relation_ids as $relation_id ) {
$this->relation->delete_relation( $id, $relation_id );
}
break;
default:
$this->replace( $id, $relation_ids );
}
}
protected function replace( $id, array $relation_ids ) {
foreach ( $this->relation->get_related_ids( $id ) as $related_id ) {
$this->relation->delete_relation( $id, $related_id );
}
foreach ( $relation_ids as $relation_id ) {
$this->relation->add_relation( $id, $relation_id );
}
}
public function get_value( $id ) {
$ids = array_filter( $this->relation->get_related_ids( $id ) );
return array_combine( $ids, $ids );
}
}