소스 파일 : /yoyak/yoyak_update_ajax.php (2019-05-27)     소스 설명 : (분류요약관리) 요약관리 항목요약 편집 (submit에 따른 서버 처리 및 반환)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php // (2019.5.27, 차재복, Cha Jae Bok, http://www.ktword.co.kr) 

# 세션 스타트 (매 웹페이지 마다 필요)
	session_start();


# db 연결 및 html 헤더부 
	include_once "../base_utils/db_conn.php";


# 전달 변수 처리
	$act = $_REQUEST[act];
		$act = mb_substr($act,0,16,'utf-8');  // 해킹 방지 (글자 수 제한)

	$id = $_REQUEST[id];
		if ( isset($_REQUEST[id]) and !empty($id) and !is_numeric($id) or $id<0 ) exit; // 해킹방지 (수치>0)
		$id = substr($id,0,10);  // 해킹 방지 (글자 수 제한)
		if(empty($id)) $id=0; // 디폴트

	$tid = $_REQUEST[tid];
		if ( isset($_REQUEST[tid]) and !empty($tid) and !is_numeric($tid) or $tid<0 ) exit; // 해킹방지 (수치>0)
		$tid = substr($tid,0,10);  // 해킹 방지 (글자 수 제한)

	$no = $_REQUEST[no];
		if ( isset($_REQUEST[no]) and !empty($no) and !is_numeric($no) or $no<0 ) exit; // 해킹방지 (수치>0)
		$no = substr($no,0,10);  // 해킹 방지 (글자 수 제한)

	$no_list = $_REQUEST[no_list];
		$no_list = substr($no_list,0,30);  // 해킹 방지 (글자 수 제한)
		$no_arr = explode(',',$no_list);
		if ($no_ar.length > 0) {
			foreach($no_arr as $key=>$value) {
				if( !is_numeric($value) or $value<=0 ) {
					// no_list 비어있거나 음수 !!!'
					return; 
				}
			}
		}

	$str = trim($_REQUEST[str]);
		$str = mysqli_real_escape_string($dbi,$str);
		$str = mb_substr($str,0,50,'utf-8');  // 해킹 방지 (글자 수 제한)
		$str = trim(strip_tags($str));
		$str = str_replace('  ',' ',$str);
		$str = str_replace(array('\\', "\0", "\n", "\r", "\x1a"), '', $str);

// echo "act=".$act.", id=".$id.", tid=".$tid.", no=".$no.", no_list=".$no_list.", str=".$str."<br>";

# 항목별 업데이트 선택
	// id 타이틀명칭 업데이트
	if ( $act=='id_rename' and is_numeric($id) and empty($no) and !empty($str) )
		id_str_update($id, $str, $dbi, 'gubun_tree_v2', 'name');
	// id 요약설명 업데이트
	if ( $act=='id_desc_update' and is_numeric($id) and empty($no) and !empty($str) )
		id_str_update($id, $str, $dbi, 'gubun_tree_v2', 'yoyak');
	// no 타이틀명칭 업데이트
	if ( $act=='no_rename' and is_numeric($id) and is_numeric($no) and !empty($str) )
		no_str_update($id, $no, $str, $dbi, 'book_idx', 'titlename');
	// no 요약설명 업데이트
	if ( $act=='no_desc_update' and is_numeric($id) and is_numeric($no) and !empty($str) )
		no_str_update($id, $no, $str, $dbi, 'book_idx', 'yoyak');

	// id 노드 위 아래로 이동
	if ( ($act=='id_up' or $act=='id_down') and is_numeric($id) ) id_move_updown($act, $id, $dbi, 'gubun_tree_v2');
	// no 노드 위 아래로 이동
	if ( ($act=='no_up' or $act=='no_down') and is_numeric($id) and is_numeric($no) ) no_move_updown($act, $id, $no, $dbi, 'book_idx');

	// id 자식 노드 생성 
	if ( $act=='id_child' and is_numeric($id) and !empty($str) ) gen_child($id, $str, $dbi, 'gubun_tree_v2');
	// id 직전 및 직후 노드 생성 
	if ( ( $act=='id_prepend' or $act=='id_append' ) and is_numeric($id) and !empty($str) ) gen_before_after($act, $id, $str, $dbi, 'gubun_tree_v2');
	// id 직후 노드 생성 
