5
5
use A17 \Twill \Facades \TwillBlocks ;
6
6
use A17 \Twill \Models \Block ;
7
7
use A17 \Twill \Models \Contracts \TwillModelContract ;
8
+ use A17 \Twill \Models \RelatedItem ;
8
9
use A17 \Twill \Repositories \Behaviors \HandleFiles ;
9
10
use A17 \Twill \Repositories \Behaviors \HandleMedias ;
10
11
use A17 \Twill \Services \Blocks \Block as BlockConfig ;
11
12
use Illuminate \Config \Repository as Config ;
12
13
use Illuminate \Support \Collection ;
13
- use Illuminate \Support \Facades \Log ;
14
- use Illuminate \Support \Facades \Schema ;
15
- use ReflectionException ;
16
14
17
15
class BlockRepository extends ModuleRepository
18
16
{
@@ -35,42 +33,37 @@ public function getCrops(string $role): array
35
33
36
34
public function hydrate (TwillModelContract $ model , array $ fields ): TwillModelContract
37
35
{
38
- if (Schema::hasTable (config ('twill.related_table ' , 'twill_related ' ))) {
39
- $ relatedItems = Collection::make ();
40
-
41
- Collection::make ($ fields ['browsers ' ])->each (function ($ items , $ browserName ) use (&$ relatedItems ) {
42
- Collection::make ($ items )->each (function ($ item ) use ($ browserName , &$ relatedItems ) {
43
- try {
44
- // @todo: Repository could be null.
45
- $ repository = $ this ->getModelRepository ($ item ['endpointType ' ] ?? $ browserName );
46
- $ relatedItems ->push (
47
- (object ) [
48
- 'related ' => $ repository ->getById ($ item ['id ' ]),
49
- 'browser_name ' => $ browserName ,
50
- ]
51
- );
52
- } catch (ReflectionException $ reflectionException ) {
53
- Log::error ($ reflectionException );
54
- }
55
- });
56
- });
57
-
58
- $ model ->setRelation ('relatedItems ' , $ relatedItems );
59
- }
36
+ $ relatedItems = collect ($ fields ['browsers ' ])
37
+ ->flatMap (fn ($ items , $ browserName ) => collect ($ items )
38
+ ->map (fn ($ item , $ position ) => RelatedItem::make ([
39
+ 'subject_id ' => $ model ->getKey (),
40
+ 'subject_type ' => $ model ->getMorphClass (),
41
+ 'related_id ' => $ item ['id ' ],
42
+ 'related_type ' => $ item ['endpointType ' ],
43
+ 'browser_name ' => $ browserName ,
44
+ 'position ' => $ position ,
45
+ ])
46
+ )
47
+ );
48
+
49
+ $ model ->setRelation ('relatedItems ' , $ relatedItems );
60
50
61
51
return parent ::hydrate ($ model , $ fields );
62
52
}
63
53
54
+ /** @param Block $model */
64
55
public function afterSave (TwillModelContract $ model , array $ fields ): void
65
56
{
66
- if (Schema::hasTable (config ('twill.related_table ' , 'twill_related ' ))) {
57
+ if (!empty ($ fields ['browsers ' ])) {
58
+ $ browserNames = collect ($ fields ['browsers ' ])->each (function ($ items , $ browserName ) use ($ model ) {
59
+ // This will create items or delete them if they are missing
60
+ $ model ->saveRelated ($ items , $ browserName );
61
+ })->keys ();
62
+
63
+ // Delete all the related items that were emptied
64
+ RelatedItem::query ()->whereMorphedTo ('subject ' , $ model )->whereNotIn ('browser_name ' , $ browserNames )->delete ();
65
+ } else {
67
66
$ model ->clearAllRelated ();
68
-
69
- if (isset ($ fields ['browsers ' ])) {
70
- Collection::make ($ fields ['browsers ' ])->each (function ($ items , $ browserName ) use ($ model ) {
71
- $ model ->saveRelated ($ items , $ browserName );
72
- });
73
- }
74
67
}
75
68
76
69
parent ::afterSave ($ model , $ fields );
@@ -82,9 +75,7 @@ public function afterDelete(TwillModelContract $object): void
82
75
$ object ->medias ()->detach ();
83
76
$ object ->files ()->detach ();
84
77
85
- if (Schema::hasTable (config ('twill.related_table ' , 'twill_related ' ))) {
86
- $ object ->clearAllRelated ();
87
- }
78
+ $ object ->clearAllRelated ();
88
79
}
89
80
90
81
public function buildFromCmsArray (array $ block , bool $ repeater = false ): array
0 commit comments