NF_Database_Models_Submission

Class NF_Database_Models_Submission.

Defined (1)

The class is defined in the following location(s).

/includes/Database/Models/Submission.php  
  1. final class NF_Database_Models_Submission 
  2. protected $_id = ''; 
  3.  
  4. protected $_status = ''; 
  5.  
  6. protected $_user_id = ''; 
  7.  
  8. protected $_form_id = ''; 
  9.  
  10. protected $_seq_num = ''; 
  11.  
  12. protected $_sub_date = ''; 
  13.  
  14. protected $_mod_date = ''; 
  15.  
  16. protected $_field_values = array(); 
  17.  
  18. protected $_extra_values = array(); 
  19.  
  20. public function __construct( $id = '', $form_id = '' ) 
  21. $this->_id = $id; 
  22. $this->_form_id = $form_id; 
  23.  
  24. if( $this->_id ) { 
  25. $sub = get_post( $this->_id ); 
  26. $this->_status = $sub->post_status; 
  27. $this->_user_id = $sub->post_author; 
  28. $this->_sub_date = $sub->post_date; 
  29. $this->_mod_date = $sub->post_modified; 
  30.  
  31. if( $this->_id && ! $this->_form_id ) { 
  32. $this->_form_id = get_post_meta( $this->_id, '_form_id', TRUE ); 
  33.  
  34. if( $this->_id && $this->_form_id ) { 
  35. $this->_seq_num = get_post_meta( $this->_id, '_seq_num', TRUE ); 
  36.  
  37. /** 
  38. * Get Submission ID 
  39. * @return int 
  40. */ 
  41. public function get_id() 
  42. return intval( $this->_id ); 
  43.  
  44. public function get_status() 
  45. return $this->_status; 
  46.  
  47. public function get_user() 
  48. return get_user_by( 'id', $this->_user_id ); 
  49.  
  50. public function get_form_id() 
  51. return intval( $this->_form_id ); 
  52.  
  53. public function get_form_title() 
  54. $form = Ninja_Forms()->form( $this->_form_id )->get(); 
  55. return $form->get_setting( 'title' ); 
  56.  
  57. public function get_seq_num() 
  58. return intval( $this->_seq_num ); 
  59.  
  60. public function get_sub_date( $format = 'm/d/Y' ) 
  61. return date( $format, strtotime( $this->_sub_date ) ); 
  62.  
  63. public function get_mod_date( $format = 'm/d/Y' ) 
  64. return date( $format, strtotime( $this->_mod_date ) ); 
  65.  
  66. /** 
  67. * Get Field Value 
  68. * Returns a single submission value by field ID or field key. 
  69. * @param int|string $field_ref 
  70. * @return string 
  71. */ 
  72. public function get_field_value( $field_ref ) 
  73. $field_id = ( is_numeric( $field_ref ) ) ? $field_ref : $this->get_field_id_by_key( $field_ref ); 
  74.  
  75. $field = '_field_' . $field_id; 
  76.  
  77. if( isset( $this->_field_values[ $field ] ) ) return $this->_field_values[ $field ]; 
  78.  
  79. $this->_field_values[ $field ] = get_post_meta($this->_id, $field, TRUE); 
  80. $this->_field_values[ $field_ref ] = get_post_meta($this->_id, $field, TRUE); 
  81.  
  82. return $this->_field_values[ $field ]; 
  83.  
  84. /** 
  85. * Get Field Values 
  86. * @return array|mixed 
  87. */ 
  88. public function get_field_values() 
  89. if( ! empty( $this->_field_values ) ) return $this->_field_values; 
  90.  
  91. $field_values = get_post_meta( $this->_id, '' ); 
  92.  
  93. foreach( $field_values as $field_id => $field_value ) { 
  94. $this->_field_values[ $field_id ] = implode( ', ', $field_value ); 
  95.  
  96. if( 0 === strpos( $field_id, '_field_' ) ) { 
  97. $field_id = substr( $field_id, 7 ); 
  98.  
  99. if( ! is_numeric( $field_id ) ) continue; 
  100.  
  101. $field = Ninja_Forms()->form()->get_field( $field_id ); 
  102. $key = $field->get_setting( 'key' ); 
  103. if( $key ) { 
  104. $this->_field_values[ $key ] = implode(', ', $field_value); 
  105.  
  106. return $this->_field_values; 
  107.  
  108. /** 
  109. * Update Field Value 
  110. * @param $field_ref 
  111. * @param $value 
  112. * @return $this 
  113. */ 
  114. public function update_field_value( $field_ref, $value ) 
  115. $field_id = ( is_numeric( $field_ref ) ) ? $field_ref : $this->get_field_id_by_key( $field_ref ); 
  116.  
  117. $this->_field_values[ $field_id ] = WPN_Helper::kses_post( $value ); 
  118.  
  119. return $this; 
  120.  
  121. /** 
  122. * Update Field Values 
  123. * @param $data 
  124. * @return $this 
  125. */ 
  126. public function update_field_values( $data ) 
  127. foreach( $data as $field_ref => $value ) 
  128. $this->update_field_value( $field_ref, $value ); 
  129.  
  130. return $this; 
  131.  
  132. public function get_extra_value( $key ) 
  133. if( ! isset( $this->_extra_values[ $key ] ) || ! $this->_extra_values[ $key ] ) { 
  134. $id = ( $this->_id ) ? $this->_id : 0; 
  135. $this->_extra_values[ $key ] = get_post_meta( $id, $key, TRUE ); 
  136.  
  137. return $this->_extra_values[ $key ]; 
  138.  
  139. public function get_extra_values( $keys ) 
  140. $values = array(); 
  141.  
  142. foreach( $keys as $key ) { 
  143. $values[ $key ] = $this->get_extra_value( $key ); 
  144.  
  145. return $values; 
  146.  
  147. public function update_extra_value( $key, $value ) 
  148. if( property_exists( $this, $key ) ) return FALSE; 
  149.  
  150. return $this->_extra_values[ $key ] = $value; 
  151.  
  152. public function update_extra_values( $values ) 
  153. foreach( $values as $key => $value ) { 
  154. $this->update_extra_value( $key, $value ); 
  155.  
  156. /** 
  157. * Find Submissions 
  158. * @param $form_id 
  159. * @param array $where 
  160. * @return array 
  161. */ 
  162. public function find( $form_id, array $where = array(), array $ids = array() ) 
  163.  
  164. $this->_form_id = $form_id; 
  165.  
  166. $args = array( 
  167. 'post_type' => 'nf_sub',  
  168. 'posts_per_page' => -1,  
  169. 'meta_query' => $this->format_meta_query( $where ) 
  170. ); 
  171.  
  172. if ( ! empty ( $ids ) ) { 
  173. $args[ 'post__in' ] = $ids; 
  174.  
  175. $subs = get_posts( $args ); 
  176.  
  177. $class = get_class( $this ); 
  178.  
  179. $return = array(); 
  180. foreach( $subs as $sub ) { 
  181. $return[] = new $class( $sub->ID, $this->_form_id ); 
  182.  
  183. return $return; 
  184.  
  185. /** 
  186. * Delete Submission 
  187. */ 
  188. public function delete() 
  189. if( ! $this->_id ) return; 
  190.  
  191. wp_delete_post( $this->_id ); 
  192.  
  193. /** 
  194. * Save Submission 
  195. * @return $this|NF_Database_Models_Submission|void 
  196. */ 
  197. public function save() 
  198. if( ! $this->_id ) { 
  199.  
  200. $sub = array( 
  201. 'post_type' => 'nf_sub',  
  202. 'post_status' => 'publish' 
  203. ); 
  204.  
  205. $this->_id = wp_insert_post( $sub ); 
  206.  
  207. // Log Error 
  208. if( ! $this->_id ) return; 
  209.  
  210. if( ! $this->_seq_num && $this->_form_id ) { 
  211.  
  212. $this->_seq_num = NF_Database_Models_Form::get_next_sub_seq( $this->_form_id ); 
  213.  
  214. $this->_save_extra_values(); 
  215.  
  216. return $this->_save_field_values(); 
  217.  
  218. public static function export( $form_id, array $sub_ids = array(), $return = FALSE ) 
  219. $date_format = Ninja_Forms()->get_setting( 'date_format' ); 
  220.  
  221. /** 
  222. * Labels 
  223. */ 
  224.  
  225. $field_labels = array( 
  226. '_seq_num' => '#',  
  227. '_date_submitted' => __( 'Date Submitted', 'ninja-forms' ) 
  228. ); 
  229.  
  230. // Legacy Filter from 2.9.* 
  231. $field_labels = apply_filters( 'nf_subs_csv_label_array_before_fields', $field_labels, $sub_ids ); 
  232.  
  233. $fields = Ninja_Forms()->form( $form_id )->get_fields(); 
  234.  
  235. /** 
  236. * If we are using an add-on that filters our field order, we don't want to call sort again. 
  237. * TODO: This is probably not the most effecient way to handle this. It should be re-thought. 
  238. */ 
  239. if ( ! has_filter( 'ninja_forms_get_fields_sorted' ) ) { 
  240. uasort( $fields, array( 'NF_Database_Models_Submission', 'sort_fields' ) ); 
  241.  
  242. $hidden_field_types = apply_filters( 'nf_sub_hidden_field_types', array() ); 
  243.  
  244. /** 
  245. * Submissions 
  246. */ 
  247.  
  248. $subs = Ninja_Forms()->form( $form_id )->get_subs( array(), FALSE, $sub_ids ); 
  249.  
  250. foreach( $subs as $sub ) { 
  251.  
  252. $value[ '_seq_num' ] = $sub->get_seq_num(); 
  253. $value[ '_date_submitted' ] = $sub->get_sub_date( $date_format ); 
  254.  
  255. foreach ($fields as $field_id => $field) { 
  256.  
  257. if (!is_int($field_id)) continue; 
  258. if( in_array( $field->get_setting( 'type' ), $hidden_field_types ) ) continue; 
  259.  
  260. if ( $field->get_setting( 'admin_label' ) ) { 
  261. $field_labels[ $field->get_id() ] = $field->get_setting( 'admin_label' ); 
  262. } else { 
  263. $field_labels[ $field->get_id() ] = $field->get_setting( 'label' ); 
  264.  
  265. $field_value = maybe_unserialize( $sub->get_field_value( $field_id ) ); 
  266.  
  267. $field_value = apply_filters('nf_subs_export_pre_value', $field_value, $field_id); 
  268. $field_value = apply_filters('ninja_forms_subs_export_pre_value', $field_value, $field_id, $form_id); 
  269. $field_value = apply_filters( 'ninja_forms_subs_export_field_value_' . $field->get_setting( 'type' ), $field_value ); 
  270.  
  271. if ( is_array($field_value ) ) { 
  272. $field_value = implode( ', ', $field_value ); 
  273.  
  274. $value[ $field_id ] = $field_value; 
  275.  
  276. $value_array[] = $value; 
  277.  
  278. $value_array = WPN_Helper::stripslashes( $value_array ); 
  279.  
  280. // Legacy Filter from 2.9.* 
  281. $value_array = apply_filters( 'nf_subs_csv_value_array', $value_array, $sub_ids ); 
  282.  
  283. $csv_array[ 0 ][] = $field_labels; 
  284. $csv_array[ 1 ][] = $value_array; 
  285.  
  286. // Get any extra data from our other plugins... 
  287. $csv_array = apply_filters( 'nf_subs_csv_extra_values', $csv_array, $subs, $form_id ); 
  288.  
  289. $today = date( $date_format, current_time( 'timestamp' ) ); 
  290. $filename = apply_filters( 'nf_subs_csv_filename', 'nf_subs_' . $today ); 
  291. $filename = $filename . ".csv"; 
  292.  
  293. if( $return ) { 
  294. return WPN_Helper::str_putcsv( $csv_array,  
  295. apply_filters( 'nf_sub_csv_delimiter', ', ' ),  
  296. apply_filters( 'nf_sub_csv_enclosure', '"' ),  
  297. apply_filters( 'nf_sub_csv_terminator', "\n" ) 
  298. ); 
  299. }else{ 
  300. header( 'Content-type: application/csv'); 
  301. header( 'Content-Disposition: attachment; filename="'.$filename .'"' ); 
  302. header( 'Pragma: no-cache'); 
  303. header( 'Expires: 0' ); 
  304. echo apply_filters( 'nf_sub_csv_bom', "\xEF\xBB\xBF" ) ; // Byte Order Mark 
  305. echo WPN_Helper::str_putcsv( $csv_array,  
  306. apply_filters( 'nf_sub_csv_delimiter', ', ' ),  
  307. apply_filters( 'nf_sub_csv_enclosure', '"' ),  
  308. apply_filters( 'nf_sub_csv_terminator', "\n" ) 
  309. ); 
  310.  
  311. die(); 
  312.  
  313. /** 
  314. * PROTECTED METHODS 
  315. */ 
  316.  
  317. /** 
  318. * Save Field Value 
  319. * @param $field_id 
  320. * @param $value 
  321. * @return $this 
  322. */ 
  323. protected function _save_field_value( $field_id, $value ) 
  324. update_post_meta( $this->_id, '_field_' . $field_id, $value ); 
  325.  
  326. return $this; 
  327.  
  328. /** 
  329. * Save Field Values 
  330. * @return $this|void 
  331. */ 
  332. protected function _save_field_values() 
  333. if( ! $this->_field_values ) return FALSE; 
  334.  
  335. foreach( $this->_field_values as $field_id => $value ) 
  336. $this->_save_field_value( $field_id, $value ); 
  337.  
  338. update_post_meta( $this->_id, '_form_id', $this->_form_id ); 
  339.  
  340. update_post_meta( $this->_id, '_seq_num', $this->_seq_num ); 
  341.  
  342. return $this; 
  343.  
  344. protected function _save_extra_values() 
  345. if( ! $this->_extra_values ) return FALSE; 
  346.  
  347. foreach( $this->_extra_values as $key => $value ) 
  348. if( property_exists( $this, $key ) ) continue; 
  349.  
  350. update_post_meta( $this->_id, $key, $value ); 
  351.  
  352.  
  353. /** 
  354. * UTILITIES 
  355. */ 
  356.  
  357. /** 
  358. * Format Meta Query 
  359. * @param array $where 
  360. * @return array 
  361. */ 
  362. protected function format_meta_query( array $where = array() ) 
  363. $return = array( 
  364. array( 
  365. 'key' => '_form_id',  
  366. 'value' => $this->_form_id 
  367. ); 
  368.  
  369. if( ! empty( $where ) ) { 
  370. foreach ($where as $ref => $value) { 
  371.  
  372. $field_id = ( is_int( $ref ) ) ? $ref : $this->get_field_id_by_key( $ref ); 
  373.  
  374. $return[] = ( is_array($value) ) ? $value : array('key' => "_field_$field_id", 'value' => $value); 
  375.  
  376. return $return; 
  377.  
  378. /** 
  379. * Get Field ID By Key 
  380. * @param $field_key 
  381. * @return mixed 
  382. */ 
  383. protected function get_field_id_by_key( $field_key ) 
  384. global $wpdb; 
  385.  
  386. $field_id = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}nf3_fields WHERE `key` = '{$field_key}' AND `parent_id` = {$this->_form_id}" ); 
  387.  
  388. return $field_id; 
  389.  
  390. public static function sort_fields( $a, $b ) 
  391. if ( $a->get_setting( 'order' ) == $b->get_setting( 'order' ) ) { 
  392. return 0; 
  393. return ( $a->get_setting( 'order' ) < $b->get_setting( 'order' ) ) ? -1 : 1; 
  394.  
  395.  
  396. } // End NF_Database_Models_Submission