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() . ' </td>
</tr>';
$structuredDataEmpty = false;
}
if ($element->getDescription()) {
$structuredData .= '<tr>
<td><span style="color:#cc2929;">Description</span></td>
<td>' . $element->getDescription() . ' </td>
</tr>';
$structuredDataEmpty = false;
}
if ($element->getKeywords()) {
$structuredData .= '<tr>
<td><span style="color:#cc2929;">Keywords</span></td>
<td>' . $element->getKeywords() . ' </td>
</tr>';
$structuredDataEmpty = false;
}
if ($element->getProperty("navigation_name")) {
$structuredData .= '<tr>
<td><span style="color:#cc2929;">Navigation</span></td>
<td>' . $element->getProperty("navigation_name") . ' </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 . ' </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));
}