Exemplo n.º 1
0
/**
 * 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;
}
/**
 * Handle table row delete
 */
function wdt_delete_table_row()
{
    global $wpdb;
    $table_id = filter_var($_POST['table_id'], FILTER_SANITIZE_NUMBER_INT);
    $id_key = filter_var($_POST['id_key'], FILTER_SANITIZE_STRING);
    $id_val = filter_var($_POST['id_val'], FILTER_SANITIZE_NUMBER_INT);
    $table_data = wdt_get_table_by_id($table_id);
    $mysql_table_name = $table_data['mysql_table_name'];
    // If current user cannot edit - do nothing
    if (!wdt_current_user_can_edit($table_data['editor_roles'], $table_id)) {
        exit;
    }
    do_action('wpdatatables_before_delete_row', $id_val, $table_id);
    // If the plugin is using WP DB
    if (!get_option('wdtUseSeparateCon')) {
        $wpdb->delete($mysql_table_name, array($id_key => $id_val));
    } else {
        $sql = new PDTSql(WDT_MYSQL_HOST, WDT_MYSQL_DB, WDT_MYSQL_USER, WDT_MYSQL_PASSWORD);
        $query = "DELETE FROM " . $mysql_table_name . " WHERE `" . $id_key . "`='" . $id_val . "'";
        $sql->doQuery($query);
    }
    exit;
}
Exemplo n.º 3
0
	  /**
	   * Handler for the table 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' => '%%no_val%%',
		      'var2' => '%%no_val%%',
		      'var3' => '%%no_val%%'
		      ), $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 !== '%%no_val%%' ? $var1 : $table_data['var1'];
		   $wdt_var2 = $var2 !== '%%no_val%%' ? $var2 : $table_data['var2'];
		   $wdt_var3 = $var3 !== '%%no_val%%' ? $var3 : $table_data['var3'];
		   
		   // Preparing column properties
		   $column_order = array();
		   $column_titles = array();
		   $column_widths = array();
		   $column_types = array();
		   $column_possible_values = array();
                   $userid_column_header = '';
		   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;
                        }
                        if( $table_data['edit_only_own_rows'] 
                                && ( $table_data['userid_column_id'] == $column->id ) ){
                            $userid_column_header = $column->orig_header;
                        }
                       $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' :
		   		case 'manual' :
	   				if($table_data['server_side']){
                                            $tbl->enableServerProcessing();
	   				}
	   				if($table_data['editable']){
                                            if(wdt_current_user_can_edit( $table_data['editor_roles'], $id )){
                                                    $tbl->enableEditing();
                                            }
	   				}
                                        if( !empty( $table_data['edit_only_own_rows'] ) ){
                                            $tbl->setOnlyOwnRows( true );
                                            $tbl->setUserIdColumn( $userid_column_header );
                                        }
		   			$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( WDTTools::curlGetData( $table_data['content'] ) );
		   			$tbl->arrayBasedConstruct( $array, 
		   				array(
		   					'data_types'=>$column_types,
		   					'column_titles'=>$column_titles
		   					)
		   			);
		   			break;
                                case 'google_spreadsheet':
                                        $array = WDTTools::extractGoogleSpreadsheetArray( $table_data['content'] );
                                        $tbl->arrayBasedConstruct( 
                                                $array, 
                                                array(
                                                        'data_types'=>$column_types,
                                                        'column_titles'=>$column_titles
                                                        )
                                        );
                                        break;
		   }
                   
                   $tbl = apply_filters( 'wpdatatables_filter_initial_table_construct', $tbl );//can be used for creating new table type
                   
		   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','//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('wpdatatables-icheck',WDT_JS_PATH.'icheck/icheck.min.js');
                        wp_enqueue_style('wpdatatables-icheck',WDT_CSS_PATH.'icheck.minimal.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();
		    }
                   $columnIndex = 1;
                   $columnsCSS = '';
		   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);
                        // Define if input cannot be empty
                        $tbl->getColumn($column->orig_header)->setNotNull( (bool) $column->input_mandatory );
                        if( $column->visible ){
                            // Get display before/after and color
                            if( $column->text_before != '' ){
                                $columnsCSS .= "\n#{$tbl->getId()} > tbody > tr > td:nth-child({$columnIndex}):before { content: '{$column->text_before}' }";
                            }
                            if( $column->text_after != '' ){
                                $columnsCSS .= "\n#{$tbl->getId()} > tbody > tr > td:nth-child({$columnIndex}):after { content: '{$column->text_after}' }";
                            }
                            if( $column->color != '' ){
                                $columnsCSS .= "\n#{$tbl->getId()} > tbody > tr > td:nth-child({$columnIndex}), "
                                                . "#{$tbl->getId()} > thead > tr > th:nth-child({$columnIndex}), "
                                                . "#{$tbl->getId()} > tfoot > tr > th:nth-child({$columnIndex}) { background-color: {$column->color} !important; }";
                            }
                            $columnIndex++;
                        }
		   }
		   $output_str = '';
		   if(!$show_only_chart){
                           if( $table_data['show_title'] ){
                                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();
		   }else{
                       if( get_option('wdtMinifiedJs') ){
                            wp_enqueue_script( 'wpdatatables',WDT_JS_PATH.'wpdatatables/wpdatatables.min.js' );
                       }else{
                            wp_enqueue_script( 'wpdatatables',WDT_JS_PATH.'wpdatatables/wpdatatables.js' );
                       }
                   }

		   if($table_data['chart'] != 'none') {
                        $tbl->setChartType(ucfirst($table_data['chart']));
                        $tbl->setChartTitle($table_data['chart_title']);
                        $output_str = $tbl->renderChart('wdt_'.$tbl->getId().'_chart') . $output_str;
                        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";
                   // Columns text before and after
                   if( !empty( $columnsCSS ) ){
                       $output_str .= $columnsCSS;
                   }
	   	   // 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";
	   	   
                   add_action( 'wp_footer', 'wdt_render_script_style_block', 99999 );
			
                   $output_str = apply_filters( 'wpdatatables_filter_rendered_table', $output_str, $id );
			
		   return $output_str;
		}