PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/gfelizola/pacaembu-institucional
PHP | 270 lines | 214 code | 22 blank | 34 comment | 62 complexity | a950072f3cfba30c73b727f20b543778 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. class ExportToValue extends ExportBase implements CFDBExport {
  20. public function export($formName, $options = null) {
  21. // Allow for multiple form name inputs, comma-delimited
  22. $tmp = explode(',', $formName);
  23. if (count($tmp) > 1) {
  24. $formName = &$tmp;
  25. }
  26. else if ($formName == '*') {
  27. $formName = null; // Allow for no form specified implying all forms
  28. }
  29. $this->setOptions($options);
  30. $this->setCommonOptions();
  31. // Security Check
  32. if (!$this->isAuthorized()) {
  33. $this->assertSecurityErrorMessage();
  34. return;
  35. }
  36. // See if a function is to be applied
  37. $funct = null;
  38. $delimiter = ', ';
  39. if ($this->options && is_array($this->options)) {
  40. if (isset($this->options['function'])) {
  41. $funct = $this->options['function'];
  42. }
  43. if (isset($this->options['delimiter'])) {
  44. $delimiter = $this->options['delimiter'];
  45. }
  46. }
  47. // Headers
  48. // don't set content type to text because in some browsers this becomes
  49. // the content type for the whole HTML page.
  50. $this->echoHeaders(); //'Content-Type: text/plain; charset=UTF-8');
  51. // Get the data
  52. $this->setDataIterator($formName);
  53. // count function or coming from cfdb-count shortcode
  54. if (count($this->showColumns) == 0 &&
  55. count($this->hideColumns) == 0) {
  56. if ($funct == 'count') {
  57. $count = 0;
  58. while ($this->dataIterator->nextRow()) {
  59. $count += 1;
  60. }
  61. if ($this->isFromShortCode) {
  62. return $count;
  63. }
  64. else {
  65. echo $count;
  66. return;
  67. }
  68. }
  69. }
  70. if ($funct) {
  71. // Apply function to dataset
  72. switch ($funct) {
  73. case 'count':
  74. $count = 0;
  75. $colsPerRow = count($this->dataIterator->displayColumns);
  76. while ($this->dataIterator->nextRow()) {
  77. $count += $colsPerRow;
  78. }
  79. if ($this->isFromShortCode) {
  80. return $count;
  81. }
  82. else {
  83. echo $count;
  84. return;
  85. }
  86. case 'min':
  87. $min = null;
  88. while ($this->dataIterator->nextRow()) {
  89. foreach ($this->dataIterator->displayColumns as $col) {
  90. $val = $this->dataIterator->row[$col];
  91. if (is_numeric($val)) {
  92. if ($min === null) {
  93. $min = $val;
  94. }
  95. else {
  96. if ($val < $min) {
  97. $min = $val;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. if ($this->isFromShortCode) {
  104. return $min;
  105. }
  106. else {
  107. echo $min;
  108. return;
  109. }
  110. case 'max':
  111. $max = null;
  112. while ($this->dataIterator->nextRow()) {
  113. foreach ($this->dataIterator->displayColumns as $col) {
  114. $val = $this->dataIterator->row[$col];
  115. if (is_numeric($val)) {
  116. if ($max === null) {
  117. $max = $val;
  118. }
  119. else {
  120. if ($val > $max) {
  121. $max = $val;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. if ($this->isFromShortCode) {
  128. return $max;
  129. }
  130. else {
  131. echo $max;
  132. return;
  133. }
  134. case 'sum':
  135. $sum = 0;
  136. while ($this->dataIterator->nextRow()) {
  137. foreach ($this->dataIterator->displayColumns as $col) {
  138. if (is_numeric($this->dataIterator->row[$col])) {
  139. $sum = $sum + $this->dataIterator->row[$col];
  140. }
  141. }
  142. }
  143. if ($this->isFromShortCode) {
  144. return $sum;
  145. }
  146. else {
  147. echo $sum;
  148. return;
  149. }
  150. case 'mean':
  151. $sum = 0;
  152. $count = 0;
  153. while ($this->dataIterator->nextRow()) {
  154. foreach ($this->dataIterator->displayColumns as $col) {
  155. if (is_numeric($this->dataIterator->row[$col])) {
  156. $count += 1;
  157. $sum += $this->dataIterator->row[$col];
  158. }
  159. }
  160. }
  161. $mean = ($count != 0) ? $sum / $count : 'undefined'; // Avoid div by zero error
  162. if ($this->isFromShortCode) {
  163. return $mean;
  164. }
  165. else {
  166. echo $mean;
  167. return;
  168. }
  169. case 'percent':
  170. $count = 0;
  171. while ($this->dataIterator->nextRow()) {
  172. foreach ($this->dataIterator->displayColumns as $col) {
  173. $count += 1;
  174. }
  175. }
  176. $total = $this->getDBRowCount($formName);
  177. $numShowCols = count($this->showColumns);
  178. if ($numShowCols > 1) {
  179. $total = $total * $numShowCols;
  180. }
  181. else if ($numShowCols == 0) {
  182. $total = $total * count($this->dataIterator->displayColumns);
  183. }
  184. if ($total != 0) {
  185. $percentNum = 100.0 * $count / $total;
  186. $percentDisplay = round($percentNum) . '%';
  187. //$percentDisplay = "$count / $total = $percentNum as $percentDisplay"; // debug
  188. }
  189. else {
  190. // Avoid div by zero error
  191. $percentDisplay = '0%';
  192. }
  193. if ($this->isFromShortCode) {
  194. return $percentDisplay;
  195. }
  196. else {
  197. echo $percentDisplay;
  198. return;
  199. }
  200. }
  201. }
  202. // At this point in the code: $funct not defined or not recognized
  203. // output values for each row/column
  204. if ($this->isFromShortCode) {
  205. $outputData = array();
  206. while ($this->dataIterator->nextRow()) {
  207. foreach ($this->dataIterator->displayColumns as $col) {
  208. $outputData[] = $this->dataIterator->row[$col];
  209. }
  210. }
  211. ob_start();
  212. switch (count($outputData)) {
  213. case 0:
  214. echo '';
  215. break;
  216. case 1:
  217. echo $outputData[0];
  218. break;
  219. default:
  220. echo implode($delimiter, $outputData);
  221. break;
  222. }
  223. $output = ob_get_contents();
  224. ob_end_clean();
  225. // If called from a shortcode, need to return the text,
  226. // otherwise it can appear out of order on the page
  227. return $output;
  228. }
  229. else {
  230. $first = true;
  231. while ($this->dataIterator->nextRow()) {
  232. foreach ($this->dataIterator->displayColumns as $col) {
  233. if ($first) {
  234. $first = false;
  235. }
  236. else {
  237. echo $delimiter;
  238. }
  239. echo $this->dataIterator->row[$col];
  240. }
  241. }
  242. }
  243. }
  244. }