1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Thinktomorrow\Chief\Management; |
5
|
|
|
|
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Thinktomorrow\Chief\Pages\Page; |
9
|
|
|
use Thinktomorrow\Chief\Sets\SetReference; |
10
|
|
|
use Thinktomorrow\Chief\Modules\TextModule; |
11
|
|
|
use Thinktomorrow\Chief\Modules\PagetitleModule; |
12
|
|
|
use Thinktomorrow\Chief\Sets\StoredSetReference; |
13
|
|
|
use Thinktomorrow\Chief\PageBuilder\UpdateSections; |
14
|
|
|
use Thinktomorrow\Chief\Relations\AvailableChildren; |
15
|
|
|
use Thinktomorrow\Chief\Fields\Types\PagebuilderField; |
16
|
|
|
use Thinktomorrow\Chief\Tests\Fakes\NewsletterModuleFake; |
17
|
|
|
use Thinktomorrow\Chief\FlatReferences\FlatReferencePresenter; |
18
|
|
|
use Thinktomorrow\Chief\Concerns\Translatable\TranslatableContract; |
19
|
|
|
|
20
|
|
|
trait ManagesPagebuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The naming convention is important here because to hook into the saving |
24
|
|
|
* flow it needs to have the save<key>Field method naming. |
25
|
|
|
* |
26
|
|
|
* @param PagebuilderField $field |
27
|
|
|
* @param Request $request |
28
|
40 |
|
*/ |
29
|
|
|
public function saveSectionsField(PagebuilderField $field, Request $request) |
|
|
|
|
30
|
40 |
|
{ |
31
|
|
|
$sections = $request->get('sections', []); |
32
|
40 |
|
|
33
|
40 |
|
$modules = $sections['modules'] ?? []; |
34
|
40 |
|
$text = $sections['text'] ?? []; |
35
|
40 |
|
$sets = $sections['pagesets'] ?? []; |
36
|
|
|
$order = $sections['order'] ?? []; |
37
|
40 |
|
|
38
|
40 |
|
UpdateSections::forModel($this->model, $modules, $text, $sets, $order) |
39
|
40 |
|
->updateModules() |
40
|
40 |
|
->updateSets() |
41
|
40 |
|
->addTextModules() |
42
|
40 |
|
->updateTextModules() |
43
|
40 |
|
->sort(); |
44
|
|
|
} |
45
|
47 |
|
|
46
|
|
|
protected function createPagebuilderField(): PagebuilderField |
47
|
47 |
|
{ |
48
|
|
|
$model = $this->model; |
49
|
47 |
|
|
50
|
|
|
$availableChildren = AvailableChildren::forParent($model); |
51
|
|
|
|
52
|
1 |
|
$modules = $availableChildren->onlyModules()->reject(function ($module) use ($model) { |
53
|
47 |
|
return $module->page_id != null && $module->page_id != $model->id; |
54
|
|
|
}); |
55
|
47 |
|
|
56
|
47 |
|
$available_modules = FlatReferencePresenter::toGroupedSelectValues($modules)->toArray(); |
57
|
47 |
|
$available_pages = FlatReferencePresenter::toGroupedSelectValues($availableChildren->onlyPages())->toArray(); |
58
|
|
|
$available_sets = FlatReferencePresenter::toGroupedSelectValues($availableChildren->onlySets())->toArray(); |
59
|
|
|
|
60
|
|
|
// Current sections |
61
|
11 |
|
$sections = $model->children()->map(function ($section, $index) { |
62
|
9 |
|
if ($section instanceof TranslatableContract) { |
63
|
|
|
$section->injectTranslationForForm(); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return [ |
67
|
11 |
|
// Module reference is by id. |
68
|
|
|
'id' => $section->flatReference()->get(), |
69
|
|
|
|
70
|
|
|
// Key is a separate value to assign each individual module. |
71
|
11 |
|
// This is separate from id to avoid vue key binding conflicts. |
72
|
11 |
|
'key' => $section->flatReference()->get(), |
73
|
11 |
|
'type' => $this->guessPagebuilderSectionType($section), |
74
|
11 |
|
'slug' => $section->slug, |
75
|
11 |
|
'editUrl' => $this->findEditUrl($section), |
76
|
|
|
'sort' => $index, |
77
|
47 |
|
'trans' => $section->trans ?? [], |
78
|
|
|
]; |
79
|
47 |
|
})->toArray(); |
80
|
47 |
|
|
81
|
47 |
|
return PagebuilderField::make('sections') |
|
|
|
|
82
|
47 |
|
->translatable($this->model->availableLocales()) |
83
|
47 |
|
->sections($sections) |
84
|
47 |
|
->availablePages($available_pages) |
85
|
|
|
->availableModules($available_modules) |
86
|
|
|
->availableSets($available_sets); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $model |
91
|
|
|
* @return string|null |
92
|
|
|
*/ |
93
|
11 |
|
private function findEditUrl($model): ?string |
94
|
|
|
{ |
95
|
11 |
|
if (! $model instanceof ManagedModel) { |
96
|
6 |
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
5 |
|
try { |
100
|
1 |
|
return app(Managers::class)->findByModel($model)->route('edit'); |
101
|
|
|
} catch (NonRegisteredManager $e) { |
102
|
|
|
return null; |
103
|
4 |
|
} |
104
|
|
|
} |
105
|
2 |
|
|
106
|
|
|
/** |
107
|
|
|
* Section type is the grouping inside the pagebuilder (specifically the menu) |
108
|
2 |
|
* |
109
|
|
|
* @param $section |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
private function guessPagebuilderSectionType($section) |
113
|
2 |
|
{ |
114
|
|
|
if ($section instanceof TextModule) { |
115
|
|
|
return 'text'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($section instanceof PagetitleModule) { |
119
|
|
|
return 'pagetitle'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($section instanceof StoredSetReference || $section instanceof SetReference) { |
123
|
|
|
// TODO: clean this up and replace 'pageset' with 'set'; |
124
|
|
|
return 'pageset'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($section instanceof Page) { |
128
|
|
|
return 'page'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// We want all other types to be registered as modules |
132
|
|
|
return 'module'; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.