//	if ( $act=='id_append' and is_numeric($id) and !empty($str) ) gen_after($id, $str, $dbi, 'gubun_tree_v2');

	// id 노드 삭제 
	if ( $act=='id_delete' and is_numeric($id) ) delete_id($id, $dbi, 'gubun_tree_v2');
	// no 노드 삭제 
	if ( $act=='no_delete' and is_numeric($id) and is_numeric($no) ) delete_no($id, $no, $dbi, 'book_idx');

// echo $act. ' ' . $id . ' ' .$no;

	// 자식 노드 이동 
	if ( $act=='move_child' and is_numeric($id) and is_numeric($tid) ) move_child($id, $tid, $dbi, 'gubun_tree_v2');
	// 직전 노드 이동 
	if ( $act=='move_before' and is_numeric($id) and is_numeric($tid) ) move_before($id, $tid, $dbi, 'gubun_tree_v2');
	// 직후 노드 이동 
	if ( $act=='move_after' and is_numeric($id) and is_numeric($tid) ) move_after($id, $tid, $dbi, 'gubun_tree_v2');

	// 다수 용어항목 분류 지정 (no_designate)
	if ( $act=='no_designate' and is_numeric($tid) ) no_designate($no_arr, $id, $tid, $dbi, 'book_idx');
	// 다수 용어항목 분류 변경 (no_change)
	if ( $act=='no_change' and is_numeric($tid) ) no_change($no_arr, $id, $tid, $dbi, 'book_idx');


