<?php

/*
=====================================================================

  프로젝트 이름 : 피리 카페 심플

  만든사람 : 피리 PIREE ( tosports@naver.com )

  홈페이지 : https://piree.kr/nett_dev

  작성날짜 : 2024년 04월 18일 목요일 오전 09시 30분 / 맑음, 15.9, 일교차 주의, 24, 8, 0%, 0%, 2.4ms / 매우 나쁨, 133, 51, 0.014

  저 작 권 : Copyright ⓒ 2024 피리 piree.kr All right reserved
              그누보드 외에 추가된 소스는~
              만든사람의 허락없이 무단으로 사용할수 없습니다.
              사용하고자 할 경우 만든사람의 허락을 받아야 합니다.
              http://.piree.kr 에 문의해 주세요.

=====================================================================
 피리 카페 심플 > 프로그램 > 피리 카페 관리 > 내 카페 > 게시판 > 게시글 다중 처리하기
=====================================================================

*/

  IF (!defined('_GNUBOARD_'))                      EXIT;

  IF ( !piree_cafe_auth_get() )
  {
    alert('접근 권한이 없습니다.', G5_URL);
  }

  include_once( piree__path($piree_prog_a[770099]['num'], '_CAFE.INFO.CHECK.inc.php') );

//.11
  @include_once( $board_cate_list_a[$board_cate_n]['board_skin_p'] .'/delete_all.head.skin.php' );

  $board_cate_n_a = array();

  $board_cate_1_a = array();

  $tmp_array = array();

  IF ( $wr_id )
  {

      $tmp_array[0] = $wr_id;

  }
  ELSE
  {

      $tmp_array = (isset($_POST['chk_wr_id']) && is_array($_POST['chk_wr_id'])) ? $_POST['chk_wr_id'] : array();

  }

  $chk_count = count($tmp_array);

  IF ( $chk_count > (G5_IS_MOBILE ?  $board_cate_list_a[$board_cate_n]['bca_mo_page_rows'] : $board_cate_list_a[$board_cate_n]['bca_pc_page_rows']) )
  {
      alert('올바른 방법으로 이용해 주십시오.');
  }

