| 1 |
<?php |
| 2 |
defined('ABSPATH') || exit; |
| 3 |
|
| 4 |
/** |
| 5 |
* Module Name: Table |
| 6 |
* Description: Display responsive data tables |
| 7 |
*/ |
| 8 |
class TB_Table_Module extends Themify_Builder_Component_Module { |
| 9 |
|
| 10 |
public static function get_module_name():string { |
| 11 |
return __('Table', 'themify'); |
| 12 |
} |
| 13 |
|
| 14 |
public static function get_module_icon():string { |
| 15 |
return 'layout-grid2'; |
| 16 |
} |
| 17 |
|
| 18 |
public static function get_js_css():array { |
| 19 |
return array( |
| 20 |
'css' => 1, |
| 21 |
'js' => 1, |
| 22 |
'match' => '.module-table.tb_freeze_table', |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Normalize stored table data into head/body/cols arrays. |
| 28 |
*/ |
| 29 |
public static function parse_data(array $mod_settings):array { |
| 30 |
$data = isset($mod_settings['table_content']) ? $mod_settings['table_content'] : array(); |
| 31 |
if (is_string($data)) { |
| 32 |
$data = json_decode($data, true); |
| 33 |
} |
| 34 |
$data = (array) $data + array( |
| 35 |
'head' => array(), |
| 36 |
'body' => array(), |
| 37 |
'cols' => array(), |
| 38 |
); |
| 39 |
$col_count = isset($data['col_count']) ? (int) $data['col_count'] : 0; |
| 40 |
if ($col_count === 0) { |
| 41 |
$col_count = count($data['head']); |
| 42 |
foreach ($data['body'] as $row) { |
| 43 |
$col_count = max($col_count, count((array) $row)); |
| 44 |
} |
| 45 |
} |
| 46 |
$head = array(); |
| 47 |
for ($i = 0; $i < $col_count; ++$i) { |
| 48 |
$head[$i] = isset($data['head'][$i]) ? (string) $data['head'][$i] : ''; |
| 49 |
} |
| 50 |
$data['head'] = $head; |
| 51 |
for ($i = 0; $i < $col_count; ++$i) { |
| 52 |
if (!isset($data['cols'][$i])) { |
| 53 |
$data['cols'][$i] = array('stack' => true, 'width' => ''); |
| 54 |
} else { |
| 55 |
$data['cols'][$i] = (array) $data['cols'][$i] + array('stack' => true, 'width' => ''); |
| 56 |
} |
| 57 |
} |
| 58 |
foreach ($data['body'] as $r => $row) { |
| 59 |
$row = array_values((array) $row); |
| 60 |
for ($i = 0; $i < $col_count; ++$i) { |
| 61 |
if (!isset($row[$i])) { |
| 62 |
$row[$i] = ''; |
| 63 |
} |
| 64 |
} |
| 65 |
$data['body'][$r] = array_slice($row, 0, $col_count); |
| 66 |
} |
| 67 |
$data['col_count'] = $col_count; |
| 68 |
return $data; |
| 69 |
} |
| 70 |
|
| 71 |
public static function get_static_content(array $module):string { |
| 72 |
$data = self::parse_data($module['mod_settings']); |
| 73 |
if (empty($data['head']) && empty($data['body'])) { |
| 74 |
return ''; |
| 75 |
} |
| 76 |
$output = '<table>'; |
| 77 |
$col_count = (int) $data['col_count']; |
| 78 |
if (!empty($data['head'])) { |
| 79 |
$output .= '<thead><tr>'; |
| 80 |
for ($i = 0; $i < $col_count; ++$i) { |
| 81 |
$cell = isset($data['head'][$i]) ? $data['head'][$i] : ''; |
| 82 |
$output .= '<th>' . wp_kses_post($cell) . '</th>'; |
| 83 |
} |
| 84 |
$output .= '</tr></thead>'; |
| 85 |
} |
| 86 |
if (!empty($data['body'])) { |
| 87 |
$output .= '<tbody>'; |
| 88 |
foreach ($data['body'] as $row) { |
| 89 |
$output .= '<tr>'; |
| 90 |
for ($i = 0; $i < $col_count; ++$i) { |
| 91 |
$cell = isset($row[$i]) ? $row[$i] : ''; |
| 92 |
$output .= '<td>' . wp_kses_post($cell) . '</td>'; |
| 93 |
} |
| 94 |
$output .= '</tr>'; |
| 95 |
} |
| 96 |
$output .= '</tbody>'; |
| 97 |
} |
| 98 |
$output .= '</table>'; |
| 99 |
return $output; |
| 100 |
} |
| 101 |
|
| 102 |
public static function get_translatable_fields($module, $classname):array { |
| 103 |
$fields = array(); |
| 104 |
$data = self::parse_data($module['mod_settings']); |
| 105 |
foreach ($data['head'] as $i => $cell) { |
| 106 |
$fields[] = array('id' => 'head-' . $i, 'value' => $cell); |
| 107 |
} |
| 108 |
foreach ($data['body'] as $r => $row) { |
| 109 |
foreach ((array) $row as $c => $cell) { |
| 110 |
$fields[] = array('id' => 'body-' . $r . '_' . $c, 'value' => $cell); |
| 111 |
} |
| 112 |
} |
| 113 |
return $fields; |
| 114 |
} |
| 115 |
|
| 116 |
public static function translate_module($module_data, $translations) { |
| 117 |
$data = self::parse_data($module_data['mod_settings']); |
| 118 |
foreach ($translations as $key => $value) { |
| 119 |
list($scope, $pos) = explode('-', $key); |
| 120 |
if ($scope === 'head') { |
| 121 |
$data['head'][(int) $pos] = $value; |
| 122 |
} elseif ($scope === 'body') { |
| 123 |
list($r, $c) = explode('_', $pos); |
| 124 |
$data['body'][(int) $r][(int) $c] = $value; |
| 125 |
} |
| 126 |
} |
| 127 |
unset($data['col_count']); |
| 128 |
$module_data['mod_settings']['table_content'] = $data; |
| 129 |
return $module_data; |
| 130 |
} |
| 131 |
} |
| 132 |
|