DB->query_record = false; //query log 기록 금지 if( $cate_table ){ $this->cate_table = $cate_table; }else{ $this->cate_table = TB_cate; } $this->dmax = 4; $this->cate_area = ""; $this->cate_auth = array(""); $this->DB->tableExists($this->cate_table); } public function set_cate_area($cate_area){ $this->cate_area = $cate_area; } public function get_cate_area(){ return $this->cate_area; } public function getAreaQry($where=""){ if( !$where ){ $where = new DB_where("and"); } if( $this->cate_area ){ $where->add("cate_area = ?", $this->cate_area); } return $where; } public function getShowQry($where=""){ if( !$where ){ $where = new DB_where("and"); } if( $this->cate_auth ){ $subwhere = $where->sub_where("or"); foreach( (array)$this->cate_auth as $val ){ $subwhere->add("cate_auth = ?", $val); } } return $where; /* switch( $auth ){ case "1" : $valrow = Array("Y","M","S","A"); break; case "2" : $valrow = Array("Y","M","S"); break; case "3" : $valrow = Array("Y","M"); break; default : $valrow = Array("Y"); break; } return " and cate_auth in ('".implode("','",$valrow)."') "; */ } public function new_cate($pa_code, $where=""){ //새 카테고리 추가 //상위카테고리의 오류확인 $pa_code = (string)$pa_code; $new_pa_code = $pa_code; if( $pa_code != 0 && $pa_code != "" && $pa_code != "0" && strlen($pa_code) % $this->dmax != 0 ){ for( $i = 0 ; $i < $this->dmax - ( strlen($pa_code) % $this->dmax ) ; $i++ ){ $new_pa_code = "0{$new_pa_code}"; } } if( !$where ){ $where = $this->getAreaQry(); } $where->add("cate_code like ?", "{$new_pa_code}%"); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; //주어진 카테고리의 1단계 아래 카테고리 중 최대값 찾음 $next_len = strlen($new_pa_code) + $this->dmax; $sql = "select max(SUBSTRING(cate_code,1,$next_len)) from {$this->cate_table} where {$whereqry}"; $pre_cate = $this->DB->queryFirstField($sql, $whererow); if( !$pre_cate ){ $pre_cate = $new_pa_code; } //1단계 아래 카테고리가 없을때-Y, 있을-N if( $pre_cate == $new_pa_code ){ for( $i = 0 ; $i < $this->dmax ; $i++ ){ $pre_cate = $pre_cate."0"; } $last_up = (string)( (int)substr($pre_cate, -$this->dmax) + 1 ); $forlen = $this->dmax - ( strlen($last_up) % $this->dmax ); for( $i = 0 ; $i < $forlen ; $i++ ){ $last_up = "0".$last_up; } $pre_cate = substr($pre_cate, 0, -$this->dmax).$last_up; $forlen = ( $this->dmax - ( strlen($pre_cate) % $this->dmax ) ) % $this->dmax; for( $i = 0 ; $i < $forlen ; $i++ ){ $pre_cate = "0".$pre_cate; } return $pre_cate; }else{ $last_up = (int)substr($pre_cate, -$this->dmax) + 1; if( $last_up < pow(10, $this->dmax) ){ $last_up = (string)$last_up; $cnt = $this->dmax - strlen($last_up); for( $i = 0 ; $i < $cnt ; $i++ ){ $last_up = "0".$last_up; } $pre_cate = substr($pre_cate, 0, -$this->dmax).$last_up; return $pre_cate; }else{ alert("카테고리 허용한도가 초과되었습니다."); return false; } } } public function up_cate($cate_code){ // 상위카테고리 리턴 return substr($cate_code, 0, strlen($cate_code) - $this->dmax); } public function sub_cate($cate_code){ // 하위카테고리 배열 리턴 $where = $this->getAreaQry(); $where = $this->getShowQry($where); $where->add("cate_code like ?", "{$cate_code}%"); $where->add("cate_code != ?", $cate_code?:""); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; $sql = "select * from {$this->cate_table} where {$whereqry} order by cate_code"; $rst = $this->DB->qry($sql, $whererow); $i = 0; $re_row = Array(); while( $sub_obj = $this->DB->fetch_array($rst) ){ foreach( (array)$sub_obj as $key => $val ){ $re_row[$key][$i] = $val; } $i++; } return $re_row; } public function sub_direct_cate($cate_code){ // 1단계아래의 하위카테고리 배열만 리턴 $temp_ren = strlen($cate_code) + $this->dmax; $where = $this->getAreaQry(); $where = $this->getShowQry($where); $where->add("cate_code like ?", "{$cate_code}%"); $where->add("cate_code != ?", $cate_code); $where->add("len(cate_code) = ?", $temp_ren); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; $sql = "select * from {$this->cate_table} where {$whereqry} order by cate_code"; $rst = $this->DB->qry($sql, $whererow); $i = 0; $re_row = Array(); while( $sub_obj = $this->DB->fetch_array($rst) ){ foreach( $sub_obj as $key => $val ){ $re_row[$key][$i] = $val; } $i++; } return $re_row; } public function history_cate($cate_code){ // $cate_code 의 상위 카테고리 배열 리턴 $floor = strlen($cate_code) / $this->dmax; for( $i = $floor ; $i > 0 ; $i-- ){ $info = $this->cate_info(substr($cate_code, 0, $this->dmax * $i)); if($info) foreach( $info as $key => $val ){ $re_row[$key][$i - 1] = $val; } } return $re_row; } public function cate_info($cate_code, $where=""){ // 카테고리정보 리턴 if( !$where ){ $where = new DB_where("and"); } $where = $this->getAreaQry($where); $where->add("cate_code = ?", $cate_code); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; $sql = "select * from {$this->cate_table} where {$whereqry}"; $row = $this->DB->queryFirstRow($sql, $whererow); return $row; } public function cate_info_id($cate_id, $where=""){ // 카테고리정보 리턴 id if( !$where ){ $where = new DB_where("and"); } $where = $this->getAreaQry($where); $where->add("cate_id = ?", $cate_id); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; $sql = "select * from {$this->cate_table} where {$whereqry}"; $row = $this->DB->queryFirstRow($sql, $whererow); return $row; } public function cate_info_data(){ // 카테고리정보 리턴 url if( $_SERVER['REQUEST_URI'] != "/" ){ $where = $this->getAreaQry(); $where->add("charindex(?, cate_data) != 0", $_SERVER['REQUEST_URI']); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; //$sql = "select * from {$this->cate_table} where cate_data is not null and cate_data != '' and {$whereqry} order by cate_code desc ".$this->DB->_limit(0,1); $sql = $this->DB->addLimit("select * from {$this->cate_table} where cate_data is not null and cate_data != '' and {$whereqry}","cate_code desc",0,1); $row = $this->DB->queryFirstRow($sql, $whererow); return $row; } } public function cate_name($cate_code, $field_name="cate_name"){ // 카테고리명 리턴 $where = $this->getAreaQry(); $where->add("cate_code = ?", $cate_code); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; $sql = "select {$field_name} from {$this->cate_table} where {$whereqry}"; return $this->DB->queryFirstField($sql, $whererow); } public function move_cate($cate_code, $dest_upcode){ // 카테고리 이동 (단순) $temp_ren = strlen($cate_code)+1; $new_code = $this->new_cate($dest_upcode); $params = Array( //"cate_code" => $this->DB->sqleval( "CONCAT('{$new_code}',SUBSTRING(cate_code,{$temp_ren},1000))" ), "cate_code" => $this->DB->sqleval( "'{$new_code}' + SUBSTRING(cate_code,{$temp_ren},1000)" ), ); $where = $this->getAreaQry(); $where->add("cate_code like ?", "{$cate_code}%"); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; return $this->DB->update($this->cate_table, $params, $whereqry, $whererow); } public function move_target_cate_id($cate_id,$dest_id,$pos_num){ // 카테고리 이동 _ id $cinfo = $this->cate_info_id($cate_id); $dinfo = $this->cate_info_id($dest_id); $cate_code = $cinfo['cate_code']; $dest_code = $dinfo['cate_code']; //if( $this->up_cate($cate_code) == $dest_code && substr($cate_code,-$this->dmax) <= $pos_num ){ $pos_num++; } return $this->move_target_cate($cate_code, $dest_code, $pos_num); } public function move_target_cate($cate_code,$dest_upcode,$pos_num){ // 카테고리 이동 $code_len = strlen($cate_code); //코드길이 $code_parent = substr($cate_code,0,-$this->dmax); //부모코드 $code_parlen = strlen($code_parent); //부모코드길이 $dest_len = strlen($dest_upcode); //대상코드길이 $dest_chilen = strlen($dest_upcode)+$this->dmax; //대상자식코드길이 $where = $this->getAreaQry(); $where->add("cate_code != ?", "{$cate_code}"); $where->add("cate_code != ?", "{$dest_upcode}"); $where->add("cate_code like ?", "{$dest_upcode}%"); $where->add("len(cate_code) = ?", strlen($dest_upcode)+$this->dmax); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; //$sql = "select cate_code from {$this->cate_table} where {$whereqry} order by cate_code asc ".$this->DB->_limit($pos_num,1); $sql = $this->DB->addLimit("select cate_code from {$this->cate_table} where {$whereqry}","cate_code asc",$pos_num,1); $dest_code = $this->DB->queryFirstField($sql, $whererow); if( $dest_code ){ //대상위치에 존재하는 코드가 있으면 $tgt_num = (int)substr($dest_code,-$this->dmax); }else{ //대상위치에 존재하는 코드가 없으면 마지막 코드 +1 $sql = $this->DB->addLimit("select cate_code from {$this->cate_table} where {$whereqry}","cate_code desc",0,1); $dest_code = $this->DB->queryFirstField($sql, $whererow); $tgt_num = (int)substr($dest_code,-$this->dmax) + 1; } if( strpos( $dest_upcode, $code_parent ) !== false && $dest_code > $cate_code ){ //같은 레벨 이동이거나 아래로 이동일 경우. (자신이 빠진것 만큼, 대상 부모의 자신의 레벨 부분을, -1만큼 보상해줘야 함.) if( $dest_upcode == $code_parent ){ //같은 레벨 이동은 $tgt_num 이 바뀌고, 하위레벨로의 이동은 upcode 가 바뀐다. $tgt_num--; }else{ $code1 = substr($dest_upcode, 0, $code_len); $code2 = substr($dest_upcode, $code_len); $dest_upcode = substr($code1,0,-$this->dmax) . addzero((int)substr($code1,-$this->dmax) - 1, $this->dmax) . $code2; } } $new_part = $dest_upcode.addzero($tgt_num,$this->dmax); $params = Array( //"cate_code" => $this->DB->sqleval( "CONCAT('xxxxxxxxxx',SUBSTRING(cate_code,".($code_len+1).",1000))" ), "cate_code" => $this->DB->sqleval( "'xxxxxxxxxx' + SUBSTRING(cate_code,".($code_len+1).",1000)" ), ); $where = $this->getAreaQry(); $where->add("cate_code like ?", "{$cate_code}%"); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; $this->DB->update($this->cate_table, $params, $whereqry, $whererow); //대상 임시보관 $params = Array( //"cate_code" => $this->DB->sqleval( "CONCAT(SUBSTRING(cate_code,1,{$code_parlen}),REPLICATE('0',".$this->dmax."-LEN(CONVERT(VARCHAR,SUBSTRING(cate_code,".($code_parlen+1).",".$this->dmax.")-1)))+CONVERT(VARCHAR,SUBSTRING(cate_code,".($code_parlen+1).",".$this->dmax.")-1),SUBSTRING(cate_code,".($code_len+1).",1000))" ), "cate_code" => $this->DB->sqleval( "SUBSTRING(cate_code,1,{$code_parlen}) + REPLICATE('0',".$this->dmax."-LEN(CONVERT(VARCHAR,SUBSTRING(cate_code,".($code_parlen+1).",".$this->dmax.")-1)))+CONVERT(VARCHAR,SUBSTRING(cate_code,".($code_parlen+1).",".$this->dmax.")-1) + SUBSTRING(cate_code,".($code_len+1).",1000)" ), ); $where = $this->getAreaQry(); $where->add("cate_code like ?", "{$code_parent}%"); $where->add("cate_code > ?", $cate_code); $where->add("cate_code not like ?", "{$cate_code}%"); $where->add("cate_code not like ?", "xxxxxxxxxx%"); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; $this->DB->update($this->cate_table, $params, $whereqry, $whererow); //원본 위치의 빠진공간 메움 $params = Array( //"cate_code" => $this->DB->sqleval( "CONCAT(SUBSTRING(cate_code,1,{$dest_len}), REPLICATE('0', ".$this->dmax."-LEN(CONVERT(VARCHAR,SUBSTRING(cate_code,".($dest_len+1).",".$this->dmax.")+1)))+CONVERT(VARCHAR,SUBSTRING(cate_code,".($dest_len+1).",".$this->dmax.")+1), SUBSTRING(cate_code,".($dest_chilen+1).",1000))" ), "cate_code" => $this->DB->sqleval( "SUBSTRING(cate_code,1,{$dest_len}) + REPLICATE('0', ".$this->dmax."-LEN(CONVERT(VARCHAR,SUBSTRING(cate_code,".($dest_len+1).",".$this->dmax.")+1)))+CONVERT(VARCHAR,SUBSTRING(cate_code,".($dest_len+1).",".$this->dmax.")+1) + SUBSTRING(cate_code,".($dest_chilen+1).",1000)" ), ); $where = $this->getAreaQry(); $where->add("cate_code like ?", "{$dest_upcode}%"); $where->add("cate_code >= ?", $new_part); $where->add("cate_code not like ?", "xxxxxxxxxx%"); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; $this->DB->update($this->cate_table, $params, $whereqry, $whererow); //대상 위치의 공간확보 $params = Array( //"cate_code" => $this->DB->sqleval( "CONCAT('{$dest_upcode}','{$new_part}',SUBSTRING(cate_code,11,1000))" ), "cate_code" => $this->DB->sqleval( "'{$new_part}' + SUBSTRING(cate_code,11,1000)" ), ); $where = $this->getAreaQry(); $where->add("cate_code like ?", "xxxxxxxxxx%"); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; return $this->DB->update($this->cate_table, $params, $whereqry, $whererow); //대상 위치로 이동 } public function drop_cate($cate_code, $cate_save_dir=""){ // 카테고리 삭제 $where = $this->getAreaQry(); $where->add("cate_code like ?", "{$cate_code}%"); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; return $this->DB->delete($this->cate_table, $whereqry, $whererow); } public function level_cate($cate_code){ // 카테고리 레벨 - 몇번째 카테고리인지 리턴 return strlen($cate_code) / $this->dmax; } public function max_level_cate(){ // 최고카테고리 레벨 $where = $this->getAreaQry(); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; $sql = "select max(len(cate_code)) from {$this->cate_table} where {$whereqry}"; return $this->DB->queryFirstField($sql, $whererow) / $this->dmax; } public function tree_cate($cate_code){ // 카테고리 트리배열 리턴 $where = $this->getAreaQry(); $where = $this->getShowQry($where); $where->add("cate_code like ?", "{$cate_code}%"); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; $sql = "select * from {$this->cate_table} where {$whereqry} order by cate_code"; $rst = $this->DB->qry($sql, $whererow); return $this->tree_cate_exe($rst); } public function tree_cate_all($cate_code){ // 카테고리 트리배열 리턴. 조회방식과 무관 $where = $this->getAreaQry(); $where->add("cate_code like ?", "{$cate_code}%"); $wqry = $where->get_where(); $whereqry = $wqry['qry']; $whererow = $wqry['val']; $sql = "select * from {$this->cate_table} where {$whereqry} order by cate_code"; $rst = $this->DB->qry($sql, $whererow); return $this->tree_cate_exe($rst); } public function tree_cate_exe($rst){ $i = 0; $tree_row = Array(); while( $cate_obj = $this->DB->fetch_array($rst) ){ $sub_row = $this->sub_cate($cate_obj['cate_code']); foreach( $cate_obj as $key => $val ){ $tree_row[$key][$i] = $val; } $tree_row['level'][$i] = $this->level_cate($cate_obj['cate_code']); $tree_row['sub'][$i] = count((Array)$sub_row['cate_code']); $i++; } return $tree_row; } public function draw_cate_all($cate_code, $page_url=""){ // 전체 카테고리 트리 작성 if( !$page_url ){ $page_url = $_SERVER['PHP_SELF']; } $tree_row = $this->tree_cate(""); $history_row = $this->history_cate($cate_code); $all_num = count($tree_row['cate_code']); $pre_level = 0; $tree_html = "
최상위
"; for( $i = 0 ; $i < $all_num ; $i++ ){ if( $pre_level >= $tree_row['level'][$i] ){ for( $k = 0 ; $k <= ( $pre_level - $tree_row['level'][$i] ) ; $k++ ){ $tree_html .= "\r\n"; } } if( $cate_code == $tree_row['cate_code'][$i] or @in_array($tree_row['cate_code'][$i], $history_row['cate_code']) ){ $view_conv = ""; }else{ $view_conv = "none"; } if( count($this->sub_direct_cate($tree_row['cate_code'][$i])) > 0 ){ $plus_img = "line_plus.gif"; }else{ $plus_img = "line_minus.gif"; } if( $cate_code == $tree_row['cate_code'][$i] ){ $open_folder = "folder_o.gif"; $fg_col = 'font-weight:bold;color:#003366'; }else{ $open_folder = "folder_c.gif"; $fg_col = ""; } $tree_html .= "\r\n
"; for( $ks = 0 ; $ks < $tree_row['level'][$i] ; $ks++ ){ $tree_html .= ""; } $tree_html .= "{$tree_row['cate_name'][$i]}
"; $tree_html .= "\r\n
"; $pre_level = $tree_row['level'][$i]; } for( $i = 0 ; $i < $pre_level ; $i++ ){ $tree_html .= "\r\n
"; } return $tree_html; } public function draw_cate_script($cate_code){ // 전체 카테고리 트리 작성(스크립트) $tree_row = $this->tree_cate(""); $history_row = $this->history_cate($cate_code); $all_num = count($tree_row['cate_code']); $pre_level = 0; $tree_html = ""; for( $i = 0 ; $i < $all_num ; $i++ ){ if( $pre_level >= $tree_row['level'][$i] ){ for( $k = 0 ; $k <= ( $pre_level - $tree_row['level'][$i] ) ; $k++ ){ $tree_html .= "\r\n"; } } if( $cate_code == $tree_row['cate_code'][$i] or @in_array($tree_row['cate_code'][$i], $history_row['cate_code']) ){ $view_conv = ""; }else{ $view_conv = "none"; } if( count($this->sub_direct_cate($tree_row['cate_code'][$i])) > 0 ){ $plus_img = "line_plus.gif"; }else{ $plus_img = "line_minus.gif"; } if( $cate_code == $tree_row['cate_code'][$i] ){ $open_folder = "folder_o.gif"; $fg_col = 'font-weight:bold;color:#003366'; }else{ $open_folder = "folder_c.gif"; $fg_col = ""; } $tree_html .= "\r\n
"; for( $ks = 0 ; $ks < $tree_row['level'][$i] ; $ks++ ){ $tree_html .= ""; } $tree_html .= "{$tree_row['cate_name'][$i]}
"; $tree_html .= "\r\n
"; $pre_level = $tree_row['level'][$i]; } for( $i = 0 ; $i < $pre_level ; $i++ ){ $tree_html .= "\r\n
"; } return $tree_html; } public function list_cate($cate_code, $url, $spr){ $word = "최상위"; $his_row = $this->history_cate($cate_code); for( $i = 0 ; $i < count($his_row['cate_code']) ; $i++ ){ $word .= "{$spr}{$his_row['menu_name'][$i]}"; } return $word; } public function get_cate($menu_id=""){ global $cmenu_id, $cmenu_cate, $d1n, $d2n, $d3n, $d4n; if($menu_id){ $row = $this->cate_info_id($menu_id); return $row['cate_code']; }else if( $cmenu_id ){ $row = $this->cate_info_id($cmenu_id); return $row['cate_code']; }else if( $cmenu_cate ){ return $cmenu_cate; }else if( $d1n ){/*이전방식 호환용*/ return ( ( $d1n )?addzero($d1n, $this->dmax):"" ).( ( $d2n )?addzero($d2n, $this->dmax):"" ).( ( $d3n )?addzero($d3n, $this->dmax):"" ).( ( $d4n )?addzero($d4n, $this->dmax):"" ); }else{ $row = $this->cate_info_data(); return $row['cate_code']; } } function tree_by_parent( $row, $cd_fld, $pcd_fld, $pcd_val ){ $nrow = array_filter($row, function($v) use ($pcd_val, $pcd_fld, $cd_fld) { return $pcd_val == $v[$pcd_fld] && !!$v[$cd_fld]; }); $arr = Array(); foreach( (Array)$nrow as $ar ){ $ar['children'] = $this->tree_by_parent($row, $cd_fld, $pcd_fld, $ar[$cd_fld]); $arr[] = $ar; } return $arr; } public function __get($name){ $method = "get_{$name}"; if( method_exists($this, $method) ){ return $this->$method(); } return false; } public function __set($name, $value){ $method = "set_{$name}"; if( method_exists($this, $method) ){ return $this->$method( $value ); } return false; } }