// 실행
  @include_once( $board_cate_list_a[$board_cate_n]['board_skin_p'] .'/delete_all.skin.php' );

  $sql_select_cafe  = "SELECT COUNT(*) FROM `". $piree['cafe_article'] ."` ";
  $sql_select_cafe .= "WHERE `wr_pi_piree_cafe_n` = '". $piree_cafe_n ."'";

  $sql_select_article = $sql_select_cafe ." AND `wr_is_comment` = '0'";

  $sql_select_comment = $sql_select_cafe ." AND `wr_is_comment` = '1'";

  $count_write = 0;

  $count_comment = 0;

  FOR ( $loop_n=$chk_count-1; $loop_n>=0; $loop_n--)
  {

      $sql_select  = "SELECT * FROM `". $piree['cafe_article'] ."` ";
      $sql_select .= "WHERE `wr_id` = '". $tmp_array[$loop_n] ."'";
      $write = sql_fetch($sql_select);

      IF ( piree_cafe_auth_get() )
      {

        ;

      }
      ELSE IF ( $piree_cafe_n > 0 && $is_cafe_manager == $piree_cafe_n )
      {

        ;

      }
      ELSE IF ( $member['mb_id'] && $member['mb_id'] == $write['mb_id'] )
      {

        ;

      }
      ELSE
      {

// 불가

        continue;

      }

      $board_cate_n_a[$write['wr_pi_board_cate_n']] = $write['wr_pi_board_cate_n'];

      $len = strlen($write['wr_reply']);

      IF ( $len < 0 )
      {
        $len = 0;
      }

      $reply = substr($write['wr_reply'], 0, $len);

//.
      $sql_select  = "SELECT COUNT(*) AS cnt FROM `". $piree['cafe_article'] ."` ";
      $sql_select .= "WHERE `wr_reply` LIKE '". $reply ."%' AND `wr_id` <> '". $write['wr_id'] ."' AND `wr_num` = '". $write['wr_num'] ."' AND `wr_is_comment` = '0'";
      $row = sql_fetch($sql);

      IF ( $row['cnt'] )
      {
          continue;
      }

// : 원글과 코멘트수가 정상적으로 업데이트 되지 않는 오류를 잡아 주셨습니다.
      $sql_select  = "SELECT `wr_id`, `mb_id`, `wr_is_comment`, `wr_content` FROM `". $piree['cafe_article'] ."` ";
      $sql_select .= "WHERE `wr_parent` = '". $write['wr_id'] ."' ORDER BY `wr_id`";
      $result = sql_query($sql_select);

      WHILE ( $row = sql_fetch_array($result) )
      {

          IF ( !$row['wr_is_comment'] )
          {

              IF ( !delete_point($row['mb_id'], $bo_table, $row['wr_id'], '쓰기') )
              {
                  insert_point($row['mb_id'], $board_cate_list_a[$board_cate_n]['bca_write_point'] * (-1), "{$board_cate_list_a[$board_cate_n]['bca_subject']} {$row['wr_id']} 글 삭제");
              }

              $sql_select  = "SELECT * FROM `". $g5['board_file_table'] ."` ";
              $sql_select .= "WHERE `bo_table` = '". $bo_table ."' AND `wr_id` = '". $row['wr_id'] ."'";
              $result2 = sql_query($sql_select);

              WHILE ( $row2 = sql_fetch_array($result2) ) 
              {

                  $delete_file = run_replace('delete_file_path', G5_DATA_PATH .'/file/'. $bo_table .'/'. str_replace('../', '',$row2['bf_file']), $row2);

                  IF( file_exists($delete_file) )
                  {
                      @unlink($delete_file);
                  }

                  IF ( preg_match("/\.({$config['cf_image_extension']})$/i", $row2['bf_file']) )
                  {

                      delete_board_thumbnail($bo_table, $row2['bf_file']);

                  }

              }

              delete_editor_thumbnail($row['wr_content']);

              $sql_delete  = "DELETE FROM `". $g5['board_file_table'] ."` ";
              $sql_delete .= "WHERE `bo_table` = '". $bo_table ."' AND wr_id = '". $row['wr_id'] ."'";
              sql_query($sql_delete);

              $count_write++;

          }
          ELSE
          {

              IF ( !delete_point($row['mb_id'], $bo_table, $row['wr_id'], '댓글') )
              {
                  insert_point($row['mb_id'], $board_cate_list_a[$board_cate_n]['bca_comment_point'] * (-1), "{$board_cate_list_a[$board_cate_n]['bca_subject']} {$write['wr_id']}-{$row['wr_id']} 댓글삭제");
              }

              $count_comment++;

          }

      }

      $sql_delete  = "DELETE FROM `". $piree['cafe_article'] ."` ";
      $sql_delete .= "WHERE `wr_parent` = '". $write['wr_id'] ."'";
      sql_query($sql_delete);

      $sql_delete  = "DELETE FROM `". $g5['board_new_table'] ."` ";
      $sql_delete .= "WHERE `bo_table` = '". $bo_table ."' AND `wr_parent` = '". $write['wr_id'] ."'";
      sql_query($sql_delete);

      $sql_delete  = "DELETE FROM `". $g5['scrap_table'] ."` ";
      $sql_delete .= "WHERE `bo_table` = '". $bo_table ."' AND `wr_id` = '". $write['wr_id'] ."'";
      sql_query($sql_delete);

      /*

      $notice_array = explode(',', trim($board_cate_list_a[$board_cate_n]['bca_notice']));
      $bo_notice = "";
      for ($k=0; $k<count($notice_array); $k++)
          if ((int)$write['wr_id'] != (int)$notice_array[$k])
              $bo_notice .= $notice_array[$k].',';
      $bo_notice = trim($bo_notice);
      */

      $bo_notice = board_notice($board_cate_list_a[$board_cate_n]['bca_notice'], $write['wr_id']);

      $sql_update  = "UPDATE `". $g5['board_table'] ."` SET ";
      $sql_update .= "`bo_notice` = '". $bo_notice ."' ";
      $sql_update .= "WHERE `bo_table` = '". $bo_table ."'";
      sql_query($sql_update);

      $board_cate_list_a[$board_cate_n]['bca_notice'] = $bo_notice;

  }

  cafe_article_count_reload($piree_cafe_n, $member['mb_id'], $board_cate_n_a, 'minus', $count_write, $count_comment);

    IF ( count($board_cate_n_a) > 0 )
    {

      FOREACH ( $board_cate_n_a AS $loop_cate_n=>$val_out)
      {

        IF ( $loop_cate_n > 0 )
        {

          IF ( $board_cate_list_a[$loop_cate_n]['bca_depth_n'] == 2 )
          {

            $loop_cate_1dep_n = $board_cate_list_a[$loop_cate_n]['bca_1_n'];

            $board_cate_1_a[$loop_cate_1dep_n] = $loop_cate_1dep_n;

          }

          $sql_select = $sql_select_article ." AND `wr_pi_board_cate_n` = '". $loop_cate_n ."'";
          $artilce_this_t = (int)sql_efv($sql_select);

          $sql_select = $sql_select_comment ." AND `wr_pi_board_cate_n` = '". $loop_cate_n ."'";
          $comment_this_t = (int)sql_efv($sql_select);

          $sql_update  = "UPDATE `". $piree['cafe_board'] ."` SET ";
          $sql_update .= "`bca_count_write`   = '". $artilce_this_t ."', ";
          $sql_update .= "`bca_count_comment` = '". $comment_this_t ."' ";
          $sql_update .= "WHERE `bca_cafe_n` = '". $piree_cafe_n ."' AND `bca_cate_n` = '". $loop_cate_n ."'";

          IF ( !sql_query($sql_update) )
          {
            alert("[ ". $board_cate_list_a[$loop_cate_n]['bca_cate_s'] ." - 2 ]의 게시글, 댓글 수를 새로고침하지 못했습니다.");
          }

        }

      }

      IF ( count($board_cate_1_a) > 0 )
      {

        FOREACH ( $board_cate_1_a AS $loop_cate_1_n=>$val_in)
        {

          IF ( $loop_cate_1_n > 0 )
          {

            $sql_select = $sql_select_article ." AND (`wr_pi_board_cate_n` BETWEEN '". $loop_cate_1_n ."' AND '". ($loop_cate_1_n+99) ."')";
            $artilce_this_t = (int)sql_efv($sql_select);

            $sql_select = $sql_select_comment ." AND (`wr_pi_board_cate_n` BETWEEN '". $loop_cate_1_n ."' AND '". ($loop_cate_1_n+99) ."')";
            $comment_this_t = (int)sql_efv($sql_select);

            $sql_update  = "UPDATE `". $piree['cafe_board'] ."` SET ";
            $sql_update .= "`bca_count_write`   = '". $artilce_this_t ."', ";
            $sql_update .= "`bca_count_comment` = '". $comment_this_t ."' ";
            $sql_update .= "WHERE `bca_cafe_n` = '". $piree_cafe_n ."' AND `bca_cate_n` = '". $loop_cate_1_n ."'";

            IF ( !sql_query($sql_update) )
            {
              alert("[ ". $board_cate_list_a[$loop_cate_1_n]['bca_cate_s'] ." - 2 ]의 게시글, 댓글 수를 새로고침하지 못했습니다.");
            }

          }

        }

      }

    }

    $sql_select = $sql_select_cafe ." AND `wr_is_comment` = '0'";
    $artilce_cafe_t = (int)sql_efv($sql_select);

    $sql_select = $sql_select_cafe ." AND `wr_is_comment` = '1'";
    $comment_cafe_t = (int)sql_efv($sql_select);

    $sql_update  = "UPDATE `". $piree['cafe_board'] ."` SET ";
    $sql_update .= "`bca_count_write`   = '". $artilce_cafe_t ."', ";
    $sql_update .= "`bca_count_comment` = '". $comment_cafe_t ."' ";
    $sql_update .= "WHERE `bca_cafe_n` = '". $piree_cafe_n ."' AND `bca_cate_n` = '0'";

    IF ( !sql_query($sql_update) )
    {
      alert("[ ". $piree_cafe_name_s ." ] 카페의 게시글, 댓글 수를 새로고침하지 못했습니다.");
    }

    $sql_update  = "UPDATE `". $piree['cafe_board'] ."` SET ";
    $sql_update .= "`bca_count_write` = `bca_count_write` - '". $count_write ."', ";
    $sql_update .= "`bca_count_comment` = `bca_count_comment` - '". $count_comment ."' ";
    $sql_update .= "WHERE `bca_cafe_n` = '". $piree_cafe_n ."' AND `bca_cate_n` = '0'";

    IF ( !sql_query($sql_update) )
    {
      alert("[ ". $piree_cafe_name_s ." ] 카페의 게시글, 댓글 수를 새로고침하지 못했습니다.");
    }

    IF ( $count_write > 0 || $count_comment > 0 )
    {

 //
 //
       $sql_update  = "UPDATE `". $g5['board_table'] ."` SET ";
       $sql_update .= "`bo_count_write` = `bo_count_write` - '". $count_write ."', ";
       $sql_update .= "`bo_count_comment` = `bo_count_comment` - '". $count_comment ."' ";
       $sql_update .= "WHERE `bo_table` = '". $piree['cafe_board_code'] ."'";
       sql_query($sql_update);

    }

//.11
  @include_once( $board_cate_list_a[$board_cate_n]['board_skin_p'] .'/delete_all.tail.skin.php' );

  delete_cache_latest($bo_table);

  run_event('bbs_delete_all', $tmp_array, $board);

  $move_url_s = go_piree_cafe_board('', $piree_cafe_n, $board_cate_n) .'&amp;page='.$page.$qstr;

  goto_url($move_url_s);

?>