solspace / craft-calendar

Calendar for Craft: The most reliable and powerful event management plugin for Craft.
http://docs.solspace.com/craft/calendar/v5
Other
15 stars 14 forks source link

Custom event behaviors are attached to occurrence clones #150

Open mikejpeters opened 2 years ago

mikejpeters commented 2 years ago

Describe the bug or issue you're experiencing

When using a custom behavior to add functionality to the Event class, the behavior gets assigned to clones of the event during calls to $event->getOcurrence()

This means that if the behavior relies on the startDate or endDate properties of it's owner, it no longer functions correctly because it's attached to a cloned occurrence where these properties have been modified.

Steps to reproduce

  1. Create a custom behavior and register it:

    use yii\base\Behavior;
    
    class EventBehavior extends Behavior
    {
       public function getStartDateInSiteTimezone(): ?Carbon
       {
           $date = $this->owner->getStartDate()->copy();
           $timezone = Craft::$app->config->general->timezone;
           return $date->shiftTimezone($timezone);
       }
    }
    Event::on(CalendarEvent::class, CalendarEvent::EVENT_DEFINE_BEHAVIORS, function (DefineBehaviorsEvent $e) {
       $e->behaviors[] = EventBehavior ::class;
    });
  2. Create an event with multiple occurrences
  3. In code, access the event, call getOccurrences(), and then try to use the behavior:

    $event->getOccurrences();
    echo $event->getStartDateInSiteTimezone();
  4. The result will be the start date of the last occurrence, because calling getOccurrences() has the side-effect of attaching the existing behavior to the occurrence instead of it's original owner

Expected behavior

Calling getOccurrences() should not interfere with custom behaviors.

Craft & Plugin Info (please complete the following information):

Additional context

The code that re-attaches the behaviors can be found here:

https://github.com/solspace/craft-calendar/blob/ba95ac7f99a43bcdc1d43358c5b5f5ac1148c3ff/packages/plugin/src/Elements/Event.php#L292-L294

jesuismaxime commented 1 year ago

Any release date related to that issue? I think this cause a major major issue : resaving a calendar resave all occurences of all events of this calendar. Causing Craft to crash because it needs to save 1700 elements for a calendar containing 70 events.