Calendar.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Demos
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * PHP sample code for the Google Calendar data API. Utilizes the
  23. * Zend Framework Gdata components to communicate with the Google API.
  24. *
  25. * Requires the Zend Framework Gdata components and PHP >= 5.2.11
  26. *
  27. * You can run this sample both from the command line (CLI) and also
  28. * from a web browser. When running through a web browser, only
  29. * AuthSub and outputting a list of calendars is demonstrated. When
  30. * running via CLI, all functionality except AuthSub is available and dependent
  31. * upon the command line options passed. Run this script without any
  32. * command line options to see usage, eg:
  33. * /usr/local/bin/php -f Calendar.php
  34. *
  35. * More information on the Command Line Interface is available at:
  36. * http://www.php.net/features.commandline
  37. *
  38. * NOTE: You must ensure that the Zend Framework is in your PHP include
  39. * path. You can do this via php.ini settings, or by modifying the
  40. * argument to set_include_path in the code below.
  41. *
  42. * NOTE: As this is sample code, not all of the functions do full error
  43. * handling. Please see getEvent for an example of how errors could
  44. * be handled and the online code samples for additional information.
  45. */
  46. /**
  47. * @see Zend_Loader
  48. */
  49. require_once 'Zend/Loader.php';
  50. /**
  51. * @see Zend_Gdata
  52. */
  53. Zend_Loader::loadClass('Zend_Gdata');
  54. /**
  55. * @see Zend_Gdata_AuthSub
  56. */
  57. Zend_Loader::loadClass('Zend_Gdata_AuthSub');
  58. /**
  59. * @see Zend_Gdata_ClientLogin
  60. */
  61. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  62. /**
  63. * @see Zend_Gdata_HttpClient
  64. */
  65. Zend_Loader::loadClass('Zend_Gdata_HttpClient');
  66. /**
  67. * @see Zend_Gdata_Calendar
  68. */
  69. Zend_Loader::loadClass('Zend_Gdata_Calendar');
  70. /**
  71. * @var string Location of AuthSub key file. include_path is used to find this
  72. */
  73. $_authSubKeyFile = null; // Example value for secure use: 'mykey.pem'
  74. /**
  75. * @var string Passphrase for AuthSub key file.
  76. */
  77. $_authSubKeyFilePassphrase = null;
  78. /**
  79. * Returns the full URL of the current page, based upon env variables
  80. *
  81. * Env variables used:
  82. * $_SERVER['HTTPS'] = (on|off|)
  83. * $_SERVER['HTTP_HOST'] = value of the Host: header
  84. * $_SERVER['SERVER_PORT'] = port number (only used if not http/80,https/443)
  85. * $_SERVER['REQUEST_URI'] = the URI after the method of the HTTP request
  86. *
  87. * @return string Current URL
  88. */
  89. function getCurrentUrl()
  90. {
  91. global $_SERVER;
  92. /**
  93. * Filter php_self to avoid a security vulnerability.
  94. */
  95. $php_request_uri = htmlentities(substr($_SERVER['REQUEST_URI'], 0, strcspn($_SERVER['REQUEST_URI'], "\n\r")), ENT_QUOTES);
  96. if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
  97. $protocol = 'https://';
  98. } else {
  99. $protocol = 'http://';
  100. }
  101. $host = $_SERVER['HTTP_HOST'];
  102. if ($_SERVER['SERVER_PORT'] != '' &&
  103. (($protocol == 'http://' && $_SERVER['SERVER_PORT'] != '80') ||
  104. ($protocol == 'https://' && $_SERVER['SERVER_PORT'] != '443'))) {
  105. $port = ':' . $_SERVER['SERVER_PORT'];
  106. } else {
  107. $port = '';
  108. }
  109. return $protocol . $host . $port . $php_request_uri;
  110. }
  111. /**
  112. * Returns the AuthSub URL which the user must visit to authenticate requests
  113. * from this application.
  114. *
  115. * Uses getCurrentUrl() to get the next URL which the user will be redirected
  116. * to after successfully authenticating with the Google service.
  117. *
  118. * @return string AuthSub URL
  119. */
  120. function getAuthSubUrl()
  121. {
  122. global $_authSubKeyFile;
  123. $next = getCurrentUrl();
  124. $scope = 'http://www.google.com/calendar/feeds/';
  125. $session = true;
  126. if ($_authSubKeyFile != null) {
  127. $secure = true;
  128. } else {
  129. $secure = false;
  130. }
  131. return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure,
  132. $session);
  133. }
  134. /**
  135. * Outputs a request to the user to login to their Google account, including
  136. * a link to the AuthSub URL.
  137. *
  138. * Uses getAuthSubUrl() to get the URL which the user must visit to authenticate
  139. *
  140. * @return void
  141. */
  142. function requestUserLogin($linkText)
  143. {
  144. $authSubUrl = getAuthSubUrl();
  145. echo "<a href=\"{$authSubUrl}\">{$linkText}</a>";
  146. }
  147. /**
  148. * Returns a HTTP client object with the appropriate headers for communicating
  149. * with Google using AuthSub authentication.
  150. *
  151. * Uses the $_SESSION['sessionToken'] to store the AuthSub session token after
  152. * it is obtained. The single use token supplied in the URL when redirected
  153. * after the user succesfully authenticated to Google is retrieved from the
  154. * $_GET['token'] variable.
  155. *
  156. * @return Zend_Http_Client
  157. */
  158. function getAuthSubHttpClient()
  159. {
  160. global $_SESSION, $_GET, $_authSubKeyFile, $_authSubKeyFilePassphrase;
  161. $client = new Zend_Gdata_HttpClient();
  162. if ($_authSubKeyFile != null) {
  163. // set the AuthSub key
  164. $client->setAuthSubPrivateKeyFile($_authSubKeyFile, $_authSubKeyFilePassphrase, true);
  165. }
  166. if (!isset($_SESSION['sessionToken']) && isset($_GET['token'])) {
  167. $_SESSION['sessionToken'] =
  168. Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token'], $client);
  169. }
  170. $client->setAuthSubToken($_SESSION['sessionToken']);
  171. return $client;
  172. }
  173. /**
  174. * Processes loading of this sample code through a web browser. Uses AuthSub
  175. * authentication and outputs a list of a user's calendars if succesfully
  176. * authenticated.
  177. *
  178. * @return void
  179. */
  180. function processPageLoad()
  181. {
  182. global $_SESSION, $_GET;
  183. if (!isset($_SESSION['sessionToken']) && !isset($_GET['token'])) {
  184. requestUserLogin('Please login to your Google Account.');
  185. } else {
  186. $client = getAuthSubHttpClient();
  187. outputCalendarList($client);
  188. }
  189. }
  190. /**
  191. * Returns a HTTP client object with the appropriate headers for communicating
  192. * with Google using the ClientLogin credentials supplied.
  193. *
  194. * @param string $user The username, in e-mail address format, to authenticate
  195. * @param string $pass The password for the user specified
  196. * @return Zend_Http_Client
  197. */
  198. function getClientLoginHttpClient($user, $pass)
  199. {
  200. $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
  201. $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  202. return $client;
  203. }
  204. /**
  205. * Outputs an HTML unordered list (ul), with each list item representing an event
  206. * in the user's calendar. The calendar is retrieved using the magic cookie
  207. * which allows read-only access to private calendar data using a special token
  208. * available from within the Calendar UI.
  209. *
  210. * @param string $user The username or address of the calendar to be retrieved.
  211. * @param string $magicCookie The magic cookie token
  212. * @return void
  213. */
  214. function outputCalendarMagicCookie($user, $magicCookie)
  215. {
  216. $gdataCal = new Zend_Gdata_Calendar();
  217. $query = $gdataCal->newEventQuery();
  218. $query->setUser($user);
  219. $query->setVisibility('private-' . $magicCookie);
  220. $query->setProjection('full');
  221. $eventFeed = $gdataCal->getCalendarEventFeed($query);
  222. echo "<ul>\n";
  223. foreach ($eventFeed as $event) {
  224. echo "\t<li>" . $event->title->text . "</li>\n";
  225. $sl = $event->getLink('self')->href;
  226. }
  227. echo "</ul>\n";
  228. }
  229. /**
  230. * Outputs an HTML unordered list (ul), with each list item representing a
  231. * calendar in the authenticated user's calendar list.
  232. *
  233. * @param Zend_Http_Client $client The authenticated client object
  234. * @return void
  235. */
  236. function outputCalendarList($client)
  237. {
  238. $gdataCal = new Zend_Gdata_Calendar($client);
  239. $calFeed = $gdataCal->getCalendarListFeed();
  240. echo "<h1>" . $calFeed->title->text . "</h1>\n";
  241. echo "<ul>\n";
  242. foreach ($calFeed as $calendar) {
  243. echo "\t<li>" . $calendar->title->text . "</li>\n";
  244. }
  245. echo "</ul>\n";
  246. }
  247. /**
  248. * Outputs an HTML unordered list (ul), with each list item representing an
  249. * event on the authenticated user's calendar. Includes the start time and
  250. * event ID in the output. Events are ordered by starttime and include only
  251. * events occurring in the future.
  252. *
  253. * @param Zend_Http_Client $client The authenticated client object
  254. * @return void
  255. */
  256. function outputCalendar($client)
  257. {
  258. $gdataCal = new Zend_Gdata_Calendar($client);
  259. $query = $gdataCal->newEventQuery();
  260. $query->setUser('default');
  261. $query->setVisibility('private');
  262. $query->setProjection('full');
  263. $query->setOrderby('starttime');
  264. $query->setFutureevents(true);
  265. $eventFeed = $gdataCal->getCalendarEventFeed($query);
  266. // option 2
  267. // $eventFeed = $gdataCal->getCalendarEventFeed($query->getQueryUrl());
  268. echo "<ul>\n";
  269. foreach ($eventFeed as $event) {
  270. echo "\t<li>" . $event->title->text . " (" . $event->id->text . ")\n";
  271. // Zend_Gdata_App_Extensions_Title->__toString() is defined, so the
  272. // following will also work on PHP >= 5.2.0
  273. //echo "\t<li>" . $event->title . " (" . $event->id . ")\n";
  274. echo "\t\t<ul>\n";
  275. foreach ($event->when as $when) {
  276. echo "\t\t\t<li>Starts: " . $when->startTime . "</li>\n";
  277. }
  278. echo "\t\t</ul>\n";
  279. echo "\t</li>\n";
  280. }
  281. echo "</ul>\n";
  282. }
  283. /**
  284. * Outputs an HTML unordered list (ul), with each list item representing an
  285. * event on the authenticated user's calendar which occurs during the
  286. * specified date range.
  287. *
  288. * To query for all events occurring on 2006-12-24, you would query for
  289. * a startDate of '2006-12-24' and an endDate of '2006-12-25' as the upper
  290. * bound for date queries is exclusive. See the 'query parameters reference':
  291. * http://code.google.com/apis/gdata/calendar.html#Parameters
  292. *
  293. * @param Zend_Http_Client $client The authenticated client object
  294. * @param string $startDate The start date in YYYY-MM-DD format
  295. * @param string $endDate The end date in YYYY-MM-DD format
  296. * @return void
  297. */
  298. function outputCalendarByDateRange($client, $startDate='2007-05-01',
  299. $endDate='2007-08-01')
  300. {
  301. $gdataCal = new Zend_Gdata_Calendar($client);
  302. $query = $gdataCal->newEventQuery();
  303. $query->setUser('default');
  304. $query->setVisibility('private');
  305. $query->setProjection('full');
  306. $query->setOrderby('starttime');
  307. $query->setStartMin($startDate);
  308. $query->setStartMax($endDate);
  309. $eventFeed = $gdataCal->getCalendarEventFeed($query);
  310. echo "<ul>\n";
  311. foreach ($eventFeed as $event) {
  312. echo "\t<li>" . $event->title->text . " (" . $event->id->text . ")\n";
  313. echo "\t\t<ul>\n";
  314. foreach ($event->when as $when) {
  315. echo "\t\t\t<li>Starts: " . $when->startTime . "</li>\n";
  316. }
  317. echo "\t\t</ul>\n";
  318. echo "\t</li>\n";
  319. }
  320. echo "</ul>\n";
  321. }
  322. /**
  323. * Outputs an HTML unordered list (ul), with each list item representing an
  324. * event on the authenticated user's calendar which matches the search string
  325. * specified as the $fullTextQuery parameter
  326. *
  327. * @param Zend_Http_Client $client The authenticated client object
  328. * @param string $fullTextQuery The string for which you are searching
  329. * @return void
  330. */
  331. function outputCalendarByFullTextQuery($client, $fullTextQuery='tennis')
  332. {
  333. $gdataCal = new Zend_Gdata_Calendar($client);
  334. $query = $gdataCal->newEventQuery();
  335. $query->setUser('default');
  336. $query->setVisibility('private');
  337. $query->setProjection('full');
  338. $query->setQuery($fullTextQuery);
  339. $eventFeed = $gdataCal->getCalendarEventFeed($query);
  340. echo "<ul>\n";
  341. foreach ($eventFeed as $event) {
  342. echo "\t<li>" . $event->title->text . " (" . $event->id->text . ")\n";
  343. echo "\t\t<ul>\n";
  344. foreach ($event->when as $when) {
  345. echo "\t\t\t<li>Starts: " . $when->startTime . "</li>\n";
  346. echo "\t\t</ul>\n";
  347. echo "\t</li>\n";
  348. }
  349. }
  350. echo "</ul>\n";
  351. }
  352. /**
  353. * Creates an event on the authenticated user's default calendar with the
  354. * specified event details.
  355. *
  356. * @param Zend_Http_Client $client The authenticated client object
  357. * @param string $title The event title
  358. * @param string $desc The detailed description of the event
  359. * @param string $where
  360. * @param string $startDate The start date of the event in YYYY-MM-DD format
  361. * @param string $startTime The start time of the event in HH:MM 24hr format
  362. * @param string $endDate The end date of the event in YYYY-MM-DD format
  363. * @param string $endTime The end time of the event in HH:MM 24hr format
  364. * @param string $tzOffset The offset from GMT/UTC in [+-]DD format (eg -08)
  365. * @return string The ID URL for the event.
  366. */
  367. function createEvent ($client, $title = 'Tennis with Beth',
  368. $desc='Meet for a quick lesson', $where = 'On the courts',
  369. $startDate = '2008-01-20', $startTime = '10:00',
  370. $endDate = '2008-01-20', $endTime = '11:00', $tzOffset = '-08')
  371. {
  372. $gc = new Zend_Gdata_Calendar($client);
  373. $newEntry = $gc->newEventEntry();
  374. $newEntry->title = $gc->newTitle(trim($title));
  375. $newEntry->where = array($gc->newWhere($where));
  376. $newEntry->content = $gc->newContent($desc);
  377. $newEntry->content->type = 'text';
  378. $when = $gc->newWhen();
  379. $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
  380. $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
  381. $newEntry->when = array($when);
  382. $createdEntry = $gc->insertEvent($newEntry);
  383. return $createdEntry->id->text;
  384. }
  385. /**
  386. * Creates an event on the authenticated user's default calendar using
  387. * the specified QuickAdd string.
  388. *
  389. * @param Zend_Http_Client $client The authenticated client object
  390. * @param string $quickAddText The QuickAdd text for the event
  391. * @return string The ID URL for the event
  392. */
  393. function createQuickAddEvent ($client, $quickAddText) {
  394. $gdataCal = new Zend_Gdata_Calendar($client);
  395. $event = $gdataCal->newEventEntry();
  396. $event->content = $gdataCal->newContent($quickAddText);
  397. $event->quickAdd = $gdataCal->newQuickAdd(true);
  398. $newEvent = $gdataCal->insertEvent($event);
  399. return $newEvent->id->text;
  400. }
  401. /**
  402. * Creates a new web content event on the authenticated user's default
  403. * calendar with the specified event details. For simplicity, the event
  404. * is created as an all day event and does not include a description.
  405. *
  406. * @param Zend_Http_Client $client The authenticated client object
  407. * @param string $title The event title
  408. * @param string $startDate The start date of the event in YYYY-MM-DD format
  409. * @param string $endDate The end time of the event in HH:MM 24hr format
  410. * @param string $icon URL pointing to a 16x16 px icon representing the event.
  411. * @param string $url The URL containing the web content for the event.
  412. * @param string $height The desired height of the web content pane.
  413. * @param string $width The desired width of the web content pane.
  414. * @param string $type The MIME type of the web content.
  415. * @return string The ID URL for the event.
  416. */
  417. function createWebContentEvent ($client, $title = 'World Cup 2006',
  418. $startDate = '2006-06-09', $endDate = '2006-06-09',
  419. $icon = 'http://www.google.com/calendar/images/google-holiday.gif',
  420. $url = 'http://www.google.com/logos/worldcup06.gif',
  421. $height = '120', $width = '276', $type = 'image/gif'
  422. )
  423. {
  424. $gc = new Zend_Gdata_Calendar($client);
  425. $newEntry = $gc->newEventEntry();
  426. $newEntry->title = $gc->newTitle(trim($title));
  427. $when = $gc->newWhen();
  428. $when->startTime = $startDate;
  429. $when->endTime = $endDate;
  430. $newEntry->when = array($when);
  431. $wc = $gc->newWebContent();
  432. $wc->url = $url;
  433. $wc->height = $height;
  434. $wc->width = $width;
  435. $wcLink = $gc->newLink();
  436. $wcLink->rel = "http://schemas.google.com/gCal/2005/webContent";
  437. $wcLink->title = $title;
  438. $wcLink->type = $type;
  439. $wcLink->href = $icon;
  440. $wcLink->webContent = $wc;
  441. $newEntry->link = array($wcLink);
  442. $createdEntry = $gc->insertEvent($newEntry);
  443. return $createdEntry->id->text;
  444. }
  445. /**
  446. * Creates a recurring event on the authenticated user's default calendar with
  447. * the specified event details.
  448. *
  449. * @param Zend_Http_Client $client The authenticated client object
  450. * @param string $title The event title
  451. * @param string $desc The detailed description of the event
  452. * @param string $where
  453. * @param string $recurData The iCalendar recurring event syntax (RFC2445)
  454. * @return void
  455. */
  456. function createRecurringEvent ($client, $title = 'Tennis with Beth',
  457. $desc='Meet for a quick lesson', $where = 'On the courts',
  458. $recurData = null)
  459. {
  460. $gc = new Zend_Gdata_Calendar($client);
  461. $newEntry = $gc->newEventEntry();
  462. $newEntry->title = $gc->newTitle(trim($title));
  463. $newEntry->where = array($gc->newWhere($where));
  464. $newEntry->content = $gc->newContent($desc);
  465. $newEntry->content->type = 'text';
  466. /**
  467. * Due to the length of this recurrence syntax, we did not specify
  468. * it as a default parameter value directly
  469. */
  470. if ($recurData == null) {
  471. $recurData =
  472. "DTSTART;VALUE=DATE:20070501\r\n" .
  473. "DTEND;VALUE=DATE:20070502\r\n" .
  474. "RRULE:FREQ=WEEKLY;BYDAY=Tu;UNTIL=20070904\r\n";
  475. }
  476. $newEntry->recurrence = $gc->newRecurrence($recurData);
  477. $gc->post($newEntry->saveXML());
  478. }
  479. /**
  480. * Returns an entry object representing the event with the specified ID.
  481. *
  482. * @param Zend_Http_Client $client The authenticated client object
  483. * @param string $eventId The event ID string
  484. * @return Zend_Gdata_Calendar_EventEntry|null if the event is found, null if it's not
  485. */
  486. function getEvent($client, $eventId)
  487. {
  488. $gdataCal = new Zend_Gdata_Calendar($client);
  489. $query = $gdataCal->newEventQuery();
  490. $query->setUser('default');
  491. $query->setVisibility('private');
  492. $query->setProjection('full');
  493. $query->setEvent($eventId);
  494. try {
  495. $eventEntry = $gdataCal->getCalendarEventEntry($query);
  496. return $eventEntry;
  497. } catch (Zend_Gdata_App_Exception $e) {
  498. var_dump($e);
  499. return null;
  500. }
  501. }
  502. /**
  503. * Updates the title of the event with the specified ID to be
  504. * the title specified. Also outputs the new and old title
  505. * with HTML br elements separating the lines
  506. *
  507. * @param Zend_Http_Client $client The authenticated client object
  508. * @param string $eventId The event ID string
  509. * @param string $newTitle The new title to set on this event
  510. * @return Zend_Gdata_Calendar_EventEntry|null The updated entry
  511. */
  512. function updateEvent ($client, $eventId, $newTitle)
  513. {
  514. $gdataCal = new Zend_Gdata_Calendar($client);
  515. if ($eventOld = getEvent($client, $eventId)) {
  516. echo "Old title: " . $eventOld->title->text . "<br />\n";
  517. $eventOld->title = $gdataCal->newTitle($newTitle);
  518. try {
  519. $eventOld->save();
  520. } catch (Zend_Gdata_App_Exception $e) {
  521. var_dump($e);
  522. return null;
  523. }
  524. $eventNew = getEvent($client, $eventId);
  525. echo "New title: " . $eventNew->title->text . "<br />\n";
  526. return $eventNew;
  527. } else {
  528. return null;
  529. }
  530. }
  531. /**
  532. * Adds an extended property to the event specified as a parameter.
  533. * An extended property is an arbitrary name/value pair that can be added
  534. * to an event and retrieved via the API. It is not accessible from the
  535. * calendar web interface.
  536. *
  537. * @param Zend_Http_Client $client The authenticated client object
  538. * @param string $eventId The event ID string
  539. * @param string $name The name of the extended property
  540. * @param string $value The value of the extended property
  541. * @return Zend_Gdata_Calendar_EventEntry|null The updated entry
  542. */
  543. function addExtendedProperty ($client, $eventId,
  544. $name='http://www.example.com/schemas/2005#mycal.id', $value='1234')
  545. {
  546. $gc = new Zend_Gdata_Calendar($client);
  547. if ($event = getEvent($client, $eventId)) {
  548. $extProp = $gc->newExtendedProperty($name, $value);
  549. $extProps = array_merge($event->extendedProperty, array($extProp));
  550. $event->extendedProperty = $extProps;
  551. $eventNew = $event->save();
  552. return $eventNew;
  553. } else {
  554. return null;
  555. }
  556. }
  557. /**
  558. * Adds a reminder to the event specified as a parameter.
  559. *
  560. * @param Zend_Http_Client $client The authenticated client object
  561. * @param string $eventId The event ID string
  562. * @param integer $minutes Minutes before event to set reminder
  563. * @return Zend_Gdata_Calendar_EventEntry|null The updated entry
  564. */
  565. function setReminder($client, $eventId, $minutes=15)
  566. {
  567. $gc = new Zend_Gdata_Calendar($client);
  568. $method = "alert";
  569. if ($event = getEvent($client, $eventId)) {
  570. $times = $event->when;
  571. foreach ($times as $when) {
  572. $reminder = $gc->newReminder();
  573. $reminder->setMinutes($minutes);
  574. $reminder->setMethod($method);
  575. $when->reminders = array($reminder);
  576. }
  577. $eventNew = $event->save();
  578. return $eventNew;
  579. } else {
  580. return null;
  581. }
  582. }
  583. /**
  584. * Deletes the event specified by retrieving the atom entry object
  585. * and calling Zend_Feed_EntryAtom::delete() method. This is for
  586. * example purposes only, as it is inefficient to retrieve the entire
  587. * atom entry only for the purposes of deleting it.
  588. *
  589. * @param Zend_Http_Client $client The authenticated client object
  590. * @param string $eventId The event ID string
  591. * @return void
  592. */
  593. function deleteEventById ($client, $eventId)
  594. {
  595. $event = getEvent($client, $eventId);
  596. $event->delete();
  597. }
  598. /**
  599. * Deletes the event specified by calling the Zend_Gdata::delete()
  600. * method. The URL is typically in the format of:
  601. * http://www.google.com/calendar/feeds/default/private/full/<eventId>
  602. *
  603. * @param Zend_Http_Client $client The authenticated client object
  604. * @param string $url The url for the event to be deleted
  605. * @return void
  606. */
  607. function deleteEventByUrl ($client, $url)
  608. {
  609. $gdataCal = new Zend_Gdata_Calendar($client);
  610. $gdataCal->delete($url);
  611. }
  612. /**
  613. * Main logic for running this sample code via the command line or,
  614. * for AuthSub functionality only, via a web browser. The output of
  615. * many of the functions is in HTML format for demonstration purposes,
  616. * so you may wish to pipe the output to Tidy when running from the
  617. * command-line for clearer results.
  618. *
  619. * Run without any arguments to get usage information
  620. */
  621. if (isset($argc) && $argc >= 2) {
  622. switch ($argv[1]) {
  623. case 'outputCalendar':
  624. if ($argc == 4) {
  625. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  626. outputCalendar($client);
  627. } else {
  628. echo "Usage: php {$argv[0]} {$argv[1]} " .
  629. "<username> <password>\n";
  630. }
  631. break;
  632. case 'outputCalendarMagicCookie':
  633. if ($argc == 4) {
  634. outputCalendarMagicCookie($argv[2], $argv[3]);
  635. } else {
  636. echo "Usage: php {$argv[0]} {$argv[1]} " .
  637. "<username> <magicCookie>\n";
  638. }
  639. break;
  640. case 'outputCalendarByDateRange':
  641. if ($argc == 6) {
  642. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  643. outputCalendarByDateRange($client, $argv[4], $argv[5]);
  644. } else {
  645. echo "Usage: php {$argv[0]} {$argv[1]} " .
  646. "<username> <password> <startDate> <endDate>\n";
  647. }
  648. break;
  649. case 'outputCalendarByFullTextQuery':
  650. if ($argc == 5) {
  651. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  652. outputCalendarByFullTextQuery($client, $argv[4]);
  653. } else {
  654. echo "Usage: php {$argv[0]} {$argv[1]} " .
  655. "<username> <password> <fullTextQuery>\n";
  656. }
  657. break;
  658. case 'outputCalendarList':
  659. if ($argc == 4) {
  660. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  661. outputCalendarList($client);
  662. } else {
  663. echo "Usage: php {$argv[0]} {$argv[1]} " .
  664. "<username> <password>\n";
  665. }
  666. break;
  667. case 'updateEvent':
  668. if ($argc == 6) {
  669. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  670. updateEvent($client, $argv[4], $argv[5]);
  671. } else {
  672. echo "Usage: php {$argv[0]} {$argv[1]} <username> <password> " .
  673. "<eventId> <newTitle>\n";
  674. }
  675. break;
  676. case 'setReminder':
  677. if ($argc == 6) {
  678. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  679. setReminder($client, $argv[4], $argv[5]);
  680. } else {
  681. echo "Usage: php {$argv[0]} {$argv[1]} <username> <password> " .
  682. "<eventId> <minutes>\n";
  683. }
  684. break;
  685. case 'addExtendedProperty':
  686. if ($argc == 7) {
  687. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  688. addExtendedProperty($client, $argv[4], $argv[5], $argv[6]);
  689. } else {
  690. echo "Usage: php {$argv[0]} {$argv[1]} <username> <password> " .
  691. "<eventId> <name> <value>\n";
  692. }
  693. break;
  694. case 'deleteEventById':
  695. if ($argc == 5) {
  696. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  697. deleteEventById($client, $argv[4]);
  698. } else {
  699. echo "Usage: php {$argv[0]} {$argv[1]} <username> <password> " .
  700. "<eventId>\n";
  701. }
  702. break;
  703. case 'deleteEventByUrl':
  704. if ($argc == 5) {
  705. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  706. deleteEventByUrl($client, $argv[4]);
  707. } else {
  708. echo "Usage: php {$argv[0]} {$argv[1]} <username> <password> " .
  709. "<eventUrl>\n";
  710. }
  711. break;
  712. case 'createEvent':
  713. if ($argc == 12) {
  714. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  715. $id = createEvent($client, $argv[4], $argv[5], $argv[6], $argv[7],
  716. $argv[8], $argv[9], $argv[10], $argv[11]);
  717. print "Event created with ID: $id\n";
  718. } else {
  719. echo "Usage: php {$argv[0]} {$argv[1]} <username> <password> " .
  720. "<title> <description> <where> " .
  721. "<startDate> <startTime> <endDate> <endTime> <tzOffset>\n";
  722. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <password> " .
  723. "'Tennis with Beth' 'Meet for a quick lesson' 'On the courts' " .
  724. "'2008-01-01' '10:00' '2008-01-01' '11:00' '-08'\n";
  725. }
  726. break;
  727. case 'createQuickAddEvent':
  728. if ($argc == 5) {
  729. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  730. $id = createQuickAddEvent($client, $argv[4]);
  731. print "Event created with ID: $id\n";
  732. } else {
  733. echo "Usage: php {$argv[0]} {$argv[1]} <username> <password> " .
  734. "<quickAddText>\n";
  735. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <password> " .
  736. "'Dinner at the beach on Thursday 8 PM'\n";
  737. }
  738. break;
  739. case 'createWebContentEvent':
  740. if ($argc == 12) {
  741. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  742. $id = createWebContentEvent($client, $argv[4], $argv[5], $argv[6],
  743. $argv[7], $argv[8], $argv[9], $argv[10], $argv[11]);
  744. print "Event created with ID: $id\n";
  745. } else {
  746. echo "Usage: php {$argv[0]} {$argv[1]} <username> <password> " .
  747. "<title> <startDate> <endDate> <icon> <url> <height> <width> <type>\n\n";
  748. echo "This creates a web content event on 2007/06/09.\n";
  749. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <password> " .
  750. "'World Cup 2006' '2007-06-09' '2007-06-10' " .
  751. "'http://www.google.com/calendar/images/google-holiday.gif' " .
  752. "'http://www.google.com/logos/worldcup06.gif' " .
  753. "'120' '276' 'image/gif'\n";
  754. }
  755. break;
  756. case 'createRecurringEvent':
  757. if ($argc == 7) {
  758. $client = getClientLoginHttpClient($argv[2], $argv[3]);
  759. createRecurringEvent($client, $argv[4], $argv[5], $argv[6]);
  760. } else {
  761. echo "Usage: php {$argv[0]} {$argv[1]} <username> <password> " .
  762. "<title> <description> <where>\n\n";
  763. echo "This creates an all-day event which occurs first on 2007/05/01" .
  764. "and repeats weekly on Tuesdays until 2007/09/04\n";
  765. echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <password> " .
  766. "'Tennis with Beth' 'Meet for a quick lesson' 'On the courts'\n";
  767. }
  768. break;
  769. }
  770. } else if (!isset($_SERVER["HTTP_HOST"])) {
  771. // running from command line, but action left unspecified
  772. echo "Usage: php {$argv[0]} <action> [<username>] [<password>] " .
  773. "[<arg1> <arg2> ...]\n\n";
  774. echo "Possible action values include:\n" .
  775. "outputCalendar\n" .
  776. "outputCalendarMagicCookie\n" .
  777. "outputCalendarByDateRange\n" .
  778. "outputCalendarByFullTextQuery\n" .
  779. "outputCalendarList\n" .
  780. "updateEvent\n" .
  781. "deleteEventById\n" .
  782. "deleteEventByUrl\n" .
  783. "createEvent\n" .
  784. "createQuickAddEvent\n" .
  785. "createWebContentEvent\n" .
  786. "createRecurringEvent\n" .
  787. "setReminder\n" .
  788. "addExtendedProperty\n";
  789. } else {
  790. // running through web server - demonstrate AuthSub
  791. processPageLoad();
  792. }