/includes/Database/Models/Form.php

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