# 업데이트 형태별 함수 정의

	// id 타이틀명칭 또는 요약설명 업데이트
	function id_str_update($id, $name, $dbi, $table_name, $field_name) {

		// update
		$query = "update {$table_name} set {$field_name}='{$name}' where id=$id";
		$result=mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

		// 업데이트 결과 select 
		$query = "select $field_name from {$table_name} where id=$id";
		$result=mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		if ( $matched=mysqli_fetch_assoc($result) ) {
			echo $matched[name];
		} 

	}


	// no 타이틀명칭 또는 요약설명 업데이트
	function no_str_update($id, $no, $name, $dbi, $table_name, $field_name) {

		// update
		$query = "update {$table_name} set {$field_name}='{$name}' where tree_id=$id and no=$no";
		$result=mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

		// 업데이트 결과 select 
		$query = "select $field_name from {$table_name} where tree_id=$id and no=$no";
		$result=mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		if ( $matched=mysqli_fetch_assoc($result) ) {
			echo $matched[name];
		} 

	}

	// ----------------------------------------------------

	// id 노드 위 아래로 이동
	function id_move_updown($act, $id, $dbi, $table_name) {

		// 선택 id의 parent,sub_seq 알아보기
		$result = mysqli_query($dbi,"select parent,sub_seq from {$table_name} where id=$id");
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		$matched = mysqli_fetch_assoc($result);
		$parent = $matched[parent];
		$sub_seq = $matched[sub_seq];

		if ($act == 'id_up') { 
			$near_offset = "(sub_seq+1)";			// 직전 id를 하향 이동
			$where_offset = "($sub_seq-1)";			// 직전 id
			$self_offset = "($sub_seq-1)";			// 해당 id를 상향 이동
		} else if ($act == 'id_down') { 
			$near_offset = "(sub_seq-1)";			// 직후 id를 상향 이동
			$where_offset = "($sub_seq+1)";			// 직후 id
			$self_offset = "($sub_seq+1)"; 			// 해당 id를 하향 이동
		}

		// 직전 또는 직후 id를 하향 이동
		mysqli_query($dbi,"update {$table_name} set sub_seq={$near_offset} where parent=$parent and sub_seq={$where_offset}");
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		if ( mysqli_affected_rows($dbi) >= 1 ) {
			// 해당 id를 상향 또는 하향 이동
			mysqli_query($dbi,"update {$table_name} set sub_seq={$self_offset} where id=$id");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
			echo "상향 또는 하향 이동 성공";

			// 해당 노드의 path2node, depth 업데이트
			path_depth_update ($id, $dbi);

		} else {
			echo "처음 또는 끝이라서 이동 없음";
		}

	}

	// no 노드 위 아래로 이동
	function no_move_updown($act, $id, $no, $dbi, $table_name) {

		// 일단 소팅하고, 번호 연속적으로 새로 매기기
		mysqli_query($dbi,"set @rank:=0");
		mysqli_query($dbi,"update {$table_name} set list_ord = @rank := @rank+1 where tree_id=$id order by list_ord,titlename");
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

		// 선택 no 항목의 순서번호 알아보기
		$result = mysqli_query($dbi,"select list_ord from {$table_name} where tree_id=$id and no=$no");
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		$matched = mysqli_fetch_assoc($result);
		$cur_list_ord = $matched[list_ord];

		// (no_up) 
		if ( $act == 'no_up' ) {
			// 직전 것을 하향 이동
			mysqli_query($dbi,"update {$table_name} set list_ord=(list_ord+1) where tree_id=$id and list_ord=($cur_list_ord-1)");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

			// (no_up) 자신을 상향 이동
			mysqli_query($dbi,"update {$table_name} set list_ord=($cur_list_ord-1) where tree_id=$id and no=$no and list_ord=($cur_list_ord)");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

		} else if ( $act == 'no_down' ) {
			// (no_down) 직후 것을 상향 이동
			mysqli_query($dbi,"update {$table_name} set list_ord=(list_ord-1) where tree_id=$id and list_ord=($cur_list_ord+1)");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

			// (no_up) 자신을 하향 이동
			mysqli_query($dbi,"update {$table_name} set list_ord=(list_ord+1) where tree_id=$id and no=$no and list_ord=($cur_list_ord)");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		}

	}

	// ----------------------------------------------------

	// id 자식 노드 생성 
	function gen_child($id, $str, $dbi, $table_name) {

		// 해당 id의 자식 존재 및 자식 최대수(번호) 알아보기
 		$query = "select * from {$table_name} where parent=$id";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		if( !($max_sub_seq = mysqli_num_rows($result)) ) {
			$max_sub_seq = 1;
		} else {
			$max_sub_seq = $max_sub_seq + 1;
		}

		// id 중 빈 번호 찾아냄
		$query = "select a.id + 1 as available from {$table_name} 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)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		$matched=mysqli_fetch_assoc($result);

		// 텅 비었으면, $available_id = 1 
		$available_id = ( empty($matched[available]) ? 1 : $matched[available] );

		// 자식 분류항목 새로이 삽입
		$query = "insert into {$table_name} (id,parent,sub_seq,name) values 
					($available_id,$id,$max_sub_seq,'{$str}')";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

		// 자식 노드 생성 항목에 path2node, depth 업데이트
		path_depth_update ($available_id, $dbi);

	}

	// id 직전 및 직후 노드 생성 
	function gen_before_after($act, $id, $str, $dbi, $table_name) {

		// 해당 id의 부모,순서번호를 알아냄
 		$query = "select parent,sub_seq from {$table_name} 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);
		$parent = $matched[parent];
		if ( $act == 'id_prepend' ) {
			$sub_seq = $matched[sub_seq] ;
		} else if ( $act == 'id_append' ) {
			$sub_seq = $matched[sub_seq] + 1 ;
		} 

		// 해당 id 부터 1씩 증가시킴
 		$query = "update {$table_name} set sub_seq=(sub_seq+1) where parent=$parent and sub_seq>=$sub_seq";
		$result = mysqli_query($dbi,$query);

		// id 중 빈 번호 확인
		$query = "select a.id + 1 as available 
						from {$table_name} 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)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		$matched = mysqli_fetch_assoc($result);
		$available_id = $matched[available];

		// 분류항목 새로이 삽입
		$query = "insert into {$table_name} (id,parent,sub_seq,name) values ($available_id,$parent,$sub_seq,'{$str}')";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

		// 직후 노드 생성 항목에 path2node, depth 업데이트
		path_depth_update ($available_id, $dbi);
	}

