-
Notifications
You must be signed in to change notification settings - Fork 1
/
FieldtypeDbMigrateRuntime.module
286 lines (251 loc) · 8.57 KB
/
FieldtypeDbMigrateRuntime.module
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
<?php namespace ProcessWire;
/**
* FieldtypeDbMigrateRuntime
*
* Not a proper fieldtype - it only exists to provide a convenient way to add an inputfield
* to templates that will render some markup at runtime.
* Based on FieldtypeRuntimeOnly
*
*/
class InputfieldDbMigrateRuntime extends Inputfield {
/**
* @param Inputfield|null $parent
* @param $renderValueMode
* @return bool
* @throws WireException
*/
public function renderReady(Inputfield $parent = null, $renderValueMode = false) {
$field = $this->hasField;
$config = $this->wire('config');
$js_path = "{$this->getPath($field)}.js";
if(file_exists($js_path)) {
$modified = filemtime($js_path);
$config->scripts->add("{$this->getUrl($field)}.js?v=$modified");
}
$css_path = "{$this->getPath($field)}.css";
if(file_exists($css_path)) {
$modified = filemtime($css_path);
$config->styles->add("{$this->getUrl($field)}.css?v=$modified");
}
return parent::renderReady($parent, $renderValueMode);
}
/**
* @param Field $field
* @return string
* @throws WireException
*/
public function getPath(Field $field) {
return $this->wire('config')->paths->siteModules . basename(__DIR__) . '/RuntimeFields/' . $field->name;
}
/**
* @param Field $field
* @return string
* @throws WireException
*/
public function getUrl(Field $field) {
return $this->wire('config')->urls->siteModules . basename(__DIR__) . '/RuntimeFields/' . $field->name;
}
/**
* @return mixed|string
*/
public function ___render() {
return $this->value;
}
/**
* @param WireInputData $input
* @return $this|InputfieldDbMigrateRuntime
*/
public function ___processInput(WireInputData $input) {
return $this;
}
}
class FieldtypeDbMigrateRuntime extends Fieldtype {
/**
* @return string[]
*/
public static function getModuleInfo() {
return array(
'title' => 'DbMigrateRuntime',
'version' => '0.0.1',
'summary' => 'An inputfield, for use with ProcessDbMigrate, that renders at runtime, with no data saved to the database.',
'author' => 'Mark Evens, based on RuntimeOnly by Robin Sallis',
'icon' => 'code',
'requires' => 'ProcessWire>=3.0.0, PHP>=5.4.0, ProcessDbMigrate',
);
}
/**
* @param Page $page
* @param Field $field
* @param $value
* @param $property
* @return bool|mixed|MarkupFieldtype|string
*/
public function ___markupValue(Page $page, Field $field, $value = null, $property = '') {
if($field->no_lister_render) {
return '';
} else {
return $this->renderMarkup($page, $field);
}
}
/**
* @param Page $page
* @param Field $field
* @return bool|mixed|string
* @throws WireException
*/
protected function renderMarkup(Page $page, Field $field) {
$php_file = wire('config')->paths->siteModules . basename(__DIR__) . '/RuntimeFields/' . $field->name . ".php";
// $php_file = "{$this->wire('config')->paths->templates}RuntimeOnly/{$field->name}.php";
if(file_exists($php_file)) {
return $this->wire('files')->render($php_file, ['page' => $page, 'field' => $field, 'inputfield' => $this]);
} else {
return sprintf($this->_('No file found at <b>%s</b>'), wire('config')->urls->siteModules . basename(__DIR__) . '/RuntimeFields/' . $field->name . ".php");
}
}
/**
* @param Page $page
* @param Field $field
* @param $value
* @return bool|mixed|string
* @throws WireException
*/
public function ___wakeupValue(Page $page, Field $field, $value) {
return $this->renderMarkup($page, $field);
}
/**
* @param Page $page
* @param Field $field
* @return bool|mixed|object|Config|Fields|Fieldtypes|Inputfield|Modules|Notices|Page|Pages|Permissions|ProcessWire|Roles|Sanitizer|Session|Templates|User|Users|Wire|WireDatabasePDO|WireDateTime|WireFileTools|WireHooks|WireInput|WireMailTools|string|null
* @throws WireException
*/
public function getInputfield(Page $page, Field $field) {
$inputfield = $this->wire(new InputfieldDbMigrateRuntime());
$inputfield->class = $this->className();
return $inputfield;
}
/**
* @param Field $field
* @return InputfieldWrapper
* @throws WireException
*/
public function ___getConfigInputfields(Field $field) {
$inputfields = parent::___getConfigInputfields($field);
$config = $this->wire('config');
// Rendering info
/* @var InputfieldMarkup $f */
$f = $this->wire('modules')->InputfieldMarkup;
$f->name = 'rendering';
$f->label = $this->_('Inputfield rendering');
$value = '<p><strong>' . $this->_('This inputfield is solely for use as part of the ProcessDbMigrate module. Use for other purposes is not recommended. This fieldtype will be uninstalled if ProcessBbMigrate is uninstalled.') . '</strong></p>';
$value .= '<p>' . sprintf($this->_('Inputfield markup will be rendered from a file at %s. In addition to the standard ProcessWire variables this file receives:'),
wire('config')->urls->siteModules . basename(__DIR__) . '/RuntimeFields/' . $field->name . ".php") . '<br>';
$value .= '<strong>$page</strong> - ' . $this->_('The page being edited.') . '<br>';
$value .= '<strong>$field</strong> - ' . $this->_('the Field object.') . '<br>';
$value .= '<strong>$inputfield</strong> - ' . $this->_('the Inputfield object.') . '</p>';
$value .= '<p>' . sprintf($this->_('JS file %s will be added to admin if that file exists.'), wire('config')->urls->siteModules . basename(__DIR__) . '/RuntimeFields/' . $field->name . ".js") . '<br>';
$value .= sprintf($this->_('CSS file %s will be added to admin if that file exists.'), wire('config')->urls->siteModules . basename(__DIR__) . '/RuntimeFields/' . $field->name . ".js") . '<br>';
$value .= '</p>';
$f->value = $value;
$inputfields->add($f);
// // NOT USED //
// // Prevent field rendering in Lister
// /* @var InputfieldCheckbox $f */
// $f = $this->wire('modules')->InputfieldCheckbox;
// $f_name = 'no_lister_render';
// $f->name = $f_name;
// $f->label = $this->_('Lister rendering');
// $f->label2 = $this->_('Do not allow field to render inside a Lister column');
// $f->checked = $field->$f_name === 1 ? 'checked' : '';
// $inputfields->add($f);
return $inputfields;
}
/**
* @param Field $field
* @return InputfieldWrapper
* @throws WireException
*/
public function ___getConfigAdvancedInputfields(Field $field) {
$inputfields = parent::___getConfigAdvancedInputfields($field);
// Remove config fields that are not applicable
$inputfields->remove($inputfields->get('autojoin'));
$inputfields->remove($inputfields->get('global'));
return $inputfields;
}
/**
* @param Field $field
* @return bool|mixed|object|Config|Fields|Fieldtypes|Modules|Notices|Page|Pages|Permissions|ProcessWire|Roles|Sanitizer|Session|Templates|User|Users|Wire|WireDatabasePDO|WireDateTime|WireFileTools|WireHooks|WireInput|WireMailTools|string|null
* @throws WireException
*/
public function ___getCompatibleFieldtypes(Field $field) {
$fieldtypes = $this->wire(new Fieldtypes());
foreach($this->wire('fieldtypes') as $fieldtype) {
if($fieldtype instanceof FieldtypeDbMigrateRuntime) $fieldtypes->add($fieldtype);
}
return $fieldtypes;
}
/**
* @param $query
* @param $table
* @param $subfield
* @param $operator
* @param $value
* @return mixed
* @throws WireException
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value) {
// This field may not be queried
throw new WireException(sprintf($this->_("Field '%s' is runtime and not queryable"), $query->field->name));
}
/**
* @param Field $field
* @param DatabaseQuerySelect $query
* @return null
*/
public function getLoadQueryAutojoin(Field $field, DatabaseQuerySelect $query) {
return null;
}
/**
* @param Page $page
* @param Field $field
* @param $value
* @return int|object|WireArray|string
*/
public function sanitizeValue(Page $page, Field $field, $value) {
return $value;
}
/**
* @param Page $page
* @param Field $field
* @param $value
* @return int|object|WireArray|string
*/
public function ___sleepValue(Page $page, Field $field, $value) {
return $value;
}
public function savePageField(Page $page, Field $field) {
return true;
}
public function loadPageField(Page $page, Field $field) {
return '';
}
public function getLoadQuery(Field $field, DatabaseQuerySelect $query) {
return $query;
}
public function ___savePageField(Page $page, Field $field) {
return true;
}
public function ___deletePageField(Page $page, Field $field) {
return true;
}
public function ___createField(Field $field) {
return true;
}
public function ___deleteField(Field $field) {
return true;
}
public function getDatabaseSchema(Field $field) {
return [];
}
public function ___install() {
}
}