forked from ryancramerdesign/FieldtypeEvents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldtypeEvents.module
More file actions
205 lines (163 loc) · 6.31 KB
/
Copy pathFieldtypeEvents.module
File metadata and controls
205 lines (163 loc) · 6.31 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
<?php
/**
* A field that stores events each with a date, location and notes
*
* This is an example of creating your own Fieldtype to represent a spreadsheet type table of information.
*
*/
class FieldtypeEvents extends FieldtypeMulti {
public static function getModuleInfo() {
return array(
'title' => 'Events',
'version' => 4,
'summary' => 'Field that stores a table of events for a page.',
'installs' => 'InputfieldEvents',
);
}
/**
* Initialize this Fieldtype
*
*/
public function init() {
parent::init();
$dir = dirname(__FILE__);
require_once("$dir/Event.php");
require_once("$dir/EventArray.php");
$this->config->scripts->add($this->config->urls->FieldtypeEvents."lib/jquery.datetimepicker.full.min.js");
$this->config->scripts->add($this->config->urls->FieldtypeEvents."lib/moment.min.js");
$this->config->scripts->add($this->config->urls->FieldtypeEvents."lib/moment-recur.min.js");
$this->config->scripts->add($this->config->urls->FieldtypeEvents."lib/jquery.popupoverlay.js");
$this->config->scripts->add($this->config->urls->FieldtypeEvents."lib/recurdates.js");
$this->config->scripts->add($this->config->urls->FieldtypeEvents."lib/jquery.dataTables.min.js");
$this->config->styles->add($this->config->urls->FieldtypeEvents."lib/jquery.datetimepicker.min.css");
$this->config->styles->add($this->config->urls->FieldtypeEvents."lib/jquery.tools.overlay.css");
$this->config->styles->add($this->config->urls->FieldtypeEvents."lib/jquery.dataTables.min.css");
}
/**
* Return the required Inputfield used to populate a field of this type
*
*/
public function getInputfield(Page $page, Field $field) {
$inputfield = $this->modules->get("InputfieldEvents");
// our inputfield requires a Page and Field (most Inputfields don't)
$inputfield->setPage($page);
$inputfield->setField($field);
return $inputfield;
}
/**
* Return a blank ready-to-populate version of a field of this type
*
*/
public function getBlankValue(Page $page, Field $field) {
$events = new EventArray($page);
$events->setTrackChanges(true);
return $events;
}
/**
* Given a raw value (value as stored in DB), return the value as it would appear in a Page object
*
* @param Page $page
* @param Field $field
* @param string|int|array $value
* @return string|int|array|object $value
*
*/
public function ___wakeupValue(Page $page, Field $field, $value) {
// if for some reason we already get a valid value, then just return it
if($value instanceof EventArray) return $value;
// start a blank value to be populated
$events = $this->getBlankValue($page, $field);
// if we were given a blank value, then we've got nothing to do: just return a blank EventArray
if(empty($value) || !is_array($value)) return $events;
// create new Event objects from each item in the array
foreach($value as $v) {
$event = new Event();
$event->date = $v['data']; // note we're converting 'data' to 'date'
$event->location = $v['location'];
$event->notes = $v['notes'];
$event->setTrackChanges(true);
$events->add($event);
}
$events->resetTrackChanges();
return $events;
}
/**
* Given an 'awake' value, as set by wakeupValue, convert the value back to a basic type for storage in DB.
*
* @param Page $page
* @param Field $field
* @param string|int|array|object $value
* @return string|int
*
*/
public function ___sleepValue(Page $page, Field $field, $value) {
$sleepValue = array();
// if we are given something other than an EventArray,
// then just return a blank array
if(!$value instanceof EventArray) return $sleepValue;
// make the events sort by date ascending
$value->sort('date');
// convert each Event to an array within sleepValue
foreach($value as $event) {
$sleepValue[] = array(
'data' => (int) $event->date, // note: date is becoming data
'location' => $event->location,
'notes' => $event->notes
);
}
return $sleepValue;
}
/**
* Given a value, make it clean for storage within a Page
*
*/
public function sanitizeValue(Page $page, Field $field, $value) {
// if given a blank value, return a valid blank value
if(empty($value)) return $this->getBlankValue($page, $field, $value);
// if given something other than an EventArray, throw an error
if(!$value instanceof EventArray) {
throw new WireException("Value set to field '$field->name' must be an EventArray");
}
// note that sanitization of individual fields within a given event is already
// performed by the Event::set() method, so we don't need to do anything else here.
return $value;
}
/**
* Format a value for output, called when a Page's outputFormatting is on
*
*/
public function formatValue(Page $page, Field $field, $value) {
// we actually don't need to do anything in here since each Event object
// is doing this work in the Event::get() method. But I've included this
// comment here just to explain where that is taking place.
return $value;
}
/**
* Return the database schema that defines an Event
*
*/
public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field);
// 'data' is a required field for any Fieldtype, and we're using it to represent our 'date' field
$schema['data'] = 'INT NOT NULL DEFAULT 0';
// our text fields
$schema['location'] = 'TINYTEXT NOT NULL';
$schema['notes'] = 'TEXT NOT NULL';
// indexes, for any fields that need to be searchable from selectors
// in this case, we're just making our 'date' field searchable
// but the others could easily be added, likely as fulltext indexes
$schema['keys']['data'] = 'KEY data(data)';
return $schema;
}
/**
* Method called when the field is database-queried from a selector
*
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value) {
// If searching 'date' then assume our default (data) field
if($subfield == 'date') $subfield = 'data';
// if value is a formatted date, convert it to unix timestamp
if(!ctype_digit("$value")) $value = strtotime($value);
return parent::getMatchQuery($query, $table, $subfield, $operator, $value);
}
}