/**
  * Verification callback for an email template plain text body.
  *
  * @param string Uncompiled body
  *
  * @return boolean
  */
 protected function _verifyBodyHtml($body)
 {
     try {
         $compiler = new XenForo_Template_Compiler_Email($body);
         $parsed = $compiler->lexAndParse();
         if ($this->getOption(self::OPTION_TEST_COMPILE)) {
             $compiler->setFollowExternal(false);
             $compiler->compileParsed($parsed, $this->get('title'), 0, 0);
         }
     } catch (XenForo_Template_Compiler_Exception $e) {
         $this->error($e->getMessage(), 'body_html');
         return false;
     }
     $this->set('body_html_parsed', serialize($parsed));
     return true;
 }
    /**
     * Compiles the given email template data and inserts the compiled output. This
     * template is assumed to be the effective value for a given title.
     *
     * @param array $template Template info
     */
    public function compileAndInsertEmailTemplate(array $template)
    {
        $languages = $this->getModelFromCache('XenForo_Model_Language')->getAllLanguages();
        $languages[0] = array('language_id' => 0);
        $db = $this->_getDb();
        $compiler = new XenForo_Template_Compiler_Email('');
        $templateId = $template['template_id'];
        $title = $template['title'];
        $subjectParsed = unserialize($template['subject_parsed']);
        $bodyTextParsed = unserialize($template['body_text_parsed']);
        $bodyHtmlParsed = unserialize($template['body_html_parsed']);
        $phrases = array();
        $checkPhrases = true;
        foreach ($languages as $language) {
            $compiler->setOutputVar('__subject');
            $compiledTemplate = $compiler->compileParsedPlainText($subjectParsed, $title, 0, $language['language_id']);
            if ($checkPhrases) {
                $phrases = array_merge($phrases, $compiler->getIncludedPhrases());
            }
            $compiler->setOutputVar('__bodyText');
            $compiledTemplate .= $compiler->compileParsedPlainText($bodyTextParsed, $title, 0, $language['language_id']);
            if ($checkPhrases) {
                $phrases = array_merge($phrases, $compiler->getIncludedPhrases());
            }
            $compiler->setOutputVar('__bodyHtml');
            $compiledTemplate .= $compiler->compileParsed($bodyHtmlParsed, $title, 0, $language['language_id']);
            if ($checkPhrases) {
                $phrases = array_merge($phrases, $compiler->getIncludedPhrases());
            }
            $db->query('
				INSERT INTO xf_email_template_compiled
					(language_id, title, template_compiled)
				VALUES
					(?, ?, ?)
				ON DUPLICATE KEY UPDATE template_compiled = VALUES(template_compiled)
			', array($language['language_id'], $title, $compiledTemplate));
            $checkPhrases = false;
        }
        $phrases = array_unique($phrases);
        $db->delete('xf_email_template_phrase', 'title = ' . $db->quote($title));
        foreach ($phrases as $includedPhrase) {
            $db->insert('xf_email_template_phrase', array('title' => $title, 'phrase_title' => $includedPhrase));
        }
    }
 protected function _verifyTemplateField($string, $fieldName)
 {
     $modifications = $this->_getTemplateModifications();
     foreach ($modifications as $k => $modification) {
         if ($modification['search_location'] != $fieldName) {
             unset($modifications[$k]);
         }
     }
     $templateWithModifications = $this->_getModificationModel()->applyTemplateModifications($string, $modifications, $modificationStatuses);
     $standardParse = true;
     $parsed = null;
     if ($modificationStatuses) {
         try {
             $compiler = new XenForo_Template_Compiler_Email($templateWithModifications);
             $parsed = $compiler->lexAndParse();
             if ($this->getOption(self::OPTION_TEST_COMPILE)) {
                 $compiler->setFollowExternal(false);
                 $compiler->compileParsed($parsed, $this->get('title'), 0, 0);
             }
             $standardParse = false;
         } catch (XenForo_Template_Compiler_Exception $e) {
             foreach ($modificationStatuses as &$status) {
                 if (is_int($status)) {
                     $status = 'error_compile';
                 }
             }
         }
     }
     if ($standardParse) {
         try {
             $compiler = new XenForo_Template_Compiler_Email($string);
             $parsed = $compiler->lexAndParse();
             if ($this->getOption(self::OPTION_TEST_COMPILE)) {
                 $compiler->setFollowExternal(false);
                 $compiler->compileParsed($parsed, $this->get('title'), 0, 0);
             }
         } catch (XenForo_Template_Compiler_Exception $e) {
             $this->error($e->getMessage(), $fieldName);
             return false;
         }
     }
     $this->set($fieldName . '_parsed', serialize($parsed));
     if (!is_array($this->_modificationStatuses)) {
         $this->_modificationStatuses = array();
     }
     $this->_modificationStatuses += $modificationStatuses;
     return true;
 }