NF_Database_Models_Form

Class NF_Database_Models_Form.

Defined (1)

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

/includes/Database/Models/Form.php  
  1. final class NF_Database_Models_Form extends NF_Abstracts_Model 
  2. protected $_type = 'form'; 
  3.  
  4. protected $_table_name = 'nf3_forms'; 
  5.  
  6. protected $_meta_table_name = 'nf3_form_meta'; 
  7.  
  8. protected $_columns = array( 
  9. 'title',  
  10. 'key',  
  11. 'created_at' 
  12. ); 
  13.  
  14. protected $_fields; 
  15.  
  16. protected static $imported_form_id; 
  17.  
  18. public function __construct( $db, $id = '' ) 
  19. add_action( 'ninja_forms_before_import_form', array( $this, 'import_form_backwards_compatibility' ) ); 
  20. parent::__construct( $db, $id ); 
  21.  
  22. public function delete() 
  23. parent::delete(); 
  24.  
  25. $fields = Ninja_Forms()->form( $this->_id )->get_fields(); 
  26.  
  27. foreach( $fields as $field ) { 
  28. $field->delete(); 
  29.  
  30. $actions = Ninja_Forms()->form( $this->_id )->get_actions(); 
  31.  
  32. foreach( $actions as $action ) { 
  33. $action->delete(); 
  34.  
  35. public static function get_next_sub_seq( $form_id ) 
  36. global $wpdb; 
  37.  
  38. // TODO: Leverage form cache. 
  39.  
  40. $last_seq_num = $wpdb->get_var( $wpdb->prepare( 
  41. 'SELECT value FROM ' . $wpdb->prefix . 'nf3_form_meta WHERE `key` = "_seq_num" AND `parent_id` = %s' 
  42. , $form_id ) ); 
  43.  
  44. if( $last_seq_num ) { 
  45. $wpdb->update( $wpdb->prefix . 'nf3_form_meta', array( 'value' => $last_seq_num + 1 ), array( 'key' => '_seq_num', 'parent_id' => $form_id ) ); 
  46. } else { 
  47. $last_seq_num = 1; 
  48. $wpdb->insert( $wpdb->prefix . 'nf3_form_meta', array( 'key' => '_seq_num', 'value' => $last_seq_num + 1, 'parent_id' => $form_id ) ); 
  49.  
  50. return $last_seq_num; 
  51.  
  52. public static function import( array $import, $id = '', $is_conversion ) 
  53. $import = apply_filters( 'ninja_forms_before_import_form', $import ); 
  54.  
  55. /** 
  56. * Create Form 
  57. */ 
  58. $form = Ninja_Forms()->form( $id )->get(); 
  59. $form->update_settings( $import[ 'settings' ] ); 
  60. $form->save(); 
  61. $form_id = $form->get_id(); 
  62.  
  63. $form_cache = array( 
  64. 'id' => $form_id,  
  65. 'fields' => array(),  
  66. 'actions' => array(),  
  67. 'settings' => $form->get_settings() 
  68. ); 
  69. $update_process = Ninja_Forms()->background_process( 'update-fields' ); 
  70. foreach( $import[ 'fields' ] as $settings ) { 
  71. if( $is_conversion ) { 
  72. $field_id = $settings[ 'id' ]; 
  73. $field = Ninja_Forms()->form($form_id)->field( $field_id )->get(); 
  74. } else { 
  75. unset( $settings[ 'id' ] ); 
  76. $field = Ninja_Forms()->form($form_id)->field()->get(); 
  77. $field->save(); 
  78.  
  79. $settings[ 'parent_id' ] = $form_id; 
  80.  
  81. array_push( $form_cache[ 'fields' ], array( 
  82. 'id' => $field->get_id(),  
  83. 'settings' => $settings 
  84. )); 
  85.  
  86. $update_process->push_to_queue(array( 
  87. 'id' => $field->get_id(),  
  88. 'settings' => $settings 
  89. )); 
  90. $update_process->save()->dispatch(); 
  91.  
  92. foreach( $import[ 'actions' ] as $settings ) { 
  93.  
  94. $action = Ninja_Forms()->form($form_id)->action()->get(); 
  95.  
  96. $action->update_settings( $settings )->save(); 
  97.  
  98. array_push( $form_cache[ 'actions' ], array( 
  99. 'id' => $action->get_id(),  
  100. 'settings' => $settings 
  101. )); 
  102.  
  103. update_option( 'nf_form_' . $form_id, WPN_Helper::utf8_encode( $form_cache ) ); 
  104.  
  105. add_action( 'admin_notices', array( 'NF_Database_Models_Form', 'import_admin_notice' ) ); 
  106.  
  107. self::$imported_form_id = $form_id; 
  108.  
  109. return $form_id; 
  110.  
  111. public static function import_admin_notice() 
  112. Ninja_Forms()->template( 'admin-notice-form-import.html.php', array( 'form_id'=> self::$imported_form_id ) ); 
  113.  
  114. public static function duplicate( $form_id ) 
  115. $form = Ninja_Forms()->form( $form_id )->get(); 
  116.  
  117. $settings = $form->get_settings(); 
  118.  
  119. $new_form = Ninja_Forms()->form()->get(); 
  120. $new_form->update_settings( $settings ); 
  121.  
  122. $form_title = $form->get_setting( 'title' ); 
  123.  
  124. $new_form_title = $form_title . " - " . __( 'copy', 'ninja-forms' ); 
  125.  
  126. $new_form->update_setting( 'title', $new_form_title ); 
  127.  
  128. $new_form->update_setting( 'lock', 0 ); 
  129.  
  130. $new_form->save(); 
  131.  
  132. $new_form_id = $new_form->get_id(); 
  133.  
  134. $fields = Ninja_Forms()->form( $form_id )->get_fields(); 
  135.  
  136. foreach( $fields as $field ) { 
  137.  
  138. $field_settings = $field->get_settings(); 
  139.  
  140. $field_settings[ 'parent_id' ] = $new_form_id; 
  141.  
  142. $new_field = Ninja_Forms()->form( $new_form_id )->field()->get(); 
  143. $new_field->update_settings( $field_settings )->save(); 
  144.  
  145. $actions = Ninja_Forms()->form( $form_id )->get_actions(); 
  146.  
  147. foreach( $actions as $action ) { 
  148.  
  149. $action_settings = $action->get_settings(); 
  150.  
  151. $new_action = Ninja_Forms()->form( $new_form_id )->action()->get(); 
  152. $new_action->update_settings( $action_settings )->save(); 
  153.  
  154. return $new_form_id; 
  155.  
  156. public static function export( $form_id, $return = FALSE ) 
  157. //TODO: Set Date Format from Plugin Settings 
  158. $date_format = 'm/d/Y'; 
  159.  
  160. $form = Ninja_Forms()->form( $form_id )->get(); 
  161.  
  162. $export = array( 
  163. 'settings' => $form->get_settings(),  
  164. 'fields' => array(),  
  165. 'actions' => array() 
  166. ); 
  167.  
  168. $fields = Ninja_Forms()->form( $form_id )->get_fields(); 
  169.  
  170. foreach( $fields as $field ) { 
  171. $export['fields'][] = $field->get_settings(); 
  172.  
  173. $actions = Ninja_Forms()->form( $form_id )->get_actions(); 
  174.  
  175. foreach( $actions as $action ) { 
  176. $export[ 'actions' ][] = $action->get_settings(); 
  177.  
  178. if( $return ) { 
  179. return $export; 
  180. } else { 
  181.  
  182. $today = date( $date_format, current_time( 'timestamp' ) ); 
  183. $filename = apply_filters( 'ninja_forms_form_export_filename', 'nf_form_' . $today ); 
  184. $filename = $filename . ".nff"; 
  185.  
  186. header( 'Content-type: application/json'); 
  187. header( 'Content-Disposition: attachment; filename="'.$filename .'"' ); 
  188. header( 'Pragma: no-cache'); 
  189. header( 'Expires: 0' ); 
  190. // echo apply_filters( 'ninja_forms_form_export_bom', "\xEF\xBB\xBF" ) ; // Byte Order Mark 
  191. echo json_encode( WPN_Helper::utf8_encode( $export ) ); 
  192.  
  193. die(); 
  194.  
  195. /** 
  196. |-------------------------------------------------------------------------- 
  197. | Backwards Compatibility 
  198. |-------------------------------------------------------------------------- 
  199. */ 
  200.  
  201. public function import_form_backwards_compatibility( $import ) 
  202. // Rename `data` to `settings` 
  203. if( isset( $import[ 'data' ] ) ) { 
  204. $import[ 'settings' ] = $import[ 'data' ]; 
  205. unset( $import[ 'data' ] ); 
  206.  
  207. // Rename `notifications` to `actions` 
  208. if( isset( $import[ 'notifications' ] ) ) { 
  209. $import[ 'actions' ] = $import[ 'notifications' ]; 
  210. unset( $import[ 'notifications' ] ); 
  211.  
  212. // Rename `form_title` to `title` 
  213. if( isset( $import[ 'settings' ][ 'form_title' ] ) ) { 
  214. $import[ 'settings' ][ 'title' ] = $import[ 'settings' ][ 'form_title' ]; 
  215. unset( $import[ 'settings' ][ 'form_title' ] ); 
  216.  
  217. // Convert `last_sub` to `_seq_num` 
  218. if( isset( $import[ 'settings' ][ 'last_sub' ] ) ) { 
  219. $import[ 'settings' ][ '_seq_num' ] = $import[ 'settings' ][ 'last_sub' ] + 1; 
  220.  
  221. // Make sure 
  222. if( ! isset( $import[ 'fields' ] ) ) { 
  223. $import[ 'fields' ] = array(); 
  224.  
  225. // `Field` to `Fields` 
  226. if( isset( $import[ 'field' ] ) ) { 
  227. $import[ 'fields' ] = $import[ 'field' ]; 
  228. unset( $import[ 'field' ] ); 
  229.  
  230. $import = apply_filters( 'ninja_forms_upgrade_settings', $import ); 
  231.  
  232. // Combine Field and Field Data 
  233. foreach( $import[ 'fields' ] as $key => $field ) { 
  234.  
  235. if( '_honeypot' == $field[ 'type' ] ) { 
  236. unset( $import[ 'fields' ][ $key ] ); 
  237. continue; 
  238.  
  239. // TODO: Split Credit Card field into multiple fields. 
  240. $field = $this->import_field_backwards_compatibility( $field ); 
  241.  
  242. if( isset( $field[ 'new_fields' ] ) ) { 
  243. foreach( $field[ 'new_fields' ] as $new_field ) { 
  244. $import[ 'fields' ][] = $new_field; 
  245. unset( $field[ 'new_fields' ] ); 
  246.  
  247. $import[ 'fields' ][ $key ] = $field; 
  248.  
  249. $has_save_action = FALSE; 
  250. foreach( $import[ 'actions' ] as $key => $action ) { 
  251. $action = $this->import_action_backwards_compatibility( $action ); 
  252. $import[ 'actions' ][ $key ] = $action; 
  253.  
  254. if( 'save' == $action[ 'type' ] ) $has_save_action = TRUE; 
  255.  
  256. if( ! $has_save_action ) { 
  257. $import[ 'actions' ][] = array( 
  258. 'type' => 'save',  
  259. 'label' => __( 'Save Form', 'ninja-forms' ),  
  260. 'active' => TRUE 
  261. ); 
  262.  
  263. $import = $this->import_merge_tags_backwards_compatibility( $import ); 
  264.  
  265. return apply_filters( 'ninja_forms_after_upgrade_settings', $import ); 
  266.  
  267. public function import_merge_tags_backwards_compatibility( $import ) 
  268. $field_lookup = array(); 
  269.  
  270. foreach( $import[ 'fields' ] as $key => $field ) { 
  271.  
  272. if( ! isset( $field[ 'id' ] ) ) continue; 
  273.  
  274. $field_id = $field[ 'id' ]; 
  275. $field_key = $field[ 'type' ] . '_' . $field_id; 
  276. $field_lookup[ $field_id ] = $import[ 'fields' ][ $key ][ 'key' ] = $field_key; 
  277.  
  278. foreach( $import[ 'actions' ] as $key => $action_settings ) { 
  279. foreach( $action_settings as $setting => $value ) { 
  280. foreach( $field_lookup as $field_id => $field_key ) { 
  281.  
  282. // Convert Tokenizer 
  283. $token = 'field_' . $field_id; 
  284. if( ! is_array( $value ) ) { 
  285. if (FALSE !== strpos($value, $token)) { 
  286. $value = str_replace($token, '{field:' . $field_key . '}', $value); 
  287.  
  288. // Convert Shortcodes 
  289. $shortcode = "[ninja_forms_field id=$field_id]"; 
  290. if( ! is_array( $value ) ) { 
  291. if (FALSE !== strpos($value, $shortcode)) { 
  292. $value = str_replace($shortcode, '{field:' . $field_key . '}', $value); 
  293.  
  294. if( ! is_array( $value ) ) { 
  295. if (FALSE !== strpos($value, '[ninja_forms_all_fields]')) { 
  296. $value = str_replace('[ninja_forms_all_fields]', '{field:all_fields}', $value); 
  297. $action_settings[ $setting ] = $value; 
  298. $import[ 'actions' ][ $key ] = $action_settings; 
  299.  
  300. return $import; 
  301.  
  302. public function import_action_backwards_compatibility( $action ) 
  303. // Remove `_` from type 
  304. if( isset( $action[ 'type' ] ) ) { 
  305. $action['type'] = str_replace('_', '', $action['type']); 
  306.  
  307. if( 'email' == $action[ 'type' ] ) { 
  308. $action[ 'to' ] = str_replace( '`', ', ', $action[ 'to' ] ); 
  309.  
  310. // Convert `name` to `label` 
  311. if( isset( $action[ 'name' ] ) ) { 
  312. $action['label'] = $action['name']; 
  313. unset($action['name']); 
  314.  
  315. return apply_filters( 'ninja_forms_upgrade_action_' . $action[ 'type' ], $action ); 
  316.  
  317. public function import_field_backwards_compatibility( $field ) 
  318. // Flatten field settings array 
  319. if( isset( $field[ 'data' ] ) && is_array( $field[ 'data' ] ) ) { 
  320. $field = array_merge( $field, $field[ 'data' ] ); 
  321. unset( $field[ 'data' ] ); 
  322.  
  323. // Drop form_id in favor of parent_id, which is set by the form. 
  324. if( isset( $field[ 'form_id' ] ) ) { 
  325. unset( $field[ 'form_id' ] ); 
  326.  
  327. // Remove `_` prefix from type setting 
  328. $field[ 'type' ] = ltrim( $field[ 'type' ], '_' ); 
  329.  
  330. // Type: `text` -> `textbox` 
  331. if( 'text' == $field[ 'type' ] ) { 
  332. $field[ 'type' ] = 'textbox'; 
  333.  
  334. if( 'submit' == $field[ 'type' ] ) { 
  335. $field[ 'processing_label' ] = 'Processing'; 
  336.  
  337. if( isset( $field[ 'email' ] ) ) { 
  338.  
  339. if( 'textbox' == $field[ 'type' ] && $field[ 'email' ] ) { 
  340. $field['type'] = 'email'; 
  341. unset( $field[ 'email' ] ); 
  342.  
  343. if( isset( $field[ 'class' ] ) ) { 
  344. $field[ 'element_class' ] = $field[ 'class' ]; 
  345. unset( $field[ 'class' ] ); 
  346.  
  347. if( isset( $field[ 'req' ] ) ) { 
  348. $field[ 'required' ] = $field[ 'req' ]; 
  349. unset( $field[ 'req' ] ); 
  350.  
  351. if( isset( $field[ 'default_value_type' ] ) ) { 
  352.  
  353. /** User Data */ 
  354. if( '_user_id' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:id}'; 
  355. if( '_user_email' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:email}'; 
  356. if( '_user_lastname' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:last_name}'; 
  357. if( '_user_firstname' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:first_name}'; 
  358. if( '_user_display_name' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:display_name}'; 
  359.  
  360. /** Post Data */ 
  361. if( 'post_id' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{post:id}'; 
  362. if( 'post_url' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{post:url}'; 
  363. if( 'post_title' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{post:title}'; 
  364.  
  365. /** System Data */ 
  366. if( 'today' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{system:date}'; 
  367.  
  368. /** Miscellaneous */ 
  369. if( '_custom' == $field[ 'default_value_type' ] && isset( $field[ 'default_value' ] ) ) { 
  370. $field[ 'default' ] = $field[ 'default_value' ]; 
  371. if( 'querystring' == $field[ 'default_value_type' ] && isset( $field[ 'default_value' ] ) ) { 
  372. $field[ 'default' ] = '{' . $field[ 'default_value' ] . '}'; 
  373.  
  374. unset( $field[ 'default_value' ] ); 
  375. unset( $field[ 'default_value_type' ] ); 
  376. } else if ( isset ( $field[ 'default_value' ] ) ) { 
  377. $field[ 'default' ] = $field[ 'default_value' ]; 
  378.  
  379. if( 'list' == $field[ 'type' ] ) { 
  380.  
  381. if ( isset( $field[ 'list_type' ] ) ) { 
  382.  
  383. if ('dropdown' == $field['list_type']) { 
  384. $field['type'] = 'listselect'; 
  385. if ('radio' == $field['list_type']) { 
  386. $field['type'] = 'listradio'; 
  387. if ('checkbox' == $field['list_type']) { 
  388. $field['type'] = 'listcheckbox'; 
  389. if ('multi' == $field['list_type']) { 
  390. $field['type'] = 'listmultiselect'; 
  391.  
  392. if( isset( $field[ 'list' ][ 'options' ] ) ) { 
  393. $field[ 'options' ] = array_values( $field[ 'list' ][ 'options' ] ); 
  394. unset( $field[ 'list' ][ 'options' ] ); 
  395.  
  396. foreach( $field[ 'options' ] as &$option ) { 
  397. if( isset( $option[ 'value' ] ) && $option[ 'value' ] ) continue; 
  398. $option[ 'value' ] = $option[ 'label' ]; 
  399.  
  400. if( 'country' == $field[ 'type' ] ) { 
  401. $field[ 'type' ] = 'listcountry'; 
  402. $field[ 'options' ] = array(); 
  403.  
  404. // Convert `textbox` to other field types 
  405. foreach( array( 'fist_name', 'last_name', 'user_zip', 'user_city', 'user_phone', 'user_email', 'user_address_1', 'user_address_2', 'datepicker' ) as $item ) { 
  406. if ( isset( $field[ $item ] ) && $field[ $item ] ) { 
  407. $field[ 'type' ] = str_replace( array( '_', 'user', '1', '2', 'picker' ), '', $item ); 
  408.  
  409. unset( $field[ $item ] ); 
  410.  
  411. if( 'timed_submit' == $field[ 'type' ] ) { 
  412. $field[ 'type' ] = 'submit'; 
  413.  
  414. if( 'checkbox' == $field[ 'type' ] ) { 
  415.  
  416. if( isset( $field[ 'calc_value' ] ) ) { 
  417.  
  418. if( isset( $field[ 'calc_value' ][ 'checked' ] ) ) { 
  419. $field[ 'checked_calc_value' ] = $field[ 'calc_value' ][ 'checked' ]; 
  420. unset( $field[ 'calc_value' ][ 'checked' ] ); 
  421. if( isset( $field[ 'calc_value' ][ 'unchecked' ] ) ) { 
  422. $field[ 'unchecked_calc_value' ] = $field[ 'calc_value' ][ 'unchecked' ]; 
  423. unset( $field[ 'calc_value' ][ 'unchecked' ] ); 
  424.  
  425. if( 'rating' == $field[ 'type' ] ) { 
  426. $field[ 'type' ] = 'starrating'; 
  427.  
  428. if( isset( $field[ 'rating_stars' ] ) ) { 
  429. $field[ 'default' ] = $field[ 'rating_stars' ]; 
  430. unset( $field[ 'rating_stars' ] ); 
  431.  
  432. if( 'number' == $field[ 'type' ] ) { 
  433.  
  434. if( ! isset( $field[ 'number_min' ] ) || ! $field[ 'number_min' ] ) { 
  435. $field[ 'num_min' ] = ''; 
  436. } else { 
  437. $field[ 'num_min' ] = $field[ 'number_min' ]; 
  438.  
  439. if( ! isset( $field[ 'number_max' ] ) || ! $field[ 'number_max' ] ) { 
  440. $field[ 'num_max' ] = ''; 
  441. } else { 
  442. $field[ 'num_max' ] = $field[ 'number_max' ]; 
  443.  
  444. if( ! isset( $field[ 'number_step' ] ) || ! $field[ 'number_step' ] ) { 
  445. $field[ 'num_step' ] = 1; 
  446. } else { 
  447. $field[ 'num_step' ] = $field[ 'number_step' ]; 
  448.  
  449. if( 'profile_pass' == $field[ 'type' ] ) { 
  450. $field[ 'type' ] = 'password'; 
  451.  
  452. $passwordconfirm = array_merge( $field, array( 
  453. 'id' => '',  
  454. 'type' => 'passwordconfirm',  
  455. 'label' => $field[ 'label' ] . ' ' . __( 'Confirm' ),  
  456. 'confirm_field' => 'password_' . $field[ 'id' ] 
  457. )); 
  458. $field[ 'new_fields' ][] = $passwordconfirm; 
  459.  
  460. if( 'desc' == $field[ 'type' ] ) { 
  461. $field[ 'type' ] = 'html'; 
  462.  
  463. if( 'credit_card' == $field[ 'type' ] ) { 
  464.  
  465. $field[ 'type' ] = 'creditcardnumber'; 
  466. $field[ 'label' ] = $field[ 'cc_number_label' ]; 
  467. $field[ 'label_pos' ] = 'above'; 
  468.  
  469. if( $field[ 'help_text' ] ) { 
  470. $field[ 'help_text' ] = '<p>' . $field[ 'help_text' ] . '</p>'; 
  471.  
  472. $credit_card_fields = array( 
  473. 'creditcardcvc' => $field[ 'cc_cvc_label' ],  
  474. 'creditcardfullname' => $field[ 'cc_name_label' ],  
  475. 'creditcardexpiration' => $field[ 'cc_exp_month_label' ] . ' ' . $field[ 'cc_exp_year_label' ],  
  476. 'creditcardzip' => __( 'Credit Card Zip', 'ninja-forms' ),  
  477. ); 
  478.  
  479.  
  480. foreach( $credit_card_fields as $new_type => $new_label ) { 
  481. $field[ 'new_fields' ][] = array_merge( $field, array( 
  482. 'id' => '',  
  483. 'type' => $new_type,  
  484. 'label' => $new_label,  
  485. 'help_text' => '',  
  486. 'desc_text' => '' 
  487. )); 
  488.  
  489. /** 
  490. * Convert inside label position over to placeholder 
  491. */ 
  492. if ( isset ( $field[ 'label_pos' ] ) && 'inside' == $field[ 'label_pos' ] ) { 
  493. if ( ! isset ( $field[ 'placeholder' ] ) || empty ( $field[ 'placeholder' ] ) ) { 
  494. $field[ 'placeholder' ] = $field[ 'label' ]; 
  495. $field[ 'label_pos' ] = 'hidden'; 
  496.  
  497. return apply_filters( 'ninja_forms_upgrade_field', $field ); 
  498.  
  499. } // End NF_Database_Models_Form