Skip to content

[Bug]: Example in readme.md is not working as expected #7

Open
@CharlieBrownCharacter

Description

@CharlieBrownCharacter

Problem

The example in readme.md in the code page of your package is not working.

Explanation

In the page of your package you have

 use InfyOm\LaravelCalendarEvents\CalendarEvent;
 use InfyOm\LaravelCalendarEvents\CalendarEventRecurrencePattern;

 $event = new CalendarEvent([
     [
         'id' => 1,
         'title' => 'Daily Repeat End on 30 Jan',
         'description' => 'Daily Repeat End on 30 Jan',
         'start_date' => '2021-01-10',
         'end_date' => '2021-01-20', // nullable
         'start_time' => '10:00:00',
         'end_time' => '12:00:00',
         'is_full_day' => false,
         'is_recurring' => true,
         'location' => 'Surat, India', // extra field. It will be automatically added to meta
         'meta' => [
             'ticket_required' => true
         ]
     ]
 ]);
 
 $event->recurring_pattern = new CalendarEventRecurrencePattern([
     'recurring_type' => RecurringFrequencyType::RECURRING_TYPE_DAILY,
     'max_occurrences' => 10, // Maximum 10 Occurrences
     'repeat_interval' => 1, // Repeat Daily
     'repeat_by_days' => ["MO", "WE", "SU"], // only repeat on Monday, Wednesday and Sunday
     'repeat_by_months' => [],
 ]);

 // Retrieve next 5 events. Returns CalendarEvent array.
 $event->getNextEvents(5);

 // Retrieve all events between 5th Jan to 15th Jan. Returns CalendarEvent array.
 $event->getEventsBetween('2021-01-05', '2021-01-15');

 // Retrieve next 2 Occurrences. Returns \Recurr\Recurrence array
 $event->getNextOccurrences(2);
 
 // If you Laravel Eloquent model matches the field names with above field name
 $event = new CalendarEvent($calendarModle);

The line $event->getEventsBetween('2021-01-05', '2021-01-15'); you would expect to get 4 events.

2021-01-06 // WEDNESDAY
2021-01-10 // SUNDAY
2021-01-11 // MONDAY
2021-01-13 // WEDNESDAY

but you get an empty array.

Enviroment

I have used php artisan tinker from Laravel:

www@761a074101a4:/var/www$ php artisan tinker
Psy Shell v0.10.8 (PHP 7.4.16 — cli) by Justin Hileman
>>> use InfyOm\LaravelCalendarEvents\CalendarEvent;
>>>  use InfyOm\LaravelCalendarEvents\CalendarEventRecurrencePattern;
>>> $event = new CalendarEvent([
...      [
...          'id' => 1,
...          'title' => 'Daily Repeat End on 30 Jan',
...          'description' => 'Daily Repeat End on 30 Jan',
...          'start_date' => '2021-01-10',
...          'end_date' => '2021-01-20', // nullable
...          'start_time' => '10:00:00',
...          'end_time' => '12:00:00',
...          'is_full_day' => false,
...          'is_recurring' => true,
...          'location' => 'Surat, India', // extra field. It will be automatically added to meta
...          'meta' => [
...              'ticket_required' => true
...          ]
...      ]
...  ]);
=> InfyOm\LaravelCalendarEvents\CalendarEvent {#4639
     +id: null,
     +title: null,
     +description: null,
     +start_date: null,
     +end_date: null,
     +start_time: null,
     +end_time: null,
     +is_full_day: null,
     +is_recurring: null,
     +meta: [
       [
         "id" => 1,
         "title" => "Daily Repeat End on 30 Jan",
         "description" => "Daily Repeat End on 30 Jan",
         "start_date" => "2021-01-10",
         "end_date" => "2021-01-20",
         "start_time" => "10:00:00",
         "end_time" => "12:00:00",
         "is_full_day" => false,
         "is_recurring" => true,
         "location" => "Surat, India",
         "meta" => [
           "ticket_required" => true,
         ],
       ],
     ],
     +excluded_dates: null,
     +recurring_pattern: null,
   }
>>> $event->recurring_pattern = new CalendarEventRecurrencePattern([
...     'recurring_type' => "DAILY",
...     'max_occurrences' => 10, // Maximum 10 Occurrences
...     'repeat_interval' => 1, // Repeat Daily
...     'repeat_by_days' => ["MO", "WE", "SU"], // only repeat on Monday, Wednesday and Sunday
...     'repeat_by_months' => [],
... ]);
=> InfyOm\LaravelCalendarEvents\CalendarEventRecurrencePattern {#4655
     +event_id: null,
     +recurring_type: "DAILY",
     +max_occurrences: 10,
     +repeat_interval: 1,
     +repeat_by_days: [
       "MO",
       "WE",
       "SU",
     ],
     +repeat_by_months: [],
   }
>>> $event->getEventsBetween('2021-01-05', '2021-01-15');
=> []

The problem

On line 277 of src/CalendarEvent.php you have this line:

if (empty($this->end_date)) {
            $this->end_date = Carbon::parse($endDate);
} else {
  if ($this->end_date > $endDate) {
    $this->end_date = Carbon::parse($endDate);
  }
}

Then on line 144 of the same file you have this line:

private function applyEndDate(Rule $rule): Rule
    {
        if (!empty($this->end_date)) {
            $endDate = Carbon::parse($this->end_date)->setTimeFromTimeString($this->end_time);
            $rule->setUntil($endDate);
        }

        return $rule;
    }

which is called from line 255. On this method you call $rule->setUntil method which in turn will do:

public function setUntil(\DateTimeInterface $until)
    {
        $this->until = $until;
        $this->count = null;

        return $this;
    }

completely overiding the count. Now, instead of getting a maximum of 10 events (remember, on the example we have max_occurrences = 10) we will get until the date $until, which in this case is $endDate.

The fix

To fix this problem we just need to add a check to verify that if we don't have a max_occurrences set, then we can set the end date

if (empty($this->recurring_pattern->max_occurrences)) {
  $this->applyEndDate($rule);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions