class.wdtbrowsechartstable.php 22.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
<?php

defined('ABSPATH') or die('Access denied.');
/**
 * Browse charts for the admin panel
 */

if (!class_exists('WP_List_Table')) {
    require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}

class WDTBrowseChartsTable extends WP_List_Table {

    /**
     * Get a list of columns. The format is:
     * 'internal-name' => 'Title'
     *
     * @since 3.1.0
     * @access public
     * @abstract
     *
     * @return array
     */
    public function get_columns() {
        return array(
            'cb' => '<input type="checkbox" />',
            'id' => 'ID',
            'title' => 'Title',
            'engine' => 'Render Engine',
            'type' => 'Chart Type',
            'shortcode' => 'Shortcode',
            'functions' => 'Functions',
        );
    }

    /**
     * Get a list of sortable columns. The format is:
     * 'internal-name' => 'orderby'
     * or
     * 'internal-name' => array( 'orderby', true )
     *
     * The second format will make the initial sorting order be descending
     *
     * @since 3.1.0
     * @access protected
     *
     * @return array
     */
    public function get_sortable_columns() {
        return array(
            'id' => array('id', true),
            'title' => array('title', false),
            'engine' => array('engine', false),
            'type' => array('type', false)
        );
    }

    /**
     * Get chart count for the browser
     *
     * @return null|string
     */
    public function getChartCount() {
        global $wpdb;

        $query = "SELECT COUNT(*) FROM {$wpdb->prefix}wpdatacharts";

        if (isset($_REQUEST['s'])) {
            if (is_numeric($_REQUEST['s'])){
                $query .= " WHERE id LIKE '" . sanitize_text_field($_POST['s']) . "'";
            }else{
                $query .= " WHERE title LIKE '%" . sanitize_text_field($_POST['s']) . "%'";
            }
        }

        $count = $wpdb->get_var($query);

        return $count;
    }

    /**
     * Get all charts for the Browse Charts (wpDataTables Charts) Page
     *
     * @return array|mixed|null|object
     */
    function getAllCharts() {
        global $wpdb;
        $query = "SELECT id, title, type, engine
                    FROM {$wpdb->prefix}wpdatacharts ";

        if (isset($_REQUEST['s'])) {
            if (is_numeric($_REQUEST['s'])){
                $query .= " WHERE id LIKE '" . sanitize_text_field($_POST['s']) . "'";
            }else{
                $query .= " WHERE title LIKE '%" . sanitize_text_field($_POST['s']) . "%'";
            }
        }

        if (isset($_REQUEST['orderby'])) {
            if (in_array(
                $_REQUEST['orderby'],
                array(
                    'id',
                    'title',
                    'engine',
                    'type'
                )
            )
            ) {
                $query .= " ORDER BY " . $_GET['orderby'];
                if ($_REQUEST['order'] == 'desc') {
                    $query .= " DESC ";
                } else {
                    $query .= " ASC ";
                }
            }
        } else {
            $query .= " ORDER BY id ASC ";
        }

        if (isset($_REQUEST['paged'])) {
            $paged = (int)$_REQUEST['paged'];
        } else {
            $paged = 1;
        }

        $tables_per_page = get_option('wdtTablesPerPage') ? get_option('wdtTablesPerPage') : 10;

        $query .= " LIMIT " . ($paged - 1) * $tables_per_page . ", " . $tables_per_page;

        $allCharts = apply_filters('wpdatatables_filter_browse_charts', $wpdb->get_results($query, ARRAY_A));

        return $allCharts;
    }

    /**
     * Set default columns value
     * @param object $item
     * @param string $column_name
     * @return string
     */
    function column_default($item, $column_name) {
        switch ($column_name) {
            case 'shortcode':
                return '<span class="wdt-shortcode bgm-green">[wpdatachart id=' . $item['id'] . ']</span>';
                break;
            case 'functions':
            case 'table_type':
	            $return_string =    ' <a type="button" 
	                                     class="wdt-duplicate-chart" 
	                                     data-chart_id="' . $item['id'] . '" 
	                                     data-chart_name="' . $item['title'] . '"
	                                     data-toggle="tooltip" title="' . __('Duplicate', 'wpdatatables') . '" 
	                                     href="#"></a>';
                $return_string .=   ' <a type="button" 
                                         class="wdt-configure" 
                                         data-table_id="' . $item['id'] . '" 
                                         data-table_name="' . $item['title'] . '" 
                                         data-toggle="tooltip" title="' . __('Configure', 'wpdatatables') . '" 
                                         href="admin.php?page=wpdatatables-chart-wizard&chart_id=' . $item['id'] . '"><i class="zmdi zmdi-settings"></i></a>';
                $return_string .=   ' <a type="button" 
                                         class="wdt-submit-delete" 
                                         data-table_id="' . $item['id'] . '" 
                                         data-table_name="' . $item['title'] . '" 
                                         data-toggle="tooltip" title="' . __('Delete', 'wpdatatables') . '" 
                                         href="'. wp_nonce_url('admin.php?page=wpdatatables-charts&action=delete&chart_id=' . $item['id'] . '', 'wdtDeleteChartNonce', 'wdtNonce' ) .'"><i class="zmdi zmdi-delete"></i></a>';
                return $return_string;
                break;
            case 'id':
            case 'title':
            default:
                return $item[$column_name];
                break;
        }
    }

    function column_title($item) {
        $actions = array(
            'edit' => '<a href="admin.php?page=wpdatatables-chart-wizard&chart_id=' . $item['id'] . '" title="' . __('Configure', 'wpdatatables') . '">' . __('Configure', 'wpdatatables') . '</a>',
            'trash' => '<a class="wdt-submit-delete" title="' . __('Delete', 'wpdatatables') . '" href="'. wp_nonce_url('admin.php?page=wpdatatables-charts&action=delete&chart_id=' . $item['id'] . '', 'wdtDeleteChartNonce', 'wdtNonce' ) .'" rel="' . $item['id'] . '">' . __('Delete', 'wpdatatables') . '</a>'
        );

        return '<a href="admin.php?page=wpdatatables-chart-wizard&chart_id=' . $item['id'] . '">' . $item['title'] . '</a> ' . $this->row_actions($actions);
    }

    /**
     * Get bulk actions
     * @return array
     */
    function get_bulk_actions() {
        $actions = array(
            'delete' => 'Delete'
        );
        return $actions;
    }

    /**
     * Customize checkbox column items
     * @param object $item
     * @return string
     */
    function column_cb($item) {
        return sprintf(
            '<div class="checkbox"><input type="checkbox" name="chart_id[]" value="%s" /><i class="input-helper"></i></div>', $item['id']
        );
    }

