|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Thinktomorrow\Chief\Templates; |
|
5
|
|
|
|
|
6
|
|
|
use Thinktomorrow\Chief\Modules\Module; |
|
7
|
|
|
use Thinktomorrow\Chief\Pages\Page; |
|
8
|
|
|
use Thinktomorrow\Chief\Relations\ActsAsParent; |
|
9
|
|
|
use Webmozart\Assert\Assert; |
|
10
|
|
|
|
|
11
|
|
|
class PageTemplateApplicator implements TemplateApplicator |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var ModuleTemplateApplicator */ |
|
14
|
|
|
private $moduleTemplateApplicator; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(ModuleTemplateApplicator $moduleTemplateApplicator) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->moduleTemplateApplicator = $moduleTemplateApplicator; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function handle($sourceModel, $targetModel): void |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var Page $sourceModel */ |
|
24
|
|
|
Assert::isInstanceOf($sourceModel, Page::class); |
|
25
|
|
|
|
|
26
|
|
|
if ($this->shouldApplyRelations($sourceModel, $targetModel)) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->applyRelations($sourceModel, $targetModel); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
// which parts to provide? |
|
33
|
|
|
// public function children(): Collection; |
|
34
|
|
|
// public function values(): array; |
|
35
|
|
|
// public function translations(): array; |
|
36
|
|
|
// public function fragments(): array; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function shouldApply($sourceModel, $targetModel): bool |
|
40
|
|
|
{ |
|
41
|
|
|
return ($sourceModel instanceof Page); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function shouldApplyRelations($sourceModel, $targetModel): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return ($sourceModel instanceof ActsAsParent && $targetModel instanceof ActsAsParent); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function applyRelations(ActsAsParent $sourceModel, ActsAsParent $targetModel): void |
|
50
|
|
|
{ |
|
51
|
|
|
foreach ($sourceModel->children() as $child) |
|
52
|
|
|
{ |
|
53
|
|
|
$duplicatedChild = null; |
|
54
|
|
|
|
|
55
|
|
|
if ($child instanceof Module && $child->isPageSpecific()) |
|
56
|
|
|
{ |
|
57
|
|
|
$duplicatedChild = $child::create([ |
|
58
|
|
|
'slug' => $targetModel->title ? $targetModel->title . '-' . $child->slug : $child->slug . '-copy', |
|
|
|
|
|
|
59
|
|
|
'page_id' => $targetModel->id, |
|
|
|
|
|
|
60
|
|
|
'page_morph_key' => $targetModel->getMorphClass() |
|
|
|
|
|
|
61
|
|
|
]); |
|
62
|
|
|
|
|
63
|
|
|
$this->moduleTemplateApplicator->handle($child, $duplicatedChild); |
|
64
|
|
|
} else |
|
65
|
|
|
{ |
|
66
|
|
|
$duplicatedChild = $child; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$targetModel->adoptChild($duplicatedChild, ['sort' => $child->relation->sort]); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|