Calendar_Information_Block::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @package SimplePortal
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
13
/**
14
 * Calendar Info Block, Displays basic calendar ... birthdays, events and holidays.
15
 *
16
 * @param mixed[] $parameters
17
 *        'events' => show events
18
 *        'future' => how many months out to look for items
19
 *        'birthdays' => show birthdays
20
 *        'holidays' => show holidays
21
 * @param int $id - not used in this block
22
 * @param boolean $return_parameters if true returns the configuration options for the block
23
 */
24
class Calendar_Information_Block extends SP_Abstract_Block
25
{
26
	/**
27
	 * Constructor, used to define block parameters
28
	 *
29
	 * @param Database|null $db
30
	 */
31
	public function __construct($db = null)
32
	{
33
		$this->block_parameters = array(
34
			'events' => 'check',
35
			'future' => 'int',
36
			'birthdays' => 'check',
37
			'holidays' => 'check',
38
		);
39
40
		parent::__construct($db);
41
	}
42
43
	/**
44
	 * Initializes a block for use.
45
	 *
46
	 * - Called from portal.subs as part of the sportal_load_blocks process
47
	 *
48
	 * @param mixed[] $parameters
49
	 * @param int $id
50
	 */
51
	public function setup($parameters, $id)
52
	{
53
		global $txt;
54
55
		$show_event = !empty($parameters['events']);
56
		$event_future = !empty($parameters['future']) ? (int) $parameters['future'] : 0;
57
		$event_future = abs($event_future);
58
		$show_birthday = !empty($parameters['birthdays']);
59
		$show_holiday = !empty($parameters['holidays']);
60
		$this->data['show_titles'] = false;
61
62
		// If they are not showing anything, not much to do !
63
		if (!$show_event && !$show_birthday && !$show_holiday)
64
		{
65
			$this->data['error_msg'] = $txt['sp_calendar_noEventsFound'];
66
			$this->setTemplate('template_sp_calendarInformation_error');
67
68
			return;
69
		}
70
71
		$now = forum_time();
72
		$today_date = date("Y-m-d", $now);
73
		$this->data['calendar'] = array(
74
			'todayEvents' => array(),
75
			'futureEvents' => array(),
76
			'todayBirthdays' => array(),
77
			'todayHolidays' => array()
78
		);
79
80
		// Load calendar events
81
		if ($show_event)
82
		{
83
			// Just today's events or looking forward a few days?
84
			if (!empty($event_future))
85
			{
86
				$event_future_date = date("Y-m-d", ($now + $event_future * 86400));
0 ignored issues
show
Bug introduced by
$now + $event_future * 86400 of type double is incompatible with the type integer|null expected by parameter $timestamp of date(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
				$event_future_date = date("Y-m-d", /** @scrutinizer ignore-type */ ($now + $event_future * 86400));
Loading history...
87
			}
88
			else
89
			{
90
				$event_future_date = $today_date;
91
			}
92
93
			// Load them in
94
			$events = sp_loadCalendarData('getEvents', $today_date, $event_future_date);
95
			ksort($events);
96
97
			$displayed = array();
98
			foreach ($events as $day => $day_events)
99
			{
100
				foreach ($day_events as $event_key => $event)
101
				{
102
					if (in_array($event['id'], $displayed))
103
					{
104
						unset($events[$day][$event_key]);
105
					}
106
					else
107
					{
108
						$displayed[] = $event['id'];
109
					}
110
				}
111
			}
112
113
			if (!empty($events[$today_date]))
114
			{
115
				$this->data['calendar']['todayEvents'] = $events[$today_date];
116
				unset($events[$today_date]);
117
			}
118
119
			if (!empty($events))
120
			{
121
				ksort($events);
122
				$this->data['calendar']['futureEvents'] = $events;
123
			}
124
		}
125
126
		// Load in today's birthdays
127
		if ($show_birthday)
128
		{
129
			$this->data['calendar']['todayBirthdays'] = current(sp_loadCalendarData('getBirthdays', $today_date));
130
			$this->data['show_titles'] = !empty($show_event) || !empty($show_holiday);
131
		}
132
133
		// Load in any holidays
134
		if ($show_holiday)
135
		{
136
			$this->data['calendar']['todayHolidays'] = current(sp_loadCalendarData('getHolidays', $today_date));
137
			$this->data['show_titles'] = !empty($show_event) || !empty($show_birthday);
138
		}
139
140
		// Done collecting information, show what we found
141
		if (empty($this->data['calendar']['todayEvents']) && empty($this->data['calendar']['futureEvents']) && empty($this->data['calendar']['todayBirthdays']) && empty($this->data['calendar']['todayHolidays']))
142
		{
143
			$this->data['error_msg'] = $txt['sp_calendar_noEventsFound'];
144
			$this->setTemplate('template_sp_calendarInformation_error');
145
146
			return;
147
		}
148
		$this->setTemplate('template_sp_calendarInformation');
149
	}
150
}
151
152
/**
153
 * Error template for this block
154
 *
155
 * @param mixed[] $data
156
 */
157
function template_sp_calendarInformation_error($data)
158
{
159
	echo $data['error_msg'];
160
}
161
162
/**
163
 * Main template for this block
164
 *
165
 * @param mixed[] $data
166
 */
167
function template_sp_calendarInformation($data)
168
{
169
	global $txt, $scripturl;
170
171
	echo '
172
		<ul class="sp_list">';
173
174
	if (!empty($data['calendar']['todayHolidays']))
175
	{
176
		if ($data['show_titles'])
177
		{
178
			echo '
179
			<li>
180
				<strong>', $txt['sp_calendar_holidays'], '</strong>
181
			</li>';
182
		}
183
184
		foreach ($data['calendar']['todayHolidays'] as $holiday)
185
		{
186
			echo '
187
			<li ', sp_embed_class('holiday'), '>', $holiday, '</li>';
188
		}
189
	}
190
191
	if (!empty($data['calendar']['todayBirthdays']))
192
	{
193
		if ($data['show_titles'])
194
		{
195
			echo '
196
			<li>
197
				<strong>', $txt['sp_calendar_birthdays'], '</strong>
198
			</li>';
199
		}
200
201
		foreach ($data['calendar']['todayBirthdays'] as $member)
202
		{
203
			echo '
204
			<li ', sp_embed_class('birthday'), '><a href="', $scripturl, '?action=profile;u=', $member['id'], '">', $member['name'], isset($member['age']) ? ' (' . $member['age'] . ')' : '', '</a></li>';
205
		}
206
	}
207
208
	if (!empty($data['calendar']['todayEvents']))
209
	{
210
		if ($data['show_titles'])
211
		{
212
			echo '
213
			<li>
214
				<strong>', $txt['sp_calendar_events'], '</strong>
215
			</li>';
216
		}
217
218
		foreach ($data['calendar']['todayEvents'] as $event)
219
		{
220
			echo '
221
			<li ', sp_embed_class('event'), '>', $event['link'], !$data['show_titles'] ? ' - ' . standardTime(forum_time(), '%d %b') : '', '</li>';
222
		}
223
	}
224
225
	if (!empty($data['calendar']['futureEvents']))
226
	{
227
		if ($data['show_titles'])
228
		{
229
			echo '
230
			<li>
231
				<strong>', $txt['sp_calendar_upcomingEvents'], '</strong>
232
			</li>';
233
		}
234
235
		foreach ($data['calendar']['futureEvents'] as $startdate => $events)
236
		{
237
			foreach ($events as $event)
238
			{
239
				echo '
240
			<li ', sp_embed_class('event'), '>', $event['link'], ' - ', standardTime(strtotime($startdate), '%d %b'), '</li>';
241
			}
242
		}
243
	}
244
245
	echo '
246
		</ul>';
247
}
248