Pimcore\Model\Document\Service::render PHP Method

render() public static method

static function to render a document outside of a view
public static render ( Document $document, array $params = [], boolean $useLayout = false ) : string
$document Pimcore\Model\Document
$params array
$useLayout boolean
return string
    public static function render(Document $document, $params = [], $useLayout = false)
    {
        $layout = null;
        $existingActionHelper = null;
        if (\Zend_Controller_Action_HelperBroker::hasHelper("layout")) {
            $existingActionHelper = \Zend_Controller_Action_HelperBroker::getExistingHelper("layout");
        }
        $layoutInCurrentAction = \Zend_Layout::getMvcInstance() instanceof \Zend_Layout ? \Zend_Layout::getMvcInstance()->getLayout() : false;
        $viewHelper = \Zend_Controller_Action_HelperBroker::getExistingHelper("ViewRenderer");
        if ($viewHelper) {
            if ($viewHelper->view === null) {
                $viewHelper->initView(PIMCORE_WEBSITE_PATH . "/views");
            }
            $view = $viewHelper->view;
        } else {
            $view = new \Pimcore\View();
        }
        // add the view script path from the website module to the view, because otherwise it's not possible to call
        // this method out of other modules to render documents, eg. sending e-mails out of an plugin with Pimcore_Mail
        $moduleDirectory = \Zend_Controller_Front::getInstance()->getModuleDirectory($document->getModule());
        if (!empty($moduleDirectory)) {
            $view->addScriptPath($moduleDirectory . "/views/layouts");
            $view->addScriptPath($moduleDirectory . "/views/scripts");
        } else {
            $view->addScriptPath(PIMCORE_FRONTEND_MODULE . "/views/layouts");
            $view->addScriptPath(PIMCORE_FRONTEND_MODULE . "/views/scripts");
        }
        $documentBackup = null;
        if ($view->document) {
            $documentBackup = $view->document;
        }
        $view->document = $document;
        if ($useLayout) {
            if (!($layout = \Zend_Layout::getMvcInstance())) {
                $layout = \Zend_Layout::startMvc();
                $layout->setViewSuffix(View::getViewScriptSuffix());
                if ($layoutHelper = $view->getHelper("layout")) {
                    $layoutHelper->setLayout($layout);
                }
            }
            $layout->setLayout("--modification-indicator--");
        }
        $params["document"] = $document;
        foreach ($params as $key => $value) {
            if (!$view->{$key}) {
                $view->{$key} = $value;
            }
        }
        $content = $view->action($document->getAction(), $document->getController(), $document->getModule(), $params);
        //has to be called after $view->action so we can determine if a layout is enabled in $view->action()
        if ($useLayout) {
            if ($layout instanceof \Zend_Layout) {
                $layout->{$layout->getContentKey()} = $content;
                if (is_array($params)) {
                    foreach ($params as $key => $value) {
                        $layout->getView()->{$key} = $value;
                    }
                }
                // when using Document\Service::render() you have to set a layout in the view ($this->layout()->setLayout("mylayout"))
                if ($layout->getLayout() != "--modification-indicator--") {
                    $content = $layout->render();
                }
                //deactivate the layout if it was not activated in the called action
                //otherwise we would activate the layout in the called action
                \Zend_Layout::resetMvcInstance();
                if (!$layoutInCurrentAction) {
                    $layout->disableLayout();
                } else {
                    $layout = \Zend_Layout::startMvc();
                    $layout->setViewSuffix(View::getViewScriptSuffix());
                    // set pimcore specifiy view suffix
                    $layout->setLayout($layoutInCurrentAction);
                    $view->getHelper("Layout")->setLayout($layout);
                    if ($existingActionHelper) {
                        \Zend_Controller_Action_HelperBroker::removeHelper("layout");
                        \Zend_Controller_Action_HelperBroker::addHelper($existingActionHelper);
                        $pluginClass = $layout->getPluginClass();
                        $front = $existingActionHelper->getFrontController();
                        if ($front->hasPlugin($pluginClass)) {
                            $plugin = $front->getPlugin($pluginClass);
                            $plugin->setLayoutActionHelper($existingActionHelper);
                        }
                    }
                }
                $layout->{$layout->getContentKey()} = null;
                //reset content
            }
        }
        if ($documentBackup) {
            $view->document = $documentBackup;
        }
        if (\Pimcore\Config::getSystemConfig()->outputfilters->less) {
            $content = \Pimcore\Tool\Less::processHtml($content);
        }
        return $content;
    }

