codegis/google-calendar-laravel

管理 Google Calendar 上的事件

2.0.2 2017-07-08 15:27 UTC

This package is not auto-updated.

Last update: 2024-09-29 02:39:16 UTC


README

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score StyleCI Total Downloads

此包使使用 Google Calendar 变得轻松。设置完成后,您可以执行以下操作

use Codegis\GoogleCalendar\Event;

//create a new event
$event = new Event;

$event->name = 'A new event';
$event->startDateTime = Carbon\Carbon::now();
$event->endDateTime = Carbon\Carbon::now()->addHour();
$event->addAttendee(['email' => 'youremail@gmail.com']);
$event->addAttendee(['email' => 'anotherEmail@gmail.com']);

$event->save();

// get all future events on a calendar
$events = Event::get();

$firstEvent = $events->first();
$firstEvent->name = 'updated name';
$firstEvent->save();

// create a new event
Event::create([
   'name' => 'A new event'
   'startDateTime' => Carbon\Carbon::now(),
   'endDateTime' => Carbon\Carbon::now()->addHour(),
]);

// delete an event
$event->delete();

Codegis 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到所有开源项目的概述 这里

明信片软件

您可以使用此包(它是 MIT 许可),但如果它进入您的生产环境,您需要向我们寄来一张您家乡的明信片,说明您正在使用我们的哪个包。

我们的地址是:Codegis,Samberstraat 69D,2060 安特卫普,比利时。

我们将在网站上发布最好的明信片。

安装

您可以通过 composer 安装此包

composer require spatie/laravel-google-calendar

接下来,必须注册服务提供商

'providers' => [
    ...
    Codegis\GoogleCalendar\GoogleCalendarServiceProvider::class,
];

可选地,必须注册 Codegis\GoogleCalendar\GoogleCalendarFacade

'aliases' => [
	...
    'GoogleCalendar' => Codegis\GoogleCalendar\GoogleCalendarFacade::class,
    ...
]

您必须使用此命令发布配置

php artisan vendor:publish --provider="Codegis\GoogleCalendar\GoogleCalendarServiceProvider"

这将发布位于您的 config 目录中的名为 laravel-google-calendar.php 的文件,其内容如下

<?php

return [

    /**
     * Path to a json file containing the credentials of a Google Service account.
     */
    'client_secret_json' => storage_path('app/laravel-google-calendar/client_secret.json'),

    /**
     *  The id of the Google Calendar that will be used by default.
     */
    'calendar_id' => '',

];

阅读 此博客文章 了解如何获取 client_secret_jsoncalendar_id 的正确值。

用法

获取事件

您可以通过简单地调用 Event::get(); 来获取所有事件,这将返回下一年内的所有事件。事件以 Codegis\GoogleCalendar\Event 对象的形式出现。

函数的完整签名是

/**
 * @param \Carbon\Carbon|null $startDateTime
 * @param \Carbon\Carbon|null $endDateTime
 * @param array $queryParameters
 * @param string|null $calendarId
 *
 * @return \Illuminate\Support\Collection
 */
public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null) : Collection

您可以在 $queryParameters 中传递的参数在 Google 日历 API 文档中的 list 请求 上列出。

访问事件的开始和结束日期

您可以使用这些获取器以 Carbon 实例的形式检索开始和结束日期

$events = Event::get();

$event[0]->startDate;
$event[0]->startDateTime;
$event[0]->endDate;
$event[0]->endDateTime;

创建一个事件

您只需创建一个 Codegis\GoogleCalendar\Event-对象

$event = new Event;

$event->name = 'A new event';
$event->startDateTime = Carbon\Carbon::now();
$event->endDateTime = Carbon\Carbon::now()->addHour();

$event->save();

您也可以调用 create 静态方法

Event::create([
   'name' => 'A new event',
   'startDateTime' => Carbon\Carbon::now(),
   'endDateTime' => Carbon\Carbon::now()->addHour(),
]);

这将创建一个具有特定开始和结束时间的事件。如果您想创建一个全天事件,必须使用 startDateendDate 而不是 startDateTimeendDateTime

$event = new Event;

$event->name = 'A new full day event';
$event->startDate = Carbon\Carbon::now();
$event->endDate = Carbon\Carbon::now()->addDay();

$event->save();

获取单个事件

Google 为每个单个事件分配一个唯一的 ID。您可以通过使用 get 方法获取事件并获取 Codegis\GoogleCalendar\Event-对象上的 id 属性来获取此 ID

// get the id of the first upcoming event in the calendar.
$calendarId = Event::get()->first()->id;

您可以使用此 ID 从 Google 获取单个事件

Event::find($calendarId);

更新一个事件

很简单,只需更改一些属性并调用 save()

$event = Event::find($eventId);

$event->name = 'My updated title';
$event->save();

删除一个事件

没有其他事情可做!

$event = Event::find($eventId);

$event->delete();

限制

Google 日历 API 提供了许多选项。此包不支持所有这些选项。例如,使用此包无法正确管理重复事件。如果您坚持只创建具有名称和日期的事件,那么您应该没问题。

更改日志

请参阅 更改日志 了解最近发生了什么。

测试

$ composer test

贡献

请参阅 贡献指南 了解详细信息。

安全性

如果您发现任何与安全相关的问题,请通过电子邮件发送至 freek@spatie.be,而不是使用问题跟踪器。

致谢

关于Codegis

Codegis 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到所有开源项目的概述 这里

许可证

MIT许可证(MIT)。更多信息请参阅许可证文件