ExportToHtmlTemplate

The Contact Form DB ExportToHtmlTemplate class.

Defined (1)

The class is defined in the following location(s).

/ExportToHtmlTemplate.php  
  1. class ExportToHtmlTemplate extends ExportBase implements CFDBExport { 
  2.  
  3. /** 
  4. * @param $formName string 
  5. * @param $options array of option_name => option_value 
  6. * @return void|string 
  7. */ 
  8. public function export($formName, $options = null) { 
  9. $this->setOptions($options); 
  10. $this->setCommonOptions(true); 
  11.  
  12. $filelinks = ''; 
  13. $wpautop = false; 
  14. $stripBR = false; 
  15. if ($this->options && is_array($this->options)) { 
  16. if (isset($this->options['filelinks'])) { 
  17. $filelinks = $this->options['filelinks']; 
  18. if (isset($this->options['wpautop'])) { 
  19. $wpautop = $this->options['wpautop'] == 'true'; 
  20. if (isset($this->options['stripbr'])) { 
  21. $stripBR = $this->options['stripbr'] == 'true'; 
  22.  
  23. // Security Check 
  24. if (!$this->isAuthorized()) { 
  25. $this->assertSecurityErrorMessage(); 
  26. return; 
  27.  
  28. // Headers 
  29. $this->echoHeaders('Content-Type: text/html; charset=UTF-8'); 
  30.  
  31.  
  32. if (empty($options) || !isset($options['content'])) { 
  33. return; 
  34.  
  35. if ($this->isFromShortCode) { 
  36. ob_start(); 
  37.  
  38. // Get the data 
  39. $submitTimeKeyName = 'Submit_Time_Key'; 
  40. $this->setDataIterator($formName, $submitTimeKeyName); 
  41.  
  42. $options['content'] = $this->modifyContent($options['content']); 
  43.  
  44. $matches = array(); 
  45. preg_match_all('/\$\{([^}]+)\}/', $options['content'], $matches); 
  46.  
  47. $colNamesToSub = array(); 
  48. $varNamesToSub = array(); 
  49. if (!empty($matches) && is_array($matches[1])) { 
  50. foreach ($matches[1] as $aSubVar) { 
  51. // Each is expected to be a name of a column 
  52. if (in_array($aSubVar, $this->dataIterator->getDisplayColumns())) { 
  53. $colNamesToSub[] = $aSubVar; 
  54. $varNamesToSub[] = '${' . $aSubVar . '}'; 
  55. else if ($aSubVar == 'submit_time') { 
  56. $colNamesToSub[] = 'submit_time'; 
  57. $varNamesToSub[] = '${submit_time}'; 
  58.  
  59.  
  60. // WordPress likes to wrap the content in <br />content<p> which messes up things when 
  61. // you are putting 
  62. // <tr><td>stuff<td></tr> 
  63. // as the content because it comes out 
  64. // <br /><tr><td>stuff<td></tr><p> 
  65. // which messed up the table html. 
  66. // So we try to identify that and strip it out. 
  67. // This is related to http://codex.wordpress.org/Function_Reference/wpautop 
  68. // see also http://wordpress.org/support/topic/shortcodes-are-wrapped-in-paragraph-tags?replies=4 
  69. if (!$wpautop) { 
  70. //echo 'Initial: \'' . htmlentities($options['content']) . '\''; 
  71. if (substr($options['content'], 0, 6) == '<br />' && 
  72. substr($options['content'], -3, 3) == '<p>') { 
  73. $options['content'] = substr($options['content'], 6, strlen($options['content']) - 6 - 3); 
  74. if (substr($options['content'], 0, 4) == '</p>' && 
  75. substr($options['content'], -3, 3) == '<p>') { 
  76. $options['content'] = substr($options['content'], 4, strlen($options['content']) - 4 - 3); 
  77. //echo '<br/>Stripped: \'' . htmlentities($options['content']) . '\''; 
  78.  
  79. if ($stripBR) { 
  80. // Strip out BR tags presumably injected by wpautop 
  81. $options['content'] = str_replace('<br />', '', $options['content']); 
  82.  
  83. // Break out sections: Before, Template, After 
  84. $before = ''; 
  85. $template = ''; 
  86. $after = ''; 
  87. if (isset($options['content'])) { 
  88. $contentParser = new CFDBShortCodeContentParser; 
  89. list($before, $template, $after) = $contentParser->parseBeforeContentAfter($options['content']); 
  90.  
  91. if ($before) { 
  92. // Allow for short codes in "before" 
  93. echo do_shortcode($before); 
  94.  
  95. while ($this->dataIterator->nextRow()) { 
  96. // todo: Evaluation IF-expressions 
  97.  
  98. if (empty($colNamesToSub)) { 
  99. // Process nested short codes 
  100. echo do_shortcode($template); 
  101. else { 
  102. $fields_with_file = null; 
  103. if ($filelinks != 'name' && 
  104. isset($this->dataIterator->row['fields_with_file']) && 
  105. $this->dataIterator->row['fields_with_file'] != null) { 
  106. $fields_with_file = explode(', ', $this->dataIterator->row['fields_with_file']); 
  107. $replacements = array(); 
  108. foreach ($colNamesToSub as $aCol) { 
  109. if ($fields_with_file && in_array($aCol, $fields_with_file)) { 
  110. switch ($filelinks) { 
  111. case 'url': 
  112. $replacements[] = $this->plugin->getFileUrl($this->dataIterator->row[$submitTimeKeyName], $formName, $aCol); 
  113. break; 
  114. case 'link': 
  115. if (isset($this->dataIterator->row[$aCol])) { 
  116. $replacements[] = 
  117. '<a href="' . 
  118. $this->plugin->getFileUrl($this->dataIterator->row[$submitTimeKeyName], $formName, $aCol) . 
  119. '">' . 
  120. htmlentities($this->dataIterator->row[$aCol], null, 'UTF-8') . 
  121. '</a>'; 
  122. } else { 
  123. $replacements[] = ''; 
  124. break; 
  125. case 'image': 
  126. case 'img': 
  127. if (isset($this->dataIterator->row[$aCol])) { 
  128. $replacements[] = 
  129. '<img src="' . 
  130. $this->plugin->getFileUrl($this->dataIterator->row[$submitTimeKeyName], $formName, $aCol) . 
  131. '" alt="' . 
  132. htmlentities($this->dataIterator->row[$aCol], null, 'UTF-8') . 
  133. '" />'; 
  134. } else { 
  135. $replacements[] = ''; 
  136. break; 
  137. case 'name': 
  138. default: 
  139. if (isset($this->dataIterator->row[$aCol])) { 
  140. $replacements[] = htmlentities($this->dataIterator->row[$aCol], null, 'UTF-8'); 
  141. } else { 
  142. if (isset($this->dataIterator->row[$aCol])) { 
  143. $replacements[] = htmlentities($this->dataIterator->row[$aCol], null, 'UTF-8'); 
  144. } else { 
  145. $replacements[] = ''; 
  146. // Preserve line breaks in the field 
  147. foreach ($replacements as $i => $repl) { 
  148. $replacements[$i] = nl2br($replacements[$i]); // preserve line breaks 
  149. // Process nested short codes 
  150. echo do_shortcode(str_replace($varNamesToSub, $replacements, $template)); 
  151.  
  152. if ($after) { 
  153. // Allow for short codes in "after" 
  154. echo do_shortcode($after); 
  155.  
  156.  
  157. if ($this->isFromShortCode) { 
  158. // If called from a shortcode, need to return the text,  
  159. // otherwise it can appear out of order on the page 
  160. $output = ob_get_contents(); 
  161. ob_end_clean(); 
  162. return $output; 
  163.  
  164.  
  165. /** 
  166. * Intended to be overridden 
  167. * @param $template string 
  168. * @return string 
  169. */ 
  170. public function modifyContent($template) { 
  171. return $template; 
  172.