/
InputfieldEvents.module
166 lines (141 loc) · 4.4 KB
/
InputfieldEvents.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
<?php namespace ProcessWire;
/**
* FieldtypeEvents: InputfieldEvent
*
* Collects input for FieldtypeEvents fields. This accompanies the FieldtypeEvents as a demonstration
* of creating a multi-value Fieldtype and Inputfield.
*
*/
class InputfieldEvents extends Inputfield {
public static function getModuleInfo() {
return array(
'title' => 'Events',
'version' => 4,
'summary' => 'Input field for events.',
'icon' => 'calendar-o',
'requires' => 'FieldtypeEvents',
);
}
/**
* Render a table row for an individual Event input
*
* @param Event $event
* @param int $n
* @param string $class
* @return string
*
*/
protected function renderRow(Event $event, $n, $class = 'Event') {
$name = $this->attr('name');
$date = $event->date ? $event->date : date('Y-m-d');
$title = $this->sanitizer->entities($event->title);
$copyIcon = wireIconMarkup('copy', 'fw');
$trashIcon = wireIconMarkup('trash-o', 'fw');
$inputClass = $this->adminTheme->getClass('input');
$out = "
<tr class='Event$n $class'>
<td class='EventClone'>
<a href='#' class='pw-tooltip' title='Clone this event'>$copyIcon</a>
</td>
<td class='EventDate'>
<input type='date' class='$inputClass' name='{$name}_date[]' value='$date' />
</td>
<td class='EventTitle'>
<input type='text' class='$inputClass' name='{$name}_title[]' value='$title' />
</td>
<td class='EventDelete'>
<a href='#' class='EventDelete pw-tooltip' title='Delete this event'>$trashIcon</a>
<input type='hidden' name='{$name}_delete[]' value='0' />
</td>
</tr>
";
return $out;
}
/**
* Render the entire input area for Events
*
* @return string
*
*/
public function ___render() {
// class attribute that adminTheme specifies for tables (optional)
$tableClass = $this->adminTheme->getClass('table');
// get Event template for creating new events
$tbody = $this->renderRow(new Event(), 0, 'Event EventTemplate');
/** @var EventArray $value */
$value = $this->attr('value');
// render rows for existing Events
foreach($value as $n => $event) {
$tbody .= $this->renderRow($event, $n+1);
}
$trashIcon = wireIconMarkup('trash-o', 'fw');
$out = "
<table class='$tableClass'>
<thead>
<tr>
<th class='EventClone'> </th>
<th class='EventDate'>Date</th>
<th class='EventTitle'>Title</th>
<th class='EventDelete'><a class='pw-tooltip' title='Delete All' href='#'>$trashIcon</a></th>
</tr>
</thead>
<tbody>
$tbody
</tbody>
</table>
";
/** @var InputfieldButton $btn add a button that triggers adding a new event */
$btn = $this->modules->get('InputfieldButton');
$btn->attr('id', $this->attr('name') . "_new");
$btn->addClass('InputfieldEventsAdd');
$btn->icon = 'plus-sign';
$btn->value = $this->_('Add New Event');
$out .= $btn->render();
return $out;
}
/**
* Render value without inputs (for when user lacks access to edit the field)
*
* To keep it simple, we are just delegating this to our existing FieldtypeEvents::markupValue(),
* but in other cases we might use a variation of the render() output without the actions and inputs.
*
* @return string
*
*/
public function ___renderValue() {
$fieldtype = $this->hasFieldtype;
return $fieldtype->markupValue($this->hasPage, $this->hasField, $this->value);
}
/**
* Process input for the Events inputs
*
* @param WireInputData $input
*
*/
public function ___processInput(WireInputData $input) {
$name = $this->attr('name');
$events = new EventArray();
$numDeleted = 0;
foreach($input["{$name}_date"] as $n => $date) {
if(!$n) continue; // template for 'new' item is the first, so we can ignore it
// check if the item is being deleted
if($input["{$name}_delete"][$n]) {
// if being deleted, then we simply skip over it
$numDeleted++;
continue;
}
// create the $event and add it to our $events
$event = new Event();
$event->date = $date;
$event->title = $input["{$name}_title"][$n];
$events->add($event);
}
// if the string values of the processed events are different from the previous,
// or if any events have been deleted, then flag this Inputfield as changed
// so that it will be automatically saved with the page
if("$events" !== "$this->value" || $numDeleted) {
$this->attr('value', $events);
$this->trackChange('value');
}
}
}