/**
  * Compiles the specified parsed template and updates the compiled table
  * and included templates list.
  *
  * @param integer $templateMapId The map ID of the template being compiled (for includes)
  * @param string|array $parsedTemplate Parsed form of the template
  * @param string $title Title of the template
  * @param integer $compileStyleId Style ID of the template
  * @param boolean $doDbWrite If non null, controls whether the DB write/template cache is written
  *
  * @return array|bool
  */
 public function compileAndInsertParsedAdminTemplate($templateMapId, $parsedTemplate, $title, $compileStyleId, $doDbWrite = null)
 {
     $isCss = substr($title, -4) == '.css';
     if ($doDbWrite === null) {
         $doDbWrite = $isCss || $compileStyleId;
     }
     $compiler = new Brivium_AdminStyleSystem_Template_Compiler_Admin('');
     $languages = $this->getModelFromCache('XenForo_Model_Language')->getAllLanguages();
     $db = $this->_getDb();
     $compiledCache = array();
     if ($isCss) {
         $compiledAdminTemplate = $compiler->compileParsed($parsedTemplate, $title, $compileStyleId, 0);
         $compiledCache[0] = $compiledAdminTemplate;
         if ($doDbWrite) {
             $this->_insertCompiledAdminTemplateRecord($compileStyleId, 0, $title, $compiledAdminTemplate);
         }
     } else {
         foreach ($languages as $language) {
             $compiledAdminTemplate = $compiler->compileParsed($parsedTemplate, $title, $compileStyleId, $language['language_id']);
             $compiledCache[$language['language_id']] = $compiledAdminTemplate;
             if ($doDbWrite) {
                 $this->_insertCompiledAdminTemplateRecord($compileStyleId, $language['language_id'], $title, $compiledAdminTemplate);
             }
         }
     }
     $mapIdQuoted = $db->quote($templateMapId);
     $ins = array();
     $includedTemplateIds = array();
     foreach ($compiler->getIncludedTemplates() as $includedMapId) {
         $ins[] = '(' . $mapIdQuoted . ', ' . $db->quote($includedMapId) . ')';
         $includedTemplateIds[] = $includedMapId;
     }
     if ($doDbWrite) {
         $db->delete('xf_brivium_admin_template_include', 'source_map_id = ' . $db->quote($templateMapId));
         if ($ins) {
             $db->query("\n\t\t\t\t\tINSERT IGNORE INTO xf_brivium_admin_template_include\n\t\t\t\t\t\t(source_map_id, target_map_id)\n\t\t\t\t\tVALUES\n\t\t\t\t\t\t" . implode(',', $ins));
         }
     }
     $ins = array();
     $includedPhraseTitles = array();
     foreach ($compiler->getIncludedPhrases() as $includedPhrase) {
         if (strlen($includedPhrase) > 75) {
             continue;
             // too long, can't be a valid phrase
         }
         $ins[] = '(' . $mapIdQuoted . ', ' . $db->quote($includedPhrase) . ')';
         $includedPhraseTitles[] = $includedPhrase;
     }
     if ($doDbWrite) {
         $db->delete('xf_brivium_admin_template_phrase', 'template_map_id = ' . $db->quote($templateMapId));
         if ($ins) {
             $db->query("\n\t\t\t\t\tINSERT IGNORE INTO xf_brivium_admin_template_phrase\n\t\t\t\t\t\t(template_map_id, phrase_title)\n\t\t\t\t\tVALUES\n\t\t\t\t\t\t" . implode(',', $ins));
         }
     }
     return array('includedTemplateIds' => $includedTemplateIds, 'failedTemplateIncludes' => $compiler->getFailedTemplateIncludes(), 'includedPhraseTitles' => $includedPhraseTitles, 'compiledCache' => $compiledCache, 'doDbWrite' => $doDbWrite);
 }
 /**
  * Verification callback to prepare a template. This isn't actually a verifier;
  * it just automatically compiles the template.
  *
  * @param string $string Uncompiled template
  *
  * @return boolean
  */
 protected function _verifyPrepareAdminTemplate($template)
 {
     $standardParse = true;
     $parsed = null;
     if (!$this->get('disable_modifications')) {
         $templateWithModifications = $this->_getModificationModel()->applyModificationsToTemplate($this->get('title'), $template, $modificationStatuses);
     } else {
         $modificationStatuses = null;
         $templateWithModifications = $template;
     }
     if ($modificationStatuses) {
         try {
             $compiler = new Brivium_AdminStyleSystem_Template_Compiler_Admin($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 Brivium_AdminStyleSystem_Template_Compiler_Admin($template);
             $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(), 'template');
             return false;
         }
     }
     $this->set('template_parsed', serialize($parsed));
     $this->_modificationStatuses = $modificationStatuses;
     return true;
 }