/deprecated/classes/subs.php

  1. <?php if ( ! defined( 'ABSPATH' ) ) exit; 
  2. /** 
  3. * Submissions. 
  4. * This class handles creating and exporting submissions. 
  5. * 
  6. * @package Ninja Forms 
  7. * @subpackage Classes/Submissions 
  8. * @copyright Copyright (c) 2014, WPNINJAS 
  9. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License 
  10. * @since 2.7 
  11. */ 
  12.  
  13. class NF_Subs { 
  14.  
  15. /** 
  16. * Get things started 
  17. *  
  18. * @access public 
  19. * @since 2.7 
  20. * @return void/ 
  21. */ 
  22. public function __construct() { 
  23.  
  24.  
  25. /** 
  26. * Create a submission. 
  27. *  
  28. * @access public 
  29. * @since 2.7 
  30. * @return int $sub_id 
  31. */ 
  32. public function create( $form_id = '' ) { 
  33. // Create Submission 
  34. $post = array( 
  35. 'post_status' => 'publish',  
  36. 'post_type' => 'nf_sub' 
  37. ); 
  38. $sub_id = wp_insert_post( $post ); 
  39.  
  40. // Add our form ID to the submission 
  41. Ninja_Forms()->sub( $sub_id )->update_form_id( $form_id ); 
  42.  
  43. // Get the current sequential ID 
  44. $last_sub = Ninja_Forms()->form( $form_id )->get_setting( 'last_sub', true ); 
  45. $seq_num = ! empty ( $last_sub ) ? $last_sub + 1 : 1; 
  46.  
  47. $seq_num = apply_filters( 'nf_sub_seq_num', $seq_num, $form_id ); 
  48.  
  49. // Add the sequential ID to the post meta 
  50. Ninja_Forms()->sub( $sub_id )->update_seq_num( $seq_num ); 
  51.  
  52. // Update our form data with the new "last seq id." 
  53. Ninja_Forms()->form( $form_id )->update_setting( 'last_sub', $seq_num ); 
  54.  
  55. // Update our sub count 
  56. Ninja_Forms()->form( $form_id )->sub_count = $seq_num - 1; 
  57.  
  58. return $sub_id; 
  59.  
  60. /** 
  61. * Get submissions based on specific critera. 
  62. *  
  63. * @since 2.7 
  64. * @param array $args 
  65. * @return array $sub_ids 
  66. */ 
  67. public function get( $args = array() ) { 
  68.  
  69. $query_args = array( 
  70. 'post_type' => 'nf_sub',  
  71. 'posts_per_page' => -1,  
  72. 'date_query' => array( 
  73. 'inclusive' => true,  
  74. ),  
  75. ); 
  76.  
  77. if( isset( $args['form_id'] ) ) { 
  78. $query_args['meta_query'][] = array( 
  79. 'key' => '_form_id',  
  80. 'value' => $args['form_id'],  
  81. ); 
  82.  
  83. if( isset( $args['seq_num'] ) ) { 
  84. $query_args['meta_query'][] = array( 
  85. 'key' => '_seq_num',  
  86. 'value' => $args['seq_num'],  
  87. ); 
  88.  
  89. if( isset( $args['user_id'] ) ) { 
  90. $query_args['author'] = $args['user_id']; 
  91.  
  92. if( isset( $args['action'])) { 
  93. $query_args['meta_query'][] = array( 
  94. 'key' => '_action',  
  95. 'value' => $args['action'],  
  96. ); 
  97.  
  98. if ( isset ( $args['meta'] ) ) { 
  99. foreach ( $args['meta'] as $key => $value ) { 
  100. $query_args['meta_query'][] = array( 
  101. 'key' => $key,  
  102. 'value' => $value,  
  103. ); 
  104.  
  105. if ( isset ( $args['fields'] ) ) { 
  106. foreach ( $args['fields'] as $field_id => $value ) { 
  107. $query_args['meta_query'][] = array( 
  108. 'key' => '_field_' . $field_id,  
  109. 'value' => $value,  
  110. ); 
  111.  
  112. if( isset( $args['begin_date'] ) AND $args['begin_date'] != '') { 
  113. $query_args['date_query']['after'] = nf_get_begin_date( $args['begin_date'] )->format("Y-m-d G:i:s"); 
  114.  
  115. if( isset( $args['end_date'] ) AND $args['end_date'] != '' ) { 
  116. $query_args['date_query']['before'] = nf_get_end_date( $args['end_date'] )->format("Y-m-d G:i:s"); 
  117.  
  118. $subs = new WP_Query( $query_args );; 
  119.  
  120. $sub_objects = array(); 
  121.  
  122. if ( is_array( $subs->posts ) && ! empty( $subs->posts ) ) { 
  123. foreach ( $subs->posts as $sub ) { 
  124. $sub_objects[] = Ninja_Forms()->sub( $sub->ID ); 
  125. }  
  126.  
  127. wp_reset_postdata(); 
  128. return $sub_objects; 
  129.  
  130. /** 
  131. * Export submissions. 
  132. *  
  133. * @access public 
  134. * @param array $sub_ids 
  135. * @param bool @return 
  136. * @since 2.7 
  137. * @return void 
  138. */ 
  139. public function export( $sub_ids = '', $return = false ) { 
  140. global $ninja_forms_fields; 
  141.  
  142. // Bail if we haven't been sent any IDs. 
  143. if ( empty( $sub_ids ) ) 
  144. return false; 
  145.  
  146. if ( ! is_array( $sub_ids ) ) 
  147. $sub_ids = array( $sub_ids ); 
  148.  
  149. $plugin_settings = nf_get_settings(); 
  150. $date_format = $plugin_settings['date_format']; 
  151.  
  152. $label_array = array(); 
  153. // Get our Form ID. 
  154. $form_id = Ninja_Forms()->sub( $sub_ids[0] )->form_id; 
  155.  
  156. // Get our list of fields. 
  157. $fields = Ninja_Forms()->form( $form_id )->fields; 
  158.  
  159. // Add our sequential number. 
  160. $label_array[0]['_seq_num'] = __( '#', 'ninja-forms' ); 
  161.  
  162. // Add our "Date" label. 
  163. $label_array[0]['_date_submitted'] = __( 'Date Submitted', 'ninja-forms' ); 
  164.  
  165. $label_array = apply_filters( 'nf_subs_csv_label_array_before_fields', $label_array, $sub_ids ); 
  166.  
  167. foreach ( $fields as $field_id => $field ) { 
  168. // Get our field type 
  169. $field_type = $field['type']; 
  170. // Check to see if our field type has been set as a "process_field". 
  171. if ( isset ( $ninja_forms_fields[ $field_type ] ) ) { 
  172. $reg_field = $ninja_forms_fields[ $field_type ]; 
  173. $process_field = $reg_field['process_field']; 
  174. } else { 
  175. $process_field = false; 
  176.  
  177. // If this field's "process_field" is set to true, then add its label to the array. 
  178. if ( $process_field ) { 
  179. if ( isset ( $field['data']['admin_label'] ) && $field['data']['admin_label'] != '' ) { 
  180. $label = $field['data']['admin_label']; 
  181. } else if( isset ( $field['data']['label'] ) ) { 
  182. $label = $field['data']['label']; 
  183. }else{ 
  184. $label = ''; 
  185.  
  186. $label_array[0][ $field_id ] = apply_filters( 'nf_subs_csv_field_label', $label, $field_id ); 
  187.  
  188. $label_array = ninja_forms_stripslashes_deep( $label_array ); 
  189. $label_array = apply_filters( 'nf_subs_csv_label_array', $label_array, $sub_ids ); 
  190.  
  191. $value_array = array(); 
  192. $x = 0; 
  193. // Loop through our submissions and create a new row for each one. 
  194. foreach ( $sub_ids as $sub_id ) { 
  195. foreach ( $label_array[0] as $field_id => $label ) { 
  196. // Make sure we aren't working with our date field, which will always have a field id of 0. 
  197. if ( $field_id !== 0 ) { 
  198. // Check to see if our field_id is numeric. If it isn't, then we're working with meta, not a field. 
  199. if ( is_numeric( $field_id ) ) { 
  200. // We're working with a field, grab the value. 
  201. $user_value = Ninja_Forms()->sub( $sub_id )->get_field( $field_id ); 
  202. } else if ( '_date_submitted' == $field_id ) { 
  203. // Get the date of our submission. 
  204. $date = strtotime( Ninja_Forms()->sub( $sub_id )->date_submitted ); 
  205. // The first item is our date field. 
  206. $user_value = date( $date_format, $date ); 
  207. } else if ( '_seq_num' == $field_id ) { 
  208. $user_value = Ninja_Forms()->sub( $sub_id )->get_seq_num(); 
  209. } else { 
  210. // We're working with a piece of meta, grabe the value. 
  211. $user_value = Ninja_Forms()->sub( $sub_id )->get_meta( $field_id ); 
  212.  
  213. // Run our value through the appropriate filters before we flatten any arrays. 
  214. $user_value = apply_filters( 'nf_subs_export_pre_value', $user_value, $field_id ); 
  215.  
  216. // Implode any arrays we might have. 
  217. if ( is_array( $user_value ) ) { 
  218. $user_value = implode( ', ', $user_value ); 
  219.  
  220. // Add an ' to the beginning = sign to prevent any CSV/Excel security issues. 
  221. if ( strpos( $user_value, '=' ) === 0 ) { 
  222. $user_value = "'" . $user_value; 
  223.  
  224. // Run our final value through the appropriate filters and assign it to the array. 
  225. $value_array[ $x ][ $field_id ] = htmlspecialchars_decode( apply_filters( 'nf_subs_csv_field_value', $user_value, $field_id ), ENT_QUOTES );  
  226. $x++; 
  227.  
  228. $value_array = ninja_forms_stripslashes_deep( $value_array ); 
  229. $value_array = apply_filters( 'nf_subs_csv_value_array', $value_array, $sub_ids ); 
  230.  
  231. $array = array( $label_array, $value_array ); 
  232. $today = date( $date_format, current_time( 'timestamp' ) ); 
  233. $filename = apply_filters( 'nf_subs_csv_filename', 'nf_subs_' . $today ); 
  234. $filename = $filename . ".csv"; 
  235.  
  236. if( $return ) { 
  237. return str_putcsv( $array,  
  238. apply_filters( 'nf_sub_csv_delimiter', ', ' ),  
  239. apply_filters( 'nf_sub_csv_enclosure', '"' ),  
  240. apply_filters( 'nf_sub_csv_terminator', "\n" ) 
  241. ); 
  242. }else{ 
  243. header( 'Content-type: application/csv'); 
  244. header( 'Content-Disposition: attachment; filename="'.$filename .'"' ); 
  245. header( 'Pragma: no-cache'); 
  246. header( 'Expires: 0' ); 
  247. echo apply_filters( 'nf_sub_csv_bom', "\xEF\xBB\xBF" ) ; // Byte Order Mark 
  248. echo str_putcsv( $array,  
  249. apply_filters( 'nf_sub_csv_delimiter', ', ' ),  
  250. apply_filters( 'nf_sub_csv_enclosure', '"' ),  
  251. apply_filters( 'nf_sub_csv_terminator', "\n" ) 
  252. ); 
  253.  
  254. die(); 
  255.  
  256.  
.