Usage Example

Exemplo n.º 1
0
 public function wordExportAction()
 {
     //error_reporting(E_ERROR);
     //ini_set("display_errors", "off");
     $id = $this->getParam("id");
     $data = \Zend_Json::decode($this->getParam("data"));
     $source = $this->getParam("source");
     $exportFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/" . $id . ".html";
     if (!is_file($exportFile)) {
         /*file_put_contents($exportFile, '<!DOCTYPE html>' . "\n" . '<html>
               <head>
                   <style type="text/css">' . file_get_contents(PIMCORE_PATH . "/static/css/word-export.css") . '</style>
               </head>
               <body>
           ');*/
         File::put($exportFile, '<style type="text/css">' . file_get_contents(PIMCORE_PATH . "/static6/css/word-export.css") . '</style>');
     }
     foreach ($data as $el) {
         try {
             $element = Element\Service::getElementById($el["type"], $el["id"]);
             $output = "";
             // check supported types (subtypes)
             if (!in_array($element->getType(), array("page", "snippet", "email", "object"))) {
                 continue;
             }
             if ($element instanceof Element\ElementInterface) {
                 $output .= '<h1 class="element-headline">' . ucfirst($element->getType()) . " - " . $element->getFullPath() . ' (ID: ' . $element->getId() . ')</h1>';
             }
             if ($element instanceof Document\PageSnippet) {
                 if ($element instanceof Document\Page) {
                     $structuredDataEmpty = true;
                     $structuredData = '
                         <table border="1" cellspacing="0" cellpadding="5">
                             <tr>
                                 <td colspan="2"><span style="color:#cc2929;font-weight: bold;">Structured Data</span></td>
                             </tr>
                     ';
                     if ($element->getTitle()) {
                         $structuredData .= '<tr>
                                 <td><span style="color:#cc2929;">Title</span></td>
                                 <td>' . $element->getTitle() . '&nbsp;</td>
                             </tr>';
                         $structuredDataEmpty = false;
                     }
                     if ($element->getDescription()) {
                         $structuredData .= '<tr>
                                 <td><span style="color:#cc2929;">Description</span></td>
                                 <td>' . $element->getDescription() . '&nbsp;</td>
                             </tr>';
                         $structuredDataEmpty = false;
                     }
                     if ($element->getKeywords()) {
                         $structuredData .= '<tr>
                                 <td><span style="color:#cc2929;">Keywords</span></td>
                                 <td>' . $element->getKeywords() . '&nbsp;</td>
                             </tr>';
                         $structuredDataEmpty = false;
                     }
                     if ($element->getProperty("navigation_name")) {
                         $structuredData .= '<tr>
                                 <td><span style="color:#cc2929;">Navigation</span></td>
                                 <td>' . $element->getProperty("navigation_name") . '&nbsp;</td>
                             </tr>';
                         $structuredDataEmpty = false;
                     }
                     $structuredData .= '</table>';
                     if (!$structuredDataEmpty) {
                         $output .= $structuredData;
                     }
                 }
                 // we need to set the parameter "pimcore_admin" here to be able to render unpublished documents
                 $reqBak = $_REQUEST;
                 $_REQUEST["pimcore_admin"] = true;
                 $html = Document\Service::render($element, array(), false);
                 $_REQUEST = $reqBak;
                 // set the request back to original
                 $html = preg_replace("@</?(img|meta|div|section|aside|article|body|bdi|bdo|canvas|embed|footer|head|header|html)([^>]+)?>@", "", $html);
                 $html = preg_replace('/<!--(.*)-->/Uis', '', $html);
                 include_once "simple_html_dom.php";
                 $dom = str_get_html($html);
                 if ($dom) {
                     // remove containers including their contents
                     $elements = $dom->find("form,script,style,noframes,noscript,object,area,mapm,video,audio,iframe,textarea,input,select,button,");
                     if ($elements) {
                         foreach ($elements as $el) {
                             $el->outertext = "";
                         }
                     }
                     $clearText = function ($string) {
                         $string = str_replace("\r\n", "", $string);
                         $string = str_replace("\n", "", $string);
                         $string = str_replace("\r", "", $string);
                         $string = str_replace("\t", "", $string);
                         $string = preg_replace('/&[a-zA-Z0-9]+;/', '', $string);
                         // remove html entities
                         $string = preg_replace('#[ ]+#', '', $string);
                         return $string;
                     };
                     // remove empty tags (where it matters)
                     $elements = $dom->find("a, li");
                     if ($elements) {
                         foreach ($elements as $el) {
                             $string = $clearText($el->plaintext);
                             if (empty($string)) {
                                 $el->outertext = "";
                             }
                         }
                     }
                     // replace links => links get [Linktext]
                     $elements = $dom->find("a");
                     if ($elements) {
                         foreach ($elements as $el) {
                             $string = $clearText($el->plaintext);
                             if (!empty($string)) {
                                 $el->outertext = "[" . $el->plaintext . "]";
                             } else {
                                 $el->outertext = "";
                             }
                         }
                     }
                     $html = $dom->save();
                     $dom->clear();
                     unset($dom);
                     // force closing tags (simple_html_dom doesn't seem to support this anymore)
                     $doc = new \DOMDocument();
                     libxml_use_internal_errors(true);
                     $doc->loadHTML('<?xml encoding="UTF-8"><article>' . $html . "</article>");
                     libxml_clear_errors();
                     $html = $doc->saveHTML();
                     $bodyStart = strpos($html, "<body>") + 6;
                     $bodyEnd = strpos($html, "</body>");
                     if ($bodyStart && $bodyEnd) {
                         $html = substr($html, $bodyStart, $bodyEnd - $bodyStart);
                     }
                     $output .= $html;
                 }
             } else {
                 if ($element instanceof Object\Concrete) {
                     $hasContent = false;
                     if ($fd = $element->getClass()->getFieldDefinition("localizedfields")) {
                         $definitions = $fd->getFielddefinitions();
                         $locale = new \Zend_Locale(str_replace("-", "_", $source));
                         if (Tool::isValidLanguage((string) $locale)) {
                             $locale = (string) $locale;
                         } else {
                             $locale = $locale->getLanguage();
                         }
                         $output .= '
                         <table border="1" cellspacing="0" cellpadding="2">
                             <tr>
                                 <td colspan="2"><span style="color:#cc2929;font-weight: bold;">Localized Data</span></td>
                             </tr>
                     ';
                         foreach ($definitions as $definition) {
                             // check allowed datatypes
                             if (!in_array($definition->getFieldtype(), array("input", "textarea", "wysiwyg"))) {
                                 continue;
                             }
                             $content = $element->{"get" . ucfirst($definition->getName())}($locale);
                             if (!empty($content)) {
                                 $output .= '
                             <tr>
                                 <td><span style="color:#cc2929;">' . $definition->getTitle() . ' (' . $definition->getName() . ')<span></td>
                                 <td>' . $content . '&nbsp;</td>
                             </tr>
                             ';
                                 $hasContent = true;
                             }
                         }
                         $output .= '</table>';
                     }
                     if (!$hasContent) {
                         $output = "";
                         // there's no content in the object, so reset all contents and do not inclide it in the export
                     }
                 }
             }
             // append contents
             if (!empty($output)) {
                 $f = fopen($exportFile, "a+");
                 fwrite($f, $output);
                 fclose($f);
             }
         } catch (\Exception $e) {
             \Logger::error("Word Export: " . $e->getMessage());
             \Logger::error($e);
         }
     }
     $this->_helper->json(array("success" => true));
 }
All Usage Examples Of Pimcore\Model\Document\Service::render