PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/form-maker/admin/models/FMModelGenerete_csv.php

https://gitlab.com/ezgonzalez/integral
PHP | 270 lines | 241 code | 14 blank | 15 comment | 41 complexity | caff0555fe685214cbba32192c675f29 MD5 | raw file
  1. <?php
  2. class FMModelGenerete_csv {
  3. ////////////////////////////////////////////////////////////////////////////////////////
  4. // Events //
  5. ////////////////////////////////////////////////////////////////////////////////////////
  6. ////////////////////////////////////////////////////////////////////////////////////////
  7. // Constants //
  8. ////////////////////////////////////////////////////////////////////////////////////////
  9. ////////////////////////////////////////////////////////////////////////////////////////
  10. // Variables //
  11. ////////////////////////////////////////////////////////////////////////////////////////
  12. ////////////////////////////////////////////////////////////////////////////////////////
  13. // Constructor & Destructor //
  14. ////////////////////////////////////////////////////////////////////////////////////////
  15. public function __construct() {
  16. }
  17. ////////////////////////////////////////////////////////////////////////////////////////
  18. // Public Methods //
  19. ////////////////////////////////////////////////////////////////////////////////////////
  20. public function get_data() {
  21. global $wpdb;
  22. $is_paypal_info = FALSE;
  23. $params = array();
  24. $group_id_s = array();
  25. $form_id = (int)$_REQUEST['form_id'];
  26. $limitstart = (int)$_REQUEST['limitstart'];
  27. $paypal_info_fields = array('currency', 'ord_last_modified', 'status', 'full_name', 'fax', 'mobile_phone', 'email', 'phone', 'address', 'paypal_info', 'ipn', 'tax', 'shipping');
  28. $paypal_info_labels = array( 'Currency', 'Last modified', 'Status', 'Full Name', 'Fax', 'Mobile phone', 'Email', 'Phone', 'Address', 'Paypal info', 'IPN', 'Tax', 'Shipping');
  29. $query = $wpdb->prepare("SELECT distinct group_id FROM " . $wpdb->prefix . "formmaker_submits where form_id=%d", $form_id);
  30. $group_id_s = $wpdb->get_col($query);
  31. $query = $wpdb->prepare("SELECT distinct element_label FROM " . $wpdb->prefix . "formmaker_submits where form_id=%d",$form_id);
  32. $labels = $wpdb->get_col($query);
  33. $query_lable = $wpdb->prepare("SELECT label_order,title FROM " . $wpdb->prefix . "formmaker where id=%d", $form_id);
  34. $rows_lable = $wpdb->get_results($query_lable);
  35. $ptn = "/[^a-zA-Z0-9_]/";
  36. $rpltxt = "";
  37. $title = isset($rows_lable[0]) ? preg_replace($ptn, $rpltxt, $rows_lable[0]->title) : '';
  38. $sorted_labels_id = array();
  39. $sorted_labels = array();
  40. $label_titles = array();
  41. $label_id = array();
  42. $label_order = array();
  43. $label_order_original = array();
  44. $label_type = array();
  45. if ($labels) {
  46. $label_all = explode('#****#', $rows_lable[0]->label_order);
  47. $label_all = array_slice($label_all, 0, count($label_all) - 1);
  48. foreach ($label_all as $key => $label_each) {
  49. $label_id_each = explode('#**id**#', $label_each);
  50. array_push($label_id, $label_id_each[0]);
  51. $label_oder_each = explode('#**label**#', $label_id_each[1]);
  52. array_push($label_order_original, $label_oder_each[0]);
  53. $label_temp = preg_replace($ptn, $rpltxt, $label_oder_each[0]);
  54. array_push($label_order, $label_temp);
  55. array_push($label_type, $label_oder_each[1]);
  56. }
  57. foreach ($label_id as $key => $label) {
  58. if (in_array($label, $labels) && $label_type[$key] !='type_arithmetic_captcha') {
  59. array_push($sorted_labels, $label_order[$key]);
  60. array_push($sorted_labels_id, $label);
  61. array_push($label_titles, stripslashes($label_order_original[$key]));
  62. }
  63. }
  64. }
  65. $m = count($sorted_labels);
  66. $wpdb->query("SET SESSION group_concat_max_len = 1000000");
  67. $query = $wpdb->prepare("SELECT group_id, ip, date, user_id_wd, GROUP_CONCAT( element_label SEPARATOR ',') as element_label, GROUP_CONCAT( element_value SEPARATOR '*:*el_value*:*') as element_value FROM " . $wpdb->prefix . "formmaker_submits where form_id= %d GROUP BY group_id ORDER BY date ASC limit %d, %d", $form_id, $limitstart, 1000);
  68. $rows = $wpdb->get_results($query, OBJECT_K);
  69. $data = array();
  70. $group_id_s_count = $limitstart + 1000 < count($group_id_s) ? $limitstart + 1000 : count($group_id_s);
  71. for ($www = $limitstart; $www < $group_id_s_count; $www++) {
  72. $i = $group_id_s[$www];
  73. $field_key = array_search($i, $label_id);
  74. if($label_type[$field_key] != 'type_arithmetic_captcha') {
  75. $data_temp = array();
  76. $tt = $rows[$i];
  77. $date = $tt->date;
  78. $ip = $tt->ip;
  79. $user_id = get_userdata($tt->user_id_wd);
  80. $username = $user_id ? $user_id->display_name : "";
  81. $useremail = $user_id ? $user_id->user_email : "";
  82. $data_temp['Submission ID'] = $i;
  83. $data_temp['Submit date'] = $date;
  84. $data_temp['Ip']=$ip;
  85. $data_temp['Submitter\'s Username']=$username;
  86. $data_temp['Submitter\'s Email Address']=$useremail;
  87. $element_labels = explode(',', $tt->element_label);
  88. $element_values = explode('*:*el_value*:*', $tt->element_value);
  89. for ($h = 0; $h < $m; $h++) {
  90. if(isset($data_temp[$label_titles[$h]]))
  91. $label_titles[$h] .= '(1)';
  92. if(in_array($sorted_labels_id[$h], $element_labels)) {
  93. $element_value = $element_values[array_search($sorted_labels_id[$h], $element_labels)];
  94. if (strpos($element_value, "*@@url@@*")) {
  95. $file_names = '';
  96. $new_files = explode("*@@url@@*", $element_value);
  97. foreach ($new_files as $new_file) {
  98. if ($new_file) {
  99. $file_names .= $new_file . ", ";
  100. }
  101. }
  102. $data_temp[stripslashes($label_titles[$h])] = $file_names;
  103. }
  104. elseif (strpos($element_value, "***br***")) {
  105. $element_value = str_replace("***br***", ', ', $element_value);
  106. if (strpos($element_value, "***quantity***")) {
  107. $element_value = str_replace("***quantity***", '', $element_value);
  108. }
  109. if (strpos($element_value, "***property***")) {
  110. $element_value = str_replace("***property***", '', $element_value);
  111. }
  112. if(substr($element_value, -2) == ', ') {
  113. $data_temp[stripslashes($label_titles[$h])]= substr($element_value, 0, -2);
  114. }
  115. else {
  116. $data_temp[stripslashes($label_titles[$h])]= $element_value;
  117. }
  118. }
  119. elseif (strpos($element_value, "***map***")) {
  120. $data_temp[stripslashes($label_titles[$h])] = 'Longitude:' . str_replace("***map***", ', Latitude:', $element_value);
  121. }
  122. elseif (strpos($element_value, "***star_rating***")) {
  123. $element = str_replace("***star_rating***", '', $element_value);
  124. $element = explode("***", $element);
  125. $data_temp[stripslashes($label_titles[$h])] = ' ' . $element[1] . '/' . $element[0];
  126. }
  127. elseif (strpos($element_value, "@@@") || $element_value == "@@@" || $element_value == "@@@@@@@@@") {
  128. $data_temp[stripslashes($label_titles[$h])] = str_replace("@@@", ' ', $element_value);
  129. }
  130. elseif (strpos($element_value, "***grading***")) {
  131. $element = str_replace("***grading***", '', $element_value);
  132. $grading = explode(":", $element);
  133. $items_count = sizeof($grading) - 1;
  134. $items = "";
  135. $total = "";
  136. for ($k = 0; $k < $items_count / 2; $k++) {
  137. $items .= $grading[$items_count / 2 + $k] . ": " . $grading[$k] . ", ";
  138. $total += $grading[$k];
  139. }
  140. $items .= "Total: " . $total;
  141. $data_temp[stripslashes($label_titles[$h])] = $items;
  142. }
  143. elseif (strpos($element_value, "***matrix***")) {
  144. $element = str_replace("***matrix***", '', $element_value);
  145. $matrix_value = explode('***', $element);
  146. $matrix_value = array_slice($matrix_value, 0, count($matrix_value) - 1);
  147. $mat_rows = $matrix_value[0];
  148. $mat_columns = $matrix_value[$mat_rows + 1];
  149. $matrix = "";
  150. $aaa = array();
  151. $var_checkbox = 1;
  152. $selected_value = "";
  153. $selected_value_yes = "";
  154. $selected_value_no = "";
  155. for ($k = 1; $k <= $mat_rows; $k++) {
  156. if ($matrix_value[$mat_rows + $mat_columns + 2] == "radio") {
  157. if ($matrix_value[$mat_rows + $mat_columns + 2 + $k] == 0) {
  158. $checked = "0";
  159. $aaa[1] = "";
  160. }
  161. else {
  162. $aaa = explode("_", $matrix_value[$mat_rows + $mat_columns + 2 + $k]);
  163. }
  164. for ($l = 1; $l <= $mat_columns; $l++) {
  165. $checked = $aaa[1] == $l ? '1' : '0';
  166. $matrix .= '['.$matrix_value[$k].','.$matrix_value[$mat_rows+1+$l].']='.$checked."; ";
  167. }
  168. }
  169. else {
  170. if ($matrix_value[$mat_rows+$mat_columns + 2] == "checkbox") {
  171. for ($l = 1; $l <= $mat_columns; $l++) {
  172. $checked = $matrix_value[$mat_rows+$mat_columns + 2 + $var_checkbox] == 1 ? '1' : '0';
  173. $matrix .= '['.$matrix_value[$k].','.$matrix_value[$mat_rows+1+$l].']='.$checked."; ";
  174. $var_checkbox++;
  175. }
  176. }
  177. else {
  178. if ($matrix_value[$mat_rows+$mat_columns + 2] == "text") {
  179. for ($l = 1; $l <= $mat_columns; $l++) {
  180. $text_value = $matrix_value[$mat_rows+$mat_columns+2+$var_checkbox];
  181. $matrix .='['.$matrix_value[$k].','.$matrix_value[$mat_rows+1+$l].']='.$text_value."; ";
  182. $var_checkbox++;
  183. }
  184. }
  185. else {
  186. for ($l = 1; $l <= $mat_columns; $l++) {
  187. $selected_text = $matrix_value[$mat_rows+$mat_columns + 2 + $var_checkbox];
  188. $matrix .= '['.$matrix_value[$k].','.$matrix_value[$mat_rows + 1 + $l].']='.$selected_text."; ";
  189. $var_checkbox++;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. $data_temp[stripslashes($label_titles[$h])] = $matrix;
  196. }
  197. else {
  198. $val = htmlspecialchars_decode($element_value);
  199. $val = stripslashes(str_replace('&#039;', "'", $val));
  200. $data_temp[stripslashes($label_titles[$h])] = ($element_value ? $val : '');
  201. }
  202. }
  203. else
  204. $data_temp[stripslashes($label_titles[$h])] = '';
  205. }
  206. $query = $wpdb->prepare("SELECT id FROM " . $wpdb->prefix . "formmaker_submits where element_label=%s AND form_id = %d AND group_id=%d",'item_total', $form_id, $i);
  207. $is_paypal = $wpdb->get_results($query);
  208. if($is_paypal) {
  209. $item_total = $wpdb->get_var($wpdb->prepare("SELECT `element_value` FROM " . $wpdb->prefix . "formmaker_submits where group_id=%d AND element_label=%s", $i, 'item_total'));
  210. $total = $wpdb->get_var($wpdb->prepare("SELECT `element_value` FROM " . $wpdb->prefix . "formmaker_submits where group_id=%d AND element_label=%s", $i, 'total'));
  211. $payment_status = $wpdb->get_var($wpdb->prepare("SELECT `element_value` FROM " . $wpdb->prefix . "formmaker_submits where group_id=%d AND element_label=%s", $i, '0'));
  212. $data_temp['Item Total'] = $item_total;
  213. $data_temp['Total'] = $total;
  214. $data_temp['Payment Status'] = $payment_status;
  215. }
  216. $query = $wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "formmaker_sessions where group_id=%d",$i);
  217. $paypal_info = $wpdb->get_results($query);
  218. if ($paypal_info) {
  219. $is_paypal_info = TRUE;
  220. }
  221. if ($is_paypal) {
  222. foreach ($paypal_info_fields as $key=>$paypal_info_field) {
  223. if ($paypal_info) {
  224. $data_temp['PAYPAL_'.$paypal_info_labels[$key]]=$paypal_info[0]->$paypal_info_field;
  225. }
  226. else {
  227. $data_temp['PAYPAL_'.$paypal_info_labels[$key]]='';
  228. }
  229. }
  230. }
  231. $data[$i] = $data_temp;
  232. }
  233. }
  234. array_push($params, $data);
  235. array_push($params, $title);
  236. array_push($params, $is_paypal_info);
  237. return $params;
  238. }
  239. ////////////////////////////////////////////////////////////////////////////////////////
  240. // Getters & Setters //
  241. ////////////////////////////////////////////////////////////////////////////////////////
  242. ////////////////////////////////////////////////////////////////////////////////////////
  243. // Private Methods //
  244. ////////////////////////////////////////////////////////////////////////////////////////
  245. ////////////////////////////////////////////////////////////////////////////////////////
  246. // Listeners //
  247. ////////////////////////////////////////////////////////////////////////////////////////
  248. }