裸摩/laravel-google-calendar-with-update-notifications

管理Google日历上的事件,带有更新通知

2.6.7 2021-07-08 08:14 UTC

README

Latest Version on Packagist Software License Quality Score StyleCI Total Downloads

关于此分支的说明

此包是一个简单的分支,允许向GoogleCalendar事件的更新方法传递选项参数。它允许在更新时启用通知,例如。如果您不需要此功能,请访问spatie/laravel-google-calendar

此包使与Google日历一起工作变得轻松。一旦设置完毕,您就可以执行以下操作

use Spatie\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();

// update existing event
$firstEvent = $events->first();
$firstEvent->name = 'updated name';
$firstEvent->save();

$firstEvent->update(['name' => 'updated again']);

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

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

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

支持我们

我们在创建最佳开源包上投入了大量资源。您可以通过购买我们的付费产品之一来支持我们。

我们非常感谢您从您的家乡寄来明信片,说明您正在使用我们的哪个包。您可以在我们的联系方式页面上找到我们的地址。我们将在我们的虚拟明信片墙上发布所有收到的明信片。

安装

您可以通过composer安装此包

composer require spatie/laravel-google-calendar

您必须使用以下命令发布配置

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

这将发布一个名为 google-calendar.php 的文件到您的配置目录,其中包含以下内容

return [
    /*
     * Path to the json file containing the credentials.
     */
    'service_account_credentials_json' => storage_path('app/google-calendar/service-account-credentials.json'),

    /*
     *  The id of the Google Calendar that will be used by default.
     */
    'calendar_id' => env('GOOGLE_CALENDAR_ID'),
];

如何获取与Google日历通信的凭证

首先,您需要获取用于使用Google API的凭证。假设您已经创建了一个Google账户并已登录。转到Google API控制台并在页眉中点击“选择项目”。

1

接下来,我们必须指定项目可以消费哪些API。从页眉中选择“启用API和服务”。

2

在下一页,搜索“日历”并从列表中选择“Google日历API”。

3

从这里,按“启用”以启用此项目的Google日历API。

4

现在,您已经创建了一个可以访问日历API的项目,是时候下载包含这些凭证的文件了。在侧边栏中点击“凭证”,然后点击“API和服务中的凭证”链接。

5

在此页面上,打开“创建凭证”下拉菜单并选择“服务帐户密钥”。

6

在下一个屏幕上,您可以给服务帐户命名。您可以命名任何您想要的名称。在服务帐户ID中,您将看到一个电子邮件地址。我们将在本指南的后面使用此电子邮件地址。选择“JSON”作为密钥类型,然后单击“创建”以下载JSON文件。您将收到一个警告,表示服务帐户没有角色,您可以安全忽略此警告并创建服务帐户而不分配角色。

7

将json保存在您的Laravel项目中,位置在包配置文件的service_account_credentials_json键中指定。由于json文件包含可能敏感的信息,我不建议将其提交到您的git仓库。

现在API站点上的所有设置都已经完成,我们还需要在Google日历网站上配置一些东西。转到Google日历,查看您想通过PHP操作的日历的设置。在“与特定人员共享”选项卡中,点击“添加人员”按钮,并添加在API网站上创建凭据时显示的服务帐户ID。

8

9

滚动到“集成日历”部分,查看日历的ID。您需要在配置文件中指定该ID。

10

用法

获取事件

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

函数的完整签名是

public static function get(Carbon $startDateTime = null, Carbon $endDateTime = null, array $queryParameters = [], string $calendarId = null): Collection

您可以在 $queryParameters 中传递的参数列表可在 Google日历API文档中有关list的文档中找到。

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

您可以使用这些getter来检索开始和结束日期,作为 Carbon 实例

$events = Event::get();

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

创建事件

您可以直接创建一个 Spatie\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();

您可以根据简单的文本字符串创建事件,如下所示

$event = new Event();

$event->quickSave('Appointment at Somewhere on April 25 10am-10:25am');

// statically
Event::quickCreate('Appointment at Somewhere on April 25 10am-10:25am');

获取单个事件

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

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

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

Event::find($eventId);

更新事件

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

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

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

或者,您可以使用更新方法

$event = Event::find($eventId)

$event->update(['name' => 'My updated title']);

删除事件

没有其他事情!

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

$event->delete();

限制

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

从v1升级到v2

v1v2 之间的唯一主要区别在于,底层使用的是Google API v2而不是v1。以下是升级所需的步骤

  • 将配置文件重命名为 laravel-google-calendargoogle-calendar
  • 在配置文件中,将 client_secret_json 键重命名为 service_account_credentials_json

变更日志

请参阅 CHANGELOG 了解最近更改的更多信息。

测试

composer test

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全

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

鸣谢

非常感谢 Sebastiaan Luca 在创建此包的v2版本中所提供的巨大帮助。

许可

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