NF_Database_FieldsController

The Ninja Forms NF Database FieldsController class.

Defined (1)

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

/includes/Database/FieldsController.php  
  1. final class NF_Database_FieldsController 
  2. private $db; 
  3. private $factory; 
  4. private $fields_data; 
  5. private $new_field_ids; 
  6. private $insert_fields; 
  7. private $insert_field_meta = array(); 
  8. private $insert_field_meta_chunk = 0; 
  9. private $update_fields = array( 'key' => '', 'label' => '', 'type' => '' ); 
  10. private $update_field_meta = array(); 
  11. private $update_field_meta_chunk = 0; 
  12. public function __construct( $form_id, $fields_data ) 
  13. global $wpdb; 
  14. $this->db = $wpdb; 
  15. $this->form_id = $form_id; 
  16. $this->fields_data = $fields_data; 
  17. public function run() 
  18. $this->db->hide_errors(); 
  19.  
  20. /** FIELDS */ 
  21. $this->parse_fields(); 
  22.  
  23. $insert_fields_query = $this->get_insert_fields_query(); 
  24. if( ! empty( $insert_fields_query ) ) { 
  25. $this->db->query( $insert_fields_query ); 
  26. $this->update_new_field_ids(); 
  27.  
  28. $update_fields_query = $this->get_update_fields_query(); 
  29. if( ! empty( $update_fields_query ) ) { 
  30. $this->db->query( $update_fields_query ); 
  31.  
  32. /** FIELD META */ 
  33. $this->parse_field_meta(); 
  34. $this->run_insert_field_meta_query(); 
  35. $this->run_update_field_meta_query(); 
  36. public function get_updated_fields_data() 
  37. return $this->fields_data; 
  38. private function parse_fields() 
  39. foreach( $this->fields_data as $field_data ) { 
  40. $field_id = $field_data[ 'id' ]; 
  41. $settings = array( 
  42. 'key' => $field_data[ 'settings' ][ 'key' ],  
  43. 'label' => $field_data[ 'settings' ][ 'label' ],  
  44. 'type' => $field_data[ 'settings' ][ 'type' ] 
  45. ); 
  46. if( ! is_numeric( $field_id ) ) { 
  47. $this->insert_field( $settings ); // New Field. 
  48. } else { 
  49. $this->update_field( $field_id, $settings ); 
  50. private function parse_field_meta() 
  51. $existing_meta = $this->get_existing_meta(); 
  52. foreach( $this->fields_data as $field_data ) { 
  53. $field_id = $field_data[ 'id' ]; 
  54. foreach( $field_data[ 'settings' ] as $key => $value ) { 
  55. if( isset( $existing_meta[ $field_id ][ $key ] ) ) { 
  56. if( $value == $existing_meta[ $field_id ][ $key ] ) continue; 
  57. $this->update_field_meta( $field_id, $key, $value ); 
  58. } else { 
  59. $this->insert_field_meta( $field_id, $key, $value ); 
  60. private function get_existing_meta() 
  61. $results = $this->db->get_results(" 
  62. SELECT m.parent_id, m.key, m.value 
  63. FROM `{$this->db->prefix}nf3_field_meta` AS m 
  64. LEFT JOIN `{$this->db->prefix}nf3_fields` AS f 
  65. ON m.parent_id = f.id 
  66. WHERE f.parent_id = {$this->form_id} 
  67. "); 
  68. $field_meta = array(); 
  69. foreach( $results as $meta ) { 
  70. if( ! isset( $field_meta[ $meta->parent_id ] ) ) $field_meta[ $meta->parent_id ] = array(); 
  71. $field_meta[ $meta->parent_id ][ $meta->key ] = $meta->value; 
  72. return $field_meta; 
  73. private function update_new_field_ids() 
  74. $field_id_lookup = $this->db->get_results(" 
  75. SELECT `key`, `id` 
  76. FROM {$this->db->prefix}nf3_fields 
  77. WHERE `parent_id` = {$this->form_id} 
  78. ", OBJECT_K); 
  79. foreach( $this->fields_data as $i => $field_data ) { 
  80. $field_key = $field_data[ 'settings' ][ 'key' ]; 
  81. if( ! is_numeric( $field_data[ 'id' ] ) && isset( $field_id_lookup[ $field_key ] ) ) { 
  82. $tmp_id = $field_data[ 'id' ]; 
  83. $this->fields_data[ $i ][ 'id' ] = $this->new_field_ids[ $tmp_id ] = $field_id_lookup[ $field_key ]->id; 
  84. public function get_new_field_ids() 
  85. return $this->new_field_ids; 
  86. /** 
  87. |-------------------------------------------------------------------------- 
  88. | INSERT (NEW) FIELDS 
  89. |-------------------------------------------------------------------------- 
  90. */ 
  91. private function insert_field( $settings ) 
  92. $this->insert_fields .= "("; 
  93. foreach ( $settings as $setting => $value ) { 
  94. $this->db->escape_by_ref( $value ); 
  95. $this->insert_fields .= "'{$value}', "; 
  96. $this->insert_fields .= "'{$this->form_id}'"; 
  97. $this->insert_fields .= '), '; 
  98. public function get_insert_fields_query() 
  99. if( ! $this->insert_fields ) return ""; 
  100. $insert_fields = rtrim( $this->insert_fields, ', ' ); // Strip trailing comma from SQl. 
  101. return " 
  102. INSERT INTO {$this->db->prefix}nf3_fields ( `key`, `label`, `type`, `parent_id` ) 
  103. VALUES {$insert_fields} 
  104. "; 
  105. /** 
  106. |-------------------------------------------------------------------------- 
  107. | UPDATE (EXISTING) FIELDS 
  108. |-------------------------------------------------------------------------- 
  109. */ 
  110. private function update_field( $field_id, $settings ) 
  111. foreach ( $settings as $setting => $value ) { 
  112. $line = "WHEN `id` = '{$field_id}' "; 
  113. $this->db->escape_by_ref( $value ); 
  114. $line .= "THEN '{$value}'"; 
  115. $this->update_fields[ $setting ] .= $line; 
  116. public function get_update_fields_query() 
  117. if( 
  118. empty( $this->update_fields[ 'key' ] ) || 
  119. empty( $this->update_fields[ 'label' ] ) || 
  120. empty( $this->update_fields[ 'type' ] ) 
  121. ) return ""; 
  122. return " 
  123. UPDATE {$this->db->prefix}nf3_fields 
  124. SET `key` = CASE {$this->update_fields[ 'key' ]} 
  125. ELSE `key` 
  126. END 
  127. , `label` = CASE {$this->update_fields[ 'label' ]} 
  128. ELSE `label` 
  129. END 
  130. , `type` = CASE {$this->update_fields[ 'type' ]} 
  131. ELSE `type` 
  132. END 
  133. "; 
  134. /** 
  135. |-------------------------------------------------------------------------- 
  136. | INSERT (NEW) META 
  137. |-------------------------------------------------------------------------- 
  138. */ 
  139. private function insert_field_meta( $field_id, $key, $value ) 
  140. static $counter; 
  141.  
  142. $value = maybe_serialize( $value ); 
  143.  
  144. $this->db->escape_by_ref( $field_id ); 
  145. $this->db->escape_by_ref( $key ); 
  146. $this->db->escape_by_ref( $value ); 
  147.  
  148. if( ! isset( $this->insert_field_meta[ $this->insert_field_meta_chunk ] ) || ! $this->insert_field_meta[ $this->insert_field_meta_chunk ] ) { 
  149. $this->insert_field_meta[ $this->insert_field_meta_chunk ] = ''; 
  150. $this->insert_field_meta[ $this->insert_field_meta_chunk ] .= "('{$field_id}', '{$key}', '{$value}' ), "; 
  151. $counter++; 
  152. if( 0 == $counter % 5000 ) $this->insert_field_meta_chunk++; 
  153. public function run_insert_field_meta_query() 
  154. if( ! $this->insert_field_meta ) return ""; 
  155. foreach( $this->insert_field_meta as $insert_field_meta ) { 
  156. $insert_field_meta = rtrim( $insert_field_meta, ', ' ); // Strip trailing comma from SQl. 
  157. $this->db->query( " 
  158. INSERT INTO {$this->db->prefix}nf3_field_meta ( `parent_id`, `key`, `value` ) 
  159. VALUES {$insert_field_meta} 
  160. "); 
  161. /** 
  162. |-------------------------------------------------------------------------- 
  163. | UPDATE (EXISTING) META 
  164. |-------------------------------------------------------------------------- 
  165. */ 
  166. private function update_field_meta( $field_id, $key, $value ) 
  167. static $counter; 
  168.  
  169. $value = maybe_serialize( $value ); 
  170. $this->db->escape_by_ref( $key ); 
  171. $this->db->escape_by_ref( $value ); 
  172. if( ! isset( $this->update_field_meta[ $this->update_field_meta_chunk ] ) || ! $this->update_field_meta[ $this->update_field_meta_chunk ] ) { 
  173. $this->update_field_meta[ $this->update_field_meta_chunk ] = ''; 
  174. $this->update_field_meta[ $this->update_field_meta_chunk ] .= " WHEN `parent_id` = '{$field_id}' AND `key` = '{$key}' THEN '{$value}'"; 
  175.  
  176. $counter++; 
  177. if( 0 == $counter % 5000 ) $this->update_field_meta_chunk++; 
  178. public function run_update_field_meta_query() 
  179. if( empty( $this->update_field_meta ) ) return ''; 
  180. foreach( $this->update_field_meta as $update_field_meta ) { 
  181. $this->db->query(" 
  182. UPDATE {$this->db->prefix}nf3_field_meta as field_meta 
  183. SET `value` = CASE {$update_field_meta} ELSE `value` END 
  184. "); 
  185. return;