-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiframe_form.php
162 lines (153 loc) · 5.18 KB
/
iframe_form.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
<?php namespace ProcessWire;
/* @var array $current_attributes */
/* @var array $hanna_tags */
/* @var Page $edited_page */
/* @var string $inputfield_name */
/* @var Modules $modules */
// Current attribute values
$tag_name = $current_attributes['name'];
unset($current_attributes['name']);
// Default attributes for tag
$default_attributes = isset($hanna_tags[$tag_name]) ? $hanna_tags[$tag_name] : [];
// Get form from hookable method
$hcd = $modules->get('HannaCodeDialog');
$form = $hcd->buildForm($tag_name, $edited_page, $current_attributes, $default_attributes, $inputfield_name);
// Work out inputfield attributes from default attributes
$options = [];
$types = [];
$descriptions = [];
$notes = [];
foreach($default_attributes as $key => $value) {
if(strpos($key, '__') === false) continue;
if(substr($key, -9) === '__options') {
$options[substr($key, 0, -9)] = $value;
unset($default_attributes[$key]);
} elseif(substr($key, -6) === '__type') {
$types[substr($key, 0, -6)] = strtolower($value);
unset($default_attributes[$key]);
} elseif(substr($key, -13) === '__description') {
$descriptions[substr($key, 0, -13)] = $value;
unset($default_attributes[$key]);
} elseif(substr($key, -7) === '__notes') {
$notes[substr($key, 0, -7)] = $value;
unset($default_attributes[$key]);
}
}
// Add fields to form if they weren't already added in hook
foreach($default_attributes as $key => $value) {
// Get the field if it already exists in the form
$f = $form->getChildByName($key);
// Otherwise create field from default attributes
if(!$f) {
// Determine field type
if(isset($options[$key])) {
// Options-type field
$type = 'InputfieldSelect';
if(isset($types[$key])) {
switch($types[$key]) {
case 'radios':
$type = 'InputfieldRadios';
break;
case 'selectmultiple':
$type = 'InputfieldSelectMultiple';
break;
case 'asmselect':
$type = 'InputfieldAsmSelect';
break;
case 'checkboxes':
$type = 'InputfieldCheckboxes';
break;
}
}
$f = $modules->get($type);
// Add options
$select_options_string = $options[$key];
$data = $modules->getModuleConfigData('TextformatterHannaCode');
$open_tag = !empty($data['openTag']) ? $data['openTag'] : '[[';
if(strpos($select_options_string, $open_tag) !== false) {
$modules->TextformatterHannaCode->formatValue($edited_page, new Field(), $select_options_string);
}
$select_options = $modules->HannaCodeDialog->prepareOptions($select_options_string, $key, $tag_name, $edited_page);
if(array_values($select_options) === $select_options) {
// Regular array
foreach($select_options as $select_option) {
$f->addOption($select_option);
}
} else {
// Associative array
$f->addOptions($select_options);
}
} else {
// Non-options-type field
$type = 'InputfieldText';
if(isset($types[$key])) {
switch($types[$key]) {
case 'textarea':
$type = 'InputfieldTextarea';
break;
case 'checkbox':
$type = 'InputfieldCheckbox';
break;
case 'pagelistselect':
$type = 'InputfieldPageListSelect';
break;
case 'pagelistselectmultiple':
$type = 'InputfieldPageListSelectMultiple';
break;
case 'integer':
$type = 'InputfieldInteger';
break;
}
}
$f = $modules->get($type);
}
// Set other field attributes
$f->attr('id+name', $key);
if(isset($descriptions[$key])) $f->description = $descriptions[$key];
if(isset($notes[$key])) $f->notes = $notes[$key];
$f->label = ucfirst(str_replace('_', ' ', $key));
if($type === 'InputfieldInteger') $f->inputType = 'number';
// Add field to form
$form->append($f);
}
// Set value if not already set in hook
if(!$f->value) {
switch($f->className) {
case 'InputfieldCheckbox':
$checked = isset($current_attributes[$key]) ? (int) $current_attributes[$key] : (int) $value;
$f->attr('checked', $checked === 1 ? 'checked' : '');
break;
case 'InputfieldPageListSelectMultiple':
case 'InputfieldPageAutocomplete':
$f->value = isset($current_attributes[$key]) ? explode(',', $current_attributes[$key]) : explode(',', $value);
break;
default:
$f->value = isset($current_attributes[$key]) ? $current_attributes[$key] : $value;
}
}
}
?>
<style>
#notices, #NotificationMenu, #NotificationGhosts { display:none !important; } /* Don't show notices/notifications */
body.modal { margin-top:5px; }
body.modal #content { padding:0 5px; }
body.modal .pw-container { padding:0; }
body.modal .pw-content #tag-head { margin:0 0 15px 0; font-size:20px; }
.InputfieldForm { margin-bottom:0; }
.pw-content p.hanna-message { margin:-5px 0 0; }
body.AdminThemeUikit #main { padding:0 !important; margin:10px 0 20px !important; }
<?php if(!$hcd->show_tracy): ?>
#tracy-debug { display:none !important; }
<?php endif; ?>
</style>
<h2 id="tag-head"><?= ucfirst(str_replace('_', ' ', $tag_name)) ?></h2>
<?= $form->render() ?>
<?php
if(!$default_attributes) {
if(!isset($hanna_tags[$tag_name])) {
echo '<p class="hanna-message">'. $this->_('Invalid tag: no Hanna tag with this name exists.') . '</p>';
} else {
echo '<p class="hanna-message">'. $this->_('This tag has no editable attributes.') . '</p>';
}
}
?>