forked from ryancramerdesign/FieldtypeEvents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputfieldEvents.module
More file actions
158 lines (132 loc) · 4.67 KB
/
Copy pathInputfieldEvents.module
File metadata and controls
158 lines (132 loc) · 4.67 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
<?php
class InputfieldEvents extends Inputfield {
public static function getModuleInfo() {
return array(
'title' => 'Events',
'version' => 3,
'summary' => 'Input field for events.',
'requires' => 'FieldtypeEvents',
);
}
protected $page;
protected $field;
public function setPage(Page $page) {
$this->page = $page;
}
public function setField(Field $field) {
$this->field = $field;
}
/**
* Render a table row for an individual Event input
*
*/
protected function renderRow(Event $event, $cnt, $class = 'Event',$templateTable = false) {
$name = $this->attr('name');
$date = $event->date > 0 ? date(Event::dateFormat, $event->date) : '';
$location = $this->sanitizer->entities($event->location);
$notes = $this->sanitizer->entities($event->notes);
$out = '';
($templateTable ? $out .= '<table class="EventTemplateTable">' : null);
$out .= '
<tr class="Event'.$cnt.' '.$class.'">
<td><a href="#" class="EventClone"><span class="ui-icon ui-icon-copy"></span></a></td>
<td>
<span class="inputValue">'.(($date != '') ? $date : '<span class="placeholder">(Click to edit)</span>').'</span>
<input type="text" name="'.$name.'_date[]" value="'.$date.'" class="datepicker" /><i class="fa fa-calendar eventsDatePicker"></i>
</td>
<td>
<span class="inputValue">'.(($location != '') ? $location : '<span class="placeholder">(Click to edit)</span>').'</span>
<input type="text" name="'.$name.'_location[]" value="'.$location.'" />
</td>
<td>
<span class="inputValue">'.(($notes != '') ? $notes : '<span class="placeholder">(Click to edit)</span>').'</span>
<input type="text" name="'.$name.'_notes[]" value="'.$notes.'" />
</td>
<td>
<a href="#" class="EventDel ui-helper-clearfix"><span class="ui-icon ui-icon-trash"></span></a>
<input type="hidden" name="'.$name.'_del[]" value="0" />
</td>
</tr>
';
($templateTable ? $out .= '</table>' : null);
return $out;
}
/**
* Render the entire input area for Events
*
*/
public function ___render() {
// get Event template for creating new events
$out = $this->renderRow(new Event(), 0, 'Event EventTemplate',true);
// render rows for existing Events
foreach($this->attr('value') as $cnt => $event) {
$tbody .= $this->renderRow($event, $cnt);
}
$out .= '
<table class="InputfieldEvents">
<thead>
<tr class="">
<th class="EventClone"> </th>
<th class="EventDate">Date</th>
<th class="EventLocation">Location</th>
<th class="EventNotes">Notes</th>
<th class="EventDel">
<a title="Delete All" href="#" class="EventDelAll"><span class="ui-icon ui-icon-trash"></span></a>
</th>
</tr>
</thead>
<tbody>'.$tbody.'</tbody>
</table>
';
$recurDatesHTML = file_get_contents(dirname(__FILE__)."/lib/recurdates.html");
$out .= $recurDatesHTML;
// add a button that triggers adding a new event
$btn = $this->modules->get('InputfieldButton');
$btn->attr('id', $this->attr('name') . "_new");
$btn->class .= " InputfieldEventsAdd";
$btn->icon = 'plus-sign';
$btn->value = $this->_('Add New Event');
$out .= $btn->render();
$btn = $this->modules->get('InputfieldButton');
$btn->attr('id', "recurDatesPopup_open");
$btn->class .= " InputfieldEventsAddMore";
$btn->icon = 'plus-sign';
$btn->value = $this->_('Add Recurring Event');
$out .= $btn->render();
return $out;
}
/**
* Process input for the Events inputs
*
*/
public function ___processInput(WireInputData $input) {
if(!$this->page || !$this->field) {
throw new WireException("This inputfield requires that you set valid 'page' and 'field' properties to it.");
}
$name = $this->attr('name');
$events = $this->field->type->getBlankValue($this->page, $this->field);
$numDeleted = 0;
foreach($input->{"{$name}_date"} as $cnt => $date) {
if(!$cnt) continue; // template for 'new' item is the first, so we can ignore it
// check if the item is being deleted
if($input->{"{$name}_del"}[$cnt]) {
// 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->location = $input->{"{$name}_location"}[$cnt];
$event->notes = $input->{"{$name}_notes"}[$cnt];
$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');
}
}
}