-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessRockPageBuilder.module.php
More file actions
248 lines (217 loc) · 7.46 KB
/
ProcessRockPageBuilder.module.php
File metadata and controls
248 lines (217 loc) · 7.46 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace ProcessWire;
use RockPageBuilder\Block;
use RockPageBuilder\FieldData;
class ProcessRockPageBuilder extends Process
{
public static function getModuleInfo()
{
return [
'title' => 'RockPageBuilder Process Module',
'version' => '1.0.3',
'summary' => 'Admin Endpoints for RockPageBuilder Module',
'icon' => 'cubes',
'requires' => [
'RockPageBuilder',
],
'installs' => [],
// all users with the page-edit permission can execute this module
'permission' => 'page-edit',
'page' => [
'name' => 'rockpagebuilder',
'parent' => 2, // admin page
'title' => 'RockPageBuilder',
'status' => Page::statusHidden,
],
];
}
/**
* Add a rpb item
*/
public function executeAdd()
{
$this->headline('Add Item');
$this->browserTitle('Add Item');
$block = $this->wire->pages->get($this->wire->input->get('block', 'int'));
$above = $this->wire->input->get('above', 'int');
if (!$block instanceof Block) throw new WireException("Invalid block");
if (!$block->editable()) throw new WireException("No access");
if (!$block->getBlockPage()->editable()) throw new WireException("No access");
$tpl = $this->wire->input->get('tpl', 'templateName');
$field = $block->getBlockField();
$f = $field->getInputfield($block);
$out = '';
// set template if we only have one allowed block
$allowed = $this->rpb()->getAllowedBlocks($f, $block->getBlockPage());
if (count($allowed) === 1) $tpl = $allowed->first()->template;
// create block if tpl is set
if ($tpl) {
$fieldData = $block->getBlockData();
if ($above) $new = $fieldData->addBefore($tpl, $block);
else $new = $fieldData->addAfter($tpl, $block);
$fieldData->save();
$this->wire->session->redirect($new->editUrl());
}
// render buttons of rockpagebuilder field
$out .= $f->renderButtons($block->getBlockPage(), true);
// add loading spinner
$out .= "<div id='rpb-reloading'><span class='loader'></span></div>";
$this->wire->config->styles->add($this->wire->config->urls->siteModules . "RockPageBuilder/assets/frontend-loggedin.min.css");
$out .= "<script>$(document).on('click', '.rpb-button', function() {
$('#rpb-reloading').addClass('show');
});</script>";
return $out;
}
/**
* Add first block
*/
public function executeAddNew()
{
$this->headline('Add Item');
$this->browserTitle('Add Item');
$page = $this->wire->pages->get($this->wire->input->get('page', 'int'));
if (!$page->editable()) throw new WireException("No access");
$field = $this->wire->fields->get($this->wire->input->get('field', 'fieldName'));
if (!$field) throw new WireException("Invalid field");
// create block if tpl is set
if ($tpl = $this->wire->input->get('tpl', 'templateName')) {
/** @var FieldData $rpb */
$rpb = $page->getUnformatted($field->name);
$new = $rpb->add($tpl);
$rpb->save();
$this->wire->session->redirect($new->editUrl());
}
$f = $field->getInputfield($page);
return $f->renderButtons($page, true);
}
/**
* Clone given block
*/
public function executeClone()
{
$block = $this->wire->pages->get($this->wire->input->get('block', 'int'));
if (!$block instanceof Block) throw new WireException("Invalid Block");
if (!$block->editable()) throw new WireException("Block is not editable");
if ($block->isTrash()) throw new WireException("Cannot clone blocks that are trashed");
try {
$block->clone();
return $this->success();
} catch (\Throwable $th) {
return $this->json($th->getMessage());
}
}
/**
* Convert given block into a widget
*/
public function executeConvertToWidget()
{
$block = $this->wire->pages->get($this->wire->input->get('block', 'int'));
if (!$block instanceof Block) throw new WireException("Invalid Block");
if (!$block->editable()) throw new WireException("Block is not editable");
if ($block->isTrash()) throw new WireException("Cannot clone blocks that are trashed");
try {
$block->toWidget();
return $this->success();
} catch (\Throwable $th) {
return $this->json($th->getMessage());
}
}
/**
* Add a repeater page
*/
public function executeRepeaterAdd()
{
$block = $this->wire->pages->get($this->wire->input->get('block', 'int'));
$sort = $this->wire->input->get('sort', 'int');
if (!$block instanceof RepeaterPage) throw new WireException("Invalid block");
$forPage = $block->getForPage();
if (!$forPage->editable()) throw new WireException("No access");
// get necessary objects
$field = $block->getForField();
$items = $forPage->getUnformatted($field->name);
// create new page
$new = $items->getNew();
$new->save();
$this->wire->pages->sort($new, $sort);
$this->wire->session->redirect($new->editUrl());
}
/**
* Clone a repeater page
*/
public function executeRepeaterClone()
{
$block = $this->wire->pages->get($this->wire->input->get('block', 'int'));
if (!$block instanceof RepeaterPage) throw new WireException("Invalid Block");
if (!$block->editable()) throw new WireException("Block is not editable");
if ($block->isTrash()) throw new WireException("Cannot clone blocks that are trashed");
try {
$this->wire->pages->clone($block);
return $this->success();
} catch (\Throwable $th) {
return $this->json($th->getMessage());
}
}
/**
* Trash a repeater page
*/
public function executeRepeaterTrash()
{
$block = $this->wire->pages->get($this->wire->input->get('block', 'int'));
if (!$block instanceof RepeaterPage) throw new WireException("Invalid Block");
if (!$block->trashable()) throw new WireException("Unable to trash this block");
$block->trash();
return $this->json("success");
}
public function executeSettingsField()
{
$block = $this->wire->input->get('block', 'int');
/** @var Block $block */
$block = $this->wire->pages->get($block);
$file = $block->filePath();
$this->headline("Add a Settings-Field to block " . $block->className());
$this->browserTitle("Add a Settings-Field to block " . $block->className());
$stub = $this->wire->files->render(__DIR__ . "/stubs/SettingsField.txt");
$alert = '';
if (!$this->wire->modules->isInstalled("RockFields")) {
$alert = "<div class='uk-alert uk-alert-danger'>Module RockFields is not installed - please install it!</div>";
}
return "$alert Add the following method to the file <strong>$file</strong>:<br><pre>$stub</pre>";
}
/**
* Trash rpb block
*/
public function executeTrash()
{
$block = $this->wire->pages->get($this->wire->input->get('block', 'int'));
if (!$block instanceof Block) throw new WireException("Invalid Block");
if (!$block->trashable()) throw new WireException("Unable to trash this block");
$block->trash();
return $this->json("success");
}
public function rockpagebuilder(): RockPageBuilder
{
return $this->wire->modules->get('RockPageBuilder');
}
/**
* @return RockPageBuilder
*/
public function rpb()
{
return $this->wire->modules->get('RockPageBuilder');
}
/**
* Send json message
* @return string
*/
public function json($msg, $error = false)
{
return json_encode([
'error' => $error,
'message' => $msg,
]);
}
public function success()
{
return $this->json('success');
}
}