ExportToValue

The Contact Form DB ExportToValue class.

Defined (1)

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

/ExportToValue.php  
  1. class ExportToValue extends ExportBase implements CFDBExport { 
  2.  
  3. public function export($formName, $options = null) { 
  4.  
  5. // Allow for multiple form name inputs, comma-delimited 
  6. $tmp = explode(', ', $formName); 
  7. if (count($tmp) > 1) { 
  8. $formName = &$tmp; 
  9. else if ($formName == '*') { 
  10. $formName = null; // Allow for no form specified implying all forms 
  11.  
  12. $this->setOptions($options); 
  13. $this->setCommonOptions(); 
  14.  
  15. // Security Check 
  16. if (!$this->isAuthorized()) { 
  17. $this->assertSecurityErrorMessage(); 
  18. return; 
  19.  
  20. // Break out sections: Before, Content, After 
  21. $before = ''; 
  22. $content = ''; 
  23. $after = ''; 
  24. if (isset($options['content'])) { 
  25. $contentParser = new CFDBShortCodeContentParser; 
  26. list($before, $content, $after) = $contentParser->parseBeforeContentAfter($options['content']); 
  27. if ($before) { 
  28. $before = do_shortcode($before); 
  29. if ($after) { 
  30. $after = do_shortcode($after); 
  31.  
  32. // See if a function is to be applied 
  33. $funct = null; 
  34. $delimiter = ', '; 
  35. if ($this->options && is_array($this->options)) { 
  36. if (isset($this->options['function'])) { 
  37. $funct = $this->options['function']; 
  38. if (isset($this->options['delimiter'])) { 
  39. $delimiter = $this->options['delimiter']; 
  40.  
  41. // Headers 
  42. // don't set content type to text because in some browsers this becomes 
  43. // the content type for the whole HTML page.  
  44. $this->echoHeaders(); //'Content-Type: text/plain; charset=UTF-8'); 
  45.  
  46. // Get the data 
  47. $this->setDataIterator($formName); 
  48.  
  49. // count function or coming from cfdb-count shortcode 
  50. if (count($this->showColumns) == 0 && 
  51. count($this->hideColumns) == 0) { 
  52. if ($funct == 'count') { 
  53. $count = 0; 
  54. while ($this->dataIterator->nextRow()) { 
  55. $count += 1; 
  56. if ($this->isFromShortCode) { 
  57. return $before . $count . $after; 
  58. else { 
  59. echo $before . $count . $after; 
  60. return; 
  61.  
  62.  
  63. if ($funct) { 
  64. // Apply function to dataset 
  65. switch ($funct) { 
  66. case 'count': 
  67. $count = 0; 
  68. $colsPerRow = count($this->dataIterator->getDisplayColumns()); 
  69. while ($this->dataIterator->nextRow()) { 
  70. $count += $colsPerRow; 
  71. if ($this->isFromShortCode) { 
  72. return $before . $count . $after; 
  73. else { 
  74. echo $before . $count . $after; 
  75. return; 
  76.  
  77. case 'min': 
  78. $min = null; 
  79. while ($this->dataIterator->nextRow()) { 
  80. foreach ($this->dataIterator->getDisplayColumns() as $col) { 
  81. $val = $this->dataIterator->row[$col]; 
  82. if (is_numeric($val)) { 
  83. if ($min === null) { 
  84. $min = $val; 
  85. else { 
  86. if ($val < $min) { 
  87. $min = $val; 
  88. if ($this->isFromShortCode) { 
  89. return $before . $min . $after; 
  90. else { 
  91. echo $before . $min . $after; 
  92. return; 
  93.  
  94. case 'max': 
  95. $max = null; 
  96. while ($this->dataIterator->nextRow()) { 
  97. foreach ($this->dataIterator->getDisplayColumns() as $col) { 
  98. $val = $this->dataIterator->row[$col]; 
  99. if (is_numeric($val)) { 
  100. if ($max === null) { 
  101. $max = $val; 
  102. else { 
  103. if ($val > $max) { 
  104. $max = $val; 
  105. if ($this->isFromShortCode) { 
  106. return $before . $max . $after; 
  107. else { 
  108. echo $before . $max . $after; 
  109. return; 
  110.  
  111.  
  112. case 'sum': 
  113. $sum = 0; 
  114. while ($this->dataIterator->nextRow()) { 
  115. foreach ($this->dataIterator->getDisplayColumns() as $col) { 
  116. if (is_numeric($this->dataIterator->row[$col])) { 
  117. $sum = $sum + $this->dataIterator->row[$col]; 
  118. if ($this->isFromShortCode) { 
  119. return $before . $sum . $after; 
  120. else { 
  121. echo $before . $sum .$after; 
  122. return; 
  123.  
  124. case 'mean': 
  125. $sum = 0; 
  126. $count = 0; 
  127. while ($this->dataIterator->nextRow()) { 
  128. foreach ($this->dataIterator->getDisplayColumns() as $col) { 
  129. if (is_numeric($this->dataIterator->row[$col])) { 
  130. $count += 1; 
  131. $sum += $this->dataIterator->row[$col]; 
  132. $mean = ($count != 0) ? $sum / $count : 'undefined'; // Avoid div by zero error 
  133. if ($this->isFromShortCode) { 
  134. return $before . $mean . $after; 
  135. else { 
  136. echo $before . $mean . $after; 
  137. return; 
  138.  
  139. case 'percent': 
  140. $count = 0; 
  141. while ($this->dataIterator->nextRow()) { 
  142. foreach ($this->dataIterator->getDisplayColumns() as $col) { 
  143. $count += 1; 
  144.  
  145. $total = $this->getDBRowCount($formName); 
  146. $numShowCols = count($this->showColumns); 
  147. if ($numShowCols > 1) { 
  148. $total = $total * $numShowCols; 
  149. else if ($numShowCols == 0) { 
  150. $total = $total * count($this->dataIterator->getDisplayColumns()); 
  151.  
  152. if ($total != 0) { 
  153. $percentNum = 100.0 * $count / $total; 
  154. $percentDisplay = round($percentNum) . '%'; 
  155. //$percentDisplay = "$count / $total = $percentNum as $percentDisplay"; // debug 
  156. else { 
  157. // Avoid div by zero error 
  158. $percentDisplay = '0%'; 
  159.  
  160. if ($this->isFromShortCode) { 
  161. return $before . $percentDisplay . $after; 
  162. else { 
  163. echo $before . $percentDisplay . $after; 
  164. return; 
  165.  
  166. // At this point in the code: $funct not defined or not recognized 
  167. // output values for each row/column 
  168. if ($this->isFromShortCode) { 
  169. $outputData = array(); 
  170. while ($this->dataIterator->nextRow()) { 
  171. foreach ($this->dataIterator->getDisplayColumns() as $col) { 
  172. $outputData[] = $this->dataIterator->row[$col]; 
  173. ob_start(); 
  174. echo $before; 
  175. switch (count($outputData)) { 
  176. case 0: 
  177. echo ''; 
  178. break; 
  179. case 1: 
  180. echo $outputData[0]; 
  181. break; 
  182. default: 
  183. echo implode($delimiter, $outputData); 
  184. break; 
  185. echo $after; 
  186. $output = ob_get_contents(); 
  187. ob_end_clean(); 
  188. // If called from a shortcode, need to return the text,  
  189. // otherwise it can appear out of order on the page 
  190. return $output; 
  191. else { 
  192. echo $before; 
  193. $first = true; 
  194. while ($this->dataIterator->nextRow()) { 
  195. foreach ($this->dataIterator->getDisplayColumns() as $col) { 
  196. if ($first) { 
  197. $first = false; 
  198. else { 
  199. echo $delimiter; 
  200. echo $this->dataIterator->row[$col]; 
  201. echo $after;