/*
	// id 직후 노드 생성 
	function gen_after($id, $str, $dbi, $table_name) {

		// 해당 id의 부모,순서번호를 알아냄
 		$query = "select parent,sub_seq from {$table_name} 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);
		$parent = $matched[parent];
		$sub_seq = $matched[sub_seq];

		// 해당 id 직후부터 1씩 증가시킴
 		$query = "update {$table_name} set sub_seq=(sub_seq+1) where parent=$parent and sub_seq>=($sub_seq+1)";
		$result = mysqli_query($dbi,$query);

		// id 중 빈 번호 확인
		$query = "select a.id + 1 as available from {$table_name} 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)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		$matched = mysqli_fetch_assoc($result);
		$available_id = $matched[available];

		// 분류항목 새로이 삽입
		$query = "insert into {$table_name} (id,parent,sub_seq,name) values ($available_id,$parent,$sub_seq+1,'{$str}')";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

		// 직후 노드 생성 항목에 path2node, depth 업데이트
		$delay_echo .= path_depth_update ($available_id, $dbi);

	}
*/

	// ----------------------------------------------------

	// id 노드 삭제 
	function delete_id($id, $dbi, $table_name) {

		// 선택 id의 parent,sub_seq 알아보기
		$query = "select a.parent,a.sub_seq
						,count(b.id) as sub_cnt
					from {$table_name} a 
						left join {$table_name} b on a.id=b.parent
					where a.id=$id
					group by a.id";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
		$matched = mysqli_fetch_assoc($result);
		$parent = $matched[parent];
		$sub_seq = $matched[sub_seq];
		$sub_cnt = $matched[sub_cnt];

		// 하부 id 있으면, 삭제 불가
		if ($sub_cnt > 0) return; 

		// 해당 id를 삭제
		mysqli_query($dbi,"delete from {$table_name} where id=$id");
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		// 삭제 성공하면, 직후 id들을 하나씩 상향 이동
		if ( mysqli_affected_rows($dbi) >= 1 ) {
			mysqli_query($dbi,"update {$table_name} set sub_seq=(sub_seq-1) where parent=$parent and sub_seq>=($sub_seq+1)");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		} 

	}


	// no 노드 삭제 
	function delete_no($id, $no, $dbi, $table_name) {

		// 해당 용어항목 순서를 알아냄
		$query = "select list_ord from {$table_name} where tree_id=$id and no=$no limit 1";
		$result = mysqli_query($dbi,$query);
		$row = mysqli_fetch_assoc($result);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 해당 용어항목 직후부터 순서번호 모두 1씩 감함
		$query = "update {$table_name} set list_ord=(list_ord-1) where tree_id=$id and list_ord>$row[list_ord]";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 해당 용어항목 순서번호 삭제
		$query = "delete from {$table_name} where tree_id=$id and no=$no limit 1"; 
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 해당 분류에 속한 용어들 갯수 update
		$query = "update gubun_tree_v2 set linked_num=(select count(*) from {$table_name} where tree_id=$id) where id=$id";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

	}

	// ----------------------------------------------------

	// 직후 노드 이동 
	function move_after($source, $target, $dbi, $table_name) {
		
		// 소스쪽 이동 항목을 발췌하면, 나머지 항목 부번호들은 1씩 감해짐
		$query = "update {$table_name} a left join (select parent,sub_seq from {$table_name} where id=$source) b on a.parent=b.parent set a.sub_seq=(a.sub_seq-1) where a.sub_seq>b.sub_seq";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 타깃 항목의 부모,부번호를 알아냄
 		$query = "select parent,sub_seq from {$table_name} where id=$target";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		$matched=mysqli_fetch_assoc($result);
		$parent = $matched[parent];
		$sub_seq = $matched[sub_seq];

		// 타킷 항목 이후 부번호들을 1씩 증가시킴
 		$query = "update {$table_name} set sub_seq=(sub_seq+1) where parent=$parent and sub_seq>$sub_seq";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 소스 항목을 타킷 항목에 맞춰 수정(이동)
		$query = "update {$table_name} set parent=$parent,sub_seq=($sub_seq+1) where id=$source";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 이동된 항목에 path2node, depth 업데이트
		path_depth_update ($source, $dbi);

	}


	// 직전 노드 이동
	function move_before($source, $target, $dbi, $table_name) {

		// 소스쪽 이동 항목을 발췌하면, 나머지 항목 부번호들은 1씩 감해짐
		$query = "update {$table_name} a left join (select parent,sub_seq from {$table_name} where id=$source) b on a.parent=b.parent set a.sub_seq=(a.sub_seq-1) where a.sub_seq>b.sub_seq";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 타깃 항목의 부모,부번호를 알아냄
 		$query = "select parent,sub_seq from {$table_name} where id=$target";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		$matched=mysqli_fetch_assoc($result);
		$parent = $matched[parent];
		$sub_seq = $matched[sub_seq];

		// 타킷 항목부터 부번호들을 1씩 증가시킴
 		$query = "update {$table_name} set sub_seq=(sub_seq+1) where parent=$parent and sub_seq>=$sub_seq";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 소스 항목을 타킷 항목에 맞춰 수정(이동)
		$query = "update {$table_name} set parent=$parent,sub_seq=($sub_seq) where id=$source";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 이동된 항목에 path2node, depth 업데이트
		path_depth_update ($source, $dbi);

	}


	// 자식 노드 이동 
	function move_child($source, $target, $dbi, $table_name) {

		// 소스쪽 이동 항목을 발췌하면, 나머지 항목 부번호들은 1씩 감해짐
		$query = "update {$table_name} a left join (select parent,sub_seq from {$table_name} where id=$source) b on a.parent=b.parent set a.sub_seq=(a.sub_seq-1) where a.sub_seq>b.sub_seq";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 타킷 항목의 하부 자식번호 중 최대 알아내기
		$query = "select ifnull(max(sub_seq),0)+1 as max_num from {$table_name} where parent=$target";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		$matched=mysqli_fetch_assoc($result);
		$max_num = $matched[max_num];

		// 소스 항목을 타킷 항목의 하부 자식으로 수정(이동)
		$query = "update {$table_name} set parent=$target,sub_seq=$max_num where id=$source";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 이동된 항목에 path2node, depth 업데이트
		path_depth_update ($source, $dbi);

	}


	// 다수의 용어해설항목을 한꺼번에 분류 변경 (no_change)
	function no_change($no_arr, $id, $tid, $dbi, $table_name) {

		// 여러 소스 no들을 ','로 구분
		$no_list = implode(',',$no_arr);

		// 현재 target_id에서 list_ord 중 최대값 구하기
		mysqli_query($dbi,"select @max:=max(list_ord) from {$table_name} where tree_id=$tid");
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// 추출된 no들의 id를 target_id로 바꾸고, list_ord 업데이트
		$query = "update
					{$table_name} a
					inner join 
					(
						select no,tree_id,@max:=@max+1 as list_ord from {$table_name} where tree_id=$id and no in ($no_list)
					) b
					on (a.tree_id=b.tree_id and a.no=b.no)
					set a.tree_id=$tid,a.list_ord=b.list_ord
				";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// source_id 분류에 속한 용어들 갯수 update
		$query = "update gubun_tree_v2 set linked_num=(select count(*) from {$table_name} where tree_id=$id) where id=$id";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		// target_id 분류에 속한 용어들 갯수 update
		$query = "update gubun_tree_v2 set linked_num=(select count(*) from {$table_name} where tree_id=$tid) where id=$tid";
		$result = mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

	}


	// 다수의 용어해설항목을 한꺼번에 분류 변경 (no_designate)
	function no_designate($no_arr, $id, $tid, $dbi, $table_name) {

		// 여러 소스 no들을 ','로 구분
		$no_list = implode(',',$no_arr);

// echo "no_list=".$no_list.", id=".$id.", tid=".$tid.", tabl_name=".$table_name."<br>";

		// 이미 분류되어진 것 있는가 여부를 체크 후 분류 작업 ";
		$result=mysqli_query($dbi,"select * from $table_name where no in ($no_list) and tree_id=$tid");
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		if ( mysqli_num_rows($result) == 0 ) {
			// 현재 list_ord 중 최대값 구하기
			mysqli_query($dbi,"select @max:=max(list_ord) from $table_name where tree_id=$tid");
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

			// 전달된 다수 no들을 해당 분류 id에 지정
			$query = "insert into $table_name
						(no,tree_id,titlename,list_ord) 
						( select no,{$tid} as tree_id,word,@max:=@max+1 as list_ord from dict_word_list where no in ({$no_list}) group by no )
					 "; // and wordtype='k' 
			$result = mysqli_query($dbi,$query);
				if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

			if ( mysqli_affected_rows($dbi)>=1) {
				// 해당 분류에 속한 용어들 갯수 update
				$query = "update gubun_tree_v2 set linked_num=(select count(*) from $table_name where tree_id=$tid) where id=$tid";
				$result = mysqli_query($dbi,$query);
					if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
			}
		} else {
			echo "이미 해당 tid에 기존 no가 있으므로, no_designate 실패<br>";
		}

	}


?>

<?php

// path2node, depth 업데이트 함수
function path_depth_update ($id, $dbi) {

	$query = "update gubun_tree_v2 set 
				path2node=concat(';0;;',replace(substring_index(getpath_v2(id),'|',-1),'::',';;'),';'),
				path2node_v2=concat('0,',replace(substring_index(getpath_v2(id),'|',-1),'::',',')),
				depth=round( ( length(path2node) - length(replace(path2node,';;','')) )/length(';;') )
				where id=".$id;
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

}

?>


Copyrightⓒ written by 차재복 (Cha Jae Bok)               기술용어해설 후원
"본 웹사이트 내 모든 저작물은 원출처를 밝히는 한 자유롭게 사용(상업화포함) 가능합니다"