/**
  * Generate PDF Document
  *
  * @param integer $pdfTemplateId          id of the PDF Template
  * @param array   $substitution           array of substitution values
  * @param string  $mailTplKey             MailTemplate key
  * @param boolean $convertToHtmlEntities  convert input to HTML entities
  *
  * @return mixed array|null
  */
 public function generatePDF($pdfTemplateId, $substitution, $mailTplKey, $convertToHtmlEntities = false)
 {
     if (empty($mailTplKey)) {
         return;
     }
     $repo = $this->cx->getDb()->getEntityManager()->getRepository('\\Cx\\Core_Modules\\Pdf\\Model\\Entity\\PdfTemplate');
     $pdfTemplates = $repo->findOneBy(array('id' => $pdfTemplateId));
     if (!$pdfTemplates || !$pdfTemplates->getHtmlContent()) {
         return;
     }
     $tplContent = $pdfTemplates->getHtmlContent();
     \Cx\Core\MailTemplate\Controller\MailTemplate::substitute($tplContent, $substitution, $convertToHtmlEntities);
     $session = $this->getComponent('Session')->getSession();
     $datetime = $this->getComponent('DateTime')->createDateTimeForUser('now')->format('d_m_Y_h_s_i');
     $title = $mailTplKey . '.pdf';
     $fileName = $mailTplKey . '_' . $datetime . '.pdf';
     $pdf = new \Cx\Core_Modules\Pdf\Model\Entity\PdfDocument();
     $pdf->SetTitle($title);
     $pdf->setContent($tplContent);
     $pdf->setDestination('F');
     $pdf->setFilePath($session->getTempPath() . '/' . $fileName);
     $pdf->Create();
     return array('filePath' => $session->getWebTempPath() . '/' . $fileName, 'fileName' => $title);
 }
