RelationshipChildren.php
1.02 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
<?php
namespace ACA\JetEngine\Editing\Storage;
use ACP;
use Jet_Engine\Relations\Relation;
class RelationshipChildren implements ACP\Editing\Storage {
/**
* @var Relation
*/
private $relation;
public function __construct( Relation $relation ) {
$this->relation = $relation;
}
public function get( $id ) {
$item_key = 'child_object_id';
$result = [];
foreach ( $this->relation->get_children( $id ) as $item ) {
$result[] = $item[ $item_key ];
}
return array_combine( $result, $result );
}
public function update( int $id, $data ): bool {
$data = is_array( $data ) ? $data : [ $data ];
$current_ids = array_keys( $this->get( $id ) );
$updated_ids = array_intersect( $current_ids, $data );
$deleted_ids = array_diff( $current_ids, $data );
$added_ids = array_diff( $data, $updated_ids );
foreach ( $deleted_ids as $delete_id ) {
$this->relation->delete_rows( $id, $delete_id );
}
foreach ( $added_ids as $added_id ) {
$this->relation->update( $id, $added_id );
}
return true;
}
}