Commit a2218fea authored by Carsten  Rose's avatar Carsten Rose
Browse files

Merge branch 'F6596-download-tt_content-uid' into 'master'

Feature #6596 uid ExcelExport / PDF

See merge request !92
parents b97bbc72 c3fe3e7b
Pipeline #1000 passed with stage
in 1 minute and 43 seconds
......@@ -6319,8 +6319,8 @@ Parameter and (element) sources
* If only one `file` is specified, the default is `file`.
* If there is a) a page defined or b) multiple elements, the default is `pdf`.
* *element sources* - for `M:pdf` or `M:zip`, all of the following three element sources might be specified multiple times.
Any combination and order of the three options are allowed.
* *element sources* - for `M:pdf` or `M:zip`, all of the following element sources may be specified multiple times.
Any combination and order of these options are allowed.
* *file*: `F:<pathFileName>` - relative or absolute pathFileName offered for a) download (single), or to be concatenated
in a PDF or ZIP.
......@@ -6340,6 +6340,12 @@ Parameter and (element) sources
* *url*: `u:<url>` - any URL, pointing to an internal or external destination.
* *uid*: `uid:<int>` - the tt_contents.uid of a QFQ PageContent record (shown on hover in the backend). This will render
only the raw QFQ processed bodytext of the specified PageContent, without additional tags, styles, or CSS includes.
QFQ will retrieve the PageContent's bodytext from the Typo3 database, parse it, and render it as a PDF. Parameters can be
passed: `uid:<int>[?arg1=value1][&arg2=value2][...]` and will be available in the SIP store for the QFQ PageContent,
or passed as wkhtmltopdf arguments, if applicable.
* *WKHTML Options* for `page`, `urlParam` or `url`:
* The 'HTML to PDF' will be done via `wkhtmltopdf`.
......@@ -6531,33 +6537,23 @@ Injecting data into the Excel file is done in the same way in both modes: a Typo
or tags) contains one or more Typo3 QFQ records. Those QFQ records will create plain ASCII output.
If the export file has to be customized (colors, pictures, headlines, ...), the `Template` mode is the preferred option.
IT's much easier to do all cusomizations via Excel and creating a template than by coding in QFQ / Excel export notation.
It's much easier to do all customizations via Excel and creating a template than by coding in QFQ / Excel export notation.
Setup
'''''
* Create a special column name `_excel` (or `_link`) in QFQ/Report. As a source, define a T3 page, which have to deliver
the dynamic content (also: `excel-export-sample_`). ::
SELECT CONCAT('d:final.xlsx|M:excel|s:1|t:Excel (new)|p:?id=exceldata') AS _link
* Create a T3 page which delivers the content.
* Create a special column name `_excel` (or `_link`) in QFQ/Report. As a source, define a T3 PageContent, which has to
deliver the dynamic content (also `_excel-export-sample`). ::
* Disable all HTML header and wrapping code on that page. It's also a good idea to limit access to such page on localhost,
your development network and your webserver address. Typoscript setup: ::
config.disableAllHeaderCode = 1
tt_content.stdWrap >
page >
page = PAGE
SELECT CONCAT('d:final.xlsx|M:excel|s:1|t:Excel (new)|uid:43') AS _link
[usergroup = *] || [IP = 127.0.0.1,192.168.1.*,<webserver IP>]
page.10 < styles.content.get
[else]
page.10 = TEXT
page.10.value = access forbidden
[global]
* Create a T3 PageContent which delivers the content.
* It is recommended to use the `uid:<int>` syntax for excel imports, because there should be no html code on the
resulting content. QFQ will retrieve the PageContent's bodytext from the Typo3 database, parse it, and pass the
result as the instructions for filling the excel file.
* Parameters can be passed: `uid:43?param=value&param2=value2` and will be accessible in the SIP Store (S) in the
QFQ PageContent.
* Use the regular QFQ Report syntax to create some output.
* The newline at the end of every line needs to be CHAR(10). To make it simpler, the special column name `... AS _XLS`
(see _XLS, _XLSs, _XLSb, _XLSn) can be used.
......@@ -6628,14 +6624,14 @@ In Report Syntax: ::
Excel export samples: ::
# From scratch (both are the same, one with '_excel' the other with '_link')
SELECT CONCAT('d:new.xlsx|t:Excel (new)|p:?id=exceldata') AS _excel
SELECT CONCAT('d:new.xlsx|t:Excel (new)|p:?id=exceldata|M:excel|s:1') AS _link
SELECT CONCAT('d:new.xlsx|t:Excel (new)|uid:54') AS _excel
SELECT CONCAT('d:new.xlsx|t:Excel (new)|uid:54|M:excel|s:1') AS _link
# Template
SELECT CONCAT('d:final.xlsx|t:Excel (template)|F:fileadmin/template.xlsx|p:?id=exceldata') AS _excel
SELECT CONCAT('d:final.xlsx|t:Excel (template)|F:fileadmin/template.xlsx|uid:24') AS _excel
# With parameter (via SIP) - get the Parameter on page 'exceldata' with '{{arg1:S}}' and '{{arg2:S}}'
SELECT CONCAT('d:final.xlsx|t:Excel (parameter)|p:?id=exceldata&_sip=1&arg1=hello&arg2=world') AS _excel
SELECT CONCAT('d:final.xlsx|t:Excel (parameter)|uid:32&arg1=hello&arg2=world') AS _excel
.. _drag_and_drop:
......
......@@ -1449,6 +1449,7 @@ const PARAM_TOKEN_DELIMITER = ':';
const TOKEN_URL = 'u';
const TOKEN_MAIL = 'm';
const TOKEN_PAGE = 'p';
const TOKEN_UID = 'uid';
const TOKEN_DOWNLOAD = 'd';
const TOKEN_COPY_TO_CLIPBOARD = 'y';
......
......@@ -87,13 +87,18 @@ class QuickFormQuery {
/**
* @var array
*/
private $t3data = array(); // FormEelement Definition: all formElement.class='native' of the loaded form
private $t3data = array(); // FormElement Definition: all formElement.class='native' of the loaded form
/**
* @var bool
*/
private $phpUnit = false;
/**
* @var bool
*/
private $inlineReport = false;
/**
* @var Session
*/
......@@ -122,15 +127,16 @@ class QuickFormQuery {
*
* @param array $t3data
* @param bool $phpUnit
* @param bool $inlineReport
*
* @throws CodeException
* @throws DbException
* @throws UserFormException
* @throws UserReportException
*/
public function __construct(array $t3data = array(), $phpUnit = false) {
public function __construct(array $t3data = array(), $phpUnit = false, $inlineReport = true) {
$this->phpUnit = $phpUnit;
$this->inlineReport = $inlineReport;
mb_internal_encoding("UTF-8");
......@@ -1369,7 +1375,8 @@ class QuickFormQuery {
$report = new Report($this->t3data, $this->eval, $this->phpUnit);
$html = '';
if ($this->store->getVar(TYPO3_BE_USER, STORE_TYPO3, SANITIZE_ALLOW_ALNUMX)) {
$beUserLoggedIn = $this->store->getVar(TYPO3_BE_USER, STORE_TYPO3, SANITIZE_ALLOW_ALNUMX);
if ($beUserLoggedIn && $this->inlineReport) {
$html .= $this->buildInlineReport();
}
$html .= $report->process($this->t3data[T3DATA_BODYTEXT]);
......@@ -1435,11 +1442,7 @@ class QuickFormQuery {
}
$bodytext = Support::htmlEntityEncodeDecode(MODE_DECODE, $_POST[REPORT_INLINE_BODYTEXT]);
$dbT3 = $this->store->getVar(SYSTEM_T3_DB_NAME, STORE_SYSTEM . STORE_EMPTY, SANITIZE_ALLOW_ALNUMX);
if ($dbT3 == '') {
$dbData = $this->store->getVar(SYSTEM_DB_NAME_DATA, STORE_SYSTEM, SANITIZE_ALLOW_ALNUMX);
$dbT3 = substr($dbData, 0, strrpos($dbData, "_") + 1) . 't3';
}
$dbT3 = $this->store->getDbT3Name();
// Update bodytext
$sql = "UPDATE $dbT3.tt_content SET bodytext = ?, tstamp = UNIX_TIMESTAMP(NOW()) WHERE uid = ?";
......
......@@ -27,6 +27,7 @@ require_once(__DIR__ . '/Monitor.php');
require_once(__DIR__ . '/../exceptions/DownloadException.php');
require_once(__DIR__ . '/Excel.php');
require_once(__DIR__ . '/../helper/DownloadPage.php');
require_once(__DIR__ . '/../QuickFormQuery.php');
//require_once(__DIR__ . '/../Evaluate.php');
//require_once(__DIR__ . '/../helper/KeyValueStringParser.php');
......@@ -134,7 +135,7 @@ class Download {
foreach ($files AS $filename) {
$mimetype = mime_content_type($filename);
if ($mimetype != 'application/pdf') {
throw new downloadException("Error concat file $filename. Mimetype 'application/pdf' expected, got: $mimetype", ERROR_DOWNLOAD_UNEXPECTED_MIME_TYPE);
throw new DownloadException("Error concat file $filename. Mimetype 'application/pdf' expected, got: $mimetype", ERROR_DOWNLOAD_UNEXPECTED_MIME_TYPE);
}
}
......@@ -214,9 +215,13 @@ class Download {
* @param string $rcData - With $downloadMode=DOWNLOAD_MODE_EXCEL, this contains the rendered code from the given T3 page.
* @return string filename - already ready or fresh exported. Fresh exported needs to be deleted later.
* @throws CodeException
* @throws DbException
* @throws DownloadException
* @throws UserFormException
* @throws UserReportException
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
private function getElement($element, $downloadMode, &$rcData) {
......@@ -232,13 +237,17 @@ class Download {
$token = $arr[0];
$value = $arr[1];
if ($token === TOKEN_UID) { // extract uid
$uidParamsArr = explode('&', $value, 2);
$uid = $uidParamsArr[0];
$value = $uidParamsArr[1] ?? ''; // additional params
}
switch ($token) {
case TOKEN_URL:
case TOKEN_URL_PARAM:
case TOKEN_PAGE:
if ($downloadMode == DOWNLOAD_MODE_EXCEL) {
case TOKEN_UID:
$urlParam = OnString::splitParam($value, $rcArgs, $rcSipEncode);
$urlParamString = KeyValueStringParser::unparse($urlParam, '=', '&');
if ($rcSipEncode) {
......@@ -246,11 +255,32 @@ class Download {
$urlParamString = $sip->queryStringToSip($urlParamString, RETURN_URL);
}
if ($downloadMode == DOWNLOAD_MODE_EXCEL) {
if ($token === TOKEN_UID) {
$rcData = $this->getEvaluatedBodytext($uid, $urlParam);
} else {
$baseUrl = $this->store->getVar(SYSTEM_BASE_URL, STORE_SYSTEM);
$rcData = DownloadPage::getContent($urlParamString, $baseUrl);
}
} else {
if ($token === TOKEN_UID) {
// create tmp html document with bodytext
$htmlText = $this->getEvaluatedBodytext($uid, $urlParam);
$tmpFilename = HelperFile::tempnam() . '.html';
$tmpFile = fopen($tmpFilename, "w") or die('Cannot create file: '.$tmpFilename);
fwrite($tmpFile, $htmlText);
fclose($tmpFile);
$rcArgsString = KeyValueStringParser::unparse($rcArgs, '=', '&');
$url = Support::mergeUrlComponents('', $tmpFilename, $rcArgsString);
$filename = $this->html2pdf->page2pdf($token, $url);
HelperFile::cleanTempFiles([$tmpFilename]);
} else {
$filename = $this->html2pdf->page2pdf($token, $value);
}
}
break;
case TOKEN_FILE:
......@@ -265,6 +295,34 @@ class Download {
return $filename;
}
/**
* @param $uid
* @param array $urlParam
*
* @return string
* @throws CodeException
* @throws DbException
* @throws DownloadException
* @throws UserFormException
* @throws UserReportException
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
private function getEvaluatedBodyText($uid, $urlParam) {
foreach($urlParam as $key => $paramValue) {
$this->store->setVar($key, $paramValue, STORE_SIP);
}
$dbT3 = $this->store->getDbT3Name();
$sql = "SELECT bodytext FROM $dbT3.tt_content WHERE uid = ?";
$tt_content = $this->db->sql($sql, ROW_EXPECT_1, [$uid]);
$qfq = new QuickFormQuery([T3DATA_BODYTEXT => $tt_content[T3DATA_BODYTEXT]], false,false);
return $qfq->process();
}
/**
* Creates a ZIP Files of all given $files
......@@ -318,6 +376,7 @@ class Download {
* @param string $outputMode OUTPUT_MODE_DIRECT | OUTPUT_MODE_FILE
* @return string Filename of the generated file. The filename only points to a real existing filename with $outputMode=OUTPUT_MODE_FILE
* @throws CodeException
* @throws DbException
* @throws DownloadException
* @throws UserFormException
* @throws UserReportException
......
......@@ -121,7 +121,7 @@ class Html2Pdf {
* Converts a Webpage (URL) to a PDF file.
* The URL might be a local Typo3 page (without hostname, starting with the parameter) or a full URL.
*
* @param string $token TOKEN_URL | TOKEN_URL_PARAM
* @param string $token TOKEN_URL | TOKEN_URL_PARAM | TOKEN_PAGE | TOKEN_UID
* @param string $url id=exportPage&r=123, www.nzz.ch/issue?id=456
*
* @return string rendered file - please delete later
......@@ -135,10 +135,11 @@ class Html2Pdf {
$host = '';
switch ($token) {
case TOKEN_UID:
case TOKEN_URL:
$arr = explode('?', $url, 2);
$host = $arr[0];
$urlParamString = empty($arr[1]) ? '' : $arr[1];
$urlParamString = $arr[1] ?? '';
break;
case TOKEN_URL_PARAM:
case TOKEN_PAGE:
......@@ -160,11 +161,9 @@ class Html2Pdf {
$urlParamString = $this->sip->queryStringToSip($urlParamString, RETURN_URL);
}
if ($urlParamString != '') {
$url = Support::mergeUrlComponents('', $host, $urlParamString);
}
if (substr($url, 0, 4) != 'http') {
if (substr($url, 0, 4) != 'http' && $token != TOKEN_UID) {
$url = 'http://' . $url;
}
......
......@@ -74,6 +74,7 @@ require_once(__DIR__ . '/Thumbnail.php');
const NAME_URL = 'url';
const NAME_MAIL = 'mail';
const NAME_PAGE = 'page';
const NAME_UID = 'uid';
const NAME_TEXT = 'text';
const NAME_DOWNLOAD = DOWNLOAD_EXPORT_FILENAME;
const NAME_COLLECT_ELEMENTS = 'downloadElements'; // array with element sources
......@@ -190,6 +191,7 @@ class Link {
TOKEN_URL => NAME_URL,
TOKEN_MAIL => NAME_MAIL,
TOKEN_PAGE => NAME_PAGE,
TOKEN_UID => NAME_UID,
TOKEN_DOWNLOAD => NAME_DOWNLOAD,
TOKEN_DOWNLOAD_MODE => NAME_DOWNLOAD_MODE,
TOKEN_TEXT => NAME_TEXT,
......@@ -235,6 +237,7 @@ class Link {
TOKEN_URL => LINK_ANCHOR,
TOKEN_MAIL => LINK_ANCHOR,
TOKEN_PAGE => LINK_ANCHOR,
TOKEN_UID => LINK_ANCHOR,
TOKEN_DOWNLOAD => LINK_ANCHOR,
TOKEN_FILE => NAME_FILE,
TOKEN_COPY_TO_CLIPBOARD => LINK_ANCHOR,
......@@ -542,7 +545,8 @@ class Link {
// Store value
if ((isset($rcTokenGiven[TOKEN_DOWNLOAD]) || isset($rcTokenGiven[TOKEN_COPY_TO_CLIPBOARD])) &&
($key == TOKEN_PAGE || $key == TOKEN_URL || $key == TOKEN_URL_PARAM || $key == TOKEN_FILE || $key == TOKEN_FILE_DEPRECATED)) {
($key == TOKEN_PAGE || $key == TOKEN_URL || $key == TOKEN_URL_PARAM || $key == TOKEN_UID ||
$key == TOKEN_FILE || $key == TOKEN_FILE_DEPRECATED)) {
$vars[NAME_COLLECT_ELEMENTS][] = $key . ':' . $value;
......
......@@ -962,4 +962,27 @@ class Store {
$store->setStore(array_merge($store->getStore($storeName), $append), $storeName, true);
}
/**
* Gets the db T3 name:
* - if defined by T3_DB_NAME in config
* - else: construct databasename_t3 from databasename_db
*
* @return string
* @throws CodeException
* @throws DbException
* @throws UserFormException
* @throws UserReportException
*/
public static function getDbT3Name() {
$dbT3Name = self::getVar(SYSTEM_T3_DB_NAME, STORE_SYSTEM . STORE_EMPTY, SANITIZE_ALLOW_ALNUMX);
if (!$dbT3Name) {
$dbIndexData = self::getVar(SYSTEM_DB_INDEX_DATA, STORE_SYSTEM);
$dbData = new Database($dbIndexData);
$dbDataName = $dbData->getDbName();
$dbT3Name = substr($dbDataName, 0, strrpos($dbDataName, "_") + 1) . 't3';
}
return $dbT3Name;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment