/htdocs/wp-content/plugins/contact-form-7-to-database-extension/ExportToValue.php

https://gitlab.com/VTTE/sitios-vtte · PHP · 296 lines · 232 code · 23 blank · 41 comment · 65 complexity · c2ca059b5810eb1b9bf29f9385e3872e MD5 · raw file

  1. <?php
  2. /*
  3. "Contact Form to Database" Copyright (C) 2011-2012 Michael Simpson (email : michael.d.simpson@gmail.com)
  4. This file is part of Contact Form to Database.
  5. Contact Form to Database is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Contact Form to Database is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Contact Form to Database.
  15. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. require_once('ExportBase.php');
  18. require_once('CFDBExport.php');
  19. require_once('CFDBShortCodeContentParser.php');
  20. class ExportToValue extends ExportBase implements CFDBExport {
  21. /**
  22. * @param string $formName
  23. * @param null $options
  24. * @return void|String
  25. */
  26. public function export($formName, $options = null) {
  27. // Allow for multiple form name inputs, comma-delimited
  28. $tmp = explode(',', $formName);
  29. if (count($tmp) > 1) {
  30. $formName = &$tmp;
  31. }
  32. else if ($formName == '*') {
  33. $formName = null; // Allow for no form specified implying all forms
  34. }
  35. $this->setOptions($options);
  36. $this->setCommonOptions();
  37. // Security Check
  38. if (!$this->isAuthorized()) {
  39. $this->assertSecurityErrorMessage();
  40. return;
  41. }
  42. // Break out sections: Before, Content, After
  43. $before = '';
  44. $content = '';
  45. $after = '';
  46. if (isset($options['content'])) {
  47. $contentParser = new CFDBShortCodeContentParser;
  48. list($before, $content, $after) = $contentParser->parseBeforeContentAfter($options['content']);
  49. }
  50. if ($before) {
  51. $before = do_shortcode($before);
  52. }
  53. if ($after) {
  54. $after = do_shortcode($after);
  55. }
  56. // See if a function is to be applied
  57. $funct = null;
  58. $delimiter = ', ';
  59. if ($this->options && is_array($this->options)) {
  60. if (isset($this->options['function'])) {
  61. $funct = $this->options['function'];
  62. }
  63. if (isset($this->options['delimiter'])) {
  64. $delimiter = $this->options['delimiter'];
  65. }
  66. }
  67. // Headers
  68. // don't set content type to text because in some browsers this becomes
  69. // the content type for the whole HTML page.
  70. $this->echoHeaders(); //'Content-Type: text/plain; charset=UTF-8');
  71. // Get the data
  72. $this->setDataIterator($formName);
  73. //$this->clearAllOutputBuffers();
  74. // count function or coming from cfdb-count shortcode
  75. if (count($this->showColumns) == 0 &&
  76. count($this->hideColumns) == 0) {
  77. if ($funct == 'count') {
  78. $count = 0;
  79. while ($this->dataIterator->nextRow()) {
  80. $count += 1;
  81. }
  82. if ($this->isFromShortCode) {
  83. return $before . $count . $after;
  84. }
  85. else {
  86. echo $before . $count . $after;
  87. return;
  88. }
  89. }
  90. }
  91. if ($funct) {
  92. // Apply function to dataset
  93. switch ($funct) {
  94. case 'count':
  95. $count = 0;
  96. $colsPerRow = count($this->dataIterator->getDisplayColumns());
  97. while ($this->dataIterator->nextRow()) {
  98. $count += $colsPerRow;
  99. }
  100. if ($this->isFromShortCode) {
  101. return $before . $count . $after;
  102. }
  103. else {
  104. echo $before . $count . $after;
  105. return;
  106. }
  107. case 'min':
  108. $min = null;
  109. while ($this->dataIterator->nextRow()) {
  110. foreach ($this->dataIterator->getDisplayColumns() as $col) {
  111. $val = $this->dataIterator->row[$col];
  112. if (is_numeric($val)) {
  113. if ($min === null) {
  114. $min = $val;
  115. }
  116. else {
  117. if ($val < $min) {
  118. $min = $val;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. if ($this->isFromShortCode) {
  125. return $before . $min . $after;
  126. }
  127. else {
  128. echo $before . $min . $after;
  129. return;
  130. }
  131. case 'max':
  132. $max = null;
  133. while ($this->dataIterator->nextRow()) {
  134. foreach ($this->dataIterator->getDisplayColumns() as $col) {
  135. $val = $this->dataIterator->row[$col];
  136. if (is_numeric($val)) {
  137. if ($max === null) {
  138. $max = $val;
  139. }
  140. else {
  141. if ($val > $max) {
  142. $max = $val;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. if ($this->isFromShortCode) {
  149. return $before . $max . $after;
  150. }
  151. else {
  152. echo $before . $max . $after;
  153. return;
  154. }
  155. case 'sum':
  156. $sum = 0;
  157. while ($this->dataIterator->nextRow()) {
  158. foreach ($this->dataIterator->getDisplayColumns() as $col) {
  159. if (is_numeric($this->dataIterator->row[$col])) {
  160. $sum = $sum + $this->dataIterator->row[$col];
  161. }
  162. }
  163. }
  164. if ($this->isFromShortCode) {
  165. return $before . $sum . $after;
  166. }
  167. else {
  168. echo $before . $sum .$after;
  169. return;
  170. }
  171. case 'mean':
  172. $sum = 0;
  173. $count = 0;
  174. while ($this->dataIterator->nextRow()) {
  175. foreach ($this->dataIterator->getDisplayColumns() as $col) {
  176. if (is_numeric($this->dataIterator->row[$col])) {
  177. $count += 1;
  178. $sum += $this->dataIterator->row[$col];
  179. }
  180. }
  181. }
  182. $mean = ($count != 0) ? $sum / $count : 'undefined'; // Avoid div by zero error
  183. if ($this->isFromShortCode) {
  184. return $before . $mean . $after;
  185. }
  186. else {
  187. echo $before . $mean . $after;
  188. return;
  189. }
  190. case 'percent':
  191. $count = 0;
  192. while ($this->dataIterator->nextRow()) {
  193. foreach ($this->dataIterator->getDisplayColumns() as $col) {
  194. $count += 1;
  195. }
  196. }
  197. $total = $this->getDBRowCount($formName);
  198. $numShowCols = count($this->showColumns);
  199. if ($numShowCols > 1) {
  200. $total = $total * $numShowCols;
  201. }
  202. else if ($numShowCols == 0) {
  203. $total = $total * count($this->dataIterator->getDisplayColumns());
  204. }
  205. if ($total != 0) {
  206. $percentNum = 100.0 * $count / $total;
  207. $percentDisplay = round($percentNum) . '%';
  208. //$percentDisplay = "$count / $total = $percentNum as $percentDisplay"; // debug
  209. }
  210. else {
  211. // Avoid div by zero error
  212. $percentDisplay = '0%';
  213. }
  214. if ($this->isFromShortCode) {
  215. return $before . $percentDisplay . $after;
  216. }
  217. else {
  218. echo $before . $percentDisplay . $after;
  219. return;
  220. }
  221. }
  222. }
  223. // At this point in the code: $funct not defined or not recognized
  224. // output values for each row/column
  225. if ($this->isFromShortCode) {
  226. $outputData = array();
  227. while ($this->dataIterator->nextRow()) {
  228. foreach ($this->dataIterator->getDisplayColumns() as $col) {
  229. $outputData[] = $this->dataIterator->row[$col];
  230. }
  231. }
  232. ob_start();
  233. echo $before;
  234. switch (count($outputData)) {
  235. case 0:
  236. echo '';
  237. break;
  238. case 1:
  239. echo $outputData[0];
  240. break;
  241. default:
  242. echo implode($delimiter, $outputData);
  243. break;
  244. }
  245. echo $after;
  246. $output = ob_get_contents();
  247. ob_end_clean();
  248. // If called from a shortcode, need to return the text,
  249. // otherwise it can appear out of order on the page
  250. return $output;
  251. }
  252. else {
  253. echo $before;
  254. $first = true;
  255. while ($this->dataIterator->nextRow()) {
  256. foreach ($this->dataIterator->getDisplayColumns() as $col) {
  257. if ($first) {
  258. $first = false;
  259. }
  260. else {
  261. echo $delimiter;
  262. }
  263. echo $this->dataIterator->row[$col];
  264. }
  265. }
  266. echo $after;
  267. }
  268. }
  269. }