set_tbl($tbl); $this->set_kind($kind); $this->set_stopLevel($stopLevel); $this->set_ofsn($ofsn); } #--------- 設定類 -------------------- #設定資料表 public function set_tbl($value) { $this->tbl = $value; } #設定類別 public function set_kind($value) { $this->kind = $value; } #設定層數 public function set_stopLevel($value) { $this->stopLevel = $value; } #設定父類別 public function set_ofsn($value) { $this->ofsn = $value; } //--------- 取得類 ------------*/ #取得資料表 public function get_tbl() { return $this->tbl; } #取得分類 public function get_kind() { return $this->kind; } #取得層數 public function get_stopLevel() { return $this->stopLevel; } #取得父類別 public function get_ofsn() { return $this->ofsn; } #get ################################################################ # 取得外鍵下拉選單 的 選項 # 傳入:($kind_arr, $width = 3) # 回傳:ForeignKeyForm ################################################################ public function get_foreignOption($foreign, $default="") { # ----得到Foreign key選單 ---------------------------- $foreignOption = ""; foreach ($foreign as $key => $value) { $selected = ""; if ($default == $key) { $selected = " selected"; } $foreignOption .= "{$value['title']}"; } return $foreignOption; } ################################################################ # 取得類別body的陣列 # 有資料表沒有 kind 、ofsn欄位 ################################################################ public function get_listArr($ofsn=0,$level=1,$enable=0) { global $db; $andKey = $enable ? " and `enable`='{$enable}'":""; #檢查目前階層是否大於層次 if ($level > $this->stopLevel) { return; } #設定下層 $downLevel = $level + 1; $sql = "select * from `{$this->tbl}` where `ofsn`='{$ofsn}' and `kind`='{$this->kind}'{$andKey} order by sort"; //die($sql); $result = $db->query($sql) or redirect_header("", 3000, $db->error."\n".$sql,true); #-------------------------------------------------------------------- $rows = []; while ($row = $result->fetch_assoc()) { //以下會產生這些變數: $sn , $ofsn , $title , $enable ,$sort $row['sn'] = intval($row['sn']); $row['enable'] = intval($row['enable']); $row['target'] = intval($row['target']); $row['title'] = htmlspecialchars($row['title'], ENT_QUOTES); $row['url'] = htmlspecialchars($row['url'], ENT_QUOTES); $row['level'] = $level; #取得底下有幾層 $row['downLevel'] = $this->get_downLevel($row['sn']); $row['sub'] = $this->get_listArr($row['sn'], $downLevel); //移動圖示 $icon['move_i'] = ($this->stopLevel == $row['downLevel'])?false:true; //資料夾圖示(最後一層沒有) $icon['folder_i'] = $this->stopLevel > $level ? true : false; //增加類別圖示 $icon['add_downLevel_i'] = $this->stopLevel > $level ? true : false; //排序圖示 $icon['sort_i'] = true; $row['icon'] = $icon; $rows[] = $row; } return $rows; } #確認底下有幾層 #get_downLevel public function get_downLevel($sn,$downLevel=0) { global $db; if ($downLevel > $this->stopLevel) { return $downLevel; } $level = $downLevel+1; $sql = "select sn from `{$this->tbl}` where ofsn='{$sn}'"; // return $sql; $result = $db->query($sql) or redirect_header("", 3000, $db->error."\n".$sql,true); while ($row = $result->fetch_assoc()) { $downLevel_tmp = $this->get_downLevel($row['sn'], $level); $downLevel = ($downLevel_tmp > $downLevel) ? $downLevel_tmp : $downLevel; } return $downLevel; } ################################################################ # 取得類別body的html # data-tt-id 本身 # data-tt-parent-id 父層 ################################################################ public function get_listHtml($list, $listBodys) { $html = ""; foreach ($list as $item => $row) { $html .= $this->get_rowHtml($listBodys, $row); if ($row['sub']) { $html .= $this->get_listHtml($row['sub'], $listBodys); } } return $html; } ################################################################ # 取得類別body的html # data-tt-id 本身 # data-tt-parent-id 父層 ################################################################ public function get_rowHtml($listBodys, $row) { #row自己的層數 $level = $this->get_thisLevel($row['sn']); //style='letter-spacing: 0;' $html = " \n"; foreach ($listBodys as $k => $format) { if ($format['valuetype'] == "text") { #新增子類別 $addLevelButton = ($k == "title" and $this->stopLevel > $level) ? " " : ""; #移動 $moveButton = ($k == "title" and $row['icon']['move_i']) ? " " : ""; #資料夾 $folderButton = ($k == "title" and $row['icon']['folder_i']) ? "" : ""; $width = ($k == "title") ? "width:65%;" : "width:100%;"; #一般input $html .= " {$folderButton}{$moveButton} {$addLevelButton} "; } elseif ($format['valuetype'] == "yesNo") { if ($row[$k] == 1) { #啟用 $title_0 = ($k == "enable") ? "停用" : "啟用"; $html .= " get_kind() . "' title='{$title_0}' atl='{$title_0}'>"; } else { #停用 $title_1 = ($k == "enable") ? "啟用" : "停用"; $html .= " get_kind() . "' title='{$title_1}' atl='{$title_1}'>"; } } elseif ($format['valuetype'] == "btn") { $html .= " \n"; $html .= " "; foreach ($format['btn'] as $btnV) { if ($btnV == "view") { } elseif ($btnV == "edit") { $html .= "get_kind() . "' class='btn btn-success btn-xs'>編輯 "; } elseif ($btnV == "del") { $html .= "刪除 "; } } $html .= " \n"; } } $html .= "\n"; return $html; } ########################################################### # 用流水號 得到自己的層數 ########################################################### public function get_thisLevel($sn, $level = 1) { global $db; if ($level > $this->stopLevel) { return $level; } $sql = "select ofsn from `{$this->tbl}` where sn='{$sn}'"; // die($sql); $result = $db->query($sql) or redirect_header("", 3000, $db->error."\n".$sql,true); list($ofsn) = $result->fetch_row(); if (!$ofsn) { return $level; } return $this->get_thisLevel($ofsn, ++$level); } #以流水號取得某筆分類資料 public function get_rowBYsn($sn) { global $db; if (empty($sn)) { return; } $sql = "select * from `$this->tbl` where sn='{$sn}'"; //die($sql); $result = $db->query($sql) or redirect_header("", 3000, $db->error."\n".$sql,true); $row = $result->fetch_assoc(); return $row; } ####################################################### # 取得父類別選單->選項(後台類別表單用)# op_form ####################################################### public function get_ofsnOption($default, $ofsn = 0, $level = 1, $indent = "", $enable = 0) { global $db; if ($level >= $this->stopLevel) { return; } $andKey = $enable ? " and `enable='{$enable}'`":""; $downLevel = $level + 1; $indent .= " "; $sql = "select * from `{$this->tbl}` where ofsn='{$ofsn}' and kind='{$this->kind}'{$andKey} order by sort"; //die($sql); $result = $db->query($sql) or redirect_header("", 3000, $db->error."\n".$sql,true); $options = ""; while ($row = $result->fetch_assoc()) { $selected = ($default == $row['sn']) ? " selected" : ""; $options .= "{$indent}{$row['title']}\n"; $options .= $this->get_ofsnOption($default, $row['sn'], $downLevel, $indent, $enable); } return $options; } } ```