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

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

Open
webard opened this issue Mar 28, 2024 · 3 comments

Comments

@webard
Copy link
Contributor

webard commented Mar 28, 2024

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
Copy link
Owner

wdelfuego commented Mar 28, 2024

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
Copy link
Contributor Author

webard commented Mar 30, 2024

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.

foreach($eventGenerator->generateEvents($this->startOfCalendar(), $this->endOfCalendar()) as $event)
{
if($event->resource()->authorizedToView($this->request) && !$this->excludeResource($event->resource()))
{
$this->allEvents[] = $event->withUrl($this->urlForResource($event->resource()));
}
}

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

@wdelfuego
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants