-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInputfieldPageTableNext.module
More file actions
323 lines (272 loc) · 10.4 KB
/
InputfieldPageTableNext.module
File metadata and controls
323 lines (272 loc) · 10.4 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
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* COPYRIGHT NOTICE
* Copyright (c) 2025 Neue Rituale GbR
* @author NR <code@neuerituale.com>
* @license MIT
*/
namespace ProcessWire;
/**
* @method string renderTable(array $columns)
* @method string renderValueWrapper(Page $context, Page $value)
* @method string buildFileNameForRenderValue(Page $section, bool $withExtension = false)
*/
class InputfieldPageTableNext extends InputfieldPageTable {
public static function getModuleInfo() {
return array(
'title' => __('PageTableNext (Input)'),
'summary' => __('Add Inputfield for PageTableNext'),
'version' => 205,
'requires' => ['PageTableNext'],
'installs' => ['PageTableNext'],
'permanent' => false,
);
}
public function init(): void {
parent::init();
$this->set('nameFormat', '{template.label|template.name}');
$this->addHookAfter('TemplateFile::fileFailed', $this, 'hookFileFailed');
}
/**
* Render Field
*
* @return string
* @throws \Exception
*/
public function ___render() : string {
$this->modules->get('JqueryUI')->use('vex');
$ptnTemplate = new TemplateFile($this->getTemplateFilePath('ptn'));
$ptnTemplate->set('value', $this->renderField());
$ptnTemplate->set('page', $this->hasPage);
$ptnTemplate->set('ptn', $this);
return $ptnTemplate->render();
}
/**
* Render the PageTable Inputfield
*
* @return string
* @throws WireException
* @throws WirePermissionException
*/
public function ___renderField() : string {
// make sure we've got enough info to generate a table
$errors = array();
if(!count($this->rowTemplates)) $errors[] = $this->_('Please configure this field with a template selection before using it.');
if(count($errors)) return "<div uk-alert class='uk-alert-danger'><p>" . implode('<br />', $errors) . "</p></div>";
// render the table
$out = $this->renderTable([]);
if($this->renderValueMode) return $out;
$editID = (int) $this->wire('input')->get('id');
if(!$editID && $this->wire('process') instanceof WirePageEditor) $editID = $this->wire('process')->getPage()->id;
$parentID = $this->parent_id ?: $editID;
// render the 'Add New' buttons for each template
$buttonsSets = [];
$respectedTags = !empty($this->hasField->respectedTags)
? explode(' ', $this->hasField->respectedTags)
: []
;
/** @var Template $template */
foreach($this->rowTemplates as $template) {
// skip templates without direct create permissions
// We don't check the inherited permissions
if(
!$template->allowNewPages() ||
($template->useRoles && !$this->user->hasPermission('page-create', $template))
) continue;
/** @var Template $template */
$button = $this->wire('modules')->get('InputfieldButton');
$button->icon = $template->icon ?: 'plus-circle';
$button->value = count($this->rowTemplates) == 1 ? $this->_x('Add New', 'button') : $template->getLabel();
$button->addClass('uk-margin-remove');
$url = $this->wire('config')->urls->admin . "page/add/?modal=1&template_id=$template->id&parent_id=$parentID&context=PageTable";
if($this->nameFormat) $url .= "&name_format=" . $this->wire('sanitizer')->entities($this->nameFormat);
$templateTags = explode(' ', $template->tags);
$intersections = array_values( array_intersect($respectedTags, $templateTags) );
$buttonsSets[count($intersections) ? $intersections[0] : $this->_('Other')][] = "<span style='display:block' class='InputfieldPageTableAdd' data-url='$url'>" . $button->render() . "</span>";
}
$out .= "<hr class='uk-margin-medium-top'><div class='InputfieldPageTableButtons uk-divider uk-margin-top uk-margin-remove-bottom uk-flex uk-flex-wrap uk-child-width-expand@m' style='gap: 20px'>";
foreach($buttonsSets as $title => $buttonSet) {
$out .= "<div class='InputfieldPageTableButtonsColumn uk-margin-small-top'>";
if(count($buttonsSets) > 1) $out .= "<h6 class='uk-margin-small-bottom uk-text-muted uk-text-italic'>$title</h6>";
$out .= '<small class="uk-flex uk-flex-wrap" style="gap: 5px">' . implode(' ', $buttonSet) . '</small>';
$out .= "</div>";
}
$out .= '</div>';
// essentials
// sort and deleted fields
if(!$this->wire('input')->get('InputfieldPageTableField')) {
$url = $this->config->urls->httpAdmin . "page/edit/?id={$editID}&InputfieldPageTableField={$this->name}";
$out = "<div class='InputfieldPageTableContainer' data-fieldname='{$this->attr('name')}' data-url='$url' data-noclose='$this->noclose'>$out</div>";
// input for sorting purposes
$value = $this->wire('sanitizer')->entities($this->attr('value'));
$name = $this->wire('sanitizer')->entities($this->attr('name'));
$out .= "<input type='hidden' name='$name' class='InputfieldPageTableSort' value='$value' />";
$out .= "<input type='hidden' name='{$name}__delete' class='InputfieldPageTableDelete' value='' />";
if($this->orphans && count($this->orphans)) {
$out .= "<p class='InputfieldPageTableOrphans'>";
$out .= "<span>" . $this->_('Children were found that may be added to this table. Check the box next to any you would like to add.') . "</span> ";
if(count($this->orphans) > 1) $out .= "<br /><a class='InputfieldPageTableOrphansAll' href='#'>" . $this->_('Select all') . "</a> ";
foreach($this->orphans as $item) {
$label = $item->title;
if(!strlen($label)) $label = $item->name;
$out .= "<label><input type='checkbox' name='{$this->name}__add_orphan[]' value='$item->id' /> <span class='detail'>$label</span></label>";
}
$out .= "</p>";
}
}
return $out;
}
/**
* Render Table
*
* @param array $columns
* @return string
* @throws \Exception
*/
public function ___renderTable(array $columns) : string {
$page = $this->hasPage;
$contentElements = [];
$fileName = $this->getTemplateFilePath('ptn-content');
// Add contentElement
/** @var Page $contentElement */
foreach($this->attr('value') as $contentElement) {
$contentElement->of(true);
$sectionOutTemplate = new TemplateFile($fileName);
$sectionOutTemplate->set('page', $page);
$sectionOutTemplate->set('value', $contentElement);
$sectionOutTemplate->set('ptn', $this);
$contentElements[] = $sectionOutTemplate;
}
$ptnTable = new TemplateFile($this->getTemplateFilePath('ptn-table'));
$ptnTable->set('page', $page);
$ptnTable->set('value', $contentElements);
$ptnTable->set('ptn', $this);
return $ptnTable->render();
}
/**
* Get template file location
*
* @param $name
* @return string
* @throws \Exception
*/
public function getTemplateFilePath($name): string {
if(empty($name)) throw new \Exception('Empty template file name');
return $this->getFileLocation(
$name . '.' . $this->config->templateExtension,
'paths'
);
}
/**
* Get file location
*
* @param $fileName
* @param string $from
* @return string
* @throws \Exception
*/
public function getFileLocation($fileName, string $from = 'urls') : string {
if(empty($fileName)) throw new \Exception('Empty file name');
$fileName = $this->sanitizer->filename($fileName);
return file_exists($this->config->paths->templates . 'PageTableNext/' . $fileName)
? $this->config->get($from)->templates . 'PageTableNext/' . $fileName
: $this->config->get($from)->FieldtypePageTableNext . 'templates/' . $fileName
;
}
/**
* Simple wrapper for Page::renderValue
*
* @param Page $context
* @param Page $value
* @return string
*/
public function ___renderValueWrapper(Page $context, Page $value): string {
$file = $this->buildFileNameForRenderValue($value, true);
if(file_exists($this->config->paths->fieldTemplates . $file)) {
$value->of(true);
return $context->renderField($this->hasField ?? '', $file, $value);
} else {
return '<small><i>'.sprintf($this->_('Template not found: "%s"'), $this->config->urls->fieldTemplates . $file).'</i></small>';
}
}
/**
* Build filename with field configured path segment.
*
* @param Page $section
* @param bool $withExtension
* @return string
*/
public function ___buildFileNameForRenderValue(Page $section, bool $withExtension = false): string {
$path = trim($this->sanitizer->path($this->hasField->pathToTemplates), '/');
$filename = $section->template->name;
if(!empty($path)) $filename = $path . '/' . $filename;
if($withExtension) $filename .= '.' . $this->config->templateExtension;
return $filename;
}
/**
* Build a exception message
* Inspired by Wire::___trackException()
*
* @param \Exception $e
* @return string
*/
public function buildExceptionMessage(\Exception $e): string {
return $e->getMessage()
. " (in "
. str_replace(wire()->config->paths->root, '/', $e->getFile())
. " line "
. $e->getLine()
. ")";
}
/**
* Prevents the entire rendering from being cancelled in the event of errors in ptn templates
*
* @param HookEvent $event
* @return void
*/
public function hookFileFailed(HookEvent $event): void {
/** @var TemplateFile $templateFile */
$templateFile = $event->object;
/** @var \Exception $e */
$e = $event->arguments(1);
if($templateFile->field instanceof PageTableField) {
$templateFile->error($this->buildExceptionMessage($e), Notice::login);
$templateFile->fileFinished();
$event->return = false;
}
}
/**
* Get the inputfield config fields
*
* @return InputfieldWrapper
*/
public function ___getConfigInputfields(): InputfieldWrapper {
$inputfields = parent::___getConfigInputfields();
// remove columns field from pageTable
$inputfields->remove('columns');
$inputfields->remove('noclose');
/** @var InputfieldTextTags */
$inputfields->add([
'type' => 'TextTags',
'label' => $this->_('Grouped add buttons'),
'name' => 'respectedTags',
'value' => $this->hasField->respectedTags,
'description' => $this->_('Add tags to the template to group them at the input field. Recommended if you use a lot of templates.'),
'notes' => $this->_('For example "text, teaser, others"'),
'allowUserTags' => false,
'tagsList' => $this->templates->getTags(),
'collapsed' => Inputfield::collapsedBlank
]);
/** @var InputfieldTextTags */
$inputfields->add([
'type' => 'Text',
'label' => $this->_('Path to content element templates'),
'name' => 'pathToTemplates',
'value' => $this->hasField->pathToTemplates,
'description' => $this->_('Path to the templates used for rendering in the backend (relative to templates/fields directory).'),
'notes' => $this->_('For example "controllers/"'),
'collapsed' => Inputfield::collapsedBlank
]);
return $inputfields;
}
}