/**
 * Saves the general settings for the table, tries to generate the table 
 * and default settings for the columns
 */
function wdt_save_table()
{
    global $wpdb, $wdt_var1, $wdt_var2, $wdt_var3;
    $_POST = apply_filters('wpdatatables_before_save_table', $_POST);
    $table_id = $_POST['table_id'];
    $table_title = $_POST['table_title'];
    $show_title = $_POST['show_title'];
    $table_type = $_POST['table_type'];
    if ($table_type == 'csv' || $table_type == 'xls') {
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            $table_content = str_replace(site_url(), str_replace('\\', '/', ABSPATH), $_POST['table_content']);
        } else {
            $table_content = str_replace(site_url(), ABSPATH, $_POST['table_content']);
        }
    } else {
        $table_content = $_POST['table_content'];
    }
    $table_hide_before_loaded = $_POST['hide_before_loaded'] == 'true';
    $table_advanced_filtering = $_POST['table_advanced_filtering'] == 'true';
    $table_filter_form = $_POST['table_filter_form'] == 'true';
    $table_tools = $_POST['table_tools'] == 'true';
    $table_sorting = $_POST['table_sorting'] == 'true';
    $table_fixed_layout = $_POST['fixed_layout'] == 'true';
    $table_word_wrap = $_POST['word_wrap'] == 'true';
    $table_display_length = $_POST['table_display_length'];
    $table_fixheader = $_POST['table_fixheader'] == 'true';
    $table_fixcolumns = $_POST['table_fixcolumns'];
    $table_serverside = $_POST['table_serverside'] == 'true';
    $table_editable = in_array($table_type, array('mysql', 'manual')) ? $_POST['table_editable'] == 'true' : false;
    $table_mysql_name = $table_editable || $table_type == 'manual' ? $_POST['table_mysql_name'] : '';
    $table_responsive = $_POST['responsive'] == 'true';
    $table_editor_roles = !empty($_POST['editor_roles']) ? $_POST['editor_roles'] : '';
    $table_edit_only_own_rows = !empty($_POST['edit_only_own_rows']) ? (int) $_POST['edit_only_own_rows'] : 0;
    $table_userid_column_id = !empty($_POST['userid_column_id']) ? (int) $_POST['userid_column_id'] : 0;
    $wdt_var1 = isset($_POST['var1_placeholder']) ? $_POST['var1_placeholder'] : '';
    $wdt_var2 = isset($_POST['var2_placeholder']) ? $_POST['var2_placeholder'] : '';
    $wdt_var3 = isset($_POST['var3_placeholder']) ? $_POST['var3_placeholder'] : '';
    if (!$table_fixheader) {
        $table_fixcolumns = -1;
    } else {
        $table_fixcolumns = (int) $table_fixcolumns;
    }
    if (!$table_id) {
        // adding new table
        // trying to generate a WPDataTable
        $res = wdt_try_generate_table($table_type, $table_content);
        if (!empty($res['error'])) {
            // if WPDataTables returns an error, replying to the page
            echo json_encode($res);
            die;
        } else {
            // if no problem reported, first saving the table parameters to DB
            $table_array = array('title' => $table_title, 'show_title' => (int) $show_title, 'table_type' => $table_type, 'content' => $table_content, 'filtering' => (int) $table_advanced_filtering, 'filtering_form' => (int) $table_filter_form, 'sorting' => (int) $table_sorting, 'fixed_layout' => (int) $table_fixed_layout, 'responsive' => (int) $table_responsive, 'word_wrap' => (int) $table_word_wrap, 'tools' => (int) $table_tools, 'display_length' => $table_display_length, 'fixed_columns' => $table_fixcolumns, 'chart' => 'none', 'chart_title' => '', 'server_side' => (int) $table_serverside, 'editable' => (int) $table_editable, 'editor_roles' => $table_editor_roles, 'mysql_table_name' => $table_mysql_name, 'hide_before_load' => $table_hide_before_loaded, 'edit_only_own_rows' => $table_edit_only_own_rows, 'userid_column_id' => $table_userid_column_id, 'var1' => $wdt_var1, 'var2' => $wdt_var2, 'var3' => $wdt_var3);
            $table_array = apply_filters('wpdatatables_filter_insert_table_array', $table_array);
            $wpdb->insert($wpdb->prefix . 'wpdatatables', $table_array);
            // get the newly generated table ID
            $table_id = $wpdb->insert_id;
            $res['table_id'] = $table_id;
            // creating default columns for the new table
            $res['columns'] = wdt_create_columns_from_table($res['table'], $table_id);
            do_action('wpdatatables_after_save_table', $table_id);
            echo json_encode($res);
            die;
        }
    } else {
        // Trying to rebuild the table and reloading the columnset
        $res = wdt_try_generate_table($table_type, $table_content);
        if (!empty($res['error'])) {
            // if WPDataTables returns an error, replying to the page
            do_action('wpdatatables_after_save_table');
            echo json_encode($res);
            die;
        } else {
            // otherwise updating the table
            $table_array = array('title' => $table_title, 'show_title' => (int) $show_title, 'table_type' => $table_type, 'content' => $table_content, 'filtering' => (int) $table_advanced_filtering, 'filtering_form' => (int) $table_filter_form, 'sorting' => (int) $table_sorting, 'fixed_layout' => (int) $table_fixed_layout, 'word_wrap' => (int) $table_word_wrap, 'responsive' => (int) $table_responsive, 'tools' => (int) $table_tools, 'display_length' => $table_display_length, 'fixed_columns' => $table_fixcolumns, 'chart' => 'none', 'chart_title' => '', 'server_side' => (int) $table_serverside, 'editable' => (int) $table_editable, 'editor_roles' => $table_editor_roles, 'mysql_table_name' => $table_mysql_name, 'hide_before_load' => $table_hide_before_loaded, 'edit_only_own_rows' => $table_edit_only_own_rows, 'userid_column_id' => $table_userid_column_id, 'var1' => $wdt_var1, 'var2' => $wdt_var2, 'var3' => $wdt_var3);
            $table_array = apply_filters('wpdatatables_filter_update_table_array', $table_array, $table_id);
            $wpdb->update($wpdb->prefix . 'wpdatatables', $table_array, array('id' => $table_id));
            $res['table_id'] = $table_id;
            // rebuilding the columnset
            $res['columns'] = wdt_create_columns_from_table($res['table'], $table_id);
            do_action('wpdatatables_after_save_table');
            echo json_encode($res);
            die;
        }
    }
}
/**
 * Saves the general settings for the table, tries to generate the table 
 * and default settings for the columns
 */
