forked from byuweb/calendar_d8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar_d8.module
executable file
·288 lines (241 loc) · 10.4 KB
/
calendar_d8.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
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
<?php
/**
* @file
* Module file for calendar_d8.
*/
use DateTime;
use DateTimeZone;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Block\BlockPluginInterface;
/**
* @defgroup calendar_d8 Example: Block
* @ingroup examples
* @{
* Demonstrates code creation of blocks.
*
* This is an example outlining how a module can define blocks that can be
* displayed on various pages of a site, or how to alter blocks provided by
* other modules.
*/
/**
* Implements hook_block_view_alter().
*
* This hook allows you to modify the output of any block in the system.
*
* In addition, instead of hook_block_view_alter(), which is called for all
* blocks, you can also use hook_block_view_BASE_BLOCK_ID_alter() to alter a
* specific block. To change only our block using
* hook_block_view_BASE_BLOCK_ID_alter, we would use the function:
* calendar_d8_block_view_example_uppercase_alter()
*
* We are going to uppercase the subject (the title of the block as shown to the
* user) of any block if the string "uppercase" appears in the block title or
* subject. Default block titles are set programmatically in the subject key;
* titles created through the UI are saved in the title key. This module creates
* an example block to demonstrate this effect (default title set
* programmatically as subject). You can also demonstrate the effect of this
* hook by creating a new block whose title has the string 'uppercase' in it
* (set as title through the UI).
*/
function calendar_d8_block_view_alter(array &$build, BlockPluginInterface $block) {
// We'll search for the string 'uppercase'.
$definition = $block->getPluginDefinition();
if ((!empty($build[ '#configuration' ][ 'label' ]) && stristr($build[ '#configuration' ][ 'label' ], 'uppercase')) || (!empty($definition[ 'subject' ]) && stristr($definition[ 'subject' ], 'uppercase'))) {
// This will uppercase the block title.
$build[ '#configuration' ][ 'label' ] = Unicode::strtoupper($build[ '#configuration' ][ 'label' ]);
}
}
/**
* {@inheritdoc}
*/
function calendar_d8_build_display($category, $serverSide = false) {
if ($serverSide) {
$html = calendar_d8_fetch_events($category);
} else {
$html = calendar_d8_client_side_events($category);
}
return $html;
}
function calendar_d8_client_side_events($category) {
$categories = str_replace(",", "+", $category);
$categories = str_replace(" ", "", $categories);
$categories = urlencode($categories);
$jsonUrl = "https://calendar.byu.edu/api/Events.json?event[min][date]=START_DATE&event[max][date]=END_DATE&categories={$categories}";
return '<script type="text/javascript">var byuCalendarEventsUrl="' . $jsonUrl . '";</script>
<div class="overall-feature-wrapper calendar-widget-block display-list">
<div id="calendar-event-column-template" style="display: none" class="feature-column">
<div class="date-wrapper">
<div class="date-weekday h6">DATE_WEEKDAY</div>
<div class="date-day white bold centered-text">DATE_MONTH_DAY</div>
</div>
<div id="calendar-event-event-template" class="event">
<div class="event-content">
<h4 class="event-title">
<a href="EVENT_HREF" target="_blank">EVENT_TITLE</a>
</h4>
<div class="location" slot="location">EVENT_LOCATION</div>
<div class="event-time">EVENT_TIME</div>
</div>
</div>
</div>
<div class="feature-column column-links">
<a href="https://calendar.byu.edu" class="full-link" id="full-events-area">
<div class="full-events">
<div class="full-events-text">FULL<div class="larger">EVENTS</div>CALENDAR</div>
<div class="full-events-button white bold centered-text">View >></div>
</div>
</a><a href="https://registrar.byu.edu/academic-calendar" class="academic-link" id="academic-area">
<div class="academic-calendar-wrapper">
<div class="academic-calendar-text">
<div class="larger">ACADEMIC</div>CALENDAR</div>
<div class="academic-calendar-button white bold centered-text">View >></div>
</div>
</a>
</div>
</div>';
}
/**
* {@inheritdoc}
*/
function calendar_d8_fetch_events($category) {
// pull how many days forward to go
$days = 29;
$tz = new DateTimeZone('America/Denver');
$start = new DateTime("now", $tz);
$startDate = $start->format('Y-m-d');
$end = new DateTime("+{$days} days", $tz);
$endDate = $end->format('Y-m-d');
// get categories
$categories = str_replace(",", "+", $category);
$categories = str_replace(" ", "", $categories);
$limit = 8;
// if they enter 0, have no limit:
if ($limit == 0) {
$limit = -1;
}
$jsonUrl = "http://calendar.byu.edu/api/Events?event[min][date]={$startDate}&event[max][date]={$endDate}&categories={$categories}";
$options = [
'http' => [
'method' => "GET",
'header' => "Accept: application/json\r\n",
],
];
$context = stream_context_create($options);
$result = file_get_contents($jsonUrl, FALSE, $context);
$jsonArr = json_decode($result, TRUE);
$html = calendar_d8_format_featured($jsonArr, $startDate, $endDate, $limit);
// $html = $jsonUrl;
return $html;
}
/**
* {@inheritdoc}
*/
function calendar_d8_format_featured($jsonArr, $startDate, $endDate, $limit) {
// do all the formatting here
if (empty($jsonArr)) {
// list is empty.
$html = "<h3>No events.</h3>";
}
else {
$html = '<div class="overall-feature-wrapper startDate-' . $startDate . ' endDate-' . $endDate . ' calendar-widget-block display-list">';
$currentTime = new DateTime();
$currentTime->setTimestamp(strtotime("now"));
$first_item = TRUE;
$count = 0;
// establish base variables
// columnDate: the yyyy-mm-dd format of the column it's looping through for
// columnEventCount: the number of events already loaded for the display (max 2 per day)
// columnId: 1,2,3 or 4
$columnDate = $startDate;
$columnEventCount = 0;
$columnId = 1;
$lastDate = date("y-m-d");
foreach ($jsonArr as $item) {
if ($count == $limit) {
break;
}
$newDate = date("y-m-d", strtotime($item[ 'StartDateTime' ]));
$new_date = new DateTime();
$new_date->setTimestamp(strtotime($item[ 'StartDateTime' ]));
if ($first_item) {
$lastDate = date("y-m-d", strtotime($item[ 'StartDateTime' ]));
$html .= '<div class="feature-column column-' . $columnId . '">';// start the first the column div
$html .= '<div class="date-wrapper"><div class="date-weekday h6">' . date("l", strtotime($item[ 'StartDateTime' ])) . '</div><div class="date-day white bold centered-text">' . date("M j", strtotime($item[ 'StartDateTime' ])) . '</div></div>';
$currentTime = $new_date;
$first_item = FALSE;
}
$diff = $currentTime->diff($new_date);
// check if it's the same date or not
if (($newDate !== $lastDate)) {
// yes, it's a new date column
$columnId++;
$columnEventCount = 0;
$html .= '</div>'; // end the column div
// if we have another column to do, continue:
if ($columnId == 5) {
break;
}
else {
// continue to next column
$html .= '<div class="feature-column column-' . $columnId . '">'; // start the next column div
$html .= '<div class="date-wrapper"><div class="date-weekday h6">' . date("l", strtotime($item[ 'StartDateTime' ])) . '</div><div class="date-day white bold centered-text">' . date("M j", strtotime($item[ 'StartDateTime' ])) . '</div></div>';
$currentTime = $new_date;
}
}
// check if we've hit the max of 2
if ($columnEventCount >= 2) {
// skip to the next day, do nothing
}
else {
//add the event to the current column
$html .= '<div class="event event-' . $columnEventCount . '">';
$html .= '<div class="event-content">';
$html .= '<h4 class="event-title"><a href="' . $item[ 'FullUrl' ] . ' " target="_blank">' . $item[ 'Title' ] . '</a></h4>';
if ($item[ 'LocationName' ] != NULL) {
$html .= '<div class="location" slot="location">' . $item[ 'LocationName' ] . '</div>';
}
if ($item[ 'AllDay' ] == 'false') {
$html .= '<div class="event-time">' . date("g:i A", strtotime($item[ 'StartDateTime' ])).'</div>';
}
else {
$html .= '<div class="event-time">All Day</div>';
}
$html .= '</div>'; //close event content wrapper
$html .= '</div>'; //close event
// end single event
$columnEventCount++;
}
$lastDate = date("y-m-d", strtotime($item[ 'StartDateTime' ]));
$count++;
} // end for loop
//if there are less than 4 feature-columns, events ran out
if($columnId < 4) {
$html .= '</div>'; //close last feature-column
$columnId++; // update column count
// Add placeholder feature-columns without events
while($columnId < 4) {
$currentDate = date("y-m-d", strtotime($lastDate . '+1 day'));
$html .= '<div class="feature-column column-' . $columnId . '">'; // start the next column div
$html .= '<div class="date-wrapper"><div class="date-weekday h6">' . date("l", strtotime($currentDate)) . '</div><div class="date-day white bold centered-text">' . date("M j", strtotime($currentDate)) . '</div></div></div>';
$lastDate = $currentDate;
$columnId++;
}
}
//add final links column
$html .= '<div class="feature-column column-links">';
$html .= '<a href="https://calendar.byu.edu" class="full-link" id="full-events-area"><div class="full-events">';
$html .= '<div class="full-events-text">FULL<div class="larger">EVENTS</div>CALENDAR</div>';
$html .= '<div class="full-events-button white bold centered-text">View >></div>';
$html .= '</div></a>';
$html .= '<a href="/academics" class="academic-link" id="academic-area"><div class="academic-calendar-wrapper">';
$html .= '<div class="academic-calendar-text"><div class="larger">ACADEMIC</div>CALENDAR</div>';
$html .= '<div class="academic-calendar-button white bold centered-text">View >></div>';
$html .= '</div></a>';
$html .= '</div>'; // end column-links
$html .= '</div>'; // ending the wrapping div with start and end date classes
}
return $html;
}
/**
* @} End of "defgroup calendar_d8".
*/