We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30585
    • 833 Posts
    Hi guys,

    I’m using a wonderful icalendar snippet to allow a user to download events into their calendar client.

    Although this snippet was built for previous versions of MODx, it seems straight-forward and it works pretty well with MODx 2.2 except that when I open the .ics file in Outlook 2010, there seems to be no events attached.

    Here’s my snippet code:

    <?php
    // iCalendar snippet
    // by rolf ahlborn
    // v0.0.1 - 2009-09-06
    //
    // ---------------------------------
    //
    // snippet will collect ressources/documents from defined folder(s)
    // and creates an ics-file for downloading and subscribing your
    // dates/events into iCal or Outlook or others
    //
    // ---------------------------------
    //
    // preparing:
    // * copy this content entirely into a new snippet and name it: iCalendar
    // * generate one or more folder with any ressources/documents
    // * each document must have a pagetitle, a publication date and
    //   normally an unpublishing date
    // * create an document to place the snippet call into
    //   i.e. [!iCalendar?startid=`16,18,52,57`!]
    //   - if no startid is given, it will take the calling doc as startid
    //   - give this document any title and set the Alias to the same name as
    //     you define $icsFileName below, i.e. myCal.ics
    //   - set the template to: (blank)
    //   - set the content type to: text/plain
    //   - you may set the behaviour to attachment, but snippet will deliver
    //     the propper mime-type header for text/calendar and content-disposition
    //   - you may hide this document from navigation and create a link anywhere 
    //     to offer the download to that file 
    // * according to the OS preferences, you or your users will have to subscribe
    //   either the address (URI) created by your snippet doument or import it locally
    //   after downloading the file - let them know... in first case, their subscribed
    //   calendars will be updated if you change your related MODx documents 
    
    
    // startid can be provided in snippet parameters and a comma separated list
    // default startid: doc itself
    $startid = (isset($startid)) ? $startid : $modx->documentIdentifier;
    
    // specify field to sort by, default menuindex
    $sortBy = (isset($sortBy)) ? $sortBy : 'menuindex';
    
    // sortdirection of collected docs, default ascending
    $sortDirection = (isset($sortDirection)) ? $sortDirection : 'ASC';
    
    // define iCalender title - will be used in your calendar application
    $icalName = 'Calendrier d\'Événements Parole De Grace';
    
    // define ics file name - filename for download file
    $icsFileName = 'GraceCal.ics';
    
    // define Timezome
    // more information: http://en.wikipedia.org/wiki/ICalendar#cite_note-8
    $icalTZ = 'America/Toronto';
    
    // set an alarm before event starts, here 60 minutes to go
    $alarmBefore = 60;
    
    // define default event length in minutes, if no unpubdate is given
    // let's say default fixed event length are 75 minutes
    $duration = 75;
    
    
    /////////////////////////////////////////////////////////////////////////////
    
    
    // figure out the parent(s)
    $parents = array();
    // if $startid is given, check for comma
    if(strpos($startid, ',') !== false) {
      // explode list to array
      $parents = explode(',', $startid);
    } else {
      // otherwise create single-item array
      $parents[] = $startid;  
    }
    
    // collect the children(s) of parent(s)
    $children = array();
    foreach($parents as $parent){
      $childArr = $modx->getAllChildren($parent ,$sortBy, $sortDirection ,'pub_date,unpub_date,id,pagetitle,parent,deleted');
      foreach($childArr as $child){
      	// we only take undeleted docs
        if(($child['deleted'] == 0)){
          $children[] = $child;
        }
      }
    }
    
    // sort array, may be commented if sorting is bad
    sort($children); 
    
    // create output string in ical style
    $output = "BEGIN:VCALENDAR\n";
    $output .= "VERSION:2.0\n";
    $output .= "PRODID:-//hi-tag//modx 0.0.1//EN\n"; // you may alter this
    $output .= "X-WR-CALNAME:" . $icalName . "\n";
    $output .= "X-WR-TIMEZONE:" . $icalTZ . "\n";
    
    // create the events from children's title, pubdate and unpubdate
    for ($c=0; $c < count($children) ; $c++){
      $evtdate = $children[$c]['pub_date'];
      $evtdateend = $children[$c]['unpub_date']; 
      
      // alternative DTEND for fixed event length or if no unpub date is given
      if($evtdateend == 0){  	  	
      	// get pubdate to get a time basis for calculation
        $y = date("Y", $evtdate);
        $m = date("m", $evtdate);
        $d = date("d", $evtdate);
        $h = date("H", $evtdate);
        $i = date("i", $evtdate);
        // add defined duration to pubdate
        $evtdateend = mktime($h, $i+$duration, 0, $m, $d, $y);
      }
      
      // add output string for every event
      $output .= "BEGIN:VEVENT\n";
      $output .= "DTSTART:" . date("Ymd", $evtdate) . "T" . date("His", $evtdate) . "Z\n";
      $output .= "DTEND:" . date("Ymd", $evtdateend) . "T" . date("His", $evtdateend) . "Z\n";
      
      $output .= "SUMMARY:" . $children[$c]['pagetitle'] . "\n";
    
      $output .= "BEGIN:VALARM\n";
      $output .= "ACTION:DISPLAY\n";
      $output .= "TRIGGER:-PT" . $alarmBefore . "M\n";
      $output .= "END:VALARM\n";
      
      $output .= "END:VEVENT\n";
    } 
    
    $output .= "END:VCALENDAR\n";
    
    // prepare output as ics download file
    header('Content-type: text/calendar');
    header('Content-Disposition: attachment; filename="' . $icsFileName . '"');
    echo $output;


    When I open the ics file with notepad, I get this:

    BEGIN:VCALENDAR
    VERSION:2.0
    PRODID:-//hi-tag//modx 0.0.1//EN
    X-WR-CALNAME:Calendrier d'Événements Parole De Grace
    X-WR-TIMEZONE:America/Toronto
    BEGIN:VEVENT
    DTSTART:19691231T190000Z
    DTEND:19691231T193330Z
    SUMMARY:Conférence-Retraite Internationale
    BEGIN:VALARM
    ACTION:DISPLAY
    TRIGGER:-PT60M
    END:VALARM
    END:VEVENT
    END:VCALENDAR
    


    Which tells me that the data was passed on properly. It also creates a calendar in Outlook with the right name, but the actual event is not created.

    Does anyone see an issue here?

    Thanks a lot
      A MODx Fanatic
      • 32756
      • 25 Posts
      Not sure if you ever figured out this issue, as I see this is a very old topic. I do see the problem in your file, though. The date for the event in your file is set to the year 1969!