Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

double event issue #577

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions includes/calendars/views/default-calendar-grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function styles()
*
* @since 3.4.3
*/
public function format_timestamp($format = 'F', $timestamp)
public function format_timestamp($timestamp, $format = 'F')
rosinghal marked this conversation as resolved.
Show resolved Hide resolved
{
$datetime = new \DateTime();
$datetime->setTimezone(new \DateTimeZone($this->calendar->timezone));
Expand Down Expand Up @@ -231,7 +231,7 @@ class="simcal-nav simcal-current"
}

foreach ($current as $k => $v) {
echo ' <span class="simcal-current-' . $k, '">' . $this->format_timestamp($v, $calendar->start) . '</span> ';
echo ' <span class="simcal-current-' . $k, '">' . $this->format_timestamp($calendar->start, $v) . '</span> ';
}

echo '</h3>';
Expand Down Expand Up @@ -281,8 +281,8 @@ class="simcal-nav simcal-current"
</thead>

<?php echo $this->draw_month(
date($this->format_timestamp('n', $calendar->start)),
date($this->format_timestamp('Y', $calendar->start))
date($this->format_timestamp($calendar->start, 'n')),
date($this->format_timestamp($calendar->start, 'Y'))
); ?>
</table>

Expand Down Expand Up @@ -363,22 +363,44 @@ private function draw_month($month, $year, $id = 0)
$timestamps = array_keys($events);
$lower_bound = array_filter($timestamps, [$this, 'filter_events_before']);
$higher_bound = array_filter($lower_bound, [$this, 'filter_events_after']);

$current_month = Carbon::createFromTimestamp($this->start, $calendar->timezone)->month;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the changes starting from this line till 382. Can you please explain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$current_month = Carbon::createFromTimestamp($this->start, $calendar->timezone)->month; $current_year = Carbon::createFromTimestamp($this->start, $calendar->timezone)->year;
in first two lines, get the current month and current year,

`$filtered = array_filter($events, function ($events_in_day) {
foreach ($events_in_day as $event) {

This foreach block filters the events to include only those that occur within the current calendar view's start and end timestamps.
It checks if the event's start time is within the range or if the event's end time falls within the range.
The 86400 seconds (24 hours) is added to the end timestamp to include events that end at the very end of the day.`

$day_events = [];
foreach ($filtered as $timestamp => $events_in_day) {
from this line Initializes an empty array to hold events for each day. It iterates over the filtered events, creating Carbon instances for the start and end times of each event, adjusted for the calendar's timezone.

for ($day = $event_start->copy(); $day->lte($event_end); $day->addDay()) {
This loop iterates through each day from the event's start to its end.
It checks if the day falls within the current month and year.
It generates a unique key for each day (day_key) and retrieves the event's unique identifier (event_id).
If the event ends at midnight (indicating it should not be counted for that day), it skips adding that event.
If the event is not already recorded for that day, it adds it to the day_events array.

$current_year = Carbon::createFromTimestamp($this->start, $calendar->timezone)->year;

$filtered =
is_array($events) && is_array($higher_bound) && !empty($events) && !empty($higher_bound)
? array_intersect_key($events, array_combine($higher_bound, $higher_bound))
: [];

// Put resulting events in an associative array, with day of the month as key for easy retrieval in calendar days loop.
$day_events = [];
foreach ($filtered as $timestamp => $events_in_day) {
foreach ($events_in_day as $event) {
if ($event instanceof Event) {
$day = intval(Carbon::createFromTimestamp($timestamp, $calendar->timezone)->endOfDay()->day);
$day_events[$day][] = $event;
$event_start = Carbon::createFromTimestamp($event->start, $calendar->timezone);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreach ($day_events as $day_key => $events) {
This final loop ensures that the events for each day are stored as a simple indexed array, removing any gaps in the keys. This is useful for consistent access to the events later in the code.

Overall, this code snippet filters and organizes calendar events based on their start and end times, ensuring that events are correctly attributed to the days they occur. It handles edge cases, such as events that span multiple days and those that end exactly at the start of a new day, to prevent double counting.

$event_end = Carbon::createFromTimestamp($event->end, $calendar->timezone);

for ($day = $event_start->copy(); $day->lte($event_end); $day->addDay()) {
if ($day->month == $current_month && $day->year == $current_year) {
$day_key = intval($day->format('j'));
$event_id = $event->uid;

if ($day->isSameDay($event_end) && $event_end->hour == 0 && $event_end->minute == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to add comment which kind of case does it handle

continue;
}

if (!isset($day_events[$day_key][$event_id])) {
$day_events[$day_key][$event_id] = $event;
}
}
}
}
}
}

foreach ($day_events as $day_key => $events) {
$day_events[$day_key] = array_values($events);
}

ksort($day_events, SORT_NUMERIC);
}

Expand Down
2 changes: 1 addition & 1 deletion includes/feeds/google.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,13 +562,13 @@ public function make_request($id = '', $time_min = 0, $time_max = 0)
// Query events in calendar.
$simple_calendar_auth_site_token = get_option('simple_calendar_auth_site_token');
$response = '';
$backgroundcolor = '';
if (
isset($simple_calendar_auth_site_token) &&
!empty($simple_calendar_auth_site_token && $is_authhelper) &&
isset($feed_type[0]->slug) &&
$feed_type[0]->slug != 'google'
) {
$backgroundcolor = '';
$response_arr = apply_filters('simple_calendar_oauth_list_events', '', $id, $args);

$response = unserialize($response_arr['data']);
Expand Down