-
Notifications
You must be signed in to change notification settings - Fork 0
/
FieldtypeRockGrid.module.php
425 lines (372 loc) · 13.8 KB
/
FieldtypeRockGrid.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php namespace ProcessWire;
/**
* Fieldtype to display agGrid inside the PW admin
*
* Bernhard Baumrock, baumrock.com
* MIT
*/
class FieldtypeRockGrid extends Fieldtype {
public $assets;
public $pageSizes = [10, 25, 50, 100];
public $translationStrings = [];
public static function getModuleInfo() {
return array(
'title' => 'RockGrid',
'author' => 'Bernhard Baumrock, baumrock.com',
'version' => '0.0.24',
'summary' => 'RockGrid Main Module',
'requires' => ['RockFinder'],
'installs' => ['InputfieldRockGrid'],
'icon' => 'table',
'autoload' => true,
);
}
/**
* module initialisation
*/
public function init() {
$this->assets = new FilenameArray();
}
/**
* attach pagerender hook when api is ready
*/
public function ready() {
// add scripts after page render
$this->wire->addHookAfter('Page::render', function($event) {
$event->return = str_replace('</head>', $this->renderAssets().'</head>', $event->return);
$event->return = str_replace(
'</body>',
"<script>document.dispatchEvent(new Event('DOMReady'))</script></body>",
$event->return
);
});
// show only json data on ajax requests
$this->wire->addHookBefore('Page::render', function($event) {
$this->modules->get('InputfieldRockGrid')->handleAJAX();
});
}
/**
* render scripts & styles html tag
* bug: the field loads ALL assets of all fields, even those not loaded on this page
*/
public function renderAssets() {
$out = "\n";
foreach($this->assets->unique() as $asset) {
// skip non-existing files
if(!is_file($asset)) continue;
$timestamp = filemtime($asset);
$asset = str_replace($this->config->paths->root,$this->config->urls->root, $asset);
$file = pathinfo($asset);
switch($file['extension']) {
case 'js':
$out .= "\n\t<script src='$asset?t=$timestamp'></script>";
break;
case 'css':
$out .= "\n\t<link rel='stylesheet' type='text/css' href='$asset?t=$timestamp'>";
break;
}
}
// add language translation strings
$out .= "\n\t<script>if(typeof RockGrid != 'undefined') {\n";
$out .= "\tRockGrid.str = {";
foreach($this->translationStrings as $name=>$str) $out .= "'$name':'". $this->sanitizer->entities($str) . "',";
$out .= "};\n";
$out .= "\tRockGrid.localeText = " . $this->getLocaleText();
$out .= "\n\t}</script>\n\n";
return $out;
}
/**
* add translation string
*/
public function x($name, $str) {
$this->translationStrings[$name] = $str;
}
/**
* aggrid locale object
*/
private function getLocaleText() {
return json_encode([
// for filter panel
'page' => __('Page'),
'more' => __('More'),
'to' => __('to'),
'of' => __('of'),
'next' => __('Next'),
'last' => __('Last'),
'first' => __('First'),
'previous' => __('Previous'),
'loadingOoo' => __('Loading...'),
// for set filter
'selectAll' => __('Select All'),
'searchOoo' => __('Search...'),
'blanks' => __('Blanc'),
// for number filter and text filter
'filterOoo' => __('Filter...'),
'applyFilter' => __('Apply Filter...'),
// for number filter
'equals' => __('Equals'),
'lessThan' => __('Less Than'),
'greaterThan' => __('Greater Than'),
// for text filter
'contains' => __('Contains'),
'startsWith' => __('Starts with'),
'endsWith' => __('Ends with'),
// the header of the default group column
'group' => __('Group'),
// tool panel
'columns' => __('Columns'),
'rowGroupColumns' => __('Pivot Cols'),
'rowGroupColumnsEmptyMessage' => __('drag cols to group'),
'valueColumns' => __('Value Cols'),
'pivotMode' => __('Pivot-Mode'),
'groups' => __('Groups'),
'values' => __('Values'),
'pivots' => __('Pivots'),
'valueColumnsEmptyMessage' => __('drag cols to aggregate'),
'pivotColumnsEmptyMessage' => __('drag here to pivot'),
'toolPanelButton' => __('tool panel'),
// other
'noRowsToShow' => __('no rows'),
// enterprise menu
'pinColumn' => __('Pin Column'),
'valueAggregation' => __('Value Aggregation'),
'autosizeThiscolumn' => __('Autosize This Column'),
'autosizeAllColumns' => __('Autosize All Columns'),
'groupBy' => __('Group by'),
'ungroupBy' => __('UnGroup by'),
'resetColumns' => __('Reset Those Cols'),
'expandAll' => __('Open-em-up'),
'collapseAll' => __('Close-em-up'),
'toolPanel' => __('Tool Panel'),
'export' => __('Export'),
'csvExport' => __('CSV Export'),
'excelExport' => __('Excel Export'),
// enterprise menu pinning
'pinLeft' => __('Pin <<'),
'pinRight' => __('Pin >>'),
'noPin' => __('DontPin <>'),
// enterprise menu aggregation and status bar
'sum' => __('Sum'),
'min' => __('Min'),
'max' => __('Max'),
'first' => __('First'),
'last' => __('Last'),
'none' => __('None'),
'count' => __('Count'),
'average' => __('Average'),
// standard menu
'copy' => __('Copy'),
'copyWithHeaders' => __('Copy With Headers'),
'ctrlC' => __('Ctrl + C'),
'paste' => __('Paste'),
'ctrlV' => __('Ctrl + V'),
]);
}
/**
* Return the associated Inputfield
*/
public function getInputfield(Page $page, Field $field) {
$inputField = $this->modules->get('InputfieldRockGrid');
$inputField->addClass('InputfieldIgnoreChanges');
$inputField->set('ajax', $field->ajax);
$inputField->set('debug', $field->debug);
$inputField->set('nocompression', $field->nocompression);
$inputField->set('pageSize', $field->pageSize);
$inputField->set('height', $field->height);
$inputField->set('assetsDir', $field->assetsDir);
return $inputField;
}
/**
* the formatted value of this field
* necessary to render the grid's markup on the frontend
*/
public function sanitizeValue(Page $page, Field $field, $value) {
if($this->process == 'ProcessPageView') {
$f = $this->getInputfield($page, $field);
$f->field = $field;
return $f->render();
}
}
###########################################################################################
/**
* config inputfields for this fieldtype
*/
public function ___getConfigInputfields(Field $field) {
$inputfields = parent::___getConfigInputfields($field);
$assetpath = '/site/assets/RockGrid/fields/';
// loading type ajax or inline
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'ajax');
$f->attr('checked', $field->ajax ? 'checked' : '');
$f->label = $this->_("Load data via AJAX");
$f->description = $this->_("If you have a large amount of data this option should be activated.");
$f->columnWidth = 33;
$inputfields->add($f);
// dont use compression for this field
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'nocompression');
$f->attr('checked', $field->nocompression ? 'checked' : '');
$f->label = $this->_("Disable compression for AJAX data");
$f->description = $this->_("If checked the field will not compress the data that is returned by the AJAX request");
$f->columnWidth = 33;
$f->showIf = 'ajax=1';
$inputfields->add($f);
// set field to debug mode
$f = $this->modules->get('InputfieldCheckbox');
$f->attr('name', 'debug');
$f->attr('checked', $field->debug ? 'checked' : '');
$f->label = $this->_("Debug mode ON");
$f->description = $this->_("If checked the field will show some debug information on the page edit screen.");
$f->columnWidth = 34;
$inputfields->add($f);
// field height
$f = $this->modules->get('InputfieldInteger');
$f->attr('name', 'height');
$f->value = $field->height === null ? 300 : $field->height;
$f->label = $this->_("Field height in px");
$f->notes = __("15 rows = 520") . "\n";
$f->notes .= __("Use 0 for auto-height grid");
$f->columnWidth = 33;
$inputfields->add($f);
// initial pagination pagesize
$f = $this->modules->get('InputfieldSelect');
$f->attr('name', 'pageSize');
$f->value = $field->pageSize ?: 0;
$f->label = $this->_("Initial Pagination PageSize");
$f->addOptions($this->pageSizes);
$f->columnWidth = 33;
$f->showIf = 'height=0';
$inputfields->add($f);
// custom assets dir
$f = $this->modules->get('InputfieldText');
$f->attr('name', 'assetsDir');
$f->label = $this->_("Custom Assets Directory");
$f->notes = "Relative to root, eg /site/modules/MyModule/grids";
$f->value = $field->assetsDir;
$f->columnWidth = 34;
$inputfields->add($f);
/** @var InputfieldMarkup $f */
$exampleFieldset = $this->wire('modules')->get('InputfieldFieldset');
$exampleFieldset->label = $this->_('Usage instructions');
$exampleFieldset->description = __('You need to define data and functionality for your field. Place the files in the folders like shown below.');
$exampleFieldset->icon = 'code';
$exampleFieldset->notes = "See also [https://gitlab.com/baumrock/FieldtypeRockGrid/wikis/quickstart](https://gitlab.com/baumrock/FieldtypeRockGrid/wikis/quickstart)";
$inputfields->add($exampleFieldset);
$f = $this->wire('modules')->get('InputfieldMarkup');
$f->label = $assetpath.$field->name.'.php';
$f->icon = 'file-o';
$f->value = $this->getCodeExample('01_basic-field-setup.php', $field);
$exampleFieldset->add($f);
$f = $this->wire('modules')->get('InputfieldMarkup');
$f->label = $assetpath.$field->name.'.js';
$f->icon = 'file-o';
$f->value = $this->getCodeExample('01_basic-field-setup.js', $field);
$f->notes = 'See all options here [https://www.ag-grid.com/javascript-grid-features/](https://www.ag-grid.com/javascript-grid-features/)';
$exampleFieldset->add($f);
return $inputfields;
}
/**
* get code example from file
*/
private function getCodeExample($file, $field) {
$out = '<pre>';
$out .= $this->sanitizer->entities(str_replace('#yourfieldname#', $field, file_get_contents(__DIR__ . '/examples/' . $file)));
$out .= '</pre>';
return $out;
}
###########################################################################################
/**
* Render a markup string of the value
*
* This is important for correctly rendering markup output in listers.
*
* @param Page $page Page that $value comes from
* @param Field $field Field that $value comes from
* @param mixed $value Optionally specify the $page->getFormatted(value), value must be a formatted value.
* If null or not specified (recommended), it will be retrieved automatically.
* @param string $property Optionally specify the property or index to render. If omitted, entire value is rendered.
* @return string|MarkupFieldtype Returns a string or object that can be output as a string, ready for output.
*
*/
public function ___markupValue(Page $page, Field $field, $value = null, $property = '') {
return $value;
}
/**
* The following functions are defined as replacements to keep this fieldtype out of the DB
*
*/
public function ___wakeupValue(Page $page, Field $field, $value) {
return $value;
}
public function ___sleepValue(Page $page, Field $field, $value) {
return $value;
}
public function getLoadQuery(Field $field, DatabaseQuerySelect $query) {
// prevent loading from DB
return $query;
}
public function ___loadPageField(Page $page, Field $field) {
// generate value at runtime rather than loading from DB
return null;
}
public function ___savePageField(Page $page, Field $field) {
// prevent saving of field
return true;
}
public function ___deletePageField(Page $page, Field $field) {
// deleting of page field not necessary
return true;
}
public function ___deleteField(Field $field) {
// deleting of field not necessary
return true;
}
public function getDatabaseSchema(Field $field) {
// no database schema necessary
return array();
}
public function ___createField(Field $field) {
// nothing necessary to create the field
return true;
}
public function getMatchQuery($query, $table, $subfield, $operator, $value) {
// we don't allow this field to be queried
throw new WireException("Field '{$query->field->name}' is runtime and not queryable");
}
public function ___getCompatibleFieldtypes(Field $field) {
// no fieldtypes are compatible
return new Fieldtypes();
}
public function getLoadQueryAutojoin(Field $field, DatabaseQuerySelect $query) {
// we don't allow this field to be autojoined
return null;
}
public function install() {
// track the installation of this module
// no data is sent to my analytics account, it's just a counter
$http = new WireHttp();
$http->post('http://www.google-analytics.com/collect', [
'v' => 1, // Version
'tid' => 'UA-76905506-1', // Tracking ID / Property ID.
'cid' => 555, // Anonymous Client ID.
't' => 'event', // hit type
'ec' => 'PWModules', // category
'ea' => 'install', // action
'el' => $this->className, // label
]);
}
public function uninstall() {
// track the uninstallation of this module
// no data is sent to my analytics account, it's just a counter
$http = new WireHttp();
$http->post('http://www.google-analytics.com/collect', [
'v' => 1, // Version
'tid' => 'UA-76905506-1', // Tracking ID / Property ID.
'cid' => 555, // Anonymous Client ID.
't' => 'event', // hit type
'ec' => 'PWModules', // category
'ea' => 'uninstall', // action
'el' => $this->className, // label
]);
}
}