-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputfieldMeasurement.module
More file actions
498 lines (455 loc) · 18.6 KB
/
InputfieldMeasurement.module
File metadata and controls
498 lines (455 loc) · 18.6 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?php namespace ProcessWire;
use MetaTunes\MeasurementClasses\{BaseMeasurement, Measurement, Dimension, MeasurementException};
/**
* ProcessWire Button Inputfield
* expected by FieldtypeMeasurement
*
* @author Mark Evens
* @license Licensed under GNU/GPL v3
* @link https://processwire.com/talk/topic/26241-fieldtypemeasurement/
*
* ProcessWire 2.x, 3.x
* Copyright (C) 2016 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
* See FieldtypeMeasurement for version history
*/
class InputfieldMeasurement extends Inputfield {
public static function getModuleInfo() {
return array(
'title' => __('Inputfield Measurement', __FILE__),
'summary' => __('Edit a measurement in specified units.', __FILE__),
'version' => '0.0.19',
// versions tied to FieldtypeMeasurement.module - see that file for version history
'author' => 'Mark Evens',
'requires' => array("FieldtypeMeasurement"),
'icon' => 'balance-scale',
);
}
protected $field;
/**
* Construct the Inputfield, setting defaults for all properties
*
*/
public function __construct() {
parent::__construct();
require_once(dirname(__FILE__) . '/Measurement.php');
}
/**
* Set the current Field
*
* @param Field $field
*
*/
public function setField(Field $field) {
$this->field = $field;
}
/**
* Page object that houses this field.
*/
protected $page;
public function setPage(Page $page)
{
$this->page = $page;
}
/**
* Per the Module interface, init() is called when the system is ready for API usage
*
*/
public function init() {
$this->wire()->config->scripts->append("https://unpkg.com/htmx.org@1.7.0");
// $this->wire()->config->scripts->append("https://unpkg.com/hyperscript.org@0.9.5"); // not used - jQuery works better
parent::init();
}
/**
* Return the completed output of this Inputfield, ready for insertion in an XHTML form
*
* @return string
*
* @throws WirePermissionException
*/
public function ___render() {
########### Get basic details ########################
//bd($this, 'THIS at start of render');
$name = $this->attr('name');
// bd($name, 'inputfield name');
$field = $this->field;
$page = $this->page;
//todo somehow if($page->$field->collapsed == 7) return $page->$field->render(); ?
// bd($this->attr('value'), __METHOD__ . ': $value at line #' . __LINE__);
$value = $this->attr('value');
if(!$value || !is_a($value, 'MetaTunes\MeasurementClasses\Measurement') || !$value->quantity){
$value = new Measurement($field->quantity);
$this->attr('value', $value);
}
// bd($value, __METHOD__ . ': $value at line #' . __LINE__);
$numberOfCols = ($field->hide_magnitude) ? 1 : 2;
$colWidth = 100 / $numberOfCols;
//bd($value, __METHOD__ . ': $value at line #' . __LINE__);
##################### HTMX AJAX LISTENER #############################
$input = $this->wire('input');
$ajax = $this->wire('config')->ajax;
//bd($ajax, __METHOD__ . ': $ajax at line #' . __LINE__);
// bd($input->get('fieldName'), __METHOD__ . ': $input->get(fieldName) at line #' . __LINE__);
if ($ajax && $input->get('fieldName') && str_starts_with($input->get('fieldName'), $this->attr('name'))) {
//bd($ajax, __METHOD__ . ': $ajax at line #' . __LINE__);
// bd($input->get('fieldName'), __METHOD__ . ': $input->get(fieldName) at line #' . __LINE__);
//bd($input->get('id'), __METHOD__ . ': $input->get(id) at line #' . __LINE__);
$name = $input->get('fieldName');
$target = $input->get('target');
$unitName = "{$name}_unit";
$oldUnitName = "{$name}_oldUnit";
$magnitudeName = "{$name}_magnitude";
$newUnit = $input->$unitName;
$oldUnit = ($input->$oldUnitName) ?: $value->unit;
$newMagnitude = $input->$magnitudeName;
//bd($oldUnit, 'old unit');
//bd($newUnit, 'new unit');
$out = $this->htmxConvert($field, $name, $target, $value, $oldUnit, $newUnit, $newMagnitude, $colWidth);
echo $out;
die();
} else {
// @debug
// bd($ajax, __METHOD__ . ': $ajax - NO AJAX AND/OR WRONG INPUTFIELD - at line #' . __LINE__);
}
######################################## END OF HTMX LISTENER ##############################
$inputfields = new InputfieldFieldset();
$notes = $value->units->get('notes');
$units = $value->units->definitions;
#################### Populate individual fields - quantity, magnitude & units ###################
// quantity
if (!$field->hide_quantity) {
$f = $this->modules->get("InputfieldMarkup");
$f->label = $value->get('quantity') . ': ' . $this->_("Notes");
$f->attr('name', "{$name}_Name");
$out = ''; //"<h3>{$value->get('quantity')}</h3>";
$f->collapsed = Inputfield::collapsedYes;
$f->columnWidth = 100;
$notes = ($notes) ? "<p>$notes</p>" : null;
//bd($units, 'units');
foreach($field->units as $unit) {
//bd($units[$unit], 'units[unit]');
$label = (isset($units[$unit]['alias']) && $units[$unit]['alias']) ? $units[$unit]['alias'] : $unit;
$extraNotes = isset($units[$unit]['notes']) ? "<h4>{$label}</h4><p>{$units[$unit]['notes']}</p>" : null;
$notes .= $extraNotes;
}
if ($notes) $out .= $notes;
$f->attr('value', $out);
$inputfields->add($f);
}
// magnitude
//bd($value->get('magnitude'), 'magnitude 3');
if (!$field->hide_magnitude) {
$f = $this->magnitudeField($field, $name, $value, $colWidth);
$inputfields->add($f);
$magnitudeHtml = $f->render();
}
// unit
$f = $this->modules->get("InputfieldSelect");
$f->label = $this->_("Unit");
$f->attr('name', "{$name}_unit");
$f->attr('value', $value->get('unit'));
//bd($value, 'value');
//bd($field->units, '$field->units');
$first = true;
foreach($field->units as $unit) {
if(!$value->get('unit') && $field->set_default) {
if($first) {
$f->attr('value', $unit);
$value->unit = $unit;
$first = false;
}
}
$shortLabel = (isset($units[$unit]) && isset($units[$unit]['shortLabel'])) ? $units[$unit]['shortLabel'] : null;
$alias = (isset($units[$unit]) && isset($units[$unit]['alias'])) ? $units[$unit]['alias'] : $unit;
if($shortLabel) $shortLabel = "($shortLabel)";
$f->addOption($unit, "$alias $shortLabel");
}
if($value->unit) $f->notes = $units[$value->get('unit')]->notes;
//bd($f->notes, 'notes');
$f->columnWidth = $colWidth;
######### Set the js variables to handle in-field conversion #############
if(!isset($field->enable_conversion)) $field->enable_conversion = 2; // Just in case, and covers the upgrade from old 'show_update' method
if(!$this->wire->config->js('InputfieldMeasurement')) {
$this->wire->config->js('InputfieldMeasurement', ['enable_conversion_Inputfield_' . $name . '_unit' => $field->enable_conversion, // This sets the var name to be enable_conversion_ suffixed by the inputfield id
'confirm' => $this->_("Convert measurement to selected units? (Null selection deletes magnitude")]);
} else {
$this->wire->config->js('InputfieldMeasurement', array_merge($this->wire->config->js('InputfieldMeasurement'), ['enable_conversion_Inputfield_' . $name . '_unit' => $field->enable_conversion]));
}
// Note that the browser will not get the js vars if the inputfield has been 'lazy-loaded' by AJAX, so we need to set it directly with a script
// (This is borrowed from InputfieldRepeater.module)
if($ajax) {
$jsValue = $this->wire->config->js('InputfieldMeasurement');
if(!empty($jsValue)) {
$f->appendMarkup .= "<script>ProcessWire.config['InputfieldMeasurement'] = " . json_encode($jsValue) . ';</script>';
}
}
########################
if(!$field->hide_magnitude && $field->enable_conversion != 0) { // 'Always', 'Ask' or unassigned conversion options
#### add HTMX to do measurement conversion ####
$targetHtml = (isset($magnitudeHtml)) ? $magnitudeHtml : '<input>'; // should always be set
$targetHtml = urlencode($targetHtml);
if($this->page->process == 'ProcessModule') {
bd([$this->page, $this->input->get->name], 'page, input');
$adminEditURL = $this->wire('config')->urls->admin . "module/edit";
bd([$name, urldecode($targetHtml)], 'name, targetHtml');
$adminEdit = "{$adminEditURL}?name={$this->input->get->name}&collapse_info=1&fieldName={$name}&target={$targetHtml}";
bd($adminEdit, __METHOD__ . ': $adminEdit at line #' . __LINE__);
} else {
$adminEditURL = $this->wire('config')->urls->admin . "page/edit/";
$adminEdit = "{$adminEditURL}?id={$this->page->id}&fieldName={$name}&target={$targetHtml}";
}
//bd($adminEdit, __METHOD__ . ': $adminEdit at line #' . __LINE__);
$target = "{$name}_magnitude";
$source = "{$name}_unit";
//bd($name, 'name used');
//bd($source, 'source used');
$f->attr([
'hx-get' => $adminEdit, // send get to InputfieldMeasurement - caught by HTMX AJAX LISTENER above
'hx-target' => "[name={$target}]", // our element to target with swap (magnitude)
'hx-indicator' => "[name={$target}]",
'hx-swap' => 'outerHTML', // we'll swap the outerHTML
'hx-trigger' => 'confirmed', // confirmed trigger is set in js after detecting change event
'hx-include' => "[name^={$name}]", // the new magnitude, unit and oldUnit
]);
########## end of hx- attributes ########################
}
//prevent selectize from hijacking the 'change' event (if SelectizeAll installed)
$f->addClass('no-selectize');
$inputfields->add($f);
// THIS FIELD MUST FOLLOW IMMEDIATELY AFTER THE SELECT FIELD BECAUSE THAT IT HOW JS LOCATES IT
$f = $this->modules->get('InputfieldText');
$f->attr('name', "{$name}_oldUnit");
$f->attr('value', '');
$f->label = __('Previously selected unit');
$f->collapsed = Inputfield::collapsedNo; // collapsedHidden prevents correct operation
$f->addClass('mmHidden', 'wrapClass');
$inputfields->add($f);
//remark
if ($field->show_remark) {
//bd($value->get('remark'), 'get remark');
$f = $this->modules->get('InputfieldTextArea');
$f->attr('name', "{$name}_remark");
$f->attr('value', $value->get('remark'));
$f->label = __('Remark');
$f->notes = __("Default rendering is as a tooltip");
$f->columnWidth = 100;
$f->collapsed = Inputfield::collapsedBlank;
$f->rows = 1;
$inputfields->add($f);
}
//bd($inputfields, 'inputfields');
//bd($this, 'THIS at end of render');
return $inputfields->render();
}
private function magnitudeField($field, $name, $value, $colWidth) {
$combi = false;
foreach($field->units as $unitOption) {
if(strpos($unitOption, '|')) {
$combi = true;
break;
}
}
if($combi) {
$f = $this->modules->get("InputfieldText");
$f->notes = __("Number or numbers separated by a 'pipe' | - e.g 2|3.4 (for combi units) ");
$magnitude = (is_array($value->get('magnitude'))) ? implode('|', $value->get('magnitude')) : $value->get('magnitude');
} else {
$f = $this->modules->get("InputfieldText");
if(wire()->modules->isInstalled('RockCalculator')) {
$f->attr("data-rockcalculator", 6); //6 digit precision
$f->notes = __("Numeric or math formula");
} else {
$f->notes = __("Numeric");
$f->precision = 6;
$f->attr('type', "number");
$f->attr('step', "any");
}
if($value->get('magnitude')) {
$magnitude = (is_array($value->get('magnitude'))) ? $value->get('magnitude')[0] : $value->get('magnitude');
} else {
$magnitude = null;
}
}
$f->label = $this->_("Magnitude");
$f->attr('name', "{$name}_magnitude");
$f->attr('value', $magnitude);
$f->addClass('InputfieldMeasurement_magnitude');
// $f->attr('placeholder', $value->get('unit'));
$f->columnWidth = $colWidth;
return $f;
}
/**
* Convert the old selected unit to the new one
* Called by HTMX get
*
* @param $target
* @param $value
* @param $newUnit
* @return array|string|string[]|null
* @throws MeasurementException
*/
private function htmxConvert($field, $name, $target, $value, $oldUnit, $newUnit, $newMagnitude, $colWidth) {
//bd([$target, $value, $oldUnit, $newUnit, $newMagnitude], __METHOD__ . ': params $target, $value, $oldUnit, $newUnit, $newMagnitude at line #' . __LINE__);
$newMagnitude = $this->wire()->sanitizer->magnitude($newMagnitude);
/* @var Measurement $value */
if($oldUnit) {
$value->set('unit', $oldUnit);
if($newUnit) {
$value->set('magnitude', $newMagnitude); // if the user has changed the magnitude as well as the unit
$value = $value->convertTo($newUnit);
//bd($newValue, 'converted');
} else {
$value->set('magnitude', '');
//bd('blanked');
}
} else {
$value->set('magnitude', $newMagnitude);
}
$target = $this->magnitudeField($field, $name, $value, $colWidth)->render();
return $target;
}
/**
* Value to render if the inputfield is not editable (e.g. locked)
*
* @return string
*/
public function renderValue(): string {
$value = $this->attr('value');
return ($value) ? $value->render() : '';
}
/**
* This Inputfield is defined empty, if the magnitude field is not populated
*
* Used by the 'required' check to see if the field is populated.
* Overwrites parent function Inputfield::isEmpty()
*
* @return bool
*
*/
public function isEmpty() {
$value = $this->attr('value');
if(!$value || !$value->magnitude) return true;
if(is_array($value->magnitude)) $value->magnitude = implode('|', $value->magnitude);
if(is_object($value) && !strlen("{$value->magnitude}")) return true;
return false;
}
/**
* Process the input from the given WireInputData (usually $input->get or $input->post), load and clean the value for use in this Inputfield.
*
* @param WireInputData $input
* @return $this
*
*/
public function ___processInput(WireInputData $input) {
try {
//bd($this, 'this at start of process input');
$name = $this->attr('name');
$value = $this->attr('value');
$input_names = array(
'magnitude' => "{$name}_magnitude",
'unit' => "{$name}_unit",
'quantity' => "{$name}_quantity",
'update' => "{$name}_update",
'remark' => "{$name}_remark"
);
if(!is_a($value, 'MetaTunes\MeasurementClasses\Measurement')) return $this;
/* @var $value Measurement */
$old = clone $value; // Don't want $old to change when $value is altered
//bd($value, 'value after start of process input');
// $value->targetSanitized = null;
$value->set('magnitude', [0]); // default to prevent errors if no input
// loop inputs and set them ( even if not changed - as magnitude format may be wrong)
foreach($input_names as $key => $name) {
// bd(['input' => $input->$name, 'value->key' => $value->get($key)], 'input name etc for ' . $key . '=>' . $name);
if(isset($input->$name)) { // deleted && $value->get($key) != $input->$name from condition
// bd($key, 'key');
if($key == 'magnitude') {
$input->$name = trim($input->$name);
if(!$input->$name) $input->$name = 0;
/*
* Set the regex for the required pattern for the magnitude field
*/
$unit = $input[$input_names['unit']];
// Now we know how many elements there should be to be separated by pipes
$unitArray = explode('|', $unit);
$unitCount = count($unitArray);
// Set the pattern...
$decimalPattern = "[+-]?([0-9]+\.?[0-9]*|\.[0-9]+)([eE][+-]?[0-9]+)?"; // matches scientific notation as well as decimal
$re = '/^';
for($i = 0; $i < $unitCount; $i++) {
if($i > 0) $re .= '\|';
$re .= $decimalPattern;
}
$re .= '$/';
// ... and check the pattern
$msg = $this->_("Change not made - magnitude not in correct format.");
if(!preg_match($re, $input->$name)) throw new MeasurementException($msg);
// If OK, carry on!
// bd([$key, $input->$name], '$input->$name');
// if(!is_numeric($input->$name)) {
// $magnitude = explode('|', $input->$name);
// $magnitude = array_filter($magnitude, 'is_numeric');
$magnitude = $this->sanitizer->magnitude($input->$name); //added by hook in FieldtypeMeasurement
$value->set($key, $magnitude);
// } else {
// $value->set($key, [$input->$name]);
// }
// bd($value->get('magnitude'), 'Magnitude0');
} else {
$value->set($key, $input->$name);
}
// $this->trackChange('value');
// bd($this->getChanges());
// bd($value->get('magnitude'), 'Magnitude1');
}
}
$new = $value;
// bd(['old' => $old, 'new' => $new], 'changes to field');
$oldUnit = $old->get('unit');
$newUnit = $new->get('unit');
// bd(['old unit' => $oldUnit, 'new unit' => $newUnit], 'change of unit?');
// bd($value->get('update'), 'update?');
if($oldUnit and $newUnit and $oldUnit != $newUnit and $value->get('update') == 1) {
// bd(['old unit' => $oldUnit, 'new unit' => $newUnit], 'change of unit');
// Check consistency of units and magnitude
// bd(['Mag' => count($value->get('magnitude')), 'Unit' => count((explode('|', $oldUnit)))], 'count compare old');
if(count($value->get('magnitude')) != count((explode('|', $oldUnit)))) {
// In theory, this should never happen as the input has already been checked
throw new MeasurementException(sprintf($this->_('Magnitude (%1$s) not consistent with original unit (%2$s)'), $value->get('magnitude'), $oldUnit));
}
$value->set('unit', $oldUnit);
$value->convertTo($newUnit);
} else {
// bd('No change');
if($value->get('unit')) {
// Check consistency of units and magnitude
// bd(['Mag' => count($value->get('magnitude')), 'Unit' => count((explode('|', $newUnit)))], 'count compare new');
if(count($value->get('magnitude')) != count((explode('|', $newUnit)))) {
// In theory, this should never happen as the input has already been checked
throw new MeasurementException(sprintf($this->_('Magnitude (%1$s) not consistent with chosen unit (%2$s)'), $value->get('magnitude'), $newUnit));
}
}
}
// NB Removed the refreshing of value per below as I do not think it is necessary any more (after fixing matrix item contexts)
//$value = new Measurement($this->field->quantity, $value->get('unit'), $value->get('magnitude'), $value->get('remark'));
//bd($value, 'value at end of process_input');
//$this->value = $value;
} catch(MeasurementException $e) {
$this->error($e->getMessage());
}
//bd($this, 'this at end of process_input');
return $this;
}
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
// No separate input settings at the moment - all in the details tab
return $inputfields;
}
public function ___getConfigAllowContext($field) {
$a = array(); // placeholder - no changes at present
return array_merge(parent::___getConfigAllowContext($field), $a);
}
}