Skip to content

Commit 222e94e

Browse files
committed
Handle title, cover and content field replacement
1 parent 235c5a1 commit 222e94e

File tree

3 files changed

+126
-8
lines changed

3 files changed

+126
-8
lines changed

src/Console/ConfigureCmsCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public function handle(Client $ozuClient): int
5858
])
5959
],
6060
'form' => [
61+
'title' => $form->titleField()?->toArray(),
62+
'cover' => $form->coverField()?->toArray(),
63+
'content' => $form->contentField()?->toArray(),
6164
'fields' => $form
6265
->customFields()
6366
->map(fn (OzuField $field) => $field->toArray())

src/OzuCms/Form/OzuImageField.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class OzuImageField extends OzuField
66
{
7-
private string|array|null $fileFilter = null;
7+
private array $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
88
private int $maxFileSizeInMB = 5;
99
private bool $hasLegend = false;
1010

@@ -15,9 +15,9 @@ public function setHasLegend(bool $hasLegend = true): self
1515
return $this;
1616
}
1717

18-
public function setFileFiler(string|array|null $fileFilter): self
18+
public function setAllowedExtensions(array $extensions): self
1919
{
20-
$this->fileFilter = $fileFilter;
20+
$this->allowedExtensions = $extensions;
2121

2222
return $this;
2323
}
@@ -38,7 +38,7 @@ public function toArray(): array
3838
{
3939
return array_merge(parent::toArray(), [
4040
'hasLegend' => $this->hasLegend,
41-
'fileFilter' => $this->fileFilter,
41+
'allowedExtensions' => $this->allowedExtensions,
4242
'maxFileSize' => $this->maxFileSizeInMB,
4343
]);
4444
}

src/OzuCms/OzuCollectionFormConfig.php

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22

33
namespace Code16\OzuClient\OzuCms;
44

5+
use Closure;
56
use Code16\OzuClient\OzuCms\Form\OzuBelongsToField;
7+
use Code16\OzuClient\OzuCms\Form\OzuEditorField;
8+
use Code16\OzuClient\OzuCms\Form\OzuEditorToolbarEnum;
69
use Code16\OzuClient\OzuCms\Form\OzuField;
10+
use Code16\OzuClient\OzuCms\Form\OzuImageField;
11+
use Code16\OzuClient\OzuCms\Form\OzuTextField;
712
use Illuminate\Support\Collection;
813

914
class OzuCollectionFormConfig
1015
{
16+
protected ?OzuTextField $titleField;
17+
protected bool $hideTitleField = false;
18+
19+
protected ?OzuImageField $coverField;
20+
protected bool $hideCoverField = false;
21+
22+
protected ?OzuEditorField $contentField;
23+
protected bool $hideContentField = false;
24+
1125
protected array $fields = [];
1226
protected ?OzuBelongsToField $belongsToField = null;
1327

@@ -18,6 +32,57 @@ public function addCustomField(OzuField $field): self
1832
return $this;
1933
}
2034

35+
public function configureTitleField(Closure $callback): self
36+
{
37+
unset($this->titleField);
38+
39+
$titleField = $this->titleField();
40+
$this->titleField = tap($titleField, fn (&$titleField) => $callback($titleField));
41+
42+
return $this;
43+
}
44+
45+
public function hideTitleField(bool $hideTitleField = true): self
46+
{
47+
$this->hideTitleField = $hideTitleField;
48+
49+
return $this;
50+
}
51+
52+
public function configureCoverField(Closure $callback): self
53+
{
54+
unset($this->coverField);
55+
56+
$coverField = $this->coverField();
57+
$this->coverField = tap($coverField, fn (&$coverField) => $callback($coverField));
58+
59+
return $this;
60+
}
61+
62+
public function hideCoverField(bool $hideCoverField = true): self
63+
{
64+
$this->hideCoverField = $hideCoverField;
65+
66+
return $this;
67+
}
68+
69+
public function configureContentField(Closure $callback): self
70+
{
71+
unset($this->contentField);
72+
73+
$contentField = $this->contentField();
74+
$this->contentField = tap($contentField, fn (&$contentField) => $callback($contentField));
75+
76+
return $this;
77+
}
78+
79+
public function hideContentField(bool $hideContentField = true): self
80+
{
81+
$this->hideContentField = $hideContentField;
82+
83+
return $this;
84+
}
85+
2186
public function declareBelongsToField(string $ozuModelClass, string $label, bool $required = true): self
2287
{
2388
$ozuCollectionKey = app($ozuModelClass)->ozuCollectionKey();
@@ -32,9 +97,59 @@ public function declareBelongsToField(string $ozuModelClass, string $label, bool
3297

3398
public function customFields(): Collection
3499
{
35-
return collect([
36-
$this->belongsToField,
37-
...$this->fields
38-
])->whereNotNull();
100+
return collect(
101+
[
102+
$this->belongsToField,
103+
...$this->fields
104+
])
105+
->whereNotNull()
106+
->values();
107+
}
108+
109+
public function titleField(): ?OzuTextField
110+
{
111+
if ($this->hideTitleField) {
112+
return null;
113+
}
114+
115+
if (!isset($this->titleField)) {
116+
$this->titleField = OzuField::makeText('title');
117+
}
118+
119+
return $this->titleField;
120+
}
121+
122+
public function coverField(): ?OzuImageField
123+
{
124+
if ($this->hideCoverField) {
125+
return null;
126+
}
127+
128+
if (!isset($this->coverField)) {
129+
$this->coverField = OzuField::makeImage('cover')
130+
->setMaxFileSizeInMB(3);
131+
}
132+
133+
return $this->coverField;
134+
}
135+
136+
public function contentField(): ?OzuEditorField
137+
{
138+
if ($this->hideContentField) {
139+
return null;
140+
}
141+
142+
if (!isset($this->contentField)) {
143+
$this->contentField = OzuField::makeEditor('content')
144+
->setToolbar([
145+
OzuEditorToolbarEnum::Bold,
146+
OzuEditorToolbarEnum::Italic,
147+
OzuEditorToolbarEnum::Separator,
148+
OzuEditorToolbarEnum::BulletList,
149+
OzuEditorToolbarEnum::Link,
150+
]);
151+
}
152+
153+
return $this->contentField;
39154
}
40155
}

0 commit comments

Comments
 (0)