    /**
     * Display chart type column values
     * @param $item
     * @return string
     */
    function column_type($item) {
        switch ($item['type']) {
            case 'google_column_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Column Chart', 'wpdatatables') . '</span>';
                break;
            case 'google_histogram':
                return '<span class="wdt-chart-type bgm-gray">' . __('Histogram', 'wpdatatables') . '</span>';
                break;
            case 'google_bar_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Bar Chart', 'wpdatatables') . '</span>';
                break;
            case 'google_area_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Area Chart', 'wpdatatables') . '</span>';
                break;
            case 'google_stepped_area_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Stepped Area Chart', 'wpdatatables') . '</span>';
                break;
            case 'google_line_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Line Chart', 'wpdatatables') . '</span>';
                break;
            case 'google_pie_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Pie Chart', 'wpdatatables') . '</span>';
                break;
            case 'google_bubble_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Bubble Chart', 'wpdatatables') . '</span>';
                break;
            case 'google_donut_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Donut Chart', 'wpdatatables') . '</span>';
                break;
            case 'google_gauge_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Gauge Chart', 'wpdatatables') . '</span>';
                break;
            case 'google_scatter_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Scatter Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_line_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Line Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_spline_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Spline Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_basic_area_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Basic Area Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_stacked_area_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Stacked Area Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_basic_bar_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Basic Bar Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_stacked_bar_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Stacked Bar Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_basic_column_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Basic Column Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_stacked_column_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Stacked Column Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_pie_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Pie Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_pie_with_gradient_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Pie With Gradient Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_donut_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Donut Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_scatter_plot':
                return '<span class="wdt-chart-type bgm-gray">' . __('Scatter Plot', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_3d_column_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('3D Column Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_3d_pie_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('3D Pie Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_3d_donut_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('3D Donut Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_treemap_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Treemap Chart', 'wpdatatables') . '</span>';
                break;
            case 'highcharts_treemap_level_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Treemap level Chart', 'wpdatatables') . '</span>';
                break;
            case 'chartjs_line_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Line Chart', 'wpdatatables') . '</span>';
                break;
            case 'chartjs_area_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Area Chart', 'wpdatatables') . '</span>';
                break;
            case 'chartjs_stacked_area_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Stacked Area Chart', 'wpdatatables') . '</span>';
                break;
            case 'chartjs_column_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Column Chart', 'wpdatatables') . '</span>';
                break;
            case 'chartjs_radar_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Radar Chart', 'wpdatatables') . '</span>';
                break;
            case 'chartjs_polar_area_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Polar Area Chart', 'wpdatatables') . '</span>';
                break;
            case 'chartjs_pie_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Pie Chart', 'wpdatatables') . '</span>';
                break;
            case 'chartjs_doughnut_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Doughnut Chart', 'wpdatatables') . '</span>';
                break;
            case 'chartjs_bubble_chart':
                return '<span class="wdt-chart-type bgm-gray">' . __('Bubble Chart', 'wpdatatables') . '</span>';
                break;
            default:
                return $item;
                break;
        }

    }

    function column_engine($item) {
        switch ($item['engine']) {
            case 'google':
                return '<span class="wdt-render-engine bgm-gray">' . __('Google', 'wpdatatables') . '</span>';
                break;
            case 'highcharts':
                return '<span class="wdt-render-engine bgm-gray">' . __('Highcharts', 'wpdatatables') . '</span>';
                break;
            case 'chartjs':
                return '<span class="wdt-render-engine bgm-gray">' . __('Chart.js', 'wpdatatables') . '</span>';
                break;
            default:
                return $item;
                break;
        }
    }

    /**
     * Prepares the list of items for displaying.
     * @uses WP_List_Table::set_pagination_args()
     *
     * @since 3.1.0
     * @access public
     * @abstract
     */
    function prepare_items() {
        $per_page = get_option('wdtTablesPerPage') ? get_option('wdtTablesPerPage') : 10;

        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);

        $this->set_pagination_args(
            array(
                'total_items' => $this->getChartCount(),
                'per_page' => $per_page
            )
        );

        $this->items = $this->getAllCharts();
    }

    /**
     * Print column headers, accounting for hidden and sortable columns.
     *
     * @since 3.1.0
     * @access public
     *
     * @staticvar int $cb_counter
     *
     * @param bool $with_id Whether to set the id attribute or not
     */
    function print_column_headers($with_id = true) {
        list($columns, $hidden, $sortable, $primary) = $this->get_column_info();

        $current_url = set_url_scheme('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
        $current_url = remove_query_arg('paged', $current_url);

        if (isset($_GET['orderby'])) {
            $current_orderby = $_GET['orderby'];
        } else {
            $current_orderby = '';
        }

        if (isset($_GET['order']) && 'desc' === $_GET['order']) {
            $current_order = 'desc';
        } else {
            $current_order = 'asc';
        }

        if (!empty($columns['cb'])) {
            static $cb_counter = 1;
            $columns['cb'] = '<label class="screen-reader-text" for="cb-select-all-' . $cb_counter . '">' . __('Select All') . '</label>'
                . '<div class="checkbox"><input id="cb-select-all-' . $cb_counter . '" type="checkbox" /><i class="input-helper"></i></div>';
            $cb_counter++;
        }

        foreach ($columns as $column_key => $column_display_name) {
            $class = array('manage-column', "column-$column_key");

            if (in_array($column_key, $hidden)) {
                $class[] = 'hidden';
            }

            if ('cb' === $column_key)
                $class[] = 'check-column';
            elseif (in_array($column_key, array('posts', 'comments', 'links')))
                $class[] = 'num';

            if ($column_key === $primary) {
                $class[] = 'column-primary';
            }

            if (isset($sortable[$column_key])) {
                list($orderby, $desc_first) = $sortable[$column_key];

                if ($current_orderby === $orderby) {
                    $order = 'asc' === $current_order ? 'desc' : 'asc';
                    $class[] = 'sorted';
                    $class[] = $current_order;
                } else {
                    $order = $desc_first ? 'desc' : 'asc';
                    $class[] = 'sortable';
                    $class[] = $desc_first ? 'asc' : 'desc';
                }

                $column_display_name = '<a href="' . esc_url(add_query_arg(compact('orderby', 'order'), $current_url)) . '"><span>' . $column_display_name . '</span><span class="sorting-indicator"></span></a>';
            }

            $tag = ('cb' === $column_key) ? 'td' : 'th';
            $scope = ('th' === $tag) ? 'scope="col"' : '';
            $id = $with_id ? "id='$column_key'" : '';

            if (!empty($class))
                $class = "class='" . join(' ', $class) . "'";

            echo "<$tag $scope $id $class>$column_display_name</$tag>";
        }
    }

    /**
     * Display no items text
     */
    function no_items() {
        _e('No wpDataCharts in the system yet.', 'wpdatatables');
    }

    /**
     * Display the table
     *
     * @since 3.1.0
     * @access public
     */
    function display() {
        $singular = $this->_args['singular'];

        $this->screen->render_screen_reader_content('heading_list');

        require_once(WDT_ROOT_PATH . '/templates/admin/browse/table_list.inc.php');
    }

    /**
     * Display the pagination.
     *
     * @since 3.1.0
     * @access protected
     *
     * @param string $which
     */
    function pagination($which) {
        if (empty($this->_pagination_args))
            return;

        $max = $this->_pagination_args['total_pages'];
        $paged = $this->get_pagenum();

        /** Stop execution if there's only 1 page */
        if ($max <= 1)
            return;

        $removable_query_args = wp_removable_query_args();
        $current_url = set_url_scheme('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
        $current_url = remove_query_arg($removable_query_args, $current_url);

        /**    Add current page to the array */
        if ($paged >= 1)
            $links[] = $paged;

        /**    Add the pages around the current page to the array */
        if ($paged >= 3) {
            $links[] = $paged - 1;
            $links[] = $paged - 2;
        }

        if (($paged + 2) <= $max) {
            $links[] = $paged + 2;
            $links[] = $paged + 1;
        }

        $disable_first = $disable_last = $disable_prev = $disable_next = false;

        if ($paged == 1) {
            $disable_first = true;
            $disable_prev = true;
        }
        if ($paged == 2) {
            $disable_first = true;
        }
        if ($paged == $max) {
            $disable_last = true;
            $disable_next = true;
        }
        if ($paged == $max - 1) {
            $disable_last = true;
        }

        require_once(WDT_ROOT_PATH . '/templates/admin/browse/pagination.inc.php');
    }

    /**
     * Display the bulk actions dropdown.
     *
     * @since 3.1.0
     * @access protected
     *
     * @param string $which The location of the bulk actions: 'top' or 'bottom'.
     * This is designated as optional for backward compatibility.
     */
    function bulk_actions($which = '') {
        if (is_null($this->_actions)) {
            $no_new_actions = $this->_actions = $this->get_bulk_actions();
            /**
             * Filters the list table Bulk Actions drop-down.
             *
             * The dynamic portion of the hook name, `$this->screen->id`, refers
             * to the ID of the current screen, usually a string.
             *
             * This filter can currently only be used to remove bulk actions.
             *
             * @since 3.5.0
             *
             * @param array $actions An array of the available bulk actions.
             */
            $this->_actions = apply_filters("bulk_actions-{$this->screen->id}", $this->_actions);
            $this->_actions = array_intersect_assoc($this->_actions, $no_new_actions);
            $two = '';
        } else {
            $two = '2';
        }

        if (empty($this->_actions))
            return;

        require(WDT_ROOT_PATH . '/templates/admin/browse/bulk_actions.inc.php');
    }

    /**
     * Generate the table navigation above or below the table
     *
     * @since 3.1.0
     * @access protected
     * @param string $which
     */
    function display_tablenav($which) {
        if ('top' === $which) {
            wp_nonce_field('bulk-' . $this->_args['plural']);
        }

        require(WDT_ROOT_PATH . '/templates/admin/browse/table_navigation.inc.php');
    }

    /**
     * Displays the search box.
     *
     * @since 3.1.0
     * @access public
     *
     * @param string $text The 'submit' button label.
     * @param string $input_id ID attribute value for the search input field.
     */
    function search_box($text, $input_id) {
        if (empty($_REQUEST['s']) && !$this->has_items())
            return;

        $input_id = $input_id . '-search-input';

        if (!empty($_REQUEST['orderby']))
            echo '<input type="hidden" name="orderby" value="' . esc_attr($_REQUEST['orderby']) . '" />';
        if (!empty($_REQUEST['order']))
            echo '<input type="hidden" name="order" value="' . esc_attr($_REQUEST['order']) . '" />';
        if (!empty($_REQUEST['post_mime_type']))
            echo '<input type="hidden" name="post_mime_type" value="' . esc_attr($_REQUEST['post_mime_type']) . '" />';
        if (!empty($_REQUEST['detached']))
            echo '<input type="hidden" name="detached" value="' . esc_attr($_REQUEST['detached']) . '" />';

        require_once(WDT_ROOT_PATH . '/templates/admin/browse/search_box.inc.php');
    }
}