reasonableframework/helper/tablewiz.php

80 lines
2.2 KiB
PHP
Raw Normal View History

2018-02-26 04:47:04 +00:00
<?php
/**
* @file tablewiz.php
* @date 2018-02-26
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief TableWiz helper
*/
if(!is_fn("tablewiz_cut_str")) {
2019-05-20 08:19:05 +00:00
function tablewiz_cut_str($str, $strlimit=0) {
$plaintext = strip_tags($str);
2018-02-26 05:12:37 +00:00
2019-05-20 08:19:05 +00:00
// if use html, do not cut text
if($strlimit > 0 && $str != $plaintext) {
$str = substr($str, 0, $strlimit);
}
2018-02-26 05:02:45 +00:00
2019-05-20 08:19:05 +00:00
return $str;
}
2018-02-26 05:02:45 +00:00
}
if(!is_fn("tablewiz_create")) {
2019-05-20 08:19:05 +00:00
function tablewiz_create($rows, $bind=array(), $domid="", $domclass="", $strlimit=0, $thead_html=array(), $tbody_html_list=array()) {
$html = "";
2018-02-26 04:47:04 +00:00
2019-05-20 08:19:05 +00:00
if(count($rows) == 0) {
return $html;
}
2018-02-26 04:47:04 +00:00
2019-05-20 08:19:05 +00:00
$dom_element_name = make_random_id(10);
$domid = empty($domid) ? "tablewiz_id_" . $dom_element_name : $domid;
$domclass = empty($domclass) ? "tablewiz_class_" . $dom_element_name : $domclass;
2018-02-26 04:47:04 +00:00
2019-05-20 08:19:05 +00:00
$html_th_elms = "";
foreach($rows[0] as $k=>$v) {
$html_th_text = array_key_empty($k, $bind) ? $k : $bind[$k];
$html_th_elms .= "<th>" . tablewiz_cut_str($html_th_text, $strlimit) . "</th>";
}
2018-02-26 04:55:02 +00:00
2019-05-20 08:19:05 +00:00
// append contents in thead
foreach($thead_html as $k=>$v) {
$html_th_elms .= "<th>" . tablewiz_cut_str($v, $strlimit) . "</th>";
}
2018-02-26 04:47:04 +00:00
2019-05-20 08:19:05 +00:00
$html_tr_elms = "";
foreach($rows as $idx=>$record) {
$html_tr_elms .= "<tr>";
foreach($record as $k=>$v) {
$html_tr_elms .= "<td>" . tablewiz_cut_str($v, $strlimit) . "</td>";
}
$html_tr_elms .= "</tr>";
2018-02-26 04:55:02 +00:00
2019-05-20 08:19:05 +00:00
// append contents in tbody
if(count($tbody_html_list) > $idx) {
$tbody_html = $tbody_html_list[$idx];
if(is_array($tbody_html)) {
foreach($tbody_html as $k=>$v) {
$html_tr_elms .= "<td>" . tablewiz_cut_str($v, $strlimit) . "</td>";
}
}
}
}
2018-02-26 04:47:04 +00:00
2019-05-20 08:19:05 +00:00
$html .= <<<EOF
2018-02-26 04:47:04 +00:00
<table id="$domid" class="$domclass" border="1" cellspacing="0">
2019-05-20 08:19:05 +00:00
<thead>
<tr>
$html_th_elms
</tr>
</thead>
<tbody>
$html_tr_elms
</tbody>
2018-02-26 04:47:04 +00:00
</table>
EOF;
2019-05-20 08:19:05 +00:00
return $html;
}
2018-02-26 04:47:04 +00:00
}