Exemplo n.º 1
0
 /**
  * Compares the current to the original template
  *
  * @param DataContainer $dc
  *
  * @return string
  *
  * @throws Contao\CoreBundle\Exception\InternalServerErrorException
  */
 public function compareTemplate(DataContainer $dc)
 {
     $objCurrentFile = new File($dc->id);
     $strName = $objCurrentFile->filename;
     $strExtension = $objCurrentFile->extension;
     $arrTemplates = TemplateLoader::getFiles();
     $blnOverridesAnotherTpl = isset($arrTemplates[$strName]);
     $strPrefix = '';
     if (($pos = strpos($strName, '_')) !== false) {
         $strPrefix = substr($strName, 0, $pos + 1);
     }
     $strBuffer = '';
     $strCompareName = null;
     $strComparePath = null;
     // By default it's the original template to compare against
     if ($blnOverridesAnotherTpl) {
         $strCompareName = $strName;
         $strComparePath = $arrTemplates[$strCompareName] . '/' . $strCompareName . '.' . $strExtension;
         if ($strComparePath !== null) {
             $strBuffer .= '<p class="tl_info" style="margin-bottom:1em">' . sprintf($GLOBALS['TL_LANG']['tl_templates']['overridesAnotherTpl'], $strComparePath) . '</p>';
         }
     } else {
         // Try to find the base template by strippig suffixes
         while (strpos($strName, '_') !== false) {
             $strName = substr($strName, 0, strrpos($strName, '_'));
             if (isset($arrTemplates[$strName])) {
                 $strCompareName = $strName;
                 $strComparePath = $arrTemplates[$strCompareName] . '/' . $strCompareName . '.' . $strExtension;
                 break;
             }
         }
     }
     // User selected template to compare against
     if (Input::post('from') && isset($arrTemplates[Input::post('from')])) {
         $strCompareName = Input::post('from');
         $strComparePath = $arrTemplates[$strCompareName] . '/' . $strCompareName . '.' . $strExtension;
     }
     if ($strComparePath !== null) {
         $objCompareFile = new File($strComparePath);
         // Abort if one file is missing
         if (!$objCurrentFile->exists() || !$objCompareFile->exists()) {
             throw new Contao\CoreBundle\Exception\InternalServerErrorException('The source or target file does not exist.');
         }
         $objDiff = new Diff($objCompareFile->getContentAsArray(), $objCurrentFile->getContentAsArray());
         $strDiff = $objDiff->render(new DiffRenderer(array('field' => $dc->id)));
         // Identical versions
         if ($strDiff == '') {
             $strBuffer .= '<p>' . $GLOBALS['TL_LANG']['MSC']['identicalVersions'] . '</p>';
         } else {
             $strBuffer .= $strDiff;
         }
     } else {
         $strBuffer .= '<p class="tl_info">' . $GLOBALS['TL_LANG']['tl_templates']['pleaseSelect'] . '</p>';
     }
     // Templates to compare against
     $arrComparable = array();
     $intPrefixLength = strlen($strPrefix);
     foreach ($arrTemplates as $k => $v) {
         if (substr($k, 0, $intPrefixLength) === $strPrefix) {
             $arrComparable[$k] = array('version' => $k, 'info' => $k . '.' . $strExtension);
         }
     }
     /** @var BackendTemplate|object $objTemplate */
     $objTemplate = new BackendTemplate('be_diff');
     // Template variables
     $objTemplate->staticTo = $dc->id;
     $objTemplate->versions = $arrComparable;
     $objTemplate->from = $strCompareName;
     $objTemplate->showLabel = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['showDifferences']);
     $objTemplate->content = $strBuffer;
     $objTemplate->theme = Backend::getTheme();
     $objTemplate->base = Environment::get('base');
     $objTemplate->language = $GLOBALS['TL_LANGUAGE'];
     $objTemplate->title = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['showDifferences']);
     $objTemplate->charset = Config::get('characterSet');
     Config::set('debugMode', false);
     throw new Contao\CoreBundle\Exception\ResponseException($objTemplate->getResponse());
 }