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