[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/metamta/view/ -> PhabricatorMetaMTAMailBody.php (source)

   1  <?php
   2  
   3  /**
   4   * Render the body of an application email by building it up section-by-section.
   5   *
   6   * @task compose  Composition
   7   * @task render   Rendering
   8   */
   9  final class PhabricatorMetaMTAMailBody {
  10  
  11    private $sections = array();
  12    private $htmlSections = array();
  13    private $attachments = array();
  14  
  15    private $viewer;
  16  
  17    public function getViewer() {
  18      return $this->viewer;
  19    }
  20  
  21    public function setViewer($viewer) {
  22      $this->viewer = $viewer;
  23    }
  24  
  25  /* -(  Composition  )-------------------------------------------------------- */
  26  
  27  
  28    /**
  29     * Add a raw block of text to the email. This will be rendered as-is.
  30     *
  31     * @param string Block of text.
  32     * @return this
  33     * @task compose
  34     */
  35    public function addRawSection($text) {
  36      if (strlen($text)) {
  37        $text = rtrim($text);
  38        $this->sections[] = $text;
  39        $this->htmlSections[] = phutil_escape_html_newlines(
  40          phutil_tag('div', array(), $text));
  41      }
  42      return $this;
  43    }
  44  
  45    public function addRemarkupSection($text) {
  46      try {
  47        $engine = PhabricatorMarkupEngine::newMarkupEngine(array());
  48        $engine->setConfig('viewer', $this->getViewer());
  49        $engine->setMode(PhutilRemarkupEngine::MODE_TEXT);
  50        $styled_text = $engine->markupText($text);
  51        $this->sections[] = $styled_text;
  52      } catch (Exception $ex) {
  53        phlog($ex);
  54        $this->sections[] = $text;
  55      }
  56  
  57      try {
  58        $mail_engine = PhabricatorMarkupEngine::newMarkupEngine(array());
  59        $mail_engine->setConfig('viewer', $this->getViewer());
  60        $mail_engine->setMode(PhutilRemarkupEngine::MODE_HTML_MAIL);
  61        $mail_engine->setConfig(
  62          'uri.base',
  63          PhabricatorEnv::getProductionURI('/'));
  64        $html = $mail_engine->markupText($text);
  65        $this->htmlSections[] = $html;
  66      } catch (Exception $ex) {
  67        phlog($ex);
  68        $this->htmlSections[] = phutil_escape_html_newlines(
  69          phutil_tag(
  70            'div',
  71            array(),
  72            $text));
  73      }
  74  
  75      return $this;
  76    }
  77  
  78    public function addRawPlaintextSection($text) {
  79      if (strlen($text)) {
  80        $text = rtrim($text);
  81        $this->sections[] = $text;
  82      }
  83      return $this;
  84    }
  85  
  86    public function addRawHTMLSection($html) {
  87      $this->htmlSections[] = phutil_safe_html($html);
  88      return $this;
  89    }
  90  
  91  
  92    /**
  93     * Add a block of text with a section header. This is rendered like this:
  94     *
  95     *    HEADER
  96     *      Text is indented.
  97     *
  98     * @param string Header text.
  99     * @param string Section text.
 100     * @return this
 101     * @task compose
 102     */
 103    public function addTextSection($header, $section) {
 104      if ($section instanceof PhabricatorMetaMTAMailSection) {
 105        $plaintext = $section->getPlaintext();
 106        $html = $section->getHTML();
 107      } else {
 108        $plaintext = $section;
 109        $html = phutil_escape_html_newlines(phutil_tag('div', array(), $section));
 110      }
 111  
 112      $this->addPlaintextSection($header, $plaintext);
 113      $this->addHTMLSection($header, $html);
 114      return $this;
 115    }
 116  
 117    public function addPlaintextSection($header, $text) {
 118      $this->sections[] = $header."\n".$this->indent($text);
 119      return $this;
 120    }
 121  
 122    public function addHTMLSection($header, $html_fragment) {
 123      $this->htmlSections[] = array(
 124        phutil_tag(
 125          'div',
 126          array(),
 127          array(
 128            phutil_tag('strong', array(), $header),
 129            phutil_tag('div', array(), $html_fragment),
 130          )),
 131      );
 132      return $this;
 133    }
 134  
 135    public function addLinkSection($header, $link) {
 136      $html = phutil_tag('a', array('href' => $link), $link);
 137      $this->addPlaintextSection($header, $link);
 138      $this->addHTMLSection($header, $html);
 139      return $this;
 140    }
 141  
 142    /**
 143     * Add a Herald section with a rule management URI and a transcript URI.
 144     *
 145     * @param string URI to rule transcripts.
 146     * @return this
 147     * @task compose
 148     */
 149    public function addHeraldSection($xscript_uri) {
 150      if (!PhabricatorEnv::getEnvConfig('metamta.herald.show-hints')) {
 151        return $this;
 152      }
 153  
 154      $this->addLinkSection(
 155        pht('WHY DID I GET THIS EMAIL?'),
 156        PhabricatorEnv::getProductionURI($xscript_uri));
 157  
 158      return $this;
 159    }
 160  
 161  
 162    /**
 163     * Add a section with reply handler instructions.
 164     *
 165     * @param string Reply handler instructions.
 166     * @return this
 167     * @task compose
 168     */
 169    public function addReplySection($instructions) {
 170      if (!PhabricatorEnv::getEnvConfig('metamta.reply.show-hints')) {
 171        return $this;
 172      }
 173      if (!strlen($instructions)) {
 174        return $this;
 175      }
 176  
 177      $this->addTextSection(pht('REPLY HANDLER ACTIONS'), $instructions);
 178  
 179      return $this;
 180    }
 181  
 182    /**
 183     * Add a section with a link to email preferences.
 184     *
 185     * @return this
 186     * @task compose
 187     */
 188    public function addEmailPreferenceSection() {
 189      if (!PhabricatorEnv::getEnvConfig('metamta.email-preferences')) {
 190        return $this;
 191      }
 192  
 193      $href = PhabricatorEnv::getProductionURI(
 194        '/settings/panel/emailpreferences/');
 195      $this->addLinkSection(pht('EMAIL PREFERENCES'), $href);
 196  
 197      return $this;
 198    }
 199  
 200    /**
 201     * Add an attachment.
 202     *
 203     * @param PhabricatorMetaMTAAttachment Attachment.
 204     * @return this
 205     * @task compose
 206     */
 207    public function addAttachment(PhabricatorMetaMTAAttachment $attachment) {
 208      $this->attachments[] = $attachment;
 209      return $this;
 210    }
 211  
 212  
 213  /* -(  Rendering  )---------------------------------------------------------- */
 214  
 215  
 216    /**
 217     * Render the email body.
 218     *
 219     * @return string Rendered body.
 220     * @task render
 221     */
 222    public function render() {
 223      return implode("\n\n", $this->sections)."\n";
 224    }
 225  
 226    public function renderHTML() {
 227      $br = phutil_tag('br');
 228      $body = phutil_implode_html($br, $this->htmlSections);
 229      return (string)hsprintf('%s', array($body, $br));
 230    }
 231  
 232    /**
 233     * Retrieve attachments.
 234     *
 235     * @return list<PhabricatorMetaMTAAttachment> Attachments.
 236     * @task render
 237     */
 238    public function getAttachments() {
 239      return $this->attachments;
 240    }
 241  
 242  
 243    /**
 244     * Indent a block of text for rendering under a section heading.
 245     *
 246     * @param string Text to indent.
 247     * @return string Indented text.
 248     * @task render
 249     */
 250    private function indent($text) {
 251      return rtrim("  ".str_replace("\n", "\n  ", $text));
 252    }
 253  
 254  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1