wdelfuego / nova-calendar

A powerful event calendar Tool for Laravel's Nova 4
Other
91 stars 26 forks source link

Cannot change url by method `withUrl()`, when events are created by generator #80

Open webard opened 6 months ago

webard commented 6 months ago

Hello,

In my app I have calendar_events table with all calendar events morphed to many other models.

My generator look like this:

class CrmCalendarEventGenerator extends CustomEventGenerator
{
    protected function modelQuery(EloquentBuilder $queryBuilder, Carbon $startOfCalendar, Carbon $endOfCalendar): EloquentBuilder
    {
        return $queryBuilder->with(['eventable'])->where('start', '>=', $startOfCalendar)
            ->where(function ($q) use ($endOfCalendar) {
                $q->where('end', '<=', $endOfCalendar)
                    ->orWhereNull('end');
            })->where('type', 'recall');
    }

    /**
     * @return array<Event>
     */
    protected function resourceToEvents(NovaResource $resource, Carbon $startOfCalendar, Carbon $endOfCalendar): array
    {
        $model = $resource->model();
        assert($model instanceof CalendarEvent);
        assert($model->eventable instanceof HasCalendarEvents);
        $event = new Event($model->eventable->getCalendarTitle(), $model->start);

        $event->notes($model->description);
        $event->addStyle($model->style ?? 'default');

        // TODO: not working
        $event->withUrl('/resources/' . Str::plural($model->eventable_type) . '/' . $model->eventable_id);

        return [$event];

    }
}

And like you see, there is no option to change url by method withUrl(), when events are created by generator. Calendar Event always redirects to CalendarEvent model, instead of my customized URL.

wdelfuego commented 6 months ago

Mhm, I see what you mean.

As you can see in the loadAllEvents method of the AbstractCalendarDataProvider of this package, the event url is being overwritten after the event generator has generated the event, so your custom URL is being overwritten.

I understand that this is suboptimal and I agree that what you expect to work, should work like that, but currently it doesn't.

A fix for now, would be to implement the urlForResource method in your CalendarDataProvider, and move your URL calculation logic to there. I think it will then work as you want.

Let me know! :)

webard commented 6 months ago

Hi,

yes, urlForResource() works great:

protected function urlForResource(NovaResource $resource): string
    {
        $model = $resource->model();
        assert($model instanceof CalendarEvent);

        return '/resources/' . Str::plural($model->eventable_type) . '/' . $model->eventable_id;
    }

but I think it is little unintuitive, when all other data is set in resourceToEvents() method.

https://github.com/wdelfuego/nova-calendar/blob/efcafb0bd3c5e5249ddb5b68d2e255713abe29a2/src/DataProvider/AbstractCalendarDataProvider.php#L293-L299

Can we move this logic to Event, and use urlForResource as default url until user set own url?

wdelfuego commented 5 months ago

Hi, yes, I agree, this needs a little refactoring. I'll leave this issue open until I get around to releasing the improvement so others can find the work-around easily.