-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessRockGrid.module.php
More file actions
311 lines (278 loc) · 8.93 KB
/
ProcessRockGrid.module.php
File metadata and controls
311 lines (278 loc) · 8.93 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
namespace ProcessWire;
use Tracy\Dumper;
/**
* @author Bernhard Baumrock, 15.05.2024
* @license MIT as of 2026-01-21
* @link https://www.baumrock.com
*/
class ProcessRockGrid extends Process
{
const permission = 'rockgrid-dashboard';
public static function getModuleInfo()
{
return [
'title' => 'RockGrid Dashboard',
'version' => json_decode(file_get_contents(__DIR__ . "/package.json"))->version,
'summary' => 'GUI for managing RockGrids',
'icon' => 'table',
'requires' => ['RockGrid'],
'permission' => self::permission,
'permissions' => [self::permission => 'May run the RockGrid Dashboard Module'],
'page' => [
'name' => 'rockgrid',
'parent' => 'setup',
'title' => 'RockGrid'
],
];
}
private function addCreateForm($form): void
{
$fs = new InputfieldFieldset();
$fs->label = 'Add New Grid';
$fs->icon = 'plus';
// $fs->collapsed = Inputfield::collapsedYes;
$form->add($fs);
$error = false;
if ($this->wire->config->debug) {
if ($this->wire->input->post->creategrid) {
$grids = rockgrid()->gridsArray;
$gridname = $this->wire->input->post->gridname;
$parts = explode("\\", $gridname, 2);
if (count($parts) !== 2) {
$gridname = "RockGrid\\" . ucfirst($gridname);
$parts = explode("\\", $gridname, 2);
}
if (count($parts) !== 2) {
$error = "Invalid Gridname";
}
if (array_key_exists($gridname, $grids)) {
$error = "The grid $gridname does already exist - please choose another name!";
}
if (!$error) {
// create grid
$namespace = $parts[0];
$name = $parts[1];
if ($namespace === 'RockGrid') $dir = 'site/templates';
else $dir = "site/modules/$namespace";
$dir = $this->wire->config->paths->root . "$dir/RockGrid";
// create files from stub
$files = wire()->files;
$files->mkdir($dir, true);
// PHP
$content = $files->fileGetContents(__DIR__ . '/stubs/Grid.txt');
$replace = [
'{namespace}' => $namespace,
'{name}' => $name,
];
$files->filePutContents(
"$dir/$name.php",
str_replace(
array_keys($replace),
array_values($replace),
$content
)
);
// JS
$content = $files->fileGetContents(__DIR__ . '/stubs/Grid.js');
$files->filePutContents(
"$dir/$name.js",
str_replace(
'{gridHash}',
rockgrid()->safeName($gridname),
$content
)
);
rockgrid()->resetCache();
$this->wire->session->redirect(
'./debug/?grid=' . rockgrid()->safeName($gridname)
);
}
}
$fs->add([
'type' => 'text',
'label' => 'Name',
'name' => 'gridname',
'notes' => 'If you enter a name only (eg Bar) this will create the grid in the templates folder (eg /site/templates/RockGrid/Bar.php).
If you enter a name with Namespace (eg Foo\\Bar) the grid will be created in a modules folder (eg /site/modules/Foo/RockGrid/Bar.php).',
'placeholder' => 'YourGridName',
]);
if ($error) $fs->children()->last()->error($error);
$fs->add([
'type' => 'submit',
'name' => 'creategrid',
'value' => 'Create Grid',
'icon' => 'check',
'columnWidth' => 100,
'wrapClass' => 'uk-margin-remove',
]);
} else {
$fs->add([
'type' => 'markup',
'value' => 'Creating new grids is only allowed if $config->debug is true.',
]);
}
}
/**
* Dashboard
*/
public function execute()
{
if (wire()->input->get->refresh) {
rockgrid()->refresh();
$this->message('List of RockGrids has been refreshed.');
wire()->session->redirect('./');
}
$this->headline('RockGrid Dashboard');
$this->browserTitle('RockGrid Dashboard');
/** @var InputfieldForm $form */
$form = $this->wire->modules->get('InputfieldForm');
// playground button
$form->add([
'type' => 'markup',
'label' => 'Playground',
'value' => '<a href="./playground/" class="uk-button uk-button-primary">Open Playground</a>',
'icon' => 'code',
]);
// add new grid
$this->addCreateForm($form);
// grids list
$form->add([
'type' => 'markup',
'label' => 'Grids',
'value' => $this->renderGrids(),
'notes' => 'To remove a grid simply delete its files on the file system.',
]);
return $form->render();
}
/**
*
*/
public function executeDebug()
{
$name = rockgrid()->rawName($this->wire->input->get->grid);
rockgrid()->debug = true;
$this->headline("Debug $name");
$this->browserTitle("Debug $name");
/** @var InputfieldForm $form */
$form = $this->wire->modules->get('InputfieldForm');
$form->prependMarkup('
<style>
#pw-content-body .tracy-dump-location-container {
display:none;
}
</style>
');
// find grid
$grid = rockgrid()->getGrid($name);
if (!$grid) wire()->session->redirect("../");
$data = $grid->getData();
if ($data === false) {
$dump = 'No data available. Implement the getData() method in your grid class to provide data.';
} else {
ob_start();
try {
$dump = Dumper::dump($data);
} catch (\Throwable $th) {
if (is_array($data)) print_r($data);
else var_dump($data);
}
$dump = ob_get_clean();
}
$step = (int)$this->wire->input->step;
$collapsed = $step === 0 ? 0 : 1;
$form->add([
'type' => 'markup',
'label' => 'Step 1: Define data of the grid in PHP getData()',
'description' => 'Result of your grid\'s getData() method:',
'value' => $dump,
'notes' => $grid->phpFile
. "\n<a href=./?grid={$grid->name}&step=1>Keep step 1 open (for development with livereload).</a>",
'entityEncodeText' => false,
'collapsed' => $step === 1 ? 0 : $collapsed,
]);
$form->add([
'type' => 'RockGrid',
'name' => 'rockgrid-debug',
'label' => 'Step 2: Define grid setup in JS',
'grid' => $name,
'notes' => $grid->jsFile
. "\n<a href=./?grid={$grid->name}&step=2>Keep step 2 open (for development with livereload).</a>",
'entityEncodeText' => false,
'collapsed' => $step === 2 ? 0 : $collapsed,
]);
return $form->render();
}
public function executePlayground()
{
$this->headline('RockGrid Playground');
$this->browserTitle('RockGrid Playground');
/** @var InputfieldForm $form */
$form = $this->wire->modules->get('InputfieldForm');
// files
$form->add([
'type' => 'markup',
'label' => 'JS File',
'value' => '<p>/site/templates/RockGridPlayground.js</p>
<button class="uk-button">Create</button>',
'icon' => 'file-code-o',
'columnWidth' => 50,
]);
$form->add([
'type' => 'markup',
'label' => 'PHP File',
'value' => '/site/templates/RockGridPlayground.php',
'icon' => 'file-code-o',
'columnWidth' => 50,
]);
// grid
$form->add([
'type' => 'RockGrid',
'name' => 'rockgrid-playground',
'label' => 'Grid',
'grid' => 'RockGrid\Playground',
]);
return $form->render();
}
private function renderGrids(): string
{
$out = '<table class="uk-table uk-table-small uk-table-striped">';
$out .= '<tr><th>Name</th><th>Path</th></tr>';
foreach (rockgrid()->gridsArray as $name => $path) {
$hash = rockgrid()->safeName($name);
$url = rockgrid()->url($path);
$copy = '
<a
title="Copy to Clipboard"
uk-tooltip
onclick="copyToClipboard()"
>
<svg style="margin-left:5px" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"><path d="M8 10a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2h-8a2 2 0 0 1-2-2z"/><path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2"/></g></svg>
</a>';
$out .= "<tr><td><a href=./debug/?grid=$hash>$name</a>$copy</td><td>$url</td></tr>";
}
$out .= '</table>
<script>
function copyToClipboard() {
const el = document.createElement("textarea");
el.value = event.target.closest("td").innerText.trim();
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
UIkit.notification({
message: "Copied to clipboard",
status: "success",
pos: "top-center",
timeout: 3000
});
}
</script>
';
$out .= '<a class=ui-button href=./?refresh=1>
<i class="fa fa-refresh"></i>
Refresh
</a>';
return $out;
}
}