-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockQuickDates.module.php
More file actions
109 lines (94 loc) · 2.7 KB
/
RockQuickDates.module.php
File metadata and controls
109 lines (94 loc) · 2.7 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
<?php
namespace ProcessWire;
use RockQuickDates\QuickDatesField;
function rockquickdates(): RockQuickDates
{
return wire()->modules->get('RockQuickDates');
}
/**
* @author Bernhard Baumrock, 06.01.2025
* @license MIT as of 2026-01-21
* @link https://www.baumrock.com
*/
class RockQuickDates extends WireData implements Module, ConfigurableModule
{
public $fields;
public function init()
{
wire()->classLoader->addNamespace('RockQuickDates', __DIR__);
// init some variables
$this->fields = new WireArray();
// hooks
wire()->addHookAfter('InputfieldDatetime::render', $this, 'renderButtons');
// devtools
$this->devTools();
}
/**
* Connect two datetime fields and calculate a duration
*/
public function addDuration(
string $durationField,
string $startField,
string $endField,
string $durationType = 'h', // h, m, s
): void {
$this->fields
->get($startField)
->setOption('connectedto', $endField)
->setOption('connectiontype', 'start')
->setOption('durationfield', $durationField)
->setOption('durationtype', $durationType);
$this->fields
->get($endField)
->setOption('connectedto', $startField)
->setOption('connectiontype', 'end')
->setOption('durationfield', $durationField)
->setOption('durationtype', $durationType);
}
public function addToField(string $fieldname, array $options = []): self
{
$data = new QuickDatesField();
$data->name = $fieldname;
$data->options->setArray($options);
$this->fields->add($data);
return $this;
}
private function devTools(): void
{
if (!wire()->config->debug) return;
if (!wire()->user->isSuperuser()) return;
if (!wire()->modules->isInstalled('RockMigrations')) return;
rockmigrations()->minify(
__DIR__ . '/src/QuickDates.less',
__DIR__ . '/dst/QuickDates.min.css'
);
rockmigrations()->minify(
__DIR__ . '/src/QuickDates.js',
__DIR__ . '/dst/QuickDates.min.js'
);
}
/**
* Config inputfields
* @param InputfieldWrapper $inputfields
*/
public function getModuleConfigInputfields($inputfields)
{
return $inputfields;
}
public function ready(): void
{
$url = wire()->config->urls($this);
wire()->config->styles->add($url . 'dst/QuickDates.min.css');
wire()->config->scripts->add($url . 'dst/QuickDates.min.js');
}
protected function renderButtons(HookEvent $event): void
{
$f = $event->object;
if (!$f instanceof InputfieldDatetime) return;
$field = $f->hasField;
if (!$field) return;
$quickField = $this->fields->get($field->name);
if (!$quickField) return;
$event->return .= $quickField->getButtonMarkup();
}
}