'; print_r($form_data_array); echo ''; // Table name for field selector settings $table_name_settings = $wpdb->prefix . 'ai_app_onsite_field_selector_settings'; $max_field_option = $wpdb->get_var("SELECT MAX(field_position) FROM $table_name_settings"); // Iterate through form data and insert/update settings foreach ($form_data_array as $index => $formData) { // Check if settings already exist for this field $existing_settings = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name_settings WHERE field_uniqueid = %s", $formData['field_uniqueid'])); // echo 'existing_settings = {{'.$formData['field_label'].'}}'; $field_label = $formData['field_label']; // Replace underscores with spaces $field_label = str_replace(' ', '_', $field_label); // Convert the first letter to lowercase $field_label = strtolower($field_label); // Enclose the key in curly braces $field_tag = '{{' . $field_label . '}}'; $field_id = $formData['field_id']; $replaceField_id = str_replace(' ', '-', $field_id); // Convert the first letter to lowercase $formField_id = strtolower($replaceField_id); if (isset($formData['field_dropdown_options'])) { // Split the string into an array using '|' as a delimiter $field_dropdown_options = explode('|', $formData['field_dropdown_options']); } else { $field_dropdown_options = []; } // Serialize the array to store in the database $field_dropdown_options = serialize($field_dropdown_options); $new_field_position = $existing_settings->field_position; // If settings already exist, update the row; otherwise, insert a new row if ($existing_settings) { // Prepare data for update $update_data = array( 'app_id' => 1, // Adjust as necessary 'field_uniqueid' => $formData['field_uniqueid'], 'field_id' => $formField_id, 'app_width' => $formData['app_width_figure'], 'field_type' => $formData['field_creator'], 'field_label' => $formData['field_label'], 'field_maxchar' => $formData['field_maxchar'], 'field_dropdown_options' => $field_dropdown_options, 'field_sample_text' => $formData['field_sample_text'], 'field_require_field_status' => $formData['section_required'] ? 1 : 0, 'field_include_in_export_email' => $formData['include_in_email'] ? 1 : 0, 'field_position' => $new_field_position, 'field_tag' => $field_tag, // 'created_at' => current_time('mysql'), 'updated_at' => gmdate("Y-m-d H:i:s") ); // Update existing row $wpdb->update( $table_name_settings, $update_data, array('id' => $existing_settings->id), array('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s'), array('%d') // where format ); } else { $field_position = $max_field_option + 1; // Prepare data for insertion $create_data = array( 'app_id' => 1, // Adjust as necessary 'field_uniqueid' => $formData['field_uniqueid'], 'field_id' => $formData['field_id'], 'app_width' => $formData['app_width_figure'], 'field_type' => $formData['field_creator'], 'field_label' => $formData['field_label'], 'field_maxchar' => $formData['field_maxchar'], 'field_dropdown_options' => $field_dropdown_options, 'field_sample_text' => $formData['field_sample_text'], 'field_require_field_status' => $formData['section_required'] ? 1 : 0, 'field_include_in_export_email' => $formData['include_in_email'] ? 1 : 0, 'field_position' => $field_position, 'field_tag' => $field_tag, 'created_at' => gmdate("Y-m-d H:i:s"), 'updated_at' => gmdate("Y-m-d H:i:s") ); // Insert new row $wpdb->insert( $table_name_settings, $create_data, array('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%s') ); } // Check if the database operation was successful if ($wpdb->last_error) { wp_send_json_error('Error savingdf data: ' . $wpdb->last_error); } } // If reached here, data saved successfully wp_send_json_success('Settings saved successfully.'); } public function ai_app_onsite_read_field_selector_callback() { global $wpdb; $table_name = $wpdb->prefix . 'ai_app_onsite_field_selector_settings'; // Fetch data from the database $results = $wpdb->get_results("SELECT * FROM $table_name ORDER BY field_position ASC", ARRAY_A); // HTML content string to store the generated HTML $html_content = ''; // Loop through each record and generate HTML content $ctr = 0; $app_width = $results[0]['app_width']; foreach ($results as $record) { $html_content .= '
'; $html_content .= ' '; $html_content .= '

'; $html_content .= '

'; if (isset($record['field_type']) && $record['field_type'] == 'dropdown') { $field_dropdown_options = unserialize($record['field_dropdown_options']); $dropdown_options = implode('|', $field_dropdown_options); $html_content .= ''; }; $html_content .= '
'; $html_content .= '
'; $html_content .= '
'; $ctr++; } // Return the HTML content $arrayName = array('app_width' => $app_width, 'html_content' => $html_content, 'count' => $ctr); //echo $html_content; wp_send_json($arrayName); exit; } public function ai_app_onsite_delete_field_selector_callback() { global $wpdb; if ( ! isset($_POST['ai_app_onsite_field_selector_nonce_field_delete']) || ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ai_app_onsite_field_selector_nonce_field_delete'])), 'ai_app_onsite_delete_field_nonce') ) { wp_send_json_error('Nonce verification failed.for this'); return false; } // Check if the field_uniqueid parameter is set if (isset($_POST['field_uniqueid'])) { // Sanitize the input to prevent SQL injection $field_uniqueid = isset($_POST['field_uniqueid']) ? sanitize_text_field(wp_unslash($_POST['field_uniqueid'])) : ''; // Table name $table_name = $wpdb->prefix . 'ai_app_onsite_field_selector_settings'; // Delete the record from the database $wpdb->delete($table_name, array('field_uniqueid' => $field_uniqueid), array('%s')); // check error first if ($wpdb->last_error) { wp_send_json_error('Error saving data: ' . $wpdb->last_error); } else { // Return a success message wp_send_json_success('Record deleted successfully'); } } else { // Return an error message if field_uniqueid parameter is not set wp_send_json_error('Missing field_uniqueid parameter'); } // Make sure no extra content is echoed exit; } public function ai_app_onsite_drag_data_save_database_callback() { if ( ! isset($_POST['ai_app_onsite_fields_selector_order_nonce']) || ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ai_app_onsite_fields_selector_order_nonce'])), 'ai_app_onsite_fields_selector_order_nonce') ) { wp_send_json_error('Nonce verification failed.'); } global $wpdb; $table_name = $wpdb->prefix . 'ai_app_onsite_field_selector_settings'; // Decode the JSON data $form_data_array = isset($_POST['form_data_array']) ? sanitize_text_field(wp_unslash($_POST['form_data_array'])) : ''; $form_data_array = json_decode($form_data_array, true); // Iterate through the form data array foreach ($form_data_array as $key => $value) { // Prepare the data to update /* $update_data = array( 'field_position' => $value['fieldOrder'], 'field_uniqueid' => $value['fieldUniqueId'] ); // Print the data to be updated (for debugging purposes) print_r($update_data);*/ $field_position = $value['fieldOrder'] / 3; // Uncomment the update query to actually update the data $wpdb->update( $table_name, // Table name array('field_position' => $field_position), // Data to update array('field_uniqueid' => $value['fieldUniqueId']), // Where clause array('%d'), // Data format (update_data format) array('%s') // Where format ); } // Send a success response wp_send_json_success('Data updated successfully.'); } } new AI_APP_ONSITE_Field_Selector_Handler(); // Instantiate the class }