소스 파일명 : item_update.php
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php // (2017.1.22, 차재복, Cha Jae Bok, cjbword@gmailcom)

// echo $action; echo $m_edit_level; echo $_SESSION['user_name'];

	if ( empty($_SESSION['user_name']) or !isset($action) or !isset($id) ) {
		exit ("update 파라미터 이상 !!! action={$action},id={$id}");
	}

		// 이동 직전
		if (isset($id) and isset($_POST[para][target]) and $action=='move_before') {
			$target = $_POST[para][target];

			mysqli_query($dbi,"update km a left join (select parent,chap from km where id=$id) b on a.parent=b.parent set a.chap=(a.chap-1) where a.chap>b.chap");
					if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

			$cur_result=mysqli_query($dbi,"select chap,parent from km where id=$target");
					if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			$cur_matched=mysqli_fetch_assoc($cur_result);
			$parent = $cur_matched[parent];
			$chap = $cur_matched[chap];

			$update_query = "update km set chap=(chap+1) where parent=$parent and chap>=$chap";
			mysqli_query($dbi,$update_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

			$move_query = "update km set parent=$parent,chap=$chap where id=$id";
			mysqli_query($dbi,$move_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		}

		// 이동 직후
		if (isset($id) and isset($_POST[para][target]) and $action=='move_after') {
			$target = $_POST[para][target];

			mysqli_query($dbi,"update km a left join (select parent,chap from km where id=$id) b on a.parent=b.parent set a.chap=(a.chap-1) where a.chap>b.chap");
					if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

			$cur_result=mysqli_query($dbi,"select chap,parent from km where id=$target");
					if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			$cur_matched=mysqli_fetch_assoc($cur_result);
			$parent = $cur_matched[parent];
			$chap = $cur_matched[chap];

			$update_query = "update km set chap=(chap+1) where parent=$parent and chap>$chap";
			mysqli_query($dbi,$update_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

			$move_query = "update km set parent=$parent,chap=$chap where id=$id";
			mysqli_query($dbi,$move_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		}

		// 이동 하부(자식)
		if (isset($id) and isset($_POST[para][target]) and $action=='move_child') {
			echo $action." : ";
			$target = $_POST[para][target];

			echo 'action='.$action.', source_id='.$id.', target_id='.$target.'<br>';

			mysqli_query($dbi,"update km a left join (select parent,chap from km where id=$id) b on a.parent=b.parent set a.chap=(a.chap-1) where a.chap>b.chap");
					if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

			$cur_result=mysqli_query($dbi,"select chap from km where parent=$target");
					if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			$count=mysqli_num_rows($cur_result);
			$chap = $count+1;

			$move_child_query = "update km set parent=$target,chap=$chap where id=$id";
			mysqli_query($dbi,$move_child_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		}

		// 동일 레벨 삽입 (insert) 또는 current level last add (형제 추가) (신규추가/형제추가/마지막추가)
		if ( isset($id) and $action=='last_add' ) {
			// update 내역 출력
//			echo '$id='.$id.', $chap='.$chap.', $depth='.$depth.', $m_title='.$m_title."<br>";

			// 현재 $id 쿼리
			$result=mysqli_query($dbi,"select count(*) as count from km where parent=$id");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			$matched=mysqli_fetch_assoc($result);
			$count = $matched[count];

			// 처음 등록(신규 추가)
			if ($count==0) {
				$edit_query = "insert into km (id,parent,chap,title,time) values (1,$id,1,'$m_title',now())";

			// 현 자식들에서 마지막 등록
			} else {
				// id 중 빈 번호 확인
				$result=mysqli_query($dbi,"select a.id + 1 as available from km a left join km b on b.id = (a.id + 1) where b.id is null order by a.id limit 0,1");
					if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
				$temp=mysqli_fetch_assoc($result);

				$edit_query = "insert into km (id,parent,chap,title,time) values ($temp[available],$id,$count+1,'$m_title',now())";
			}

			mysqli_query($dbi,$edit_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

			// 에러 유무
//			echo (mysqli_affected_rows($dbi) > 0 ? "insert 성공 !!!" : "insert 실패 !!!" )."<br>";
		}

		//  항목 업데이트
		if (isset($m_abbr) and isset($id) and !empty($m_title) and $action=='update') {
//			echo "action=".$action.", m_title=".$m_title.", id=".$id.", m_abbr=".$m_abbr."<br>";

/*			// 해킹 방어
			$insert_abbr = html_entity_decode($m_abbr);
			$insert_abbr = htmlentities($insert_abbr);
			$insert_abbr = str_replace('`','&grave;',$insert_abbr);
			$insert_abbr = str_replace('"','&quot;',$insert_abbr);
			$insert_abbr = str_replace("'","&#39;",$insert_abbr);
*/
			$insert_abbr = $m_abbr;
			$insert_abbr = mysqli_real_escape_string($dbi,$insert_abbr);

			// 시작과 끝에 whitespace, <p><br></p> 삭제
//			$insert_abbr = rtrim($insert_abbr);
//			$insert_abbr = preg_replace('/^(<p>&nbsp;<\/p>)+$/','',$insert_abbr);
			$insert_abbr = my_trim ($insert_abbr);

			$edit_query = "insert into km (id,title,abbr,time) values ($id,if('$m_title'='',null,'$m_title'),'".$insert_abbr."',now()) on duplicate key update id=$id,title='$m_title',abbr='".$insert_abbr."',time=now()";
			mysqli_query($dbi,$edit_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			// echo (mysqli_affected_rows($dbi)==0 ? "update 실패 !!!" : "update 성공 !!!");
		}

		// 자식 추가 (insert)
		if (isset($id) and $action=='child_add' and !empty($m_title)) {
			echo "action=".$action.", m_title=".$m_title.", id=".$id.", parent=".$id."<br>";

			$result=mysqli_query($dbi,"select count(*) as count from km where parent=$id");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			$matched=mysqli_fetch_assoc($result);
			$count = $matched[count];

			// id 중 빈 번호 확인
			$result=mysqli_query($dbi,"select a.id + 1 as available from km a left join km b on b.id = (a.id + 1) where b.id is null order by a.id limit 0,1");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			$temp=mysqli_fetch_assoc($result);

			// 자식 처음 등록
			if ($count==0) {
				$edit_query = "insert into km (id,parent,chap,title,time) values ($temp[available],$id,1,'$m_title',now())";

			// 현 자식들에서 마지막 등록
			} else {
				$edit_query = "insert into km (id,parent,chap,title,time) values ($temp[available],$id,$count+1,'$m_title',now())";
			}

			mysqli_query($dbi,$edit_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		}

		// 다음 추가 (insert)
		if (isset($id) and !empty($_POST[para][title]) and $action=='next_add') {
			$title = $_POST[para][title];
			$parent = $_POST[para][parent];
			$chap = $_POST[para][chap];

			// 전달 매개변수 확인
			// echo "action=".$action.", id=".$id.", title=".$title.", parent=".$parent.", chap=".$chap."<br>";

			// 현재 chap 뒤 모두 +1 씩 업데이트
			$update_query = "update km set parent=$parent,chap=(chap+1) where (parent=$parent and chap>$chap)";
			mysqli_query($dbi,$update_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

			// id 중 빈 번호 확인
			$result=mysqli_query($dbi,"select a.id + 1 as available from km a left join km b on b.id = (a.id + 1) where b.id is null order by a.id limit 0,1");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			$temp=mysqli_fetch_assoc($result);

			// 현재 chap 위치 직후에 insert
			$insert_query = "insert into km (id,parent,chap,title,time) values ($temp[available],$parent,$chap+1,'$title',now())";
			mysqli_query($dbi,$insert_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		}

		// 이전 추가 (insert)
		if (isset($id) and !empty($_POST[para][title]) and $action=='prev_add') {
			$title = $_POST[para][title];
			$parent = $_POST[para][parent];
			$chap = $_POST[para][chap];

			// 전달 매개변수 확인
			echo "action=".$action.", id=".$id.", title=".$title.", parent=".$parent.", chap=".$chap."<br>";

			// 현재 chap 포함하여, 모두 +1 씩 업데이트
			$update_query = "update km set parent=$parent,chap=(chap+1) where (parent=$parent and chap>=$chap)";
			mysqli_query($dbi,$update_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

			// id 중 빈 번호 확인
			$result=mysqli_query($dbi,"select a.id + 1 as available from km a left join km b on b.id = (a.id + 1) where b.id is null order by a.id limit 0,1");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			$temp=mysqli_fetch_assoc($result);

			// 현재 chap 이전 위치에 insert
			$insert_query = "insert into km (id,parent,chap,title,time) values ($temp[available],$parent,$chap,'$title',now())";
			mysqli_query($dbi,$insert_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		}

		// 항목 삭제 (delete)
		if (isset($id) and $action=='delete') {

			// 전달 매개변수 확인
//			echo "action=".$action.", id=".$id."<br>";

			// 자식 있는가 확인
			$result=mysqli_query($dbi,"select * from km where parent=$id");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			$count=mysqli_num_rows($result);
			if ($count>0) exit("대상 항목이 자식 노드를 갖고 있어서 삭제할 수 없습니다.");

			// 대상 항목 삭제
			$delete_query = "delete from km where id=$id limit 1";
			mysqli_query($dbi,$delete_query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		}


?>