Repository navigation
Expand file tree
/
Copy pathclass.eventcalendar.plugin.php
More file actions
426 lines (390 loc) · 14.1 KB
/
Copy pathclass.eventcalendar.plugin.php
File metadata and controls
426 lines (390 loc) · 14.1 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
$PluginInfo['EventCalendar'] = [
'Name' => 'Event Calendar',
'Description' => 'Adds an event date field to new discussions so that they can be treated as events',
'Version' => '0.5.0',
'RequiredApplications' => ['Vanilla' => '>=2.3'],
'SettingsUrl' => '/settings/eventcalendar',
'RequiredPlugins' => false,
'RequiredTheme' => false,
'MobileFriendly' => true,
'HasLocale' => true,
'RegisterPermissions' => false,
'RegisterPermissions' => [
'Plugins.EventCalendar.Add',
'Plugins.EventCalendar.Manage',
'Plugins.EventCalendar.Notify',
'Plugins.EventCalendar.View'
],
'Author' => 'Robin Jurinka',
'AuthorUrl' => 'http://vanillaforums.org/profile/r_j',
'License' => 'MIT'
];
/**
* Plugin that adds a date field to new discussions.
*
* New discussions could be entered with an "event date". Plugin creates
* additional views where such discussions are shown on a calendar.
*
* @package EventCalendar
* @author Robin Jurinka
* @license MIT
*/
class EventCalendarPlugin extends Gdn_Plugin {
/**
* Prepare needed system changes.
*
* Initiate db structure change and create a custom
* route for calendar view.
*
* @return void.
* @package EventCalendar
* @since 0.1
*/
public function setup() {
// Change db structure.
$this->structure();
// Set initial config settings.
touchConfig('EventCalendar.CustomRoute', 'eventcalendar');
// Set custom route to plugin page.
$router = Gdn::router();
$pluginPage = 'vanilla/eventcalendar$1';
$newRoute = '^'.c('EventCalendar.CustomRoute', 'eventcalendar').'(/.*)?$';
if (!$router->matchRoute($newRoute)) {
$router->setRoute($newRoute, $pluginPage, 'Internal');
}
}
/**
* Add event date field to Discussion table.
*
* @return void.
* @package EventCalendar
* @since 0.1
*/
public function structure() {
Gdn::structure()->table('Discussion')
->column('EventCalendarDate', 'date', true)
->column('EventCalendarDateEnd', 'date', true)
->set(false, false);
}
/**
* Reset custom route.
*
* @return void.
* @package EventCalendar
* @since 0.1
*/
public function onDisable() {
Gdn::router()->deleteRoute('^'.c('EventCalendar.CustomRoute', 'eventcalendar').'(/.*)?$');
}
/**
* Settings screen to choose categories and set custom url.
*
* @param object $sender SettingsController.
* @return void.
* @package EventCalendar
* @since 0.3
*/
public function settingsController_eventCalendar_create($sender) {
$sender->permission('Garden.Settings.Manage');
$sender->title(t('EventCalendar.SettingsTitle'));
$sender->addSideMenu('dashboard/settings/plugins');
$sender->description('Description', t('EventCalendar.SettingsDescription'));
if ($sender->Form->authenticatedPostBack()) {
$formPostValues = $sender->Form->formValues();
// Save serialized CategoryIDs.
saveToConfig(
'EventCalendar.CategoryIDs',
serialize($formPostValues['CategoryIDs'])
);
// Set new route if needed
$newUrl = $formPostValues['CustomRoute'];
$oldUrl = c('CustomRoute');
if ($oldUrl != $newUrl) {
// Delete old custom route.
$router = Gdn::router();
$router->deleteRoute('^'.$oldUrl.'(/.*)?$');
// Set new custom route.
$pluginPage = 'vanilla/eventcalendar$1';
$newRoute = '^'.$newUrl.'(/.*)?$';
if (!$router->matchRoute($newRoute)) {
$router->setRoute(
$newRoute,
'vanilla/eventcalendar$1',
'Internal'
);
}
}
} else {
$sender->Form->setValue(
'CategoryIDs',
c('EventCalendar.CategoryIDs')
);
$sender->Form->setValue(
'CustomRoute',
c('EventCalendar.CustomRoute')
);
}
$categories = CategoryModel::categories();
unset($categories[-1]);
$sender->setData('Schema', [
'CategoryIDs' => [
'Control' => 'CheckBoxList',
'LabelCode' => 'Categories',
'Items' => $categories,
'Description' => 'Please choose categories in which the creation of events should be allowed',
'Options' => ['ValueField' => 'CategoryID', 'TextField' => 'Name']
],
'CustomRoute' => [
'Control' => 'TextBox',
'LabelCode' => 'Custom Url',
'Description' => 'The event calendar will be accessible under that url'
]
]);
$sender->render('settings', '', 'plugins/EventCalendar');
}
/**
* Add menu entry for calendar to custom menu.
*
* @param object $sender GardenController.
* @return void.
* @package EventCalendar
* @since 0.1
*/
public function base_render_before($sender) {
if ($sender->Menu && checkPermission('Plugins.EventCalendar.View')) {
$sender->Menu->addLink(
t('EventCalendar'),
t('Event Calendar'),
c('EventCalendar.CustomRoute', 'eventcalendar')
);
}
}
/**
* Add input fields to New Discussion view.
*
* Check for custom permission. Allows creation of events in current
* and next year.
* Datefield is prefilled with current date by eventcalendar.js.
*
* @param object $sender PostController.
* @return void.
* @package EventCalendar
* @since 0.1
*/
public function postController_beforeBodyInput_handler($sender) {
if (!checkPermission([
'Plugins.EventCalendar.Add',
'Plugins.EventCalendar.Manage'
])) {
return;
}
$sender->addJsFile('eventcalendar.js', 'plugins/EventCalendar');
$sender->addDefinition(
'EventCalendarCategoryIDs',
json_encode(unserialize(c('EventCalendar.CategoryIDs')))
);
$categoryID = $sender->Data['Category']['CategoryID'];
// initially don't hide elements in allowed categories
$cssClass = 'P EventCalendarInput';
if (!in_array($categoryID, unserialize(c('EventCalendar.CategoryIDs')))) {
$cssClass .= ' hidden';
}
$year = date('Y');
$yearRange = $year.'-'.($year + 1);
$fields = explode(',', t('EventCalendar.DateOrder', 'day,month,year'));
echo '<div class="', $cssClass, '">';
echo $sender->Form->label('Date de début', 'EventCalendarDate');
echo $sender->Form->date('EventCalendarDate', [
'YearRange' => $yearRange,
'Fields' => $fields
]);
echo '</div>';
echo '<div class="', $cssClass, '">';
echo $sender->Form->label('Date de fin', 'EventCalendarDateEnd');
echo $sender->Form->date('EventCalendarDateEnd', [
'YearRange' => $yearRange,
'Fields' => $fields
]);
echo '</div>';
}
/**
* Check permission and validate event date input.
*
* @param object $sender DiscussionModel.
* @param mixed $args EventArguments.
*
* @return void.
* @package EventCalendar
* @since 0.1
*/
public function discussionModel_beforeSaveDiscussion_handler($sender, $args) {
// Reset event date and return if wrong category or no right to add event.
$session = Gdn::session();
if (
!in_array(
$args['FormPostValues']['CategoryID'],
unserialize(c('EventCalendar.CategoryIDs'))
) ||
!$session->checkPermission([
'Plugins.EventCalendar.Add',
'Plugins.EventCalendar.Manage'
])
) {
$args['FormPostValues']['EventCalendarDate'] = '';
$args['FormPostValues']['EventCalendarDateEnd'] = '';
return;
}
// Add custom validation text.
$sender->Validation->applyRule(
'EventCalendarDate',
'Required',
t('EventDate.Required', 'Please enter an event date')
);
$sender->Validation->applyRule(
'EventCalendarDate',
'Date',
t('EventDate.IsDate', 'The event date you\'ve entered is invalid')
);
$sender->Validation->applyRule(
'EventCalendarDateEnd',
'Date',
t('EventDate.IsDate', 'The event date you\'ve entered is invalid')
);
/**
* The following code would be useful if events are saved with a time.
* For times, users timezones must be considered.
*/
/*
// Make date independent from users hour offset.
if ($sender->Validation->results()) {
return;
}
// $hourOffset = $session->hourOffset()
$args['FormPostValues']['EventCalendarDate'] = Gdn_Format::toDateTime(
strtotime($args['FormPostValues']['EventCalendarDate']) -
Gdn::session()->User->HourOffset * 3600
);
*/
}
/**
* Return nicely formatted html for an event date.
*
* @param date $eventDate The date to format.
* @param boolean $includeIcon Whether an icon should be included or not.
*
* @return string Formatted event date.
* @package EventCalendar
* @since 0.1
*/
private function formatEventCalendarDate($eventDate = '0000-00-00', $includeIcon = true) {
if (
!$eventDate ||
!checkPermission(['Plugins.EventCalendar.View']) ||
!$eventDate ||
$eventDate == '0000-00-00'
) {
return;
}
if ($includeIcon) {
$icon = '<img src="'.smartAsset('/plugins/EventCalendar/design/images', true).'/eventcalendar.png" />';
} else {
$icon = '';
}
return sprintf(
t('EventCalendar.DateMarkup', '<div class="EventCalendarDate">%2$s On %1$s</div>'),
strftime(t('EventCalendar.DateFormat', '%A, %e %B %Y'), strtotime($eventDate)),
$icon
);
}
/**
* Add event date to discussion.
*
* @param object $sender DiscussionController.
* @param object $args EventArguments.
* @return void.
* @package EventCalendar
* @since 0.1
*/
public function discussionController_afterDiscussionTitle_handler($sender, $args) {
echo $this->formatEventCalendarDate($args['Discussion']->EventCalendarDate);
}
/**
* Add event date to discussions list.
*
* @param object $sender DiscussionsController.
* @param object $args EventArguments.
* @return void.
* @package EventCalendar
* @since 0.1
*/
public function discussionsController_afterDiscussionTitle_handler($sender, $args) {
echo $this->formatEventCalendarDate($args['Discussion']->EventCalendarDate);
}
/**
* Add event date to discussion in categories.
*
* @param object $sender CategoriesController.
* @param object $args EventArguments.
* @return void.
* @package EventCalendar
* @since 0.1
*/
public function categoriesController_afterDiscussionTitle_handler($sender, $args) {
echo $this->formatEventCalendarDate($args['Discussion']->EventCalendarDate);
}
/**
* Show calendar view.
*
* @param object $sender VanillaController.
* @param array $args /Year/Month to show.
* @return void.
* @package EventCalendar
* @since 0.1
*/
public function vanillaController_eventCalendar_create($sender, $args = []) {
$sender->permission('Plugins.EventCalendar.View');
$eventCalendarModel = new EventCalendarModel();
// $sender->clearCssFiles();
$sender->addCssFile('style.css');
$sender->addCssFile('eventcalendar.css', 'plugins/EventCalendar');
$sender->addJsFile('eventcalendar.js', 'plugins/EventCalendar');
$sender->MasterView = 'default';
$sender->addModule('NewDiscussionModule');
$sender->addModule('CategoriesModule');
$sender->addModule('BookmarkedModule');
// only show current year +/- 1
$year = (int)$args[0];
$currentYear = date('Y');
if ($year < $currentYear -1 || $year > $currentYear + 1) {
$year = $currentYear;
}
// sanitize month
$month = sprintf("%02s", (int)$args[1]);
if ($month < 1 || $month > 12) {
$month = date('m');
}
$monthFirst = mktime(0, 0, 0, $month, 1, $year);
$daysInMonth = date('t', $monthFirst);
$monthLast = mktime(0, 0, 0, $month, $daysInMonth, $year);
$sender->canonicalUrl(url(c('EventCalendar.CustomRoute', 'eventcalendar'), true));
$sender->setData('Title', t('Event Calendar'));
$sender->setData('Breadcrumbs', [[
'Name' => t('Event Calendar'),
'Url' => $sender->canonicalUrl()
]]);
$sender->setData('Month', $month);
$sender->setData('Year', $year);
$sender->setData('MonthFirst', $monthFirst);
$sender->setData('MonthLast', $monthLast);
$sender->setData('PreviousMonth', date('Y', $monthFirst - 1).'/'.date('m', $monthFirst - 1));
$sender->setData('NextMonth', date('Y', $monthLast + 86400).'/'.date('m', $monthLast + 86400));
$sender->setData('DaysInMonth', $daysInMonth);
$plusUn = $month+1;
$sender->setData('Events', $eventCalendarModel->get("{$year}-{$month}-01", "{$year}-{$plusUn}-{$daysInMonth}"));
$sender->setData('CanonicalUrl', $sender->canonicalUrl());
$sender->setData('Title', strftime(t('Calendrier pour %B %Y'), $monthFirst));
$viewName = 'month';
$sender->render($viewName, '', 'plugins/EventCalendar');
}
}