Jump to content


Module: Google Calendar Loader

Module

  • Please log in to reply
8 replies to this topic

#1 ryan

ryan

    Hero Member

  • Administrators
  • 2,882 posts
  • 236

  • LocationAtlanta, GA

Posted 23 December 2011 - 01:58 PM

Given a Google Calendar XML feed URL, this module will pull it, cache it, and let you foreach() it or render it.

Download at: https://github.com/r.../MarkupLoadGCal

USAGE


The MarkupLoadRSS module is used from your template files. To use it, you get a copy of the module, tell it what URL to load from (with the load method), and then execute a find() query with a selector string. It works basically the same as a $pages->find("selector string").

<?php

   $cal = $modules->get("MarkupLoadGCal");
   $cal->load('http://calendar XML feed');
   $items = $cal->find('from=2011-12-1, to=2011-12-31');
   foreach($items as $item) echo "<p>{$item->title}</p>";

To get a Google calendar URL, you would go into your (or another) calendar on Google and click to the calendar's settings. At the bottom of the screen, you will see a section called "Calendar Address" with XML, ICAL, and HTML. Copy the XML address, and use that with this module. If you just want one to test with, use the one from my examples below: http://www.google.co...com/public/full

Note: you must use the 'full' not 'basic' calendar from Google Calendar. You can identify if you've got the right one because it will end with 'full' rather than 'basic', as in the URL above.

Selector Properties

The selector properties you may provide to find() are:
  • from: Starting date in any common date/time format (or unix timestamp).
  • to: Ending date in any common date/time format (or unix timestamp).
  • keywords: Keyword(s) to search for in the Google calendar events.
  • limit: Max number of items to load (default = 100).
  • sort: May be 'date', '-date', 'modified' or '-modified'.
  • html: Set to '0' if you don't want the event description in HTML.
The find() method will return the found items. You can then foreach() these items to generate your output. A render() method is also included with the items which provides some default output, if you want it.

Calendar Item (Event) Properties

Each calendar item has the following properties:
  • title: The title of the event
  • description: Detailed description of the event (in HTML, unless disabled)
  • location: Where the event will take place
  • author: Author of this item
  • from: Timestamp of when the event begins
  • to: Timestamp of when the event ends
  • dateFrom: Formatted date string of when the event begins
  • dateTo: Formatted date string of when the event ends
See the module file for additional configuration options.


EXAMPLES




Example #1: Simplest example

<?php

   // get the calendar module
   $cal = $modules->get("MarkupLoadGCal");

   // set the feed URL: may be any google calendar XML feed URL
   $cal->load('http://www.google.com/calendar/feeds/3icgo6ucgvsf6bi5orld9moqqc%40group.calendar.google.com/public/full');

   // find all items for December, 2011
   $items = $cal->find('from=2011-12-1, to=2011-12-31');

   // render items using built-in rendering
   echo $items->render();


Example #2: Rendering your own items

<?php

   $cal = $modules->get("MarkupLoadGCal");
   $cal->load('http://www.google.com/calendar/feeds/3icgo6ucgvsf6bi5orld9moqqc%40group.calendar.google.com/public/full');
   $items = $cal->find('from=2011-12-1, to=2011-12-31');

   foreach($items as $item) {

	   echo "

	   <h2>{$item->title}</h2>

	   <p>
	   <b>Date From:</b> {$item->dateFrom} (Timestamp: {$item->from}) <br />
	   <b>Date To:</b> {$item->dateTo} (Timestamp: {$item->to}) <br />
	   <b>Location:</b> {$item->location} <br />
	   <b>Author:</b> {$item->author}
	   </p>

	   {$item->description}
	";
   }


Example #3: Finding Keywords

<?php

   $cal = $modules->get("MarkupLoadGCal");
   $cal->load('http://www.google.com/calendar/feeds/3icgo6ucgvsf6bi5orld9moqqc%40group.calendar.google.com/public/full');
   $items = $cal->find("from=Aug 1 2011, to=Dec 1 2011, keywords=Eddie Owen");
   echo $items->render();


ADDITIONAL INFO



Options
See the module's class file (MarkupLoadGCal) for a description of all options in the comments of the $options and $markup arrays at the top of the class file.

Handling Errors
If an error occurred when loading the feed, the $cal will have an 'error' property populated with a message of what error occurred:

<?php
   $items = $cal->find("...");
   if(empty($items) && $cal->error) {
	   // an error occurred
	   echo "Error! " . $cal->error;
   }

$items will be blank if an error occurs, but it will always be of the same type, so it's up to you whether you want to detect errors. If you don't, then your calendar output will just indicate that nothing was found.

Cache
By default your calendar queries are cached for an hour. You can change the cache time by setting the $cal->cache property to the number of seconds you want it to cache.

---
Edit: Added note that you must use the 'full' google calendar feed rather than the 'basic' one.

#2 Soma

Soma

    Hero Member

  • Moderators
  • 1,051 posts
  • 108

  • LocationSH, Switzerland

