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