[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActionMailer/ -> AkMailComposer.php (source)

   1  <?php
   2  
   3  
   4  class AkMailComposer extends AkObject
   5  {
   6      var $Message;
   7      var $ActionMailer;
   8      var $parts = array();
   9      var $raw_message = '';
  10      var $composed_message = array();
  11      var $_boundary_stack = array();
  12      var $boundary = '';
  13  
  14  
  15      function init(&$ActionMailer)
  16      {
  17          $this->ActionMailer =& $ActionMailer;
  18          $this->Message =& $ActionMailer->Message;
  19      }
  20  
  21      function build()
  22      {
  23          $args = func_get_args();
  24          $method_name = array_shift($args);
  25          $this->ActionMailer->initializeDefaults($method_name);
  26          $this->_callActionMailerMethod($method_name, $args);
  27          $this->_prepareInlineBodyParts();
  28      }
  29  
  30  
  31      function getRawMessage($MessageOrPart = null, $force_overload = false)
  32      {
  33          $Message = empty($MessageOrPart) ? $this->Message : $MessageOrPart;
  34          if($force_overload || empty($Message->raw_message)){
  35              list($raw_headers, $raw_body) = $this->getRawHeadersAndBody($Message);
  36              $Message->raw_message = $raw_headers.
  37              AK_ACTION_MAILER_EOL.AK_ACTION_MAILER_EOL.
  38              $raw_body;
  39          }
  40          return $Message->raw_message;
  41      }
  42  
  43  
  44      function getRawHeadersAndBody($MessageOrPart = null)
  45      {
  46          $Message = empty($MessageOrPart) ? $this->Message : $MessageOrPart;
  47          $raw_body_or_parts = $this->getRawBodyOrRawParts($Message);
  48  
  49          if(is_array($raw_body_or_parts)){
  50              $raw_body = '';
  51              $this->openMultipartBlock();
  52              if(!$Message->hasContentType()){
  53                  $Message->setContentType('multipart/related');
  54              }
  55              $Message->content_type_attributes['boundary'] = $this->getBoundary();
  56              $Message->_skip_adding_date_to_headers = !$Message->isMainMessage();
  57  
  58              $raw_headers = $Message->getRawHeaders();
  59              foreach ($raw_body_or_parts as $raw_part_headers=>$raw_part_body){
  60                  $raw_body .=
  61                  AK_ACTION_MAILER_EOL.
  62                  AK_ACTION_MAILER_EOL.
  63                  '--'.
  64                  $this->getBoundary().
  65                  AK_ACTION_MAILER_EOL.
  66                  $raw_part_headers.
  67                  AK_ACTION_MAILER_EOL.
  68                  AK_ACTION_MAILER_EOL.
  69                  $raw_part_body;
  70              }
  71              $raw_body .= AK_ACTION_MAILER_EOL.'--'.$this->getBoundary().'--'.AK_ACTION_MAILER_EOL;
  72  
  73              $this->closeMultipartBlock();
  74          }else{
  75              $raw_headers = $Message->getRawHeaders();
  76              $raw_body = $raw_body_or_parts;
  77          }
  78  
  79          return array($raw_headers, $raw_body);
  80      }
  81  
  82  
  83      function getRawBodyOrRawParts($MessageOrPart = null)
  84      {
  85          $Message = empty($MessageOrPart) ? $this->Message : $MessageOrPart;
  86          $body = $Message->getBody();
  87          if(empty($body) && ($Message->hasParts() || $Message->hasAttachments())){
  88              $result = array();
  89              foreach (array_keys($Message->parts) as $k){
  90                  $Part = $Message->parts[$k];
  91                  list($raw_headers, $raw_body) = $this->getRawHeadersAndBody($Part);
  92                  $result[$raw_headers] = $raw_body;
  93              }
  94              return $result;
  95          }
  96          return $body;
  97      }
  98  
  99      function openMultipartBlock()
 100      {
 101          $this->setBoundary($this->getBoundaryString());
 102      }
 103  
 104      function closeMultipartBlock()
 105      {
 106          $this->latest_closed_boundary = array_pop($this->_boundary_stack);
 107      }
 108  
 109  
 110      function setBoundary($boundary)
 111      {
 112          $this->boundary = $boundary;
 113          array_push($this->_boundary_stack, $boundary);
 114          return $this->boundary;
 115      }
 116  
 117      function getBoundary()
 118      {
 119          return $this->boundary;
 120      }
 121  
 122  
 123      function getBoundaryString()
 124      {
 125          return md5(Ak::randomString(10).time());
 126      }
 127  
 128  
 129  
 130  
 131      function _callActionMailerMethod($method_name, $params = array())
 132      {
 133          if(method_exists($this->ActionMailer, $method_name)){
 134              call_user_func_array(array(&$this->ActionMailer, $method_name), $params);
 135          }else{
 136              trigger_error(Ak::t('Could not find the method %method on the model %model', array('%method'=>$method_name, '%model'=>$this->ActionMailer->getModelName())), E_USER_ERROR);
 137          }
 138          $this->_setAttributesIfRequired();
 139      }
 140      
 141      function _setAttributesIfRequired()
 142      {
 143          if(empty($this->ActionMailer->_setter_has_been_called)){
 144              $attributes = array();
 145              foreach ((array)$this->ActionMailer as $k=>$v){
 146                  if(gettype($v) != 'object' && $k[0] != '_'){
 147                      $attributes[$k] = $v;
 148                  }
 149              }
 150              $this->ActionMailer->set($attributes);
 151          }
 152      }
 153  
 154  
 155      function _prepareInlineBodyParts($message_content_type = 'multipart/alternative')
 156      {
 157          if(!$this->_hasRenderedBody()){
 158              if(!$this->_renderMultiPartViews($message_content_type)){
 159                  $this->_renderMainTemplateIfNeeded();
 160              }
 161              $this->_moveBodyToPart();
 162          }
 163      }
 164  
 165      function _renderMainTemplateIfNeeded()
 166      {
 167          if($this->_shouldRenderMainTemplate()){
 168              $this->Message->setBody($this->_renderMainTemplate());
 169              return true;
 170          }
 171          return false;
 172      }
 173  
 174      function _renderMainTemplate()
 175      {
 176          return $this->ActionMailer->renderMessage($this->ActionMailer->template, $this->Message->body);
 177      }
 178  
 179      function _renderMultiPartViews($message_content_type)
 180      {
 181          if(empty($this->Message->parts)){
 182              $parts = $this->_getPartsWithRenderedTemplates();
 183              $this->Message->setParts($parts, 'append', true);
 184              if(!empty($this->Message->parts)){
 185                  $this->Message->content_type = $message_content_type;
 186                  $this->Message->sortParts();
 187              }
 188          }
 189          return !empty($parts);
 190      }
 191  
 192      function _hasRenderedBody()
 193      {
 194          return is_string($this->Message->body);
 195      }
 196  
 197      function _moveBodyToPart()
 198      {
 199          if (!empty($this->Message->parts) && is_string($this->Message->body)){
 200              array_unshift($this->Message->parts, array('charset' => $this->Message->charset, 'body' => $this->Message->body));
 201              $this->ActionMailer->body = null;
 202          }
 203      }
 204  
 205      function _shouldRenderMainTemplate()
 206      {
 207          $result = empty($this->Message->parts);
 208          if(!$result && empty($this->Message->implicit_parts_order) && $this->_hasIndividualTemplate()){
 209              $result = true;
 210          }
 211          return $result;
 212      }
 213  
 214  
 215      function _hasIndividualTemplate()
 216      {
 217          $templates = $this->_getAvailableTemplates();
 218          foreach ($templates as $template){
 219              $parts = explode('.',$template);
 220              if(count($parts) == 2 && $parts[0] == $this->ActionMailer->template){
 221                  return true;
 222              }
 223          }
 224          return false;
 225      }
 226  
 227  
 228      function &_getPartsWithRenderedTemplates()
 229      {
 230          $templates = $this->_getAvailableTemplates();
 231          $alternative_multiparts = array();
 232          $parts = array();
 233          foreach ($templates as $template_name){
 234              if(preg_match('/^([^\.]+)\.([^\.]+\.[^\.]+)\.(tpl)$/',$template_name, $match)){
 235                  if($this->ActionMailer->template == $match[1]){
 236                      $content_type = str_replace('.','/', $match[2]);
 237  
 238                      $parts[] = array(
 239                      'content_type' => $content_type,
 240                      'disposition' => 'inline',
 241                      'charset' => @$this->Message->charset,
 242                      'body' => $this->ActionMailer->renderMessage($this->ActionMailer->getTemplatePath().DS.$template_name, $this->Message->body));
 243                  }
 244              }
 245          }
 246          return $parts;
 247      }
 248  
 249      function _getAvailableTemplates()
 250      {
 251          $path = $this->ActionMailer->getTemplatePath();
 252          if(!isset($templates[$path])){
 253              $templates[$path] = array_map('basename', Ak::dir($path, array('dirs'=>false)));
 254          }
 255          return $templates[$path];
 256      }
 257  
 258  
 259  }
 260  
 261  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6