<?php

require_once('Transformer.php');

class 
ICalendar extends Transformer {

  public function 
transform() {
    
// make sure we have the required arguments
    
if (empty($this->extraArgs['startDateField']))
      throw new 
MissingArgumentsException(array('startDateField'));
    
    
// get the dom
    
$xmlDoc  = new DOMDocument();
    if (!
$xmlDoc->loadXML($this->xmlContents)) {
      throw new 
Exception("Unable to DOMify XML (XML looks like: ".$this->xmlContents.')');
    }
    
$this->xpath   = new DOMXPath($xmlDoc);
    
    
// issue the correct headers
    
header("Content-type: text/Calendar");
    
header("Content-Disposition: inline; filename=cal.ics");
    
    
// maybe there was an error, so let's check it
    
$messages $this->xpath->query("/results/message");
    if (
$messages->length 0) {
      echo 
"BEGIN:VCALENDAR\n";
      echo 
"PRODID:Dapper iCalendar Generator\n";
      echo 
"X-WR-CALNAME:Error\n";
      echo 
"VERSION:2.0\n";
      echo 
"BEGIN:VEVENT\n";
      echo 
"DTSTART;VALUE=DATE:".date('Ymd')."\n";
      echo 
"DTEND;VALUE=DATE:".date('Ymd')."\n";
      
$summary $messages->item(0)->nodeValue;
      echo 
"SUMMARY:$summary\n";
      echo 
"DESCRIPTION;ENCODING=QUOTED-PRINTABLE:$summary\n";
      echo 
"END:VEVENT\n";
      echo 
"END:VCALENDAR\n";
      exit;
    }

    echo 
"BEGIN:VCALENDAR\n";
    echo 
"PRODID:Dapper iCalendar Generator\n";
    echo 
"X-WR-CALNAME:".$this->xpath->query("/elements/dapper/dappTitle")->item(0)->nodeValue."\n";
    echo 
"VERSION:2.0\n";

    
// grab all the start date fields
    
$dates $xmlDoc->getElementsByTagName($this->extraArgs['startDateField']);
    for (
$i 0$i $dates->length$i++) {
      
$startDate   $this->formatDate($dates->item($i)->nodeValue);
      
$endDate     null;
      
$location    null;
      
$summary     "No Title";
      
$description "No description";
      
      if (isset(
$startDate)) {
        echo 
"BEGIN:VEVENT\n";
        if (
strlen($startDate) == 8)
          echo 
"DTSTART;VALUE=DATE:$startDate\n";
        else
          echo 
"DTSTART:$startDate\n";        
        
        
// now deal with additional fields (only works with groups
        
$groupNode $dates->item($i)->parentNode;
        if (
$groupNode->getAttribute('type') == 'group') {
          
          foreach (array(
'endDateField''descriptionField''titleField''locationField') as $arg) {
            if (!empty(
$this->extraArgs[$arg])) {
              
$matchingKids $groupNode->getElementsByTagName($this->extraArgs[$arg]);
              if (
$matchingKids && $matchingKids->item(0) && $matchingKids->item(0)->nodeValue) {
                
$nodeValue $matchingKids->item(0)->nodeValue;
                switch (
$arg) {
                  case 
'titleField':
                    
$summary $matchingKids->item(0)->nodeValue;
                    break;
                  case 
'descriptionField':
                    
$description preg_replace('/[\n\r\f]/','=0D=OA='$matchingKids->item(0)->nodeValue);
                    break;
                  case 
'endDateField':
                    
$endDate $this->formatDate($matchingKids->item(0)->nodeValue);
                    break;
                  case 
'descriptionField':
                    
$location preg_replace('/[\n\r\f]/','=0D=OA='$matchingKids->item(0)->nodeValue);
                    break;
                }
              }
            }
          }
        }
        
        if (!isset(
$endDate))
          
$endDate $startDate;        
        if (
strlen($endDate) == 8)
          echo 
"DTEND;VALUE=DATE:$endDate\n";
        else
          echo 
"DTEND:$endDate\n";

        echo 
"SUMMARY:$summary\n";
        echo 
"DESCRIPTION;ENCODING=QUOTED-PRINTABLE:$description\n";
        
        if (isset(
$location))
          echo 
"LOCATION:$location\n";
        
        echo 
"END:VEVENT\n";
      }
    }
    
    echo 
"END:VCALENDAR\n";
  }

  public static function 
getDetails() {
    
$details = array();
    
    
$details['args']['startDateField']   = array('type'     => DAPP_FIELD,
                                                 
'required' => true,
                                                 
'display'  => 'Event Start Date');

    
$details['args']['endDateField']     = array('type'     => DAPP_FIELD,
                                                 
'required' => false,
                                                 
'display'  => 'Event End Date');

    
$details['args']['titleField']       = array('type'     => DAPP_FIELD,
                                                 
'required' => false,
                                                 
'display'  => 'Even Title');

    
$details['args']['descriptionField'] = array('type'     => DAPP_FIELD,
                                                 
'required' => false,
                                                 
'display'  => 'Event Description');

    
$details['args']['locationField']    = array('type'     => DAPP_FIELD,
                                                 
'required' => false,
                                                 
'display'  => 'Event Location');
    
$details['description']   = 'Transforms the output of a Dapp into an '.
                                
'iCalendar which can be used in Google Calendar, '.
                                
'Sunbird, iCal, and other programs.';
    
    return 
$details;
  }

  function 
formatDate($stringDate) {
    if (
strtotime($stringDate) !== false) {
      if (
date('His'strtotime($stringDate)) == '000000')
        
$date date('Ymd'strtotime($stringDate));
      else
        
$date date('Ymd\THis'strtotime($stringDate));
      return 
$date;
    }
    
    return 
null;
  }

}

?>