<?php

require_once('Transformer.php');

// XXX
// XXX: much of the functionaly is copied from HTML.php - should push up
// XXX

class Email extends Transformer {

  public function 
transform() {
    
    
// make sure there is a recipient
    
if (empty($this->extraArgs['recipient'])) {
      throw new 
MissingArgumentsException(array('recipient'));
    }
    
    
$mailContents 'The following is an automatically generated email '.
                    
'from Dapper.'."\n\n";
    
    
$xmlDoc  = new DOMDocument();
    
$xmlDoc->loadXML($this->xmlContents);
    
$xmlRoot $xmlDoc->documentElement;    
    
$xpath   = new DOMXPath($xmlDoc);        
    
    
// get dapp details
    
$dappDetails = array();
    
$urlNodes $xpath->query("//elements/dapper/urls/url");
    
$dappUrls = array();
    for (
$i=0$i<$urlNodes->length$i++) {
      
$dappUrls[] = $urlNodes->item($i)->nodeValue;
    }
    
$dappDetails['Built from URLs'] = join(', ',$dappUrls);
    
$dappDetails['Execution Time']  = $xpath->query("//elements/dapper/executionTime")->item(0)->nodeValue;

    
$groupsOrFields $xmlRoot->childNodes;
    
$groupNodes     = array();
    
$fieldNodes     = array();
    for (
$i=0$i<$groupsOrFields->length$i++) {
      
$childNode $groupsOrFields->item($i);

      if (
$childNode->nodeType != XML_ELEMENT_NODE)
        continue;

      if (
$childNode->getAttribute('type') == 'group')
        
$groupNodes[] = $childNode;
      elseif (
$childNode->getAttribute('type') == 'field')
        
$fieldNodes[] = $childNode;
    }

    
// print dapp details
    
$mailContents.="Dapp Details\n";
    foreach (
$dappDetails as $desc => $val) {
      
$mailContents.="  ".$desc.': '.$val."\n";
    }
    
$mailContents.="\n";

    foreach (
$groupNodes as $groupNode) {
      
$mailContents.=$groupNode->tagName."\n";
      
$fields $groupNode->childNodes;
      for (
$j=0$j<$fields->length$j++) {
        
$mailContents.='  '.$this->getField($fields->item($j))."\n";
      }
      
$mailContents.="\n";
      
$mailContents.="--------------------------------------------";
      
$mailContents.="\n";
    }
    
    if (
sizeof($fieldNodes)) {
      foreach (
$fieldNodes as $fieldNode) {
        
$mailContents.=$this->getField($fieldNode)."\n";
      }
    }    
    
    
// send the mail
    
if (mail($this->extraArgs['recipient'],
             
'Dapp Mail',
             
$mailContents,
             
'From: Dapper <emailTransformer@thedapper.com>'))
      echo 
"Success";
    else
      echo 
"Failure";
  }
  
  private function 
getField($fieldNode) {
    if (
$fieldNode->nodeType != XML_ELEMENT_NODE)
      return 
'';

    if (
$fieldNode->getAttribute('type') == 'field') {
      return 
$fieldNode->tagName.': '.$fieldNode->nodeValue;
    }
  }
  
  public static function 
getDetails() {
    
$details = array();
    
    
$details['args']['recipient'] = array('type'     => USER_FREEFORM,
                                          
'required' => true,
                                          
'display'  => 'Email Address',
                                          
'valid'    => '.*@.*\..*');
    
    
$details['description']   = 'Transforms the output of a Dapp into plain '.
                                
'text and emails it to specified email address';
    
    return 
$details;
  }

}

?>