.
*/
require_once('ExportBase.php');
require_once('CFDBExport.php');
require_once('CFDBShortCodeContentParser.php');
class ExportToHtmlTemplate extends ExportBase implements CFDBExport {
/**
* @param $formName string
* @param $options array of option_name => option_value
* @return void|string
*/
public function export($formName, $options = null) {
$this->setOptions($options);
$this->setCommonOptions(true);
$filelinks = '';
$wpautop = false;
$stripBR = false;
$substituteEmptyStringForUnknownFields = false;
if ($this->options && is_array($this->options)) {
if (isset($this->options['filelinks'])) {
$filelinks = $this->options['filelinks'];
}
if (isset($this->options['wpautop'])) {
$wpautop = $this->options['wpautop'] == 'true';
}
if (isset($this->options['stripbr'])) {
$stripBR = $this->options['stripbr'] == 'true';
}
if (isset($this->options['unknownfields'])) {
$substituteEmptyStringForUnknownFields = $this->options['unknownfields'] == 'true';
}
}
// Security Check
if (!$this->isAuthorized()) {
$this->assertSecurityErrorMessage();
return;
}
// Headers
$this->echoHeaders('Content-Type: text/html; charset=UTF-8');
if (empty($options) || !isset($options['content'])) {
return;
}
// Get the data
$submitTimeKeyName = 'Submit_Time_Key';
$this->setDataIterator($formName, $submitTimeKeyName);
//$this->clearAllOutputBuffers(); // will mess up admin view of single entry
if ($this->isFromShortCode) {
ob_start();
}
$options['content'] = $this->modifyContent($options['content']);
$matches = array();
preg_match_all('/\$\{([^}]+)\}/', $options['content'], $matches);
$colNamesToSub = array();
$varNamesToSub = array();
if (!empty($matches) && is_array($matches[1])) {
foreach ($matches[1] as $aSubVar) {
// Each is expected to be a name of a column
if (in_array($aSubVar, $this->dataIterator->getDisplayColumns())) {
$colNamesToSub[] = $aSubVar;
$varNamesToSub[] = '${' . $aSubVar . '}';
}
else if ($aSubVar == 'submit_time') {
$colNamesToSub[] = 'submit_time';
$varNamesToSub[] = '${submit_time}';
}
}
}
// WordPress likes to wrap the content in
content
which messes up things when // you are putting //
// which messed up the table html.
// So we try to identify that and strip it out.
// This is related to http://codex.wordpress.org/Function_Reference/wpautop
// see also http://wordpress.org/support/topic/shortcodes-are-wrapped-in-paragraph-tags?replies=4
if (!$wpautop) {
//echo 'Initial: \'' . htmlentities($options['content']) . '\'';
if (substr($options['content'], 0, 6) == '
' &&
substr($options['content'], -3, 3) == '
') { $options['content'] = substr($options['content'], 6, strlen($options['content']) - 6 - 3); } if (substr($options['content'], 0, 4) == '
' && substr($options['content'], -3, 3) == '') {
$options['content'] = substr($options['content'], 4, strlen($options['content']) - 4 - 3);
}
//echo '
Stripped: \'' . htmlentities($options['content']) . '\'';
}
if ($stripBR) {
// Strip out BR tags presumably injected by wpautop
$options['content'] = str_replace('
', '', $options['content']);
}
// Break out sections: Before, Template, After
$before = '';
$template = '';
$after = '';
if (isset($options['content'])) {
$contentParser = new CFDBShortCodeContentParser;
list($before, $template, $after) = $contentParser->parseBeforeContentAfter($options['content']);
}
if ($before) {
// Allow for short codes in "before"
echo do_shortcode($before);
}
while ($this->dataIterator->nextRow()) {
// todo: Evaluation IF-expressions
if (empty($colNamesToSub)) {
// Process nested short codes
echo do_shortcode($template);
}
else {
$fields_with_file = null;
if ($filelinks != 'name' &&
isset($this->dataIterator->row['fields_with_file']) &&
$this->dataIterator->row['fields_with_file'] != null) {
$fields_with_file = explode(',', $this->dataIterator->row['fields_with_file']);
}
$replacements = array();
foreach ($colNamesToSub as $aCol) {
if ($fields_with_file && in_array($aCol, $fields_with_file)) {
switch ($filelinks) {
case 'url':
$replacements[] = $this->plugin->getFileUrl($this->dataIterator->row[$submitTimeKeyName], $formName, $aCol);
break;
case 'link':
if (isset($this->dataIterator->row[$aCol])) {
$replacements[] =
'' .
htmlentities($this->dataIterator->row[$aCol], null, 'UTF-8') .
'';
} else {
$replacements[] = '';
}
break;
case 'image':
case 'img':
if (isset($this->dataIterator->row[$aCol])) {
$replacements[] =
'';
} else {
$replacements[] = '';
}
break;
case 'name':
default:
if (isset($this->dataIterator->row[$aCol])) {
$replacements[] = htmlentities($this->dataIterator->row[$aCol], null, 'UTF-8');
}
}
} else {
if (isset($this->dataIterator->row[$aCol])) {
$replacements[] = htmlentities($this->dataIterator->row[$aCol], null, 'UTF-8');
} else {
$replacements[] = '';
}
}
}
// Preserve line breaks in the field
foreach ($replacements as $i => $repl) {
$replacements[$i] = nl2br($replacements[$i]); // preserve line breaks
}
// Replace variables
$output = str_replace($varNamesToSub, $replacements, $template);
if ($substituteEmptyStringForUnknownFields) {
$output = preg_replace('/\${[^}]+}/', '', $output);
}
// Process nested short codes
echo do_shortcode($output);
}
}
if ($after) {
// Allow for short codes in "after"
echo do_shortcode($after);
}
if ($this->isFromShortCode) {
// If called from a shortcode, need to return the text,
// otherwise it can appear out of order on the page
$output = ob_get_contents();
ob_end_clean();
return $output;
}
}
/**
* Intended to be overridden
* @param $template string
* @return string
*/
public function modifyContent($template) {
return $template;
}
}