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

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

Open
CharlieBrownCharacter opened this issue Dec 5, 2021 · 1 comment
Open
Labels
bug Something isn't working

Comments

@CharlieBrownCharacter
Copy link

CharlieBrownCharacter commented Dec 5, 2021

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);
}
@vishalinfyom
Copy link
Contributor

Hi @CharlieBrownCharacter

Thanks for reporting the issue with the solution.

We just want to know where you had put that fix, we mean in which file & line number?

Thanks

@mitulgolakiya mitulgolakiya changed the title Example in readme.md is not working as expected [Bug]: Example in readme.md is not working as expected Feb 15, 2022
@mitulgolakiya mitulgolakiya added the bug Something isn't working label Feb 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants