laravel-fullcalendar icon indicating copy to clipboard operation
laravel-fullcalendar copied to clipboard

Add new function to output JSON events

Open Kryptonit3-zz opened this issue 7 years ago • 0 comments
trafficstars

Add the following to Calendar.php

    /**
     * Return events in Json for ajax requests with events callback url
     *
     * @return json $this
     */
    public function eventsToJson()
    {
        $json = $this->eventCollection->toJson();

        return $json;
    }

Then in your controller you could do

    public function all(Request $request)
    {
        $start = $request->start;
        $end = $request->end;

        $pastDue = Job::pastDue()->get();

        $jobs = Job::whereBetween('start', [$start, $end])
                     ->orWhereBetween('end', [$start, $end])->get();

        $events = [];

        foreach ($jobs as $job) {
            // IS SURVEY
            if ($job->is_survey) {
                // Active
                if (( ! $job->completed) && $job->end->gte(Carbon::now())) {
                    $events[] = \Calendar::event(
                        $job->name, //event title
                        $job->all_day, //full day event?
                        ($job->all_day) ? $job->start->format('Y-m-d') : $job->start,
                        ($job->all_day) ? $job->end->format('Y-m-d') : $job->end,
                        null, //optionally, you can specify an event ID
                        [
                            'url' => route('customers.jobs.show', [$job->customer_id, $job->id]),
                            'color' => '#6c757d',
                            'borderColor' => '#6c757d',
                            'textColor' => '#fff',

                        ]
                    );
                }
                // Completed
                if ($job->completed) {
                    $events[] = \Calendar::event(
                        $job->name, //event title
                        $job->all_day, //full day event?
                        ($job->all_day) ? $job->start->format('Y-m-d') : $job->start,
                        ($job->all_day) ? $job->end->format('Y-m-d') : $job->end,
                        null, //optionally, you can specify an event ID
                        [
                            'url' => route('customers.jobs.show', [$job->customer_id, $job->id]),
                            'color' => '#fff',
                            'borderColor' => '#6c757d',
                            'textColor' => '#6c757d',
                        ]
                    );
                }
                // Past Due
                if (( ! $job->completed) && $job->end->lt(Carbon::now())) {
                    $events[] = \Calendar::event(
                        $job->name, //event title
                        $job->all_day, //full day event?
                        ($job->all_day) ? $job->start->format('Y-m-d') : $job->start,
                        ($job->all_day) ? $job->end->format('Y-m-d') : $job->end,
                        null, //optionally, you can specify an event ID
                        [
                            'url' => route('customers.jobs.show', [$job->customer_id, $job->id]),
                            'color' => '#6c757d',
                            'borderColor' => '#dc3545',
                            'textColor' => '#fff',
                        ]
                    );
                }
            } else {
            // IS JOB
                // Active
                if (( ! $job->completed) && $job->end->gte(Carbon::now())) {
                    $events[] = \Calendar::event(
                        $job->name, //event title
                        $job->all_day, //full day event?
                        ($job->all_day) ? $job->start->format('Y-m-d') : $job->start,
                        ($job->all_day) ? $job->end->format('Y-m-d') : $job->end,
                        null, //optionally, you can specify an event ID
                        [
                            'url' => route('customers.jobs.show', [$job->customer_id, $job->id]),
                            'color' => '#007bff',
                            'borderColor' => '#007bff',
                            'textColor' => '#fff',
                        ]
                    );
                }
                // Completed
                if ($job->completed) {
                    $events[] = \Calendar::event(
                        $job->name, //event title
                        $job->all_day, //full day event?
                        ($job->all_day) ? $job->start->format('Y-m-d') : $job->start,
                        ($job->all_day) ? $job->end->format('Y-m-d') : $job->end,
                        null, //optionally, you can specify an event ID
                        [
                            'url' => route('customers.jobs.show', [$job->customer_id, $job->id]),
                            'color' => '#fff',
                            'borderColor' => '#007bff',
                            'textColor' => '#007bff',
                        ]
                    );
                }
                // Past Due
                if (( ! $job->completed) && $job->end->lt(Carbon::now())) {
                    $events[] = \Calendar::event(
                        $job->name, //event title
                        $job->all_day, //full day event?
                        ($job->all_day) ? $job->start->format('Y-m-d') : $job->start,
                        ($job->all_day) ? $job->end->format('Y-m-d') : $job->end,
                        null, //optionally, you can specify an event ID
                        [
                            'url' => route('customers.jobs.show', [$job->customer_id, $job->id]),
                            'color' => '#007bff',
                            'borderColor' => '#dc3545',
                            'textColor' => '#fff',
                        ]
                    );
                }
            }
        }

        $calendar = \Calendar::addEvents($events)
                    ->setOptions([
                        'header' => [
                            'left' => 'prev,next today',
                            'center' => 'title',
                            'right' => ''
                        ],
                    ])
                    ->setCallbacks([
                        'eventRender' => 'function eventRender(event,element,view) {
                            element.prop("title", event.title)
                        }',
                        'events' => '"' . route('jobs') . '"'
                    ]);

        if ($request->ajax()) {
            return $calendar->eventsToJson();
        }

        return view('customers.jobs.index', compact(['calendar','pastDue']));
    }

That way it will load the view, the calendar, and get new data on month changes.

Kryptonit3-zz avatar May 12 '18 15:05 Kryptonit3-zz