-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessRockFinder2.module.php
343 lines (298 loc) · 10 KB
/
ProcessRockFinder2.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php namespace ProcessWire;
/**
* RockFinder2
*
* @author Bernhard Baumrock, 08.08.2019
* @license Licensed under MIT
*/
class ProcessRockFinder2 extends Process {
public static function getModuleInfo() {
return [
'title' => 'RockFinder2 Process Module',
'summary' => 'RockFinder2 Backend',
'version' => '0.0.1',
'author' => 'Bernhard Baumrock',
'icon' => 'search',
'requires' => ['RockFinder2'],
'page' => [
'name' => 'rockfinder2',
'title' => 'RockFinder2',
'parent' => 'setup',
],
'nav' => [
[
'url' => 'console/',
'label' => 'Tracy Console',
'icon' => 'bug',
],
]
];
}
/**
* @var RockFinder2
*/
public $rf;
/**
* Init the module
*/
public function init() {
parent::init();
$this->rf = $this->wire->RockFinder2;
}
/**
* RockFinder2 Setup Page
*/
public function execute() {
$this->headline('Rockfinder2 Setup Page');
$this->browserTitle('Rockfinder2 Setup Page');
/** @var InputfieldForm $form */
$form = $this->modules->get('InputfieldForm');
// load vex
$this->wire('modules')->get('JqueryUI')->use('vex');
// actions
if($this->input->post('newFinder', 'string')) $this->createNewFinder();
if($this->input->get('del', 'string')) $this->deleteFinder();
// available finders
$out = '';
$out .= '<table class="uk-table uk-table-divider uk-table-striped">';
$out .= '<thead><tr><th>Name</th><th class="uk-width-expand">Description</th><th class="uk-width-auto">Delete</th></tr></thead>';
$out .= '<tbody>';
$path = $this->getPath();
$files = $this->files->find($path, [
'extensions' => ['php'],
'excludeDirNames' => ['bak'],
]);
foreach($files as $file) {
$info = (object)pathinfo($file);
$desc = '';
$line2 = file($file)[1];
if(strpos($line2, '// ') === 0) $desc = str_replace('// ', '', $line2);
$del = "<td class='uk-text-center'><a href='./?del={$info->filename}' data-name='{$info->filename}' class='delFinder'><i class='fa fa-trash'></i></a></td>";
$out .= "<tr><td class='uk-text-nowrap'><a href='./sandbox/?name={$info->filename}'>{$info->filename}</a></td><td>$desc</td>$del</tr>";
}
$out .= '</tbody>';
$out .= '</table>';
$b = $this->modules->get('InputfieldButton');
$b->href = './sandbox';
$b->value = 'Open new sandbox';
$b->icon = 'file-o';
$desc = $b->render();
$form->add([
'type' => 'markup',
'label' => 'Available Finders',
'description' => $desc,
'entityEncodeText' => false,
'value' => $out,
]);
// actions
$out = '';
$f = $this->modules->get('InputfieldText');
$f->name = 'newFinder';
$f->attr('placeholder', 'Finder name');
$f->attr('style', 'width: 250px;');
$d = $this->modules->get('InputfieldText');
$d->name = 'newFinderDesc';
$d->attr('placeholder', 'Finder description');
$d->attr('style', 'width: 350px;');
$b = $this->modules->get('InputfieldSubmit');
$b->name = 'submit_newFinder';
$b->icon = 'plus';
$b->value = 'Create new Finder';
$form->add([
'type' => 'markup',
'label' => 'Actions',
'value' => $f->render() . $d->render() . $b->render(),
]);
return $form->render();
}
/**
* Process Module to use tracy console for debugging
*/
public function executeConsole() {
$nl = "\n";
$this->config->scripts->add("https://unpkg.com/tabulator-tables@4.6.3/dist/js/tabulator.min.js");
$this->config->styles->add("https://unpkg.com/tabulator-tables@4.6.3/dist/css/tabulator.min.css");
return '<strong>Open the tracy console and try this dump:</strong>
<pre><code>$f = new RockFinder2();'.$nl
.'$f->find("template=admin");'.$nl
.'$f->addColumns(["title", "created", "status"]);'.$nl
.'$f->dump();</code></pre>
<style>#tracy-debug-panel-ConsolePanel {
left: 0 !important;
top: 0 !important;
width: 100vw !important;
height: 100vh !important;
}</style>
<script>
// open the console automatically
localStorage.setItem("tracy-debug-panel-ConsolePanel", "{}");
</script>';
}
/**
* Create new finder
*/
public function createNewFinder($name = null) {
if(!$name) $name = $this->input->post('newFinder', 'string');
if(!$name) throw new WireException("No valid name set for finder");
// check if finder already exists
if($this->rf->findByName($name)) return $this->error("Finder $name already exists");
// is directory writeable?
$path = $this->getPath();
if(!is_writable($path)) return $this->error("Directory $path is not writeable");
// copy demo finder to path
$content = file(__DIR__ . '/includes/demo.php');
$desc = $this->input->post('newFinderDesc', 'string');
if($desc) $content[1] = "// $desc\n";
file_put_contents($path . $name . '.php', $content);
// redirect to this finder
$this->session->redirect("./sandbox/?name=$name");
}
/**
* Delete a finder
*/
public function deleteFinder($name = null) {
if(!$name) $name = $this->input->get('del', 'string');
if(!$name) throw new WireException("No valid name set");
$file = $this->rf->getFile($name);
if($file) $this->files->unlink($file, $this->getPath());
$this->session->redirect('./');
}
/**
* Get path of assets files
* @return string
*/
public function getPath() {
return $this->config->paths->assets . "RockFinder2/";
}
/**
* übersicht über alle leistungen
*/
public function ___executeSandbox() {
$this->handleSaveAction();
$name = $this->input->get('name', 'string');
$headline = 'RF2 Sandbox';
if($name) $headline .= " - $name";
$this->headline($headline);
$this->wire('processBrowserTitle', $headline);
$form = $this->modules->get('InputfieldForm');
$form->action = './';
$form->id = 'sandboxform';
$out = '';
$out .= '<p>';
$out .= '<a href="'.$this->page->url.'">< Back to overview page</a>';
if($name) {
$apiUrl = $this->rf->url . '?name=' . $name . '&type=debug';
$out .= " | API Endpoint URL: <a href='$apiUrl'>$apiUrl</a></p>";
}
// code field
$f = $this->modules->get('InputfieldTextarea');
if($ace = $this->modules->get('InputfieldAceExtended')) {
$ace->rows = 10;
$ace->theme = 'monokai';
$ace->mode = 'php';
$ace->setAdvancedOptions(array(
'highlightActiveLine' => false,
'showLineNumbers' => false,
'showGutter' => false,
'tabSize' => 2,
'printMarginColumn' => false,
));
$f = $ace;
}
$f->notes = "Execute on CTRL+ENTER or ALT+ENTER, save on CTRL+S\n"
."Backups are stored in " . $this->rf->bak;
if(!$ace) $f->notes .= "\nYou can install 'InputfieldAceExtended' for better code editing";
$f->description = "The code must return a RockFinder2 instance!";
// get code
$code = file_get_contents(__DIR__ . '/includes/demo.php');
if($name) {
$file = $this->rf->getFiles($name);
if(!$file) throw new WireException("Finder for $name not found");
$code = file_get_contents($file);
// add link to file
$f->entityEncodeText = false;
$link = $this->getIdeLink($file);
$f->description .= " <a href='$link'>Open file in IDE</a>";
}
$f->name = 'code';
$f->label = 'Code to execute';
$f->icon = 'code';
$f->wrapAttr('data-name', $name);
$f->value = $code;
$form->add($f);
// add debug field
$form->add([
'type' => 'markup',
'name' => 'debug',
'label' => 'Debug Info',
'icon' => 'bug',
'collapsed' => Inputfield::collapsedYes,
'value' => '<div id="debuginfo" style="display: none;"></div>',
'notes' => "An instance of this finder is available in the console as 'finder'",
]);
// add tabulator field
if($this->modules->isInstalled('RockTabulator')) {
$form->add([
'type' => 'RockTabulator',
'name' => 'rockfinder2_sandbox',
'label' => 'RockTabulator',
'icon' => 'table',
'notes' => "An instance of this tabulator is available in the console as 'grid'",
'initMsg' => '',
]);
}
else {
$form->add([
'type' => 'markup',
'name' => 'tabulator',
'label' => 'RockTabulator',
'icon' => 'table',
'value' => 'Install RockTabulator to see data here',
]);
}
$out .= $form->render();
return $out;
}
/**
* Handle sandbox save actions
*/
public function handleSaveAction() {
if(!$this->input->post('action', 'string') == 'save') return;
$name = $this->input->get('name', 'string');
try {
// check access
if(!$this->user->isSuperuser()) throw new WireException("No access!");
if(!$name) throw new WireException("Saving disabled in sandbox");
// get file
$file = $this->rf->getFiles($name);
$code = $this->input->post('code', 'string');
if(!is_writable($file)) throw new WireException("File not writable!");
// write old code to tmp history
$path = $this->rf->bak;
$old = file_get_contents($file);
file_put_contents($path.$name.'-'.date('Ymd-His').".php", $old);
// do cleanup
$this->rf->getBackupFiles($name);
// write code to file
file_put_contents($file, $code);
die("Changes saved!");
} catch (\Throwable $th) {
die($th->getMessage());
}
}
/**
* Get ide link from file and line
* @param string $file
* @param int $line
* @return string
*/
public function getIdeLink($file, $line = 0) {
if($this->modules->isInstalled('TracyDebugger')) {
$tracy = $this->modules->get('TracyDebugger');
$link = str_replace('%file', $file, $tracy->editor);
$link = str_replace('%line', $line, $link);
return $link;
}
return false;
}
}