<?php // (2020.10.23, 차재복, Cha Jae Bok, http://www.ktword.co.kr)
// 코드 항목 신규 추가
function code_new_add($id, $dbi) {
# code 테이블
// code id 중 빈 번호 확인
$query = "select a.id + 1 as available
from code a left join {$table_name} b on b.id = (a.id + 1)
where b.id is null
order by a.id limit 0,1";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched = mysqli_fetch_assoc($result);
$available_id = $matched[available];
// code no 중 빈 번호 확인
$query = "select a.no + 1 as available
from code a left join code b on b.no = (a.no + 1)
where b.no is null
order by a.no limit 0,1";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched = mysqli_fetch_assoc($result);
$available_no = $matched[available];
// code 신규 id,no 삽입
$query = "insert into code (no,id,id_seq,id_name,date) values ($available_no,$available_id,1,'임시',now())";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
# reform 테이블
// reform 테이블의 날짜 업데이트
$query = "update reform set date=now() where id=$id";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
# reform_more 테이블
// reform_more 테이블의 현재 id에 more_type, more_ptr 업데이트
$query = "insert reform_more (id,more_type,more_ptr) values ($id,1,$available_id)";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
// 결과 리턴
if (empty($err_msg)) {
if (mysqli_affected_rows($dbi) > 0){
$notice = "reform_more 테이블에 신규 코드 추가 성공";
} else {
$notice = "reform_more 테이블에 신규 코드 추가 실패";
}
}
$return = array('err_msg'=>$err_msg,'notice'=>$notice);
echo json_encode($return, JSON_UNESCAPED_UNICODE);
}
// 코드 항목 업데이트
function code_update($no, $str, $msg, $dbi, $table_name='code') {
$str = mysqli_real_escape_string($dbi,$str);
$query = "update $table_name set id_name='{$msg}',code='{$str}',date=now() where no={$no}";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
if (mysqli_affected_rows($dbi) > 0){
$query = "select id_name,code,date_format(date,'%Y-%m-%d') as date from $table_name where no={$no}";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
$matched = mysqli_fetch_assoc($result);
$return = array('code'=>$matched[code],'date'=>$matched[date],'name'=>$matched[id_name]);
echo json_encode($return, JSON_UNESCAPED_UNICODE);
} else {
echo false;
}
}
// 메뉴얼 항목 업데이트
// if ($ch == 'man_update' and !empty($str) and !empty($msg) and is_numeric($no)) {
function manual_update($no, $str, $msg, $dbi, $table_name='manual') {
// 해킹 방어
$msg = trim($msg);
$msg = mysqli_real_escape_string($dbi,$msg);
$str = trim($str);
$str = mysqli_real_escape_string($dbi,$str);
$query = "update manual set id_name='{$msg}',txt='{$str}',date=now() where no={$no}";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
if (mysqli_affected_rows($dbi) > 0){
$query = "select id_name,txt,date_format(date,'%Y-%m-%d') as date from manual where no={$no}";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched = mysqli_fetch_assoc($result);
} else {
$err_msg = '업데이트 실패!';
}
$return = array('err_msg'=>$err_msg,'code'=>$matched[txt],'date'=>$matched[date],'name'=>$matched[id_name]);
echo json_encode($return, JSON_UNESCAPED_UNICODE);
}
// 메뉴얼 항목 삭제
function manual_delete($no, $id, $dbi) {
$query = "select id_seq from manual where no=$no and id=$id limit 1";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched = mysqli_fetch_assoc($result);
$current_id_seq = $matched[id_seq];
$query = "update manual set id_seq=(id_seq-1) where id=$id and id_seq>$current_id_seq";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$query = "delete from manual where no=$no and id=$id limit 1";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
// 현재 id 산하 자식 없으면, reform_more 해당 레코드 삭제
$query = "select count(*) as cnt from manual where id=$id";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched = mysqli_fetch_assoc($result);
$cnt = $matched[cnt];
if ($cnt < 1) {
// reform_more 해당 레코드 삭제
$query = "delete from reform_more where more_type='2' and more_ptr=$id limit 1";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
if (mysqli_affected_rows($dbi) < 1) $notice .= 'reform_more 1건 삭제 못함';
}
$return = array('err_msg'=>$err_msg);
echo json_encode($return, JSON_UNESCAPED_UNICODE);
}
// 메뉴얼 항목을 바로다음에 추가
function manual_insert($id, $no, $dbi) {
// manual 현재 항목 세부정보 추출
$query = "select id,id_seq from manual where id=$id and no=$no limit 1";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched = mysqli_fetch_assoc($result);
$cur_seq = $matched[id_seq];
$cur_id = $matched[id];
if( mysqli_num_rows($result) <= 0 ) {
$err_msg .= "해당되는 항목 없음({$no})";
} else {
// manual no 중 빈 번호 확인
$query = "select a.no + 1 as available
from manual a left join manual b on b.no = (a.no + 1)
where b.no is null
order by a.no limit 0,1";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched = mysqli_fetch_assoc($result);
$available_no = $matched[available];
// manual 직후 id_seq 부터 1씩 증가시킴
$query = "update manual set id_seq=(id_seq+1) where parent=$id and id_seq>$cur_seq";
$result = mysqli_query($dbi, $query);
// manual 직후 항목 생성
$query = "insert into manual (no,id,id_seq,id_name,date) values ($available_no,$cur_id,$cur_seq+1,'임시',now())";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
}
// 결과 JSON 송출
$return = array('err_msg'=>$err_msg,'no'=>$available_no,'name'=>'(임시)','date'=>date('Y-m-d'),'seq'=>($cur_seq+1));
echo json_encode($return, JSON_UNESCAPED_UNICODE);
}
// 메뉴얼 항목 신규 추가
function manual_new_add($id, $dbi) {
# manual 테이블
// manual id 중 빈 번호 확인
$query = "select a.id + 1 as available
from manual a left join manual b on b.id = (a.id + 1)
where b.id is null
order by a.id limit 0,1";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched = mysqli_fetch_assoc($result);
$available_id = $matched[available];
// manual no 중 빈 번호 확인
$query = "select a.no + 1 as available
from manual a left join manual b on b.no = (a.no + 1)
where b.no is null
order by a.no limit 0,1";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched = mysqli_fetch_assoc($result);
$available_no = $matched[available];
// manual 신규 id,no 삽입
$query = "insert into manual (no,id,id_seq,id_name,date) values ($available_no,$available_id,1,'임시',now())";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
# reform 테이블
// reform 테이블의 날짜 업데이트
$query = "update reform set date=now() where id=$id";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
// reform 테이블의 부모 id
$query = "select parent from reform where id=$id";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched = mysqli_fetch_assoc($result);
$parent_id = $matched[parent];
# reform_more 테이블
// reform_more 테이블의 현재 id에 more_type, more_ptr 업데이트
$query = "insert reform_more (id,more_type,more_ptr) values ($id,2,$available_id)";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
# 현재 Li 노드의 정보
// $currentParentInfo = nextParentChilds($parent_id, $dbi);
$currentLiInfo = currentLiInfo($id, $dbi);
$return = array('err_msg'=>$err_msg,'currentLiInfo'=>$currentLiInfo);
echo json_encode($return, JSON_UNESCAPED_UNICODE);
}
// 메뉴얼 항목 끝에 추가
function manual_add($id, $ch, $sub_ch, $msg, $str, $dbi, $table_name='manual') {
$title = mysqli_real_escape_string($dbi,$msg);
$content = mysqli_real_escape_string($dbi,$str);
// no 중 빈 번호 찾아냄
$query = "select a.no + 1 as available from {$table_name} a left join {$table_name} b on b.no = (a.no + 1)
where b.no is null order by a.no limit 0,1";
$result=mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched=mysqli_fetch_assoc($result);
// 텅 비었으면, $available_no = 1 (초기값)
$available_no = ( empty($matched[available]) ? 1 : $matched[available] );
// 해당 id의 자식 최대 번호 알아보기
$query = "select max(id_seq) as max_id_seq from {$table_name} where id=$id";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
$matched = mysqli_fetch_assoc($result);
$max_id_seq = $matched[max_id_seq] + 1;
$query = "insert into {$table_name} (no,id,id_seq,id_name,txt,date) values ($available_no,$id,$max_id_seq,'{$title}','{$content}',now())";
$result = mysqli_query($dbi,$query);
if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
// 결과 리턴
if (mysqli_affected_rows($dbi) > 0){
$return = array('notice'=>'메뉴얼 항목 추가 성공 !!!');
} else {
$return = array('err_msg'=>$err_msg.'메뉴얼 항목 추가 에러','notice'=>$query);
}
echo json_encode($return, JSON_UNESCAPED_UNICODE);
}
?>