forked from TimOetting/kirby-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.php
113 lines (77 loc) · 2.84 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
require_once(panel()->roots()->fields()."/structure/controller.php");
use Kirby\Panel\Structure;;
use Kirby\Panel\Models\Page\Blueprint\Field;
class BuilderStructure extends Structure {
protected $fieldsets;
public function __construct($model) {
parent::__construct($model, "builder");
}
public function forFieldset($field, $fieldsetName) {
parent::forField($field);
$this->fieldsets = $this->config->get("fieldsets");
$fieldsetConfig = new Field($this->fieldsets[$fieldsetName], $this->model);
$this->config = $fieldsetConfig;
return $this;
}
}
class BuilderFieldController extends StructureFieldController {
public function add() {
$self = $this;
$model = $this->model();
$structure = $this->structure($model);
$modalsize = $this->field()->modalsize();
$fieldsetName = get("fieldset");
$fieldsetStructure = $this->fieldsetStructure($fieldsetName);
if(!$fieldsetStructure)
return $this->modal('error', array(
'text' => 'No fieldset with name "'. $fieldsetName . '" found.'
));
$form = $this->form('add', array($model, $fieldsetStructure), function($form) use($model, $structure, $self, $fieldsetName) {
$form->validate();
if(!$form->isValid()) {
return false;
}
$data = $form->serialize();
$data["_fieldset"] = $fieldsetName;
$structure->add($data);
$self->notify(':)');
$self->redirect($model);
});
$form->attr('action', panel()->urls()->current()."?fieldset=".get("fieldset"));
return $this->modal('add', compact('form', 'modalsize'));
}
public function update($entryId) {
$self = $this;
$model = $this->model();
$structure = $this->structure($model);
$entry = $structure->find($entryId);
$fieldsetStructure = $this->fieldsetStructure($entry->_fieldset);
if(!$fieldsetStructure)
return $this->modal('error', array(
'text' => 'No fieldset with name "'. $fieldsetName . '" found.'
));
if(!$entry) {
return $this->modal('error', array(
'text' => 'The item could not be found'
));
}
$modalsize = $this->field()->modalsize();
// $style = $this->field()->style();
$form = $this->form('update', array($model, $fieldsetStructure, $entry), function($form) use($model, $structure, $self, $entryId) {
// run the form validator
$form->validate();
if(!$form->isValid()) {
return false;
}
$structure->update($entryId, $form->serialize());
$self->notify(':)');
$self->redirect($model);
});
return $this->modal('update', compact('form', 'modalsize'));
}
private function fieldsetStructure($fieldsetName) {
$structure = new BuilderStructure($this->model());
return $structure->forFieldset($this->fieldname(), $fieldsetName);
}
}