Jump to content


Photo

Module: Google Calendar Loader

Module

  • Please log in to reply
21 replies to this topic

#1 ryan

ryan

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • 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
  • 5,057 posts
  • 3821

  • 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 | modules created | support me, flattr my work flattr.com


#3 Soma

Soma

    Hero Member

  • Moderators
  • 5,057 posts
  • 3821

  • 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 | modules created | support me, flattr my work flattr.com


#4 ryan

ryan

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • 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

    Starter

  • Members
  • 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

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • 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

    Full Member

  • Members
  • PipPipPip
  • 83 posts
  • 30

  • 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

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • 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

    Full Member

  • Members
  • PipPipPip
  • 83 posts
  • 30

  • LocationSanta Barbara, CA

Posted 27 February 2012 - 07:32 PM

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

#10 ffub

ffub

    Jr. Member

  • Members
  • PipPip
  • 37 posts
  • 20

  • LocationLondon, UK

Posted 20 September 2012 - 09:22 AM

Hiya,

I'm using this module on a couple of sites now, but most notably on the site for my climbing club: http://nlmc.co.uk/meets/

The previous events list is getting pretty long and I was wondering how complicated it would be to add pagination?

Stephen

#11 ryan

ryan

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • LocationAtlanta, GA

Posted 20 September 2012 - 03:13 PM

It's meant to be paginated within date ranges using the dateFrom() and dateTo() methods that you can call. Here's an example that uses the Google Calendar Loader and paginates by month/year:

http://washingtonwaldorf.org/calendar/

$month = $input->get->month ? (int) $input->get->month : date('n'); 
$year = $input->get->year ? (int) $input->get->year : date('Y'); 

$dateFrom = strtotime("$year-$month-01 00:00");
$dateTo = strtotime("+1 month", $dateFrom)-1; 

$cal = $modules->get('MarkupLoadGCal');
$cal->dateFrom($dateFrom)->dateTo($dateTo); 
$cal->load('your-gcal-email@domain.com');

echo "<h1>" . date('F Y', $dateFrom) . "</h1>"; // i.e. September 2012

foreach($cal as $item) {
  // output your items
}



#12 renobird

renobird

    Hero Member

  • Members
  • PipPipPipPipPip
  • 687 posts
  • 607

  • LocationGainesville, Florida

Posted 01 October 2012 - 01:57 PM

Hi Ryan,

Does this currently allow for multiple feeds?
I have a "master" calendar that has a lot of individual calendars.

#13 ryan

ryan

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • LocationAtlanta, GA

Posted 02 October 2012 - 12:55 PM

I've been away from this for a bit, but I don't recall there being reference to multiple feeds outside of what you send to the $cal->load(...) call. But of course, you can create as many instances of the module, and load as many feeds as you want, using that method.

#14 renobird

renobird

    Hero Member

  • Members
  • PipPipPipPipPip
  • 687 posts
  • 607

  • LocationGainesville, Florida

Posted 02 October 2012 - 06:20 PM

Thanks Ryan,

I was hoping to transition to using Google Calendar as a way to manage all our calendars (and there are a lot).
I tested it some today, and Google Calendar isn't going to provide all the functionality we need. However, it is well suited for some of our more specialized calendars that are essentially a list of dates — in which case this module will be perfect. :)

#15 ojohnsen

ojohnsen

    Newbie

  • Members
  • Pip
  • 2 posts
  • 1

Posted 04 December 2012 - 11:16 PM

I have been getting this message when using gcal and the items->render() method.

The feed works when entered staight into a browser.

Any ideas?

Unable to load GCal XML feed at http://www.google.co...gleevents=true:

#16 ryan

ryan

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • LocationAtlanta, GA

Posted 05 December 2012 - 01:04 PM

Two possibilities I can think of:

1. Your PHP installation doesn't have allow_url_fopen enabled. You can check this by viewing your phpinfo. My guess is this might be the case?

2. The calendar URL isn't public and requires authentication. Meaning you've got to be logged into your gmail or something in order to view it. This one would be easy to test, as you could log-out of any google services and try it (or just post the URL here or by PM to me).

#17 ojohnsen

ojohnsen

    Newbie

  • Members
  • Pip
  • 2 posts
  • 1

Posted 06 December 2012 - 12:44 AM

Ryan,

Thank you for the heads up on the allow_url_fopen. That is the problem.

I added the following code to the loadXMLData method.


$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xmlData= curl_exec($ch);
curl_close($ch);
//$xmlData = @file_get_contents($url);

This retrieved the xml data, sort of. The makeCalendar method does too much to the url to make it work properly.

However, if you change the code to this - $xmlData = $this->loadXmlData($calendarID); in the makeCalendar method. then it renders out the calendar.

I think though this will eliminate the features of the start-min, start-max, order by, etc.

Perhaps you have some more insight into how we could get this to work properly using curl?

Olen

#18 ryan

ryan

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • LocationAtlanta, GA

Posted 06 December 2012 - 11:24 AM

I haven't actually ever used CURL other than copy/paste from examples, so don't know what the proper method is to make it behave the way we'd want. I've always avoided CURL just because the examples always look so darn cryptic. :) But PW now comes with a WireHttp class that uses sockets and should give us an independent way of handling this stuff that doesn't rely upon allow_url_fopen or CURL. Actually, it attempts to use allow_url_fopen, but if the option isn't there, it falls back to using sockets. Meaning, it should work anywhere. The equivalent of your CURL example would be this:

$http = new WireHttp();
$data = $http->get($url); // for a GET request
// ...or...
$data = $http->post($url); // for a POST request 

Should any GET or POST vars need to be included, then they must be passed in an array as the second parameter:

$vars = array('foo' => 'bar'); 
$data = $http->get($url, $vars); 


#19 Marty Walker

Marty Walker

    Sr. Member

  • Members
  • PipPipPipPip
  • 476 posts
  • 274

  • LocationKatoomba, AU

Posted 17 December 2013 - 07:44 PM

Hi,

 

Does anyone know if Google have changed anything with their XML feeds. I can't get this to work at all. My code is from the simple example:

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

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

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

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

The only error I get is:

Error: Cannot break/continue 1 level (line 342 of /Users/martin/Sites/mumble/_html/site/modules/MarkupLoadGCal/MarkupLoadGCal.module)


#20 ryan

ryan

    Reiska

  • Administrators
  • 7,797 posts
  • 6572

  • LocationAtlanta, GA

Posted 28 December 2013 - 03:03 PM

That actually looks like a bug in this module. The error message is correct, as there's a "continue" in the middle of the function (not sure what I was thinking–I must have converted a loop to a function and not hit a date that would trigger the line). I'm not in the office where I can fix it, but I think it should be safe to just comment out or remove that line. 







Also tagged with one or more of these keywords: Module

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users