<?php
/*--------------------------------------------------------- 
* File:dropDownMenu.php
* Author:Tomix,邱洪榮
* Description: 下拉框類
* Created:2009
* -------------------------------------------------------
* Modified history:2023/08/23
* Copyright © 2023 蘋果世界股份有限公司 版權所有
* -------------------------------------------------------
*/
class dropDownMenu {
	public $id = "dropDownList1";		// 控件id
	public $dataSource;					// 控件收據源
	public $showTree = false;			// 是否顯示成樹形
	public $showUndefine = false;		// 是否顯示未定義項
	public $undefineText = "Undefine";	// 顯示未定義項時文字(默認值為空字符串:"")
	public $undefineValue = "";			// 顯示未定義項時值(默認值為空字符串:"")
	public $idField;					// ID字段
	public $pidField;					// 父ID字段
	public $valueField;					// 值字段
	public $titleField;					// 標題(文本)字段
	public $rootID;						// 根ID值,默認為空字串"",0,null
	public $selectedValue = "";			// 選中值
	public $separator = "&nbsp;&nbsp;";	// 上下級選項前導符號
	public $muen = "menu";				// 用于判斷
	public $counter = 1;
	public $divmark;					// 標記頂div
	
	private $clientAttributes = [];
	
	public function dropDownMenu() {}
	
	public function getContent(): string {
		$html = "";
		if (!is_array($this->dataSource)) return "Data source is not an array!";

		$html .= ($this->showTree) ? $this->getTreeItems() : $this->getAllItems();	// 是否樹狀顯示

		return $html;
	}
	
	// 添加客戶端屬性
	public function addClientAttribute($name, $value) {
		$arr["name"] = $name;
		$arr["value"] = $value;
		$this->clientAttributes[] = $arr;
	}
	
	// 呈現控件
	public function render() {
		echo $this->getContent();
	}
	
	// 取得樹狀數據所有項
	private function getTreeItems(): string {
		$returnValue = "";
		$level = 1;
		foreach ($this->dataSource as $arr) {
			$rootArray = array();
			if (isset($this->rootID)) {
				$rootArray[0] = $this->rootID;
			} else {
				$rootArray[0] = "";
				$rootArray[1] = "0";
				$rootArray[2] = null;
			}    
			
			if (in_array($arr[$this->pidField], $rootArray, true)) {
				// 取根的下一級選項
				$returnValue .= $this->buildMenuLink($arr[$this->titleField], $arr[$this->valueField], $level);
				// 取所有子選項
				$returnValue .= $this->getItems($arr[$this->idField], 2, $level);
				$this->counter += 1;
			}            
		}
		return $returnValue;
	}
	
	// 根據父ID取得子選項
	private function getItems($pid, $level, $counter): string {
		$i = 1;    // 用于計數
		$returnValue = "";
		foreach ($this->dataSource as $arr) {
			if ($arr[$this->pidField] === $pid) {
				$returnValue .= $this->buildMenuLink($arr[$this->titleField], $arr[$this->valueField], $level, $counter . "_" . $i);
				// 遞迴取得子選項
				$returnValue .= $this->getItems($arr[$this->idField], $level + 1, $counter . "_" . $i);
				$i++;
			}
		}
		return $returnValue;
	}
	
	// 取得所有項,不區分上下級
	private function getAllItems(): string {
		$returnValue = "";
		foreach ($this->dataSource as $arr) {
			$returnValue .= $this->buildMenuLink($arr[$this->titleField], $arr[$this->valueField]);
		}
		return $returnValue;
	}
	
	// 构建菜单链接
	private function buildMenuLink($title, $value, $level = 1, $id = ""): string {
		return ($this->selectedValue === $value) ? "<div class=\"news_lift_button0" . $level . "\" style=\"display:none\" id=\"" . $id . "\" m=\"menu\"><a href=\"javascript:shownMenu('" . $id . "','" . $value . "','" . $this->divmark . "')\">" . $title . "</a></div>" : "<div class=\"news_lift_button0" . $level . "\" style=\"display:none\" id=\"" . $id . "\" m=\"menu\"><a href=\"javascript:shownMenu('" . $id . "','" . $value . "','" . $this->divmark . "')\">" . $title . "</a></div>";
	}
}
?>