/**
 * Handler for the shortcode
 */
function wpdatatable_shortcode_handler($atts, $content = null)
{
    global $wpdb, $wdt_var1, $wdt_var2, $wdt_var3;
    extract(shortcode_atts(array('id' => '0', 'show_only_chart' => false, 'no_scripts' => 0, 'var1' => '', 'var2' => '', 'var3' => ''), $atts));
    // Protection
    if (!$id) {
        return false;
    }
    $table_data = wdt_get_table_by_id($id);
    $column_data = wdt_get_columns_by_table_id($id);
    // Do action before rendering a table
    do_action('wpdatatables_before_render_table', $id);
    // Shortcode variables
    $wdt_var1 = $var1;
    $wdt_var2 = $var2;
    $wdt_var3 = $var3;
    // Preparing column properties
    $column_order = array();
    $column_titles = array();
    $column_widths = array();
    $column_types = array();
    $column_possible_values = array();
    foreach ($column_data as $column) {
        $column_order[(int) $column->pos] = $column->orig_header;
        if ($column->display_header) {
            $column_titles[$column->orig_header] = $column->display_header;
        }
        if ($column->width) {
            $column_widths[$column->orig_header] = $column->width;
        }
        if ($column->column_type != 'autodetect') {
            $column_types[$column->orig_header] = $column->column_type;
        }
        $column_possible_values[$column->orig_header] = $column->possible_values;
    }
    $tbl = new WPDataTable();
    $tbl->setWpId($id);
    if (empty($table_data['content'])) {
        return __('wpDataTable with provided ID not found!', 'wpdatatables');
    }
    switch ($table_data['table_type']) {
        case 'mysql':
            if ($table_data['server_side']) {
                $tbl->enableServerProcessing();
            }
            if ($table_data['editable']) {
                if (wdt_current_user_can_edit($table_data['editor_roles'], $id)) {
                    $tbl->enableEditing();
                }
            }
            $tbl->queryBasedConstruct($table_data['content'], array(), array('data_types' => $column_types, 'column_titles' => $column_titles));
            break;
        case 'xls':
        case 'csv':
            $tbl->excelBasedConstruct($table_data['content'], array('data_types' => $column_types, 'column_titles' => $column_titles));
            break;
        case 'xml':
            $tbl->XMLBasedConstruct($table_data['content'], array('data_types' => $column_types, 'column_titles' => $column_titles));
            break;
        case 'json':
            $tbl->jsonBasedConstruct($table_data['content'], array('data_types' => $column_types, 'column_titles' => $column_titles));
            break;
        case 'serialized':
            $array = unserialize(file_get_contents($table_data['content']));
            $tbl->arrayBasedConstruct($array, array('data_types' => $column_types, 'column_titles' => $column_titles));
            break;
    }
    if ($table_data['hide_before_load']) {
        $tbl->hideBeforeLoad();
    } else {
        $tbl->showBeforeLoad();
    }
    $tbl->reorderColumns($column_order);
    $tbl->wdtDefineColumnsWidth($column_widths);
    $tbl->setColumnsPossibleValues($column_possible_values);
    // Applying form
    $tbl->setFilteringForm($table_data['filtering_form']);
    // Applying responsiveness
    if ($table_data['responsive']) {
        $tbl->setResponsive(true);
    }
    // Applying filter, if enabled
    if ($table_data['filtering']) {
        $tbl->enableAdvancedFilter();
    }
    if (!$no_scripts) {
        wp_enqueue_script('jquery-ui-core');
        wp_enqueue_script('jquery-ui-progressbar');
        wp_enqueue_script('jquery-ui-datepicker');
        wp_enqueue_script('jquery-ui-button');
        wp_enqueue_style('dashicons');
        wp_enqueue_script('wdt_google_charts', 'https://www.google.com/jsapi');
        wp_enqueue_script('formstone-selecter', WDT_JS_PATH . 'selecter/jquery.fs.selecter.min.js');
        wp_enqueue_style('formstone-selecter', WDT_CSS_PATH . 'jquery.fs.selecter.css');
        wp_enqueue_script('formstone-picker', WDT_JS_PATH . 'picker/jquery.fs.picker.min.js');
        wp_enqueue_style('formstone-picker', WDT_CSS_PATH . 'jquery.fs.picker.css');
        wp_enqueue_script('remodal-popup', WDT_JS_PATH . 'popup/jquery.remodal.min.js');
        wp_enqueue_style('remodal-popup', WDT_CSS_PATH . 'jquery.remodal.css');
        wp_enqueue_script('pickadate-main', WDT_JS_PATH . 'datepicker/picker.js');
        wp_enqueue_script('pickadate-date', WDT_JS_PATH . 'datepicker/picker.date.js');
        wp_enqueue_style('pickadate-main', WDT_CSS_PATH . 'datepicker.default.css');
        wp_enqueue_style('pickadate-date', WDT_CSS_PATH . 'datepicker.default.date.css');
    } else {
        $tbl->disableScripts();
    }
    foreach ($column_data as $column) {
        // Set filter types
        $tbl->getColumn($column->orig_header)->setFilterType($column->filter_type);
        // Set CSS class
        $tbl->getColumn($column->orig_header)->addCSSClass($column->css_class);
        // set visibility
        if (!$column->visible) {
            $tbl->getColumn($column->orig_header)->hide();
        }
        // Set default value
        $tbl->getColumn($column->orig_header)->setDefaultValue($column->default_value);
        // Check the default values passed from URL
        if (isset($_GET['wdt_column_filter'])) {
            foreach ($_GET['wdt_column_filter'] as $fltColKey => $fltDefVal) {
                $tbl->getColumn($fltColKey)->setDefaultValue($fltDefVal);
            }
        }
        // Set hiding on phones and tablets for responsiveness
        if ($tbl->isResponsive()) {
            if ($column->hide_on_phones) {
                $tbl->getColumn($column->orig_header)->hideOnPhones();
            }
            if ($column->hide_on_tablets) {
                $tbl->getColumn($column->orig_header)->hideOnTablets();
            }
        }
        // if grouping enabled for this column, passing it to table class
        if ($column->group_column) {
            $tbl->groupByColumn($column->orig_header);
        }
        if ($column->sort_column !== '0') {
            $tbl->setDefaultSortColumn($column->orig_header);
            if ($column->sort_column == '1') {
                $tbl->setDefaultSortDirection('ASC');
            } elseif ($column->sort_column == '2') {
                $tbl->setDefaultSortDirection('DESC');
            }
        }
        if ($table_data['chart'] != 'none') {
            if ($column->use_in_chart) {
                $tbl->addChartSeries($column->orig_header);
            }
            if ($column->chart_horiz_axis) {
                $tbl->setChartHorizontalAxis($column->orig_header);
            }
        }
        // Set ID column if specified
        if ($column->id_column) {
            $tbl->setIdColumnKey($column->orig_header);
        }
        // Set front-end editor input type
        $tbl->getColumn($column->orig_header)->setInputType($column->input_type);
    }
    $output_str = '';
    if (!$show_only_chart) {
        if ($table_data['title']) {
            $output_str .= apply_filters('wpdatatables_filter_table_title', empty($table_data['title']) ? '' : '<h2>' . $table_data['title'] . '</h2>', $id);
        }
        if (!$table_data['sorting']) {
            $tbl->sortDisable();
        }
        if (!$table_data['tools']) {
            $tbl->disableTT();
        }
        // display length
        if ($table_data['display_length'] != 0) {
            $tbl->setDisplayLength($table_data['display_length']);
        } else {
            $tbl->disablePagination();
        }
        if (get_option('wdtInterfaceLanguage') != '') {
            $tbl->setInterfaceLanguage(get_option('wdtInterfaceLanguage'));
        }
        $output_str .= $tbl->generateTable();
    }
    if ($table_data['chart'] != 'none') {
        $tbl->setChartType(ucfirst($table_data['chart']));
        $tbl->setChartTitle($table_data['chart_title']);
        $tbl->printChart('wdt_' . $tbl->getId() . '_chart');
        if (get_option('wdtRenderCharts') == 'above') {
            $output_str = '<div id="wdt_' . $tbl->getId() . '_chart" class="wpDataTables wdt_chart"></div>' . $output_str;
        } else {
            $output_str .= '<div id="wdt_' . $tbl->getId() . '_chart" class="wpDataTables wdt_chart"></div>';
        }
    }
    // Generate the style block
    $output_str .= "<style>\n";
    // Table layout
    $customCss = get_option('wdtCustomCss');
    if ($table_data['fixed_layout'] || $table_data['word_wrap']) {
        $output_str .= $table_data['fixed_layout'] ? "table.wpDataTable { table-layout: fixed !important; }\n" : '';
        $output_str .= $table_data['word_wrap'] ? "table.wpDataTable td, table.wpDataTable th { white-space: normal !important; }\n" : '';
    }
    if ($customCss) {
        $output_str .= stripslashes_deep($customCss);
    }
    if (get_option('wdtNumbersAlign')) {
        $output_str .= "table.wpDataTable td.numdata { text-align: right !important; }\n";
    }
    $output_str .= "</style>\n";
    $customJs = get_option('wdtCustomJs');
    if ($customJs) {
        $output_str .= '<script type="text/javascript">' . stripslashes_deep($customJs) . '</script>';
    }
    // Color and font settings
    $wdtFontColorSettings = get_option('wdtFontColorSettings');
    if (!empty($wdtFontColorSettings)) {
        $wdtFontColorSettings = unserialize($wdtFontColorSettings);
        $tpl = new PDTTpl();
        $tpl->addData('wdtFontColorSettings', $wdtFontColorSettings);
        $tpl->setTemplate('style_block.inc.php');
        $style_block_html = $tpl->returnData();
        $style_block_html = apply_filters('wpdatatables_filter_style_block', $style_block_html, $id);
        $output_str .= $style_block_html;
    }
    $output_str = apply_filters('wpdatatables_filter_rendered_table', $output_str, $id);
    return $output_str;
}