Exemplo n.º 2
0
 /**
  * Parses the main template in order to finish request
  * @todo Remove usage of globals
  * @global type $themesPages
  * @global null $moduleStyleFile
  * @global array $_CONFIG
  * @global type $subMenuTitle
  * @global type $_CORELANG
  * @global type $plainCmd
  * @global type $cmd
  */
 protected function finalize()
 {
     global $themesPages, $moduleStyleFile, $_CONFIG, $subMenuTitle, $_CORELANG, $plainCmd, $cmd;
     if ($this->mode == self::MODE_FRONTEND) {
         // parse system
         $parsingTime = $this->stopTimer();
         $this->template->setVariable('PARSING_TIME', $parsingTime);
         $this->parseGlobalPlaceholders($themesPages['sidebar']);
         $this->template->setVariable(array('SIDEBAR_FILE' => $themesPages['sidebar'], 'JAVASCRIPT_FILE' => $themesPages['javascript'], 'BUILDIN_STYLE_FILE' => $themesPages['buildin_style'], 'DATE_YEAR' => date('Y'), 'DATE_MONTH' => date('m'), 'DATE_DAY' => date('d'), 'DATE_TIME' => date('H:i'), 'BUILDIN_STYLE_FILE' => $themesPages['buildin_style'], 'JAVASCRIPT_LIGHTBOX' => '<script type="text/javascript" src="lib/lightbox/javascript/mootools.js"></script>
                 <script type="text/javascript" src="lib/lightbox/javascript/slimbox.js"></script>', 'JAVASCRIPT_MOBILE_DETECTOR' => '<script type="text/javascript" src="lib/mobiledetector.js"></script>'));
         if (!empty($moduleStyleFile)) {
             $this->template->setVariable('STYLE_FILE', "<link rel=\"stylesheet\" href=\"{$moduleStyleFile}\" type=\"text/css\" media=\"screen, projection\" />");
         }
         if (!$this->resolvedPage->getUseSkinForAllChannels() && isset($_GET['pdfview']) && intval($_GET['pdfview']) == 1) {
             $pageTitle = $this->resolvedPage->getTitle();
             $extenstion = empty($pageTitle) ? null : '.pdf';
             $objPDF = new \Cx\Core_Modules\Pdf\Model\Entity\PdfDocument();
             $objPDF->SetTitle($pageTitle . $extenstion);
             $objPDF->setContent($this->template->get());
             $objPDF->Create();
             exit;
         }
         // fetch the parsed webpage
         $this->template->setVariable('JAVASCRIPT', 'javascript_inserting_here');
         $endcode = $this->template->get();
         /**
          * Get all javascripts in the code, replace them with nothing, and register the js file
          * to the javascript lib. This is because we don't want something twice, and there could be
          * a theme that requires a javascript, which then could be used by a module too and therefore would
          * be loaded twice.
          */
         /* Finds all uncommented script tags, strips them out of the HTML and
          * stores them internally so we can put them in the placeholder later
          * (see JS::getCode() below)
          */
         \JS::findJavascripts($endcode);
         /*
          * Proposal:  Use this
          *     $endcode = preg_replace_callback('/<script\s.*?src=(["\'])(.*?)(\1).*?\/?>(?:<\/script>)?/i', array('JS', 'registerFromRegex'), $endcode);
          * and change JS::registerFromRegex to use index 2
          */
         // i know this is ugly, but is there another way
         $endcode = str_replace('javascript_inserting_here', \JS::getCode(), $endcode);
         // do a final replacement of all those node-urls ({NODE_<ID>_<LANG>}- placeholders) that haven't been captured earlier
         $endcode = preg_replace('/\\[\\[([A-Z0-9_-]+)\\]\\]/', '{\\1}', $endcode);
         \LinkGenerator::parseTemplate($endcode);
         // remove the meta tag X-UA-Compatible if the user agent ist neighter internet explorer nor chromeframe
         if (!preg_match('/(msie|chromeframe)/i', $_SERVER['HTTP_USER_AGENT'])) {
             $endcode = preg_replace('/<meta.*?X-UA-Compatible.*?>/i', '', $endcode);
         }
         // replace links from before contrexx 3
         $ls = new \LinkSanitizer($this, $this->getCodeBaseOffsetPath() . \Env::get('virtualLanguageDirectory') . '/', $endcode);
         $this->endcode = $ls->replace();
     } else {
         // backend meta navigation
         if ($this->template->blockExists('backend_metanavigation')) {
             // parse language navigation
             if ($this->template->blockExists('backend_language_navigation') && $this->template->blockExists('backend_language_navigation_item')) {
                 $backendLanguage = \FWLanguage::getActiveBackendLanguages();
                 if (count($backendLanguage) > 1) {
                     $this->template->setVariable('TXT_LANGUAGE', $_CORELANG['TXT_LANGUAGE']);
                     foreach ($backendLanguage as $language) {
                         $languageUrl = \Env::get('init')->getUriBy('setLang', $language['id']);
                         $this->template->setVariable(array('LANGUAGE_URL' => contrexx_raw2xhtml($languageUrl), 'LANGUAGE_NAME' => $language['name'], 'LANGUAGE_CSS' => \Env::get('init')->getBackendLangId() == $language['id'] ? 'active' : ''));
                         $this->template->parse('backend_language_navigation_item');
                     }
                     $this->template->parse('backend_language_navigation');
                 } else {
                     $this->template->hideBlock('backend_language_navigation');
                 }
             }
             $this->template->touchBlock('backend_metanavigation');
         }
         // page parsing
         $parsingTime = $this->stopTimer();
         //                var_dump($parsingTime);
         /*echo ($finishTime[0] - $startTime[0]) . '<br />';
           if (!isset($_SESSION['asdf1']) || isset($_GET['reset'])) {
               $_SESSION['asdf1'] = 0;
               $_SESSION['asdf2'] = 0;
           }
           echo $_SESSION['asdf1'] . '<br />';
           if ($_SESSION['asdf1'] > 0) {
               echo $_SESSION['asdf2'] / $_SESSION['asdf1'];
           }
           $_SESSION['asdf1']++;
           $_SESSION['asdf2'] += ($finishTime[0] - $startTime[0]);//*/
         $objAdminNav = new \adminMenu($plainCmd);
         $objAdminNav->getAdminNavbar();
         $this->template->setVariable(array('SUB_MENU_TITLE' => $subMenuTitle, 'FRONTEND_LANG_MENU' => \Env::get('init')->getUserFrontendLangMenu(), 'TXT_GENERATED_IN' => $_CORELANG['TXT_GENERATED_IN'], 'TXT_SECONDS' => $_CORELANG['TXT_SECONDS'], 'TXT_LOGOUT_WARNING' => $_CORELANG['TXT_LOGOUT_WARNING'], 'PARSING_TIME' => $parsingTime, 'LOGGED_NAME' => htmlentities($this->getUser()->objUser->getProfileAttribute('firstname') . ' ' . $this->getUser()->objUser->getProfileAttribute('lastname'), ENT_QUOTES, CONTREXX_CHARSET), 'TXT_LOGGED_IN_AS' => $_CORELANG['TXT_LOGGED_IN_AS'], 'TXT_LOG_OUT' => $_CORELANG['TXT_LOG_OUT'], 'MODULE_INDEX' => MODULE_INDEX, 'JAVASCRIPT' => \JS::getCode(), 'CX_EDITION' => $_CONFIG['coreCmsEdition'], 'CX_VERSION' => $_CONFIG['coreCmsVersion'], 'CX_CODE_NAME' => $_CONFIG['coreCmsCodeName'], 'CX_STATUS' => $_CONFIG['coreCmsStatus'], 'CX_RELEASE_DATE' => date(ASCMS_DATE_FORMAT_DATE, $_CONFIG['coreCmsReleaseDate']), 'CX_NAME' => $_CONFIG['coreCmsName']));
         // Style parsing
         if (file_exists($this->codeBaseAdminTemplatePath . '/css/' . $cmd . '.css')) {
             // check if there's a css file in the core section
             $this->template->setVariable('ADD_STYLE_URL', $this->codeBaseAdminTemplateWebPath . '/css/' . $cmd . '.css');
             $this->template->parse('additional_style');
         } elseif (file_exists($this->codeBaseModulePath . '/' . $cmd . '/template/backend.css')) {
             // of maybe in the current module directory
             $this->template->setVariable('ADD_STYLE_URL', $this->codeBaseModuleWebPath . '/' . $cmd . '/template/backend.css');
             $this->template->parse('additional_style');
         } elseif (file_exists($this->codeBaseCoreModulePath . '/' . $cmd . '/template/backend.css')) {
             // or in the core module directory
             $this->template->setVariable('ADD_STYLE_URL', $this->codeBaseCoreModuleWebPath . '/' . $cmd . '/template/backend.css');
             $this->template->parse('additional_style');
         } else {
             $this->template->hideBlock('additional_style');
         }
         /*echo '<pre>';
           print_r($_SESSION);
           /*echo '<b>Overall time: ' . (microtime(true) - $timeAtStart) . 's<br />';
           echo 'Max RAM usage: ' . formatBytes(memory_get_peak_usage()) . '<br />';
           echo 'End RAM usage: ' . formatBytes(memory_get_usage()) . '<br /></b>';*/
         $endcode = $this->template->get();
         // replace links from before contrexx 3
         $ls = new \LinkSanitizer($this, $this->getCodeBaseOffsetPath() . $this->getBackendFolderName() . '/', $endcode);
         $this->endcode = $ls->replace();
     }
     \DBG::log("(Cx: {$this->id}) Request parsing completed after {$parsingTime}");
 }