HEX
Server: Apache
System: Linux WWW 6.1.0-40-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.153-1 (2025-09-20) x86_64
User: web11 (1011)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/ivs.kaunokolegija.lt/laravel/app/Http/Controllers/GoogleCalendarController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Google\Client;
use Google\Service\Calendar;
use Google\Service\Calendar\Event;

class GoogleCalendarController extends Controller
{

    public function authorizeGoogleCalendar()
    {
        $client = new Client();
        $client->setClientId(get_setting('google_calendar_client_id'));
        $client->setClientSecret(get_setting('google_calendar_client_secret'));
        $client->setRedirectUri(route('google.calendar.callback'));
        $client->addScope(\Google\Service\Calendar::CALENDAR);

        return redirect($client->createAuthUrl());
    }

    public function handleGoogleCallback(Request $request)
    {
        $client = new Client();
        $client->setClientId(get_setting('google_calendar_client_id'));
        $client->setClientSecret(get_setting('google_calendar_client_secret'));
        $client->setRedirectUri(route('google.calendar.callback'));
        $client->authenticate($request->input('code'));

        $accessToken = $client->getAccessToken();

        \App\Models\Setting::updateOrCreate(
            ['key' => 'google_calendar_token'],
            ['value' => json_encode($accessToken)]
        );

        return redirect()->route('admin.future-activity-plannings.index')->with('success', 'Google Calendar connected successfully!');
    }

    public function syncToGoogleCalendar(FutureActivityPlanning $activity)
    {
        $client = new Client();
        $client->setAccessToken(json_decode(get_setting('google_calendar_token'), true));

        $service = new Calendar($client);

        $event = new Event([
            'summary' => $activity->name,
            'start' => ['dateTime' => $activity->activity_start, 'timeZone' => 'UTC'],
            'end' => ['dateTime' => $activity->activity_end, 'timeZone' => 'UTC'],
        ]);

        $service->events->insert('primary', $event);
    }

}