| 1 |
<?php |
| 2 |
|
| 3 |
namespace King_Addons; |
| 4 |
|
| 5 |
use Elementor\Widget_Base; |
| 6 |
use Elementor\Controls_Manager; |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Table_Builder extends Widget_Base |
| 13 |
{ |
| 14 |
public function get_name() |
| 15 |
{ |
| 16 |
return 'king-addons-table-builder'; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_title() |
| 20 |
{ |
| 21 |
return esc_html__('KNG Table', 'king-addons'); |
| 22 |
} |
| 23 |
|
| 24 |
public function get_icon() |
| 25 |
{ |
| 26 |
return 'king-addons-icon king-addons-data-table'; |
| 27 |
} |
| 28 |
|
| 29 |
public function get_categories() |
| 30 |
{ |
| 31 |
return ['king-addons']; |
| 32 |
} |
| 33 |
|
| 34 |
public function get_keywords() |
| 35 |
{ |
| 36 |
return ['table', 'data table', 'builder', 'kng table', 'interactive table']; |
| 37 |
} |
| 38 |
|
| 39 |
protected function register_controls(): void |
| 40 |
{ |
| 41 |
$this->start_controls_section( |
| 42 |
'section_content', |
| 43 |
[ |
| 44 |
'label' => esc_html__('Table', 'king-addons'), |
| 45 |
] |
| 46 |
); |
| 47 |
|
| 48 |
$this->add_control( |
| 49 |
'table_id', |
| 50 |
[ |
| 51 |
'label' => esc_html__('Select Table', 'king-addons'), |
| 52 |
'type' => Controls_Manager::SELECT, |
| 53 |
'options' => $this->get_table_options(), |
| 54 |
'default' => '', |
| 55 |
] |
| 56 |
); |
| 57 |
|
| 58 |
$this->add_control( |
| 59 |
'theme_override', |
| 60 |
[ |
| 61 |
'label' => esc_html__('Theme Override', 'king-addons'), |
| 62 |
'type' => Controls_Manager::SELECT, |
| 63 |
'options' => [ |
| 64 |
'' => esc_html__('Default', 'king-addons'), |
| 65 |
'dark' => esc_html__('Dark', 'king-addons'), |
| 66 |
'light' => esc_html__('Light', 'king-addons'), |
| 67 |
], |
| 68 |
'default' => '', |
| 69 |
] |
| 70 |
); |
| 71 |
|
| 72 |
$this->add_control( |
| 73 |
'show_search', |
| 74 |
[ |
| 75 |
'label' => esc_html__('Show Search', 'king-addons'), |
| 76 |
'type' => Controls_Manager::SWITCHER, |
| 77 |
'default' => 'yes', |
| 78 |
] |
| 79 |
); |
| 80 |
|
| 81 |
$this->add_control( |
| 82 |
'show_pagination', |
| 83 |
[ |
| 84 |
'label' => esc_html__('Show Pagination', 'king-addons'), |
| 85 |
'type' => Controls_Manager::SWITCHER, |
| 86 |
'default' => 'yes', |
| 87 |
] |
| 88 |
); |
| 89 |
|
| 90 |
$this->add_control( |
| 91 |
'enable_sort', |
| 92 |
[ |
| 93 |
'label' => esc_html__('Enable Sorting', 'king-addons'), |
| 94 |
'type' => Controls_Manager::SWITCHER, |
| 95 |
'default' => 'yes', |
| 96 |
] |
| 97 |
); |
| 98 |
|
| 99 |
$this->end_controls_section(); |
| 100 |
|
| 101 |
Core::renderProFeaturesSection($this, '', Controls_Manager::RAW_HTML, 'table-builder', [ |
| 102 |
'Advanced filters and column types', |
| 103 |
'Conditional formatting and badges', |
| 104 |
'Inline charts and expandable rows', |
| 105 |
'Google Sheets sync and JSON/PDF export', |
| 106 |
'Analytics and role-based access' |
| 107 |
]); |
| 108 |
} |
| 109 |
|
| 110 |
private function get_table_options(): array |
| 111 |
{ |
| 112 |
$post_type = class_exists('King_Addons\Data_Table_Builder') ? Data_Table_Builder::POST_TYPE : 'kng_table'; |
| 113 |
$tables = get_posts([ |
| 114 |
'post_type' => $post_type, |
| 115 |
'post_status' => 'publish', |
| 116 |
'posts_per_page' => 50, |
| 117 |
'orderby' => 'title', |
| 118 |
'order' => 'ASC', |
| 119 |
]); |
| 120 |
|
| 121 |
$options = [ |
| 122 |
'' => esc_html__('Select a table', 'king-addons'), |
| 123 |
]; |
| 124 |
|
| 125 |
foreach ($tables as $table) { |
| 126 |
$options[$table->ID] = $table->post_title; |
| 127 |
} |
| 128 |
|
| 129 |
return $options; |
| 130 |
} |
| 131 |
|
| 132 |
protected function render(): void |
| 133 |
{ |
| 134 |
$settings = $this->get_settings_for_display(); |
| 135 |
$table_id = isset($settings['table_id']) ? absint($settings['table_id']) : 0; |
| 136 |
|
| 137 |
if ($table_id === 0) { |
| 138 |
$link = admin_url('admin.php?page=king-addons-table-builder&view=add'); |
| 139 |
echo '<div class="kng-table-empty">'; |
| 140 |
echo esc_html__('Select a table in the widget settings or create one in Table Builder.', 'king-addons'); |
| 141 |
echo ' <a href="' . esc_url($link) . '" target="_blank" rel="noopener">' . esc_html__('Create Table', 'king-addons') . '</a>'; |
| 142 |
echo '</div>'; |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
$shortcode = '[kng_table id="' . $table_id . '"'; |
| 147 |
|
| 148 |
if (!empty($settings['theme_override'])) { |
| 149 |
$shortcode .= ' theme="' . esc_attr($settings['theme_override']) . '"'; |
| 150 |
} |
| 151 |
|
| 152 |
if ($settings['show_search'] !== 'yes') { |
| 153 |
$shortcode .= ' search="false"'; |
| 154 |
} |
| 155 |
|
| 156 |
if ($settings['show_pagination'] !== 'yes') { |
| 157 |
$shortcode .= ' pagination="false"'; |
| 158 |
} |
| 159 |
|
| 160 |
if ($settings['enable_sort'] !== 'yes') { |
| 161 |
$shortcode .= ' sort="false"'; |
| 162 |
} |
| 163 |
|
| 164 |
$shortcode .= ']'; |
| 165 |
|
| 166 |
echo do_shortcode($shortcode); |
| 167 |
} |
| 168 |
} |
| 169 |
|