PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/form-maker/frontend/models/FMModelForm_submissions.php

https://gitlab.com/hunt9310/ras
PHP | 296 lines | 240 code | 32 blank | 24 comment | 58 complexity | 06cede83011100edd3c8106908d12882 MD5 | raw file
  1. <?php
  2. class FMModelForm_submissions {
  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. function showsubmissions($form_id, $startdate, $enddate, $submit_date, $submitter_ip, $username, $useremail, $form_fields, $csv, $xml, $title, $search, $ordering, $entries, $views, $conversion_rate, $pagination, $stats) {
  21. global $wpdb;
  22. $user = wp_get_current_user();
  23. $userGroups = $wpdb->get_var($wpdb->prepare("SELECT `user_id_wd` FROM " . $wpdb->prefix . "formmaker WHERE id='%d'", $form_id));
  24. $users = explode(',', $userGroups);
  25. $users = array_slice($users, 0, count($users) - 1);
  26. $show_submits = false;
  27. if(!is_user_logged_in()) {
  28. if(!in_array('guest', $users))
  29. return false;
  30. }
  31. else {
  32. foreach($user->roles as $user_role) {
  33. if(in_array($user_role, $users))
  34. $show_submits = true;
  35. }
  36. if(!$show_submits)
  37. return false;
  38. }
  39. $from = $startdate;
  40. $to = $enddate;
  41. $filter_order = ((isset($_POST['order_by']) && esc_html(stripslashes($_POST['order_by'])) != '') ? esc_html(stripslashes($_POST['order_by'])) : 'group_id');
  42. $filter_order_Dir = ((isset($_POST['asc_or_desc']) && ($_POST['asc_or_desc'] == 'asc' || $_POST['asc_or_desc'] == 'desc')) ? esc_html($_POST['asc_or_desc']) : 'asc');
  43. $ip_search = $submitter_ip;
  44. $username_search = strtolower($username);
  45. $useremail_search = strtolower($useremail);
  46. $where = array();
  47. $lists['startdate'] = ((isset($_POST['startdate'])) ? esc_html(stripslashes($_POST['startdate'])) : '');
  48. $lists['enddate'] = ((isset($_POST['enddate'])) ? esc_html(stripslashes($_POST['enddate'])) : '');
  49. $lists['hide_label_list'] = ((isset($_POST['hide_label_list'])) ? esc_html(stripslashes($_POST['hide_label_list'])) : '');
  50. $lists['ip_search'] = ((isset($_POST['ip_search'])) ? esc_html(stripslashes($_POST['ip_search'])) : '');
  51. $lists['username_search'] = ((isset($_POST['username_search'])) ? esc_html(stripslashes($_POST['username_search'])) : '');
  52. $lists['useremail_search'] = ((isset($_POST['useremail_search'])) ? esc_html(stripslashes($_POST['useremail_search'])) : '');
  53. $limit = ((isset($_POST['page_number'])) ? ((int) $_POST['page_number'] - 1) * 20 : 0);
  54. if ( $lists['ip_search'] ) {
  55. $where[] = 'ip LIKE "%' . esc_sql($lists['ip_search']) . '%"';
  56. }
  57. if ( $lists['username_search'] ) {
  58. $where[] = 'user_id_wd IN (SELECT `id` FROM `' . $wpdb->prefix . 'users` WHERE `display_name` LIKE "%' . esc_sql($lists['username_search']) . '%")';
  59. }
  60. if ( $lists['useremail_search'] ) {
  61. $where[] = 'user_id_wd IN (SELECT `id` FROM `' . $wpdb->prefix . 'users` WHERE `user_email` LIKE "%' . esc_sql($lists['useremail_search']) . '%")';
  62. }
  63. if($from) {
  64. if($lists['startdate'] != '') {
  65. if(strtotime($from) > strtotime($lists['startdate']))
  66. $where[] = "`date` >= '" . $from . " 00:00:00'";
  67. else
  68. $where[] ="`date`>='" . $lists['startdate'] . " 00:00:00' ";
  69. }
  70. else
  71. $where[] = "`date` >= '" . $from . " 00:00:00'";
  72. }
  73. else {
  74. if($lists['startdate']!='')
  75. $where[] =" `date`>='".$lists['startdate']." 00:00:00' ";
  76. }
  77. if($to) {
  78. if($lists['enddate']!='') {
  79. if(strtotime($to) < strtotime($lists['enddate']))
  80. $where[] = "`date` <= '".$to." 23:59:59'";
  81. else
  82. $where[] ="`date`<='".$lists['enddate']." 23:59:59' ";
  83. }
  84. else
  85. $where[] = "`date` <= '".$to." 23:59:59'";
  86. }
  87. else {
  88. if($lists['enddate']!='')
  89. $where[] ="`date`<='".$lists['enddate']." 23:59:59' ";
  90. }
  91. $form_title = $wpdb->get_var($wpdb->prepare("SELECT `title` FROM " . $wpdb->prefix . "formmaker WHERE id='%d'", $form_id));
  92. $where[] = 'form_id="' . (int)$form_id . '"';
  93. $where = ( count( $where ) ? ' ' . implode( ' AND ', $where ) : '' );
  94. $orderby = ' ';
  95. if ($filter_order == 'id' or $filter_order == 'title' or $filter_order == 'mail') {
  96. $orderby = ' ORDER BY `date` desc';
  97. }
  98. else {
  99. if ($filter_order == 'group_id' or $filter_order == 'date' or $filter_order == 'ip') {
  100. $orderby = ' ORDER BY '.$filter_order .' '. $filter_order_Dir .'';
  101. }
  102. else {
  103. if ($filter_order == 'display_name' or $filter_order == 'user_email') {
  104. $orderby = ' ORDER BY (SELECT `'.$filter_order.'` FROM `' . $wpdb->prefix . 'users` WHERE id=user_id_wd) '. $filter_order_Dir .'';
  105. }
  106. }
  107. }
  108. $query = "SELECT distinct element_label FROM " . $wpdb->prefix . "formmaker_submits WHERE ". $where;
  109. $labels = $wpdb->get_col($query);
  110. $query = $wpdb->prepare("SELECT id FROM " . $wpdb->prefix . "formmaker_submits WHERE form_id='%d' and element_label=0 limit 0, 1", $form_id);
  111. $ispaypal = $wpdb->get_var($query);
  112. $query = $wpdb->prepare('SELECT count(distinct group_id) FROM ' . $wpdb->prefix . 'formmaker_submits where form_id ="%d"', $form_id);
  113. $total_entries = $wpdb->get_var($query);
  114. $sorted_labels_type= array();
  115. $sorted_labels_id= array();
  116. $sorted_labels= array();
  117. $label_titles=array();
  118. $rows_ord = array();
  119. $rows = array();
  120. $total = 0;
  121. $join_count='';
  122. $checked_ids = '';
  123. $stats_fields = '';
  124. if($labels) {
  125. $label_id= array();
  126. $label_order= array();
  127. $label_order_original= array();
  128. $label_type= array();
  129. $this_form = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "formmaker WHERE id='%d'", $form_id));
  130. $checked_ids = $this_form->frontend_submit_fields;
  131. $stats_fields = $this_form->frontend_submit_stat_fields;
  132. if(strpos($this_form->label_order, 'type_paypal_')) {
  133. $this_form->label_order=$this_form->label_order."item_total#**id**#Item Total#**label**#type_paypal_payment_total#****#total#**id**#Total#**label**#type_paypal_payment_total#****#0#**id**#Payment Status#**label**#type_paypal_payment_status#****#";
  134. }
  135. $label_all = explode('#****#',$this_form->label_order);
  136. $label_all = array_slice($label_all,0, count($label_all)-1);
  137. foreach($label_all as $key => $label_each) {
  138. $label_id_each=explode('#**id**#',$label_each);
  139. array_push($label_id, $label_id_each[0]);
  140. $label_order_each=explode('#**label**#', $label_id_each[1]);
  141. array_push($label_order_original, $label_order_each[0]);
  142. $ptn = "/[^a-zA-Z0-9_]/";
  143. $rpltxt = "";
  144. $label_temp=preg_replace($ptn, $rpltxt, $label_order_each[0]);
  145. array_push($label_order, $label_temp);
  146. array_push($label_type, $label_order_each[1]);
  147. }
  148. $join_query=array();
  149. $join_where=array();
  150. $join='';
  151. $is_first=true;
  152. foreach($label_id as $key => $label) {
  153. if(in_array($label, $labels)) {
  154. array_push($sorted_labels_type, $label_type[$key]);
  155. array_push($sorted_labels, $label_order[$key]);
  156. array_push($sorted_labels_id, $label);
  157. array_push($label_titles, $label_order_original[$key]);
  158. $search_temp = isset($_POST[$form_id.'_'.$label.'_search']) ? $_POST[$form_id.'_'.$label.'_search'] : '';
  159. $search_temp = strtolower( $search_temp );
  160. $lists[$form_id.'_'.$label.'_search'] = $search_temp;
  161. if ( $search_temp ) {
  162. $join_query[] ='search';
  163. $join_where[] =array('label'=>$label, 'search'=>esc_sql($search_temp));
  164. }
  165. }
  166. }
  167. if(strpos($filter_order,"_field")) {
  168. if (in_array(str_replace("_field", "", $filter_order), $labels)) {
  169. $join_query[] ='sort';
  170. $join_where[] =array('label'=>str_replace("_field", "", $filter_order));
  171. }
  172. }
  173. $cols = 'group_id';
  174. if ($filter_order == 'date' or $filter_order == 'ip') {
  175. $cols = 'group_id, date, ip';
  176. }
  177. switch(count($join_query)) {
  178. case 0:
  179. $join='SELECT distinct group_id FROM ' . $wpdb->prefix . 'formmaker_submits WHERE '. $where;
  180. break;
  181. case 1:
  182. if($join_query[0]=='sort') {
  183. $join = 'SELECT group_id FROM ' . $wpdb->prefix . 'formmaker_submits WHERE '.$where.' AND element_label="'.$join_where[0]['label'].'" ';
  184. $join_count = 'SELECT count(group_id) FROM ' . $wpdb->prefix . 'formmaker_submits WHERE form_id="'.esc_sql((int)$form_id).'" AND element_label="'.$join_where[0]['label'].'" ';
  185. $orderby = ' ORDER BY `element_value` '. $filter_order_Dir .'';
  186. }
  187. else
  188. $join='SELECT group_id FROM ' . $wpdb->prefix . 'formmaker_submits WHERE element_label="'.$join_where[0]['label'].'" AND element_value LIKE "%'.$join_where[0]['search'].'%" AND '. $where;
  189. break;
  190. default:
  191. $join='SELECT t.group_id FROM (SELECT '.$cols.' FROM ' . $wpdb->prefix . 'formmaker_submits WHERE '.$where.' AND element_label="'.$join_where[0]['label'].'" AND element_value LIKE "%'.$join_where[0]['search'].'%" ) as t ';
  192. for($key=1; $key< count($join_query); $key++) {
  193. if($join_query[$key]=='sort') {
  194. $join.='LEFT JOIN (SELECT group_id as group_id'.$key.', element_value FROM ' . $wpdb->prefix . 'formmaker_submits WHERE '.$where.' AND element_label="'.$join_where[$key]['label'].'") as t'.$key.' ON t'.$key.'.group_id'.$key.'=t.group_id ';
  195. $orderby = ' ORDER BY t'.$key.'.`element_value` '. $filter_order_Dir .'';
  196. }
  197. else
  198. $join.='INNER JOIN (SELECT group_id as group_id'.$key.' FROM ' . $wpdb->prefix . 'formmaker_submits WHERE '.$where.' AND element_label="'.$join_where[$key]['label'].'" AND element_value LIKE "%'.$join_where[$key]['search'].'%" ) as t'.$key.' ON t'.$key.'.group_id'.$key.'=t.group_id ';
  199. }
  200. break;
  201. }
  202. $pos = strpos($join, 'SELECT t.group_id');
  203. if ($pos === false)
  204. $query = str_replace(array('SELECT group_id','SELECT distinct group_id'), array('SELECT count(distinct group_id)','SELECT count(distinct group_id)'), $join);
  205. else
  206. $query = str_replace('SELECT t.group_id', 'SELECT count(t.group_id)', $join);
  207. $total = $wpdb->get_var($query);
  208. $query = $join.' '.$orderby . ($pagination ? ' limit ' . $limit . ', 20 ' : '') . ' ';
  209. $rows_ord = $wpdb->get_col($query);
  210. $where2 = array();
  211. $where2 [] ="group_id='0'";
  212. foreach($rows_ord as $rows_ordd) {
  213. $where2 [] ="group_id='".esc_sql($rows_ordd)."'";
  214. }
  215. $where2 = ( count( $where2 ) ? ' WHERE ' . implode( ' OR ', $where2 ).'' : '' );
  216. $query = "SELECT * FROM " . $wpdb->prefix . "formmaker_submits ".$where2.'';
  217. $rows = $wpdb->get_results($query);
  218. if($join_count) {
  219. $total_sort = $wpdb->get_var($join_count);
  220. if($total_sort != $total_entries)
  221. $join_count = $total_sort;
  222. else
  223. $join_count = '';
  224. }
  225. }
  226. $query = $wpdb->prepare('SELECT views FROM ' . $wpdb->prefix . 'formmaker_views WHERE form_id="%d"', $form_id);
  227. $total_views = $wpdb->get_var($query);
  228. $pageNav = "";// $pageNav = new JPagination( $total, $limitstart, $limit );
  229. $lists['order_Dir'] = $filter_order_Dir;
  230. $lists['order'] = $filter_order;
  231. $lists['total'] = $total;
  232. $lists['limit'] = (int) ($limit / 20 + 1);
  233. return array("rows" => $rows, "lists" => $lists, "pageNav" => $pageNav, "sorted_labels" => $sorted_labels, "label_titles" => $label_titles, "rows_ord" => $rows_ord, "sorted_labels_id" => $sorted_labels_id, "sorted_labels_type" => $sorted_labels_type, "total_entries" => $total_entries, "total_views" => $total_views, "join_count" => $join_count, "form_title" => $form_title, "checked_ids" => $checked_ids, "stats_fields" => $stats_fields);
  234. }
  235. function show_stats() {
  236. global $wpdb;
  237. $form_id = (isset($_POST['form_id']) ? esc_html(stripslashes($_POST['form_id'])) : 0);
  238. $id = (isset($_POST['stat_id']) ? esc_html(stripslashes($_POST['stat_id'])) : 0);
  239. $from = (isset($_POST['from']) ? esc_html(stripslashes($_POST['from'])) : 0);
  240. $to = (isset($_POST['to']) ? esc_html(stripslashes($_POST['to'])) : 0);
  241. $where = ' AND form_id=' . $form_id;
  242. if($from != '')
  243. $where .= " AND `date`>='".$from." 00:00:00' ";
  244. if($to != '')
  245. $where .= " AND `date`<='".$to." 23:59:59' ";
  246. $query = "SELECT element_value FROM " . $wpdb->prefix . "formmaker_submits WHERE element_label='" . $id . "'" . $where;
  247. $choices = $wpdb->get_col($query);
  248. return $choices;
  249. }
  250. function paypal_info($id) {
  251. global $wpdb;
  252. $query = $wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "formmaker_sessions where group_id='%d'", $id);
  253. return $wpdb->get_row($query);
  254. }
  255. ////////////////////////////////////////////////////////////////////////////////////////
  256. // Getters & Setters //
  257. ////////////////////////////////////////////////////////////////////////////////////////
  258. ////////////////////////////////////////////////////////////////////////////////////////
  259. // Private Methods //
  260. ////////////////////////////////////////////////////////////////////////////////////////
  261. ////////////////////////////////////////////////////////////////////////////////////////
  262. // Listeners //
  263. ////////////////////////////////////////////////////////////////////////////////////////
  264. }