-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessRecurringEvents.module
More file actions
275 lines (215 loc) · 10.8 KB
/
ProcessRecurringEvents.module
File metadata and controls
275 lines (215 loc) · 10.8 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/* Module adapted from Mr Nice Guy's form post on December 5, 2015
* at https://processwire.com/talk/topic/11613-create-virtual-pages-for-repeatingrecurring-events-in-an-event-calendar/
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* Or read it online: http://www.gnu.org/licenses/licenses.html *GPL
*
*
*
* @author Clip Magic
*
* ProcessWire 3.x
* Copyright (C) 2011 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class ProcessRecurringEvents extends WireData implements Module, ConfigurableModule {
#---------------------
# Module info
#---------------------
public static function getModuleInfo() {
return array(
'title' => 'Recurring Events',
'summary' => 'Adds a hook function to create recurring events and add/update pages to Processwire when a checkbox is checked on page save',
'version' => "0.0.1",
'author' => "Clip Magic",
'href' => "http://www.clipmagic.com.au",
'autoload' => true,
'requires' => array("FieldtypeOptions", "FieldtypeCheckbox", "ProcessPageClone", "FieldtypeDatetime")
);
}
#---------------------
# Module config defaults
#---------------------
public static function getDefaults() {
// Default template, etc
return array(
"event_templates" => array(), // array - field for event detail templates
"start_date" => "", // string - field for start date datetime
"end_date" => "", // string - field for end date datetime
"recurring" => "", // string - field for recurring checkbox
"recurrences" => "", // string - field for number of recurrences
"interval" => "", // string - field for number of recurrences
"max_recurrences" => 0 // int - maximum number of allowed recurrences
);
}
public static function getModuleConfigInputfields(array $data) {
$modules = wire('modules');
$fields = wire('fields');
$tmpTemplates = wire('templates');
foreach($tmpTemplates as $template) { // exclude system fields
if($template->flags & Template::flagSystem) continue;
$templates[] = $template;
}
$form = new InputfieldWrapper();
$defaults = self::getDefaults();
$data = array_merge($defaults, $data);
// Select template for Event detail page
$f = $modules->get("InputfieldAsmSelect");
$f->name = "event_templates";
$f->label = __("Template(s) for the single event pages");
$f->description = __("Choose the template for the event detail page.");
foreach($templates as $template) $f->addOption($template->name);
$f->value = $data['event_templates'];
$f->notes = __('Choose the template you use for Calendar event details.');
$form->add($f);
// Fieldset - start and end date fields
$fieldset = $modules->get("InputfieldFieldset");
$fieldset->name = "event_dates";
$fieldset->label = "Event Date fields";
$form->add($fieldset);
// Select the datetime field to for the event start date
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'start_date');
$f->label = 'Datetime field for the start date of the Event';
$dtFields = $fields->find('type=FieldtypeDatetime');
foreach($dtFields as $txt) $f->addOption($txt->name);
$f->value = $data['start_date'];
$f->columnWidth = 50;
$f->notes = __('Create FieldtypeDatetime field for the start date and add it to your Event detail template.');
$fieldset->add($f);
// Select the datetime field to for the event end date
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'end_date');
$f->label = 'Datetime field for the end date of the Event';
$dtFields = $fields->find('type=FieldtypeDatetime');
foreach($dtFields as $txt) $f->addOption($txt->name);
$f->value = $data['end_date'];
$f->columnWidth = 50;
$f->notes = __('Create FieldtypeDatetime field for the end date and add it to your Event detail template.');
$fieldset->add($f);
// Fieldset - Recurring event fields
$fieldset = $modules->get("InputfieldFieldset");
$fieldset->name = "recurring_info";
$fieldset->label = "Recurring Event fields";
$form->add($fieldset);
// Select the checkbox field to be used to indicate the event is recurring
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'recurring');
$f->label = 'Checkbox for Recurring Events';
$cbFields = $fields->find('type=FieldtypeCheckbox');
foreach($cbFields as $chbx) $f->addOption($chbx->name);
$f->value = $data['recurring'];
$f->columnWidth = 25;
$f->notes = __('Create at least one FieldtypeCheckbox field and add it to your Event detail template');
$fieldset->add($f);
// Select the text field to indicate the number of recurrences
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'n_recurrences');
$f->label = 'Text field for the number of Event recurrences';
$cbFields = $fields->find('type=FieldtypeText');
foreach($cbFields as $txt) $f->addOption($txt->name);
$f->value = $data['n_recurrences'];
$f->columnWidth = 25;
$f->notes = __('Create at least one FieldtypeText field and add it to your Event detail template. Strongly recommend adding pattern [0-9] and max length = 2');
$fieldset->add($f);
// Max number of recurrences
$f = $modules->get('InputfieldText');
$f->attr('name', 'max_recurrences');
$f->label = __('Maximum number of recurrences');
$f->value = $data['max_recurrences'] ? $data['max_recurrences'] : 20;
$f->columnWidth = 25;
$fieldset->add($f);
// Select the options field for the recurrence interval
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'interval');
$f->label = 'Field for the recurrence interval';
$cbFields = $fields->find('type=FieldtypeSelect|FieldtypeCheckbox|FieldtypeOptions');
foreach($cbFields as $txt) $f->addOption($txt->name);
$f->value = $data['interval'];
$f->notes = __('Create at least one single option type field - Select, Options or Checkbox for the interval with options: day, week, month, year');
$f->columnWidth = 25;
$fieldset->add($f);
return $form;
}
#---------------------
# Module init
#---------------------
public function __construct() {
$this->defaults = wire('modules')->getModuleConfigData('ProcessRecurringEvents');
}
public function init() {
$this->pages->addHookAfter('save', $this, 'hookSave');
}
public function install() {
}
public function uninstall() {
}
public function ___ready() {
}
#---------------------
# Module functions
#---------------------
public function hookSave($event) {
$page = $event->arguments[0];
if (!in_array($page->template, $this->defaults['event_templates']))
return;
$fields = wire()->fields;
$start_date = $this->defaults['start_date'];
$end_date = $this->defaults['end_date'];
$recurring = $this->defaults['recurring'];
$interval = $this->defaults['interval'];
$recurrences = $this->defaults['n_recurrences'];
$maxRecurrences = $this->defaults['max_recurrences'];
# confirm recurring is checked on the page being saved and the page is OK to clone
if ($page->$recurring==1 && !$page->isUnpublished() && !$page->isTrash()) {
$pages = wire('pages');
// ensure the number of recurrences does not exceed the maximum set in the module config
$page->$recurrences = $page->$recurrences <= $maxRecurrences ? $page->$recurrences : $maxRecurrences;
$intervalString = strtolower($page->$interval->title); // day, week, month, year
for ($i=1; $i <= $page->$recurrences; $i++) {
// Dont create duplicate duplicates! Give new page name of original page name plus hyphen and number of $i
$renamed = $page->name . "-" . $i;
$np = $pages->get("name=$renamed");
if (!$np->name) {
// preset field values for cloned page
$options = array(
'set' => array (
$recurring => 0,
'status' => $page->status
)
);
$np = $pages->clone($page, $page->parent, true, $options);
}
// Now update the fields... inc removing the '(copy)' from the cloned page
$np->of(false);
$np->title = $page->title;
$np->status = $page->status;
$np->recurring = 0;
$nextInterval = '+'.$i.' '.$intervalString; // construct the $interval selector for strtotimestamp +1 week or +1day, increase with iteration by 1
$np->$start_date = strtotime($nextInterval, $page->$start_date);
// returns null/blank if no end_date in original page
$np->$end_date = strtotime($nextInterval, $page->$end_date);
$np->save();
unset($np); // save memory ???
}
$this->message("Sucessfully created/updated ".($i-1)." copies of recurring event {$page->title}");
}
}
}?>