<?php // (2020.11.29, 차재복, Cha Jae Bok, cjbword@gmailcom) 
function multi_path_rows($id,$m_temp1,$dbi) {
# 해당 no 및 id 유무 별로 multi cur_path 찾기
	// 용어 항목
//	if (!empty($m_temp1) and !empty($id)) {
	if (!empty($m_temp1) ) {
		$query = "select id,path2node from (select no,tree_id from book_idx where no=$m_temp1) a left join gubun_tree_v2 b on a.tree_id=b.id";
		$result=mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		$multi_rows=array();
		while ( $matched=mysqli_fetch_assoc($result) ) {
			if (!empty($id) and $id == $matched[id]) { 
				array_unshift($multi_rows,$matched);
			} else {
				$multi_rows[] = $matched;
			}
		}
	// 분류 항목 (1개)
	} else if (empty($m_temp1) and !empty($id)) {
//		$query = "select id,path2node from gubun_tree_v2 where id=$id";
		$query = "select id,concat('0,',replace(substring_index(getpath_v2(id),'|',-1),'::',',')) as path2node from gubun_tree_v2 where id=$id";
		$result=mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		$matched=mysqli_fetch_assoc($result);
		$multi_rows[0] = $matched;
	// 에러 방지
	} else if (empty($m_temp1) and empty($id)) {
		$multi_rows[0] = array('id'=>0,'path2node'=>'0');
	}
// echo 'count($multi_rows)='.count($multi_rows)."<br>";
// echo "<pre>";print_r($multi_rows);echo "</pre>";
	return $multi_rows;
}
# 각 id별 세부 용어 항목명 보이기
function detail_items_display($id,$temp,$m_temp1,$dbi,$choice) {
		$query="select titlename,no,list_ord,tree_id as id from book_idx where tree_id=$id order by list_ord, titlename";
		$result = mysqli_query($dbi,$query);
		$cnt = mysqli_num_rows($result);
		$n=1;
		while ( $matched=mysqli_fetch_assoc($result) ) {
            if(!empty($choice)) {
			    echo ($choice=='contents' ? "   " : " ");
    			echo " $n. ";
	    		if ( $matched[no] == $m_temp1 ) echo "<b>";
		    		echo "<a href='view.php?nav=2&id=".$id."&m_temp1=".$matched[no]."'>";
			    		echo $matched[titlename];
				    echo "</a>";
    			if ( $matched[no] == $m_temp1 ) echo "</b>";
	    		echo "<br>";
            }
			$arr[] = array('no'=>$matched[no],'titlename'=>$matched[titlename]);
			$n=$n+1;
		}
		return $arr;
}
function db_qry ($cur_path,$cur_id,$dbi) {
// echo '$cur_path='.$cur_path."<br>";
# 쿼리문
	// 각 레별별 해당 노드들 쿼리
	$query = "select a.id,a.parent,a.sub_seq,a.name,a.linked_num,@path:=getpath_v2(a.id),
					@pre_ord:=substring_index(substring_index(@path,'|',2),'|',-1) as pre_ord, 
					char_length(@pre_ord)-char_length(replace(@pre_ord,'.','')) as depth,
					substring_index(@path,'|',-1) as path_id,
					substring_index(substring_index(@path,'|',-2),'|',1) as path_str
					,count(b.id) as sub_cnt
				from gubun_tree_v2 a 
					left join gubun_tree_v2 b on a.id=b.parent
				where a.id<>0 and a.parent in (".$cur_path.")
				group by a.id
				order by pre_ord"
				;
		$result=mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		$prev=1; $i=0;
		while ( $matched = mysqli_fetch_assoc($result) ) {
			// 쿼리 결과 레코드를 배열화
			$set[] = $matched;
			// $set에서 현재 $cur_id의 배열 index 확인
			if ($cur_id == $matched[id]) $cur_row = $matched;
			// cur_path 항목별로, 직속 자식들 별도 array 확보
			if ( strpos($cur_path.',',$matched[parent].',') !== false ) {
				$per_parent_rows[$matched[parent]][] = $matched;
			} 
			// 마지막 cur_id도 포함
//			if ($matched[id]==$cur_id and $matched[depth]) $per_parent_rows[$matched[id]][] = $matched;
			$i=$i+1;
		}
//echo $cur_id."<br>";
//echo $cur_path."<br>";
//echo 'count($per_parent_rows)='.count($per_parent_rows)."<br>";
//		return array('set' => $set, 'cur_idx' => $cur_idx, 'parent_idx_list' => $parent_idx_list);
		return array('set' => $set, 'cur_row'=> $cur_row,'per_parent_rows' => $per_parent_rows);
}
# 각 path별 1 라인 출력
function path2line ($per_parent_rows,$row,$m_temp1,$dbi) {
		// path to node 의 id,str 추출
		$pid_list = explode('::',$row[path_id]);
		array_unshift($pid_list,'0'); // 루트 id도 포함시킴
		$pstr_list = explode('::',$row[path_str]);
		array_unshift($pstr_list,'Top'); // 루트 str도 포함시킴
		// path to node ( Top > 타이틀1 > ... ) 형태로 Tooltip 구현
				for ($i=0;$i<=$row[depth];$i++) {
					echo "<span class='sub_list_container'>";
						// ( Top > 타이틀1 > ... ) 형태로 클릭 대상 보여줌
						echo "<a href='view.php?nav=2&id=".$pid_list[$i]."'  >";
							echo $pstr_list[$i];
						echo "</a>";
						if ($i<$row[depth]) echo " > ";
						// Tooltip
							echo "<span class='sub_list_text'>"; 
								if ($row[linked_num] > 0) {
									$temp_arr = detail_items_display($pid_list[$i],$num,$m_temp1,$dbi,"path");
									if ($i==$row[depth]) $cur_sub_list = $temp_arr;
								}
								if (count($per_parent_rows[$pid_list[$i]]) > 0) {
									foreach($per_parent_rows[$pid_list[$i]] as $key => $value) {
										echo " [<a href='view.php?nav=2&id=".$value[id]."'	>".$value[name]."</a>]";
										echo "<br>";
									}
								}
							echo "</span>";
//						}
					echo "</span>";
				}
		return $cur_sub_list;
}
?>