-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInputfieldRecurringDates.module
196 lines (165 loc) · 5.84 KB
/
InputfieldRecurringDates.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
<?php namespace ProcessWire;
use RRule\RRule;
/**
* FieldtypeEvents: InputfieldEvent
*
* Collects input for FieldtypeEvents fields. This accompanies the FieldtypeEvents as a demonstration
* of creating a multi-value Fieldtype and Inputfield.
*
*/
class InputfieldRecurringDates extends Inputfield
{
public static function getModuleInfo()
{
return array(
'title' => 'Recurring Dates',
'version' => 001,
'summary' => 'Field that lets you define a recurring date rule.',
'icon' => 'calendar-o',
'requires' => 'AlpineJS'
);
}
public function __construct()
{
parent::__construct();
}
public function init()
{
// Load Alpine.js in <header>
$this->setAttribute('pageSize', 10);
$this->set('startDateInput', 'date');
$this->set('hardLimit', 5000);
$this->modules->AlpineJS;
}
public function ___render()
{
/** @var RecurringDate $recurring_dates */
$recurring_dates = $this->value;
$this->setAttribute('class', $this->getAttribute('class') . ' main-input uk-width-1-1');
$occurrences = $recurring_dates->occurrences;
if($recurring_dates->rrule){
$rrule_array = $recurring_dates->rrule->getRule();
if ($this->startDateInput == "datetime") {
$rrule_array['DTSTART'] = (new \DateTime($rrule_array['DTSTART']))->format('Y-m-d H:i:s');
} else {
$rrule_array['DTSTART'] = (new \DateTime($rrule_array['DTSTART']))->format('Y-m-d');
}
if ($recurring_dates->rrule) {
$rule_json = json_encode($rrule_array);
}
}else{
$rule_json = '';
}
$this->setAttribute('class', 'uk-input main-input');
if ($rule_json) {
$this->setAttribute('value', [$rule_json]);
}
$this->setAttribute('x-ref', 'main-input');
$this->setAttribute('x-model', '_rrule');
$this->setAttribute('data-rrule', $rule_json);
$this->setAttribute('type', 'hidden');
$this->setAttribute('data-settings', $recurring_dates->settings);
$out = "";
$filePath = "{$this->config->paths->siteModules}FieldtypeRecurringDates/partials/AlpineComponent.php";
$alpineComponent = wireRenderFile($filePath, [
'fieldtype' => $this->hasFieldtype,
'inputfield' => $this,
'occurrences' => $occurrences,
'inputfieldValue' => $recurring_dates
]);
$out .= $alpineComponent;
return $out;
}
public function ___renderValue()
{
$fieldtype = $this->hasFieldtype;
return $fieldtype->markupValue($this->hasPage, $this->hasField, $this->value);
}
public function __getEventsUrl()
{
}
public function ___processInput(WireInputData $input)
{
$name = $this->attr('name');
$recurring_date_obj = new RecurringDate();
$value = $input[$name];
$settings = $input[$name . "_settings"];
if ($value === "") return $recurring_date_obj;
$recurring_date_obj->settings = $settings;
$settings_obj = json_decode($settings, true);
$value = json_decode($value, true);
if (!$value['DTSTART']) {
return $recurring_date_obj;
}
if ($settings_obj['limit_mode'] == "count") {
unset($value['UNTIL']);
}
if ($settings_obj['limit_mode'] == "until") {
unset($value['COUNT']);
}
// New RRule from input
$rrule = new RRule($value);
$recurring_date_obj->rrule = $rrule;
// Compare existing value with new RRule array
$existing_rule = null;
if ($this->attr('value')->rrule) {
$existing_rule = $this->attr('value')->rrule->getRule();
}
// Check if RRule array value is different than existing one
if ($existing_rule !== $rrule->getRule()) {
$this->val($recurring_date_obj);
$this->trackChange('value');
return $this;
}
return $this;
}
/**
* Get setting
*
* @param string $key
* @return mixed
*
*/
public function getDateStartInputType()
{
switch ($this->startDateInput) {
case "datetime":
return "datetime-local";
default:
return "date";
}
}
public function ___getConfigInputfields()
{
// Get the defaults and $inputfields wrapper we can add to
$inputfields = parent::___getConfigInputfields();
// Add a new Inputfield to it
$f = $this->modules->get('InputfieldInteger');
$f->attr('name', 'pageSize');
$f->label = $this->_('Page size');
$f->value = $this->pageSize;
$inputfields->add($f);
/** @var InputfieldSelect $f */
$f = $this->modules->get('InputfieldSelect');
$f->attr('name', 'startDateInput');
$f->label = $this->_("Input type for start date");
$f->addOptions([
'date' => $this->_('Date'),
'datetime' => $this->_('Both date and time')]);
//bd($this->startDateInput);
$f->val($this->getSetting('startDateInput'));
$inputfields->add($f);
/** @var InputfieldInteger $f */
$f = $this->modules->get('InputfieldInteger');
$f->attr('name', 'hardLimit');
$f->label = $this->_("Hard limit on the 'Never' option of the interface. Otherwise, the RRule calculation would run into an infinite loop");
$f->val($this->getSetting('hardLimit'));
$inputfields->add($f);
return $inputfields;
}
public function ___getConfigAllowContext($field)
{
$a = array('pageSize', 'startDateInput', 'hardLimit');
return array_merge(parent::___getConfigAllowContext($field), $a);
}
}