カレンダーイベントの取得
以下、イベントの取得部分にかかわるサンプルコードです。
// カレンダーリストごとに処理
foreach ($listFeed as $list) {
// カレンダーリストのidの取得
$listId = $list->id;
// カレンダーのユーザー名取得
$user = substr($listId, strrpos($listId, "/") + 1);
// クエリにパラメータ設定【 1 】
$query = $serviceCal->newEventQuery();
$query->setUser($user);
$query->setVisibility('private');
$query->setProjection('full');
$query->setOrderby('starttime');
$query->setSortorder('descend');
// イベントフィードの取得【 2 】
try {
$eventFeed = $serviceCal->getCalendarEventFeed($query);
print $eventFeed->getEditLink();
} catch (Zend_Gdata_App_Exception $e) {
echo "エラー: " . $e->getMessage();
}
// 取得したイベントの数だけループ
foreach ($eventFeed as $event) {
// 一つのイベントフィードから必要な情報を取得【 3 】
$date = date('Y/m/d', strtotime($event->when[0]->startTime));
$startTime = date('H:i:s', strtotime($event->when[0]->startTime));
$endTime = date('H:i:s', strtotime($event->when[0]->endTime));
$title = $event->title;
$eventId = $event->id;
$author = $event->content;
// htmlテーブルに取得した情報を表示
echo "<form method='POST'>";
echo "<tr>";
echo "<td>" . $date . "</td>";
echo "<td>" . $startTime . "</td>";
echo "<td>" . $endTime . "</td>";
echo "<td>" . $title . "</td>";
echo "<td>" . $author . "</td>";
echo "<td><button type='submit' name='updateEvent'>編集</button></td>";
echo "<td><button type='submit' name='deleteEvent'>削除</button></td>";
echo "</tr>";
echo "<input type='hidden' name='targetEventId' value='" . $eventId . "'>";
echo "<input type='hidden' name='targetTitle' value='" . $title . "'>";
echo "</form>";
}
echo "</table>";
echo "<br />";
echo "<br />";
}
クエリにパラメータ設定【 1 】
カレンダーイベントを取得するために、Zend_Gdata_EventQueryオブジェクトを作成します。これは欲しいイベントを要求するためのクエリURLを作成するためのオブジェクトです。
// クエリにパラメータ設定【 1 】
$query = $serviceCal->newEventQuery();
$query->setUser($user);
$query->setVisibility('private');
$query->setProjection('full');
$query->setOrderby('starttime');
$query->setSortorder('descend');
上記パラメータをセットすることで次のようなURLが生成されます。これは「$query->getQueryUrl()」を実行することで確認できます。
http://www.google.com/calendar/feeds/【$user】/private/full?orderby=starttime&sortorder=descend
上記ユーザー部分($user)ですが、ここでカレンダーリストを識別するための文字列が入ります。これはブラウザからアクセスするGoogleカレンダーサービスの画面では「カレンダーID」として確認できます。Googleカレンダーにアクセスし、[マイカレンダーの下部にある設定リンク]-[各カレンダー名のリンク]-[カレンダーのアドレス欄]で確認できます)。
通常は'default'を指定することで現在ログインしているユーザーのカレンダーリストを指定しますが、今回のサンプルでは複数のカレンダーリストを扱っているため、カレンダーリストの識別が必要になります。$userにあたる文字列はカレンダーリストのidから抽出しています。
// カレンダーリストのidの取得 $listId = $list->id; // カレンダーのユーザー名取得 $user = substr($listId, strrpos($listId, "/") + 1);
$listIdには「http://www.google.com/calendar/feeds/default/【カレンダーを識別するID】」が入っています。カレンダーを識別する名前を抽出するためにこの文字列の最後の「/」の位置を特定し、それ以降を文字列として切り出して$userに格納しています。
クエリはZend_Gdata_Queryオブジェクトとして表され、そのサブクラスを使用することでさまざまな要求を行うことができます。上記含め主なクエリパラメータを紹介します。
| クエリパラメータ | 内容 | 引数 | 設定内容 |
|---|---|---|---|
| setFutureevents | 未来のイベントのみ表示するか | true | 未来のみ表示 |
| false(デフォルト値) | 過去も表示 | ||
| setOrderby | フィードの並び順 | starttime | フィードの開始日時順 |
| lastmodified(デフォルト値) | 更新順 | ||
| setProjection | 取得したいデータの種類や量 | full | すべて |
| basic | メタデータ情報を各イベントのcontentフィールドの可読形式で格納 | ||
| composite | fullとほとんど同じだがコメント情報も含める | ||
| setQuery | 指定した文字列を含むイベントを取得 | 文字列 | - |
| setSortorder | フィードのソート順 | descend(デフォルト値) | 日付の新しい順 |
| ascend | 日付の古い順 | ||
| setStartMax | 指定した日付を除きこれより古いイベントを取得 | 日付 | - |
| setStartMin | 指定した日時を含めこれより新しいイベントを取得 | 日付 | - |
| setUser | どのユーザーのカレンダーを検索するか | defaul(デフォルト値) | 現在ログインしているユーザー |
| ユーザー名 | その指定したユーザー | ||
| setVisibility | カレンダーの種類 | public | 公開カレンダー |
| private | 非公開カレンダー |
クエリパラメータにはこれ以外にもGoogleカレンダーに特化したパラメータや、全サービスで共通のパラメータなどさまざまあります。
イベントフィードの取得【 2 】
Zend_Gdata_CalenderオブジェクトのgetCalendarEventFeedメソッドの引数に、先ほどのクエリオブジェクトを与えることで、イベントフィードであるZend_Gdata_Calendar_EventFeedオブジェクトを取得できます。
try {
$eventFeed = $serviceCal->getCalendarEventFeed($query);
print $eventFeed->getEditLink();
} catch (Zend_Gdata_App_Exception $e) {
echo "エラー: " . $e->getMessage();
}
getCalendarEventFeedメソッドの引数にはEventQueryオブジェクトでなく、それによって生成されるURLにあたる文字列を引数として与えても構いません。
EventFeedオブジェクトに格納されるデータはブラウザでクエリURLを入力することでXMLファイルとして確認できます。
<feed>
<id>http://www.google.com/calendar/feeds/default/private/full</id>
<updated>2009-02-13T14:21:06.000Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#event"/>
<title type="text">【Googleのアカウント】</title>
<subtitle type="text">【Googleのアカウント】</subtitle>
<link rel="alternate" type="text/html" href="http://www.google.com/calendar/embed?src=【Googleのアカウント】"/>
<link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://www.google.com/calendar/feeds/default/private/full"/>
<link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="http://www.google.com/calendar/feeds/default/private/full/batch"/>
<link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/default/private/full?max-results=25&sortorder=descend&orderby=starttime"/>
<author>
<name>【Googleに登録したユーザー名】</name>
<email>【Googleのアカウント】</email>
</author>
<generator version="1.0" uri="http://www.google.com/calendar">Google Calendar</generator>
<openSearch:totalResults>5</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<gCal:timezone value="Asia/Tokyo"/>
<gCal:timesCleaned value="0"/>
<entry>
<id>http://www.google.com/calendar/feeds/default/private/full/【ID】</id>
<published>2009-02-13T14:18:16.000Z</published>
<updated>2009-02-13T14:18:16.000Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#event"/>
<title type="text">ネットワーク勉強会</title>
<content type="text">小林</content>
<link rel="alternate" type="text/html" href="http://www.google.com/calendar/event?eid=【イベントID】" title="alternate"/>
<link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/default/private/full/【ID】"/>
<author>
<name>【Googleに登録したユーザー名】</name>
<email>【Googleのアカウント】</email>
</author>
<gd:comments>
<gd:feedLink href="http://www.google.com/calendar/feeds/default/private/full/【ID】/comments"/>
</gd:comments>
<gd:eventStatus value="http://schemas.google.com/g/2005#event.confirmed"/>
<gd:visibility value="http://schemas.google.com/g/2005#event.default"/>
<gd:transparency value="http://schemas.google.com/g/2005#event.opaque"/>
<gCal:uid value="【ID】@google.com"/>
<gCal:sequence value="0"/>
<gd:when startTime="2009-02-13T16:00:00.000+09:00" endTime="2009-02-13T17:00:00.000+09:00">
<gd:reminder minutes="10" method="email"/>
<gd:reminder minutes="10" method="alert"/>
</gd:when>
<gd:who rel="http://schemas.google.com/g/2005#event.organizer" valueString="【Googleに登録したユーザー名】" email="【Googleのアカウント】"/>
<gd:where valueString=""/>
</entry>
(entryタブの繰り返し)
</feed>
一つのイベントフィードから必要な情報を取得【 3 】
取得したイベントフィードの一つのエントリから必要となる情報を取得します。今回はイベントの対象日、開始時間、終了時間、タイトル、イベントのID、登録者(Googleカレンダーサービス上は詳細欄ですが、今回はここをイベント登録者として使用します)を取得します。
イベントフィードであるZend_Gdata_Calendar_EventFeedオブジェクトにはイベントであるZend_Gdata_Calendar_Eventオブジェクトが配列として格納されていますので、一つ一つforeach文にて取得します。
// 取得したイベントの数だけループ
foreach ($eventFeed as $event) {
// 一つのイベントフィードから必要な情報を取得【 3 】
$date = date('Y/m/d', strtotime($event->when[0]->startTime));
$startTime = date('H:i:s', strtotime($event->when[0]->startTime));
$endTime = date('H:i:s', strtotime($event->when[0]->endTime));
$title = $event->title;
$eventId = $event->id;
$author = $event->content;
// htmlテーブルに取得した情報を表示
(略)
}
各イベントであるZend_Gdata_Calendar_Eventオブジェクトには、そのイベントの多数の情報が格納されています。「イベントオブジェクト->プロパティ」で指定することで次のような情報を取得できます。
| プロパティ | 内容 |
|---|---|
| author | 登録者 |
| content | 詳細 |
| id | イベントのID |
| title | タイトル。必須項目 |
| visibility | 公開か非公開か |
| when | 期間。必須項目 |
| where | 場所 |
また、whenプロパティについてはさらにサブプロパティがあります。
| プロパティ | 内容 |
|---|---|
| startTime | RFC3339形式で指定する開始時刻 |
| endTime | RFC3339形式で指定する終了時刻 |
| valueString | 文字列で指定する期間("This Afternoon"など) |
| reminders | リマインダ |
remindersプロパティにもサブプロパティがあります。リマインダそのものは一つのイベントにつき5つまで指定することができますが、時間に関するminuite、hours、days、absoluteTimeはどれか一つに統一する必要があります。
| プロパティ | 内容 |
|---|---|
| method | 通知方法。alert、emailもしくはsms |
| minutes | 何分前に送信するか |
| hours | 何時間前に送信するか |
| days | 何日前に送信するか |
| absoluteTime | 指定した時刻 |