Posted 23 December 2011 - 02:19 PM

Ryan is on fire! :D

I'm sure this will be very useful. So many new things to try out. Thanks for this module!
@somartist - wire("soma")->is("creative|coder")->say("hello");

#3 Soma

Soma

    Hero Member

  • Moderators
  • 1,051 posts
  • 108

  • LocationSH, Switzerland

Posted 24 December 2011 - 07:30 AM

I'm just trying to get this working. I'm not sure if I'm doing it correctly, but I get nothing but a warning.

6 times a waring like:
Warning: MarkupLoadGCal::makeCalendarItem() [markuploadgcal.makecalendaritem]: Node no longer exists in /Applications/XAMPP/xamppfiles/htdocs/pw2.ch/site/modules/MarkupLoadGCal/MarkupLoadGCal.module on line 334
...
then error:
Fatal error: Cannot break/continue 1 level in /Applications/XAMPP/xamppfiles/htdocs/pw2.ch/site/modules/MarkupLoadGCal/MarkupLoadGCal.module on line 340

I'm using this code:

$cal = $modules->get("MarkupLoadGCal"); 
				   	$cal->load('https://www.google.com/calendar/feeds/ph.urlich%40gmail.com/public/basic'); 
				    $items = $cal->find('from=2011-12-1, to=2012-01-31'); 
				    
				    foreach($items as $item) {

				        echo "

				        <h2>{$item->title}</h2>

				        <p>
				        <b>Date From:</b> {$item->dateFrom} (Timestamp: {$item->from}) <br />
				        <b>Date To:</b> {$item->dateTo} (Timestamp: {$item->to}) <br />
				        <b>Location:</b> {$item->location} <br />
				        <b>Author:</b> {$item->author}
				        </p>

				        {$item->description}
					";
				    }

When I use your example xml url it works. But the ones I get are also different...
https://www.google.c...om/public/basic

@somartist - wire("soma")->is("creative|coder")->say("hello");

#4 ryan

ryan

    Hero Member

  • Administrators
  • 2,882 posts
  • 236

  • LocationAtlanta, GA

Posted 24 December 2011 - 08:55 AM

Sorry I missed this in the instructions (will update later) bit this module currently requires the 'full' not 'basic' calendar. So you'd need to replace basic with full in your URL. It looks to me like your full has read access disabled. Try with the example feed if you want.

#5 Jan Romero

Jan Romero

    Newbie

  • Members
  • Pip
  • 1 posts
  • 0

  • LocationGermany

Posted 09 February 2012 - 04:00 PM

I get the same error when I try to $cal->find() a month that has an event spanning into it:

Error Cannot break/continue 1 level (line 340

Events that continue beyond the loaded timespan are fine.

That line goes as follows, and, judging from the comment, I don't want that functionality anyways, so I commented it out. Now my output looks good, although I'm not very comfortable with messing with the code...
// filter out events that started before the requested time
// these will be multi-day events that span into the requested time
if($from && $a->from < $from) continue;

The point in my code that throws the error:
$items = $Calendar->find("from=$year-$month-1, to=$year-$month-$numdays, sort=date");


Otherwise everything is going super swell, thanks a lot for ProcessWire!

#6 ryan

ryan

    Hero Member

  • Administrators
  • 2,882 posts
  • 236

  • LocationAtlanta, GA

Posted 09 February 2012 - 06:11 PM

Jan, thanks for letting me know about the error. Do you have a calendar URL I could test with to reproduce it? Feel free to PM to me if it's something you don't want to post publicly. I'll be happy to fix this and update the source code.

#7 Hani

Hani

    Jr. Member

  • Members
  • PipPip
  • 30 posts
  • 2

  • LocationSanta Barbara, CA

Posted 23 February 2012 - 01:13 PM

Thanks for this module, Ryan!

For anyone using this module and has recurring events on their calendars, you're going to want to add one line of code to the makeCalendar() function if it isn't behaving as you expect.  (Recurring events' behavior is wonky in the sorted order. I didn't look too hard to find a pattern.)

Anyway, right before the following line:
$url = rtrim($url, '?&');

Add the following:
$url .= 'singleevents=true&';

This will render recurring events as single events in the XML feed.  Come to think of it, It may be good to make this change in the module anyway since it doesn't have any negative effects on single events.

#8 ryan

ryan

    Hero Member

  • Administrators
  • 2,882 posts
  • 236

  • LocationAtlanta, GA

Posted 24 February 2012 - 01:30 PM

Thanks Hani, I have updated the module to add a new 'recurring' option which is set to true by default (and translates to singleevents=true' in the URL).

#9 Hani

Hani

    Jr. Member

  • Members
  • PipPip
  • 30 posts
  • 2

  • LocationSanta Barbara, CA

Posted 27 February 2012 - 07:32 PM

Great idea, Ryan!  Even better than my static fix!  Thanks!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

ServInt
Web hosting by ServInt