function wdt_save_table()
{
    global $wpdb;
    $_POST = apply_filters('wpdatatables_before_save_table', $_POST);
    $table_id = $_POST['table_id'];
    $table_title = $_POST['table_title'];
    $table_type = $_POST['table_type'];
    if ($table_type == 'csv' || $table_type == 'xls') {
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            $table_content = str_replace(site_url(), str_replace('\\', '/', ABSPATH), $_POST['table_content']);
        } else {
            $table_content = str_replace(site_url(), ABSPATH, $_POST['table_content']);
        }
    } else {
        $table_content = $_POST['table_content'];
    }
    $table_hide_before_loaded = $_POST['hide_before_loaded'] == 'true';
    $table_advanced_filtering = $_POST['table_advanced_filtering'] == 'true';
    $table_filter_form = $_POST['table_filter_form'] == 'true';
    $table_tools = $_POST['table_tools'] == 'true';
    $table_sorting = $_POST['table_sorting'] == 'true';
    $table_fixed_layout = $_POST['fixed_layout'] == 'true';
    $table_word_wrap = $_POST['word_wrap'] == 'true';
    $table_display_length = $_POST['table_display_length'];
    $table_fixheader = $_POST['table_fixheader'] == 'true';
    $table_fixcolumns = $_POST['table_fixcolumns'];
    $table_chart = $_POST['table_chart'];
    $table_charttitle = $_POST['table_charttitle'];
    $table_serverside = $_POST['table_serverside'] == 'true';
    $table_editable = $table_type == 'mysql' ? $_POST['table_editable'] == 'true' : false;
    $table_mysql_name = $table_editable ? $_POST['table_mysql_name'] : '';
    $table_responsive = $_POST['responsive'] == 'true';
    $table_editor_roles = !empty($_POST['editor_roles']) ? $_POST['editor_roles'] : '';
    if (!$table_fixheader) {
        $table_fixcolumns = -1;
    } else {
        $table_fixcolumns = (int) $table_fixcolumns;
    }
    if (!$table_id) {
        // adding new table
        // trying to generate a WPDataTable
        $res = wdt_try_generate_table($table_type, $table_content);
        if (!empty($res['error'])) {
            // if WPDataTables returns an error, replying to the page
            echo json_encode($res);
            die;
        } else {
            // if no problem reported, first saving the table parameters to DB
            $table_array = array('title' => $table_title, 'table_type' => $table_type, 'content' => $table_content, 'filtering' => (int) $table_advanced_filtering, 'filtering_form' => (int) $table_filter_form, 'sorting' => (int) $table_sorting, 'fixed_layout' => (int) $table_fixed_layout, 'responsive' => (int) $table_responsive, 'word_wrap' => (int) $table_word_wrap, 'tools' => (int) $table_tools, 'display_length' => $table_display_length, 'fixed_columns' => $table_fixcolumns, 'chart' => $table_chart, 'chart_title' => $table_charttitle, 'server_side' => (int) $table_serverside, 'editable' => (int) $table_editable, 'editor_roles' => $table_editor_roles, 'mysql_table_name' => $table_mysql_name, 'hide_before_load' => $table_hide_before_loaded);
            $table_array = apply_filters('wpdatatables_filter_insert_table_array', $table_array);
            $wpdb->insert($wpdb->prefix . 'wpdatatables', $table_array);
            // get the newly generated table ID
            $table_id = $wpdb->insert_id;
            $res['table_id'] = $table_id;
            // creating default columns for the new table
            $res['columns'] = wdt_create_columns_from_table($res['table'], $table_id);
            do_action('wpdatatables_after_save_table', $table_id);
            echo json_encode($res);
            die;
        }
    } else {
        // editing existing table
        $query = 'SELECT * 
						FROM ' . $wpdb->prefix . 'wpdatatables
						WHERE id=' . $table_id;
        $table_data = $wpdb->get_row($query, ARRAY_A);
        // checking if table type or content has changed
        if ($table_data['content'] == $table_content && $table_data['table_type'] == $table_type) {
            // if it didn't change only updating the record
            // and receiving the columnset
            $table_array = array('title' => $table_title, 'table_type' => $table_type, 'content' => $table_content, 'filtering' => (int) $table_advanced_filtering, 'filtering_form' => (int) $table_filter_form, 'sorting' => (int) $table_sorting, 'fixed_layout' => (int) $table_fixed_layout, 'word_wrap' => (int) $table_word_wrap, 'responsive' => (int) $table_responsive, 'tools' => (int) $table_tools, 'display_length' => $table_display_length, 'fixed_columns' => $table_fixcolumns, 'chart' => $table_chart, 'chart_title' => $table_charttitle, 'server_side' => (int) $table_serverside, 'editable' => (int) $table_editable, 'editor_roles' => $table_editor_roles, 'mysql_table_name' => $table_mysql_name, 'hide_before_load' => $table_hide_before_loaded);
            $table_array = apply_filters('wpdatatables_filter_update_table_array', $table_array, $table_id);
            $wpdb->update($wpdb->prefix . 'wpdatatables', $table_array, array('id' => $table_id));
            $res['table_id'] = $table_id;
            $res['columns'] = wdt_get_columns_by_table_id($table_id);
            do_action('wpdatatables_after_save_table');
            echo json_encode($res);
            die;
        } else {
            // if it changed trying to rebuild the table and reloading the columnset
            $res = wdt_try_generate_table($table_type, $table_content);
            if (!empty($res['error'])) {
                // if WPDataTables returns an error, replying to the page
                do_action('wpdatatables_after_save_table');
                echo json_encode($res);
                die;
            } else {
                // otherwise updating the table
                $table_array = array('title' => $table_title, 'table_type' => $table_type, 'content' => $table_content, 'filtering' => (int) $table_advanced_filtering, 'filtering_form' => (int) $table_filter_form, 'sorting' => (int) $table_sorting, 'fixed_layout' => (int) $table_fixed_layout, 'word_wrap' => (int) $table_word_wrap, 'responsive' => (int) $table_responsive, 'tools' => (int) $table_tools, 'display_length' => $table_display_length, 'fixed_columns' => $table_fixcolumns, 'chart' => $table_chart, 'chart_title' => $table_charttitle, 'server_side' => (int) $table_serverside, 'editable' => (int) $table_editable, 'editor_roles' => $table_editor_roles, 'mysql_table_name' => $table_mysql_name, 'hide_before_load' => $table_hide_before_loaded);
                $table_array = apply_filters('wpdatatables_filter_update_table_array', $table_array, $table_id);
                $wpdb->update($wpdb->prefix . 'wpdatatables', $table_array, array('id' => $table_id));
                $res['table_id'] = $table_id;
                // rebuilding the columnset
                $res['columns'] = wdt_create_columns_from_table($res['table'], $table_id);
                do_action('wpdatatables_after_save_table');
                echo json_encode($res);
                die;
            }
        }
    }
}
 /**
  * Generates a wpDataTable based on WP data query
  */
 public function generateWdtBasedOnQuery($table_data)
 {
     global $wpdb;
     $table_data['query'] = wpdatatables_sanitize_query($table_data['query']);
     $table_array = array('title' => '', 'table_type' => 'mysql', 'content' => '', 'filtering' => 1, 'filtering_form' => 0, 'sorting' => 1, 'fixed_layout' => 0, 'responsive' => 0, 'word_wrap' => 1, 'tools' => 1, 'display_length' => 10, 'fixed_columns' => 0, 'chart' => 'none', 'chart_title' => '', 'server_side' => 0, 'editable' => 0, 'editor_roles' => '', 'mysql_table_name' => '', 'hide_before_load' => 1);
     $table_array['content'] = $table_data['query'];
     $res = wdt_try_generate_table('mysql', $table_array['content']);
     $wpdb->insert($wpdb->prefix . 'wpdatatables', $table_array);
     // get the newly generated table ID
     $table_id = $wpdb->insert_id;
     $res['table_id'] = $table_id;
     // creating default columns for the new table
     $res['columns'] = wdt_create_columns_from_table($res['table'], $table_id);
     do_action('wpdatatables_after_save_table', $table_id);
     return $res;
 }