-
Notifications
You must be signed in to change notification settings - Fork 0
/
RockHeadless.module.php
214 lines (185 loc) · 5.57 KB
/
RockHeadless.module.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php namespace ProcessWire;
use TD;
/**
* @author Bernhard Baumrock, 23.02.2022
* @license Licensed under MIT
* @link https://www.baumrock.com
*/
class RockHeadless extends WireData implements Module {
const prefix = 'rockheadless_';
const endpoint = 'api';
private $callbacks;
public static function getModuleInfo() {
return [
'title' => 'RockHeadless',
'version' => '0.0.5',
'summary' => 'Provide easy json feeds for using PW as Headless CMS',
'autoload' => true,
'singular' => true,
'icon' => 'bolt',
];
}
public function init() {
$this->wire('rockheadless', $this);
$callbacks = $this->wire(new WireData()); /** @var WireData $callbacks */
$this->callbacks = $callbacks;
if($this->wire->user->isSuperuser()) {
$this->addHookAfter("ProcessPageEdit::buildFormContent", $this, "addGUI");
$this->addHookAfter("ProcessPageEdit::processInput", $this, "sleep");
}
}
public function ready() {
$url = self::endpoint;
$this->addHookAfter("/$url/{path}", $this, "serve");
}
/**
* Add GUI field to page editor
* @return void
*/
public function addGUI(HookEvent $event) {
$fields = $event->return;
/** @var Page $page */
$page = $event->object->getPage();
if(!$page->numChildren) return;
$data = $this->wakeup($page);
/** @var InputfieldCheckbox $check */
$check = $this->wire(new InputfieldCheckbox());
$check->name = self::prefix."expose";
$check->label = ' Expose this page to RockHeadless API';
$check->attr('checked', $data->expose ? 'checked' : '');
$check->attr('uk-toggle', ".rh-toggle");
/** @var InputfieldText $f */
$f = $this->wire(new InputfieldText());
$f->name = self::prefix."fields";
$f->value = implode(", ", $data->fields ?: []);
$f->addClass('rh-toggle');
$f->attr('uk-tooltip', 'Fields to expose (comma separated)');
$f->attr('hidden', !$data->expose);
$f->attr('placeholder', 'id, title');
/** @var InputfieldText $selector */
$selector = $this->wire(new InputfieldText());
$selector->name = self::prefix."selector";
$selector->value = $data->selector ?: "parent={page}";
$selector->addClass('rh-toggle');
$selector->attr('uk-tooltip', 'Selector used for finding pages');
$selector->attr('hidden', !$data->expose);
$url = $this->endpoint($page);
$fields->add([
'type' => 'markup',
'label' => 'RockHeadless',
'value' => $check->render()
.$f->render()
.$selector->render(),
'notes' => "API-Endpoint: [$url]($url)",
'collapsed' => !$data->expose,
'icon' => 'bolt',
]);
}
/**
* Get API endpoint for given page
* @return string
*/
public function endpoint($page) {
return $this->wire->pages->get(1)->httpUrl()
.self::endpoint
.$page->path;
}
/**
* Show json for given page
* @return string
*/
public function ___getData($page) {
$data = $this->wakeup($page);
$fields = (array)$data->fields; // prevent wrong ide lints
$selector = str_replace("{page}", $page, $data->selector);
$pages = $this->wire->pages->findRaw($selector, $fields);
$callbacks = $this->callbacks;
// early exit if no callbacks
if(!$this->hasHookedField($data)) return array_values($pages);
// loop page data and execute callbacks
foreach($pages as $i=>$p) {
foreach($p as $field=>$val) {
if(!$cb = $callbacks->$field) continue;
try {
$val = $cb->__invoke((object)$p, $page);
} catch (\Throwable $th) {
$this->log($th->getMessage());
$val = '';
}
$pages[$i][$field] = $val;
}
}
return array_values($pages);
}
/**
* Is one of the fields hooked?
* @return bool
*/
public function hasHookedField($data) {
foreach($this->callbacks as $field=>$callback) {
if(in_array($field, $data->fields)) return true;
}
return false;
}
/**
* Register a return callback
*/
public function return($field, $callback) {
$this->callbacks->set($field, $callback);
}
/**
* Serve JSON
* @return string
*/
public function serve(HookEvent $event) {
$path = $event->path;
$page = $this->wire->pages->findOne($path);
if(!$page->id) return;
$sudo = $this->wire->user->isSuperuser();
// get api settings
$api = $this->wakeup($page);
if(!$api->expose) return;
$data = $this->getData($page);
if(!$this->wire->config->ajax AND $sudo) {
try {
TD::dumpBig($data);
return true;
} catch (\Throwable $th) {
echo json_encode($data);
}
}
return json_encode($data);
}
/**
* Save data to DB
* @return void
*/
public function sleep(HookEvent $event) {
$form = $event->arguments(0);
if(!$form instanceof InputfieldForm) return;
$page = $event->object->getPage();
$input = $this->wire->input;
$data = [
'expose' => $input->post(self::prefix."expose", 'string'),
'fields' => explode(",", str_replace(" ","", $input->post(self::prefix."fields", 'string'))),
'selector' => $input->post(self::prefix."selector", 'text'),
];
$page->meta('rockheadless_data', $data);
}
/**
* Get rockheadless data from page metadata
* @return WireData
*/
public function wakeup($page) {
$data = $this->wire(new WireData()); /** @var WireData $data */
$data->setArray([
'expose' => false,
'fields' => [],
'selector' => '',
]);
$arr = $page->meta('rockheadless_data');
if(!is_array($arr)) $arr = [];
$data->setArray($arr);
return $data;
}
}