<?php

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

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

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

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

  작성날짜 : 2024년 04월 13일 토요일 오후 20시 09분 / 맑음, 22.2, 건조주의보, 26, 9, 0%, 0%, 1.4ms / 나쁨, 58, 21

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

=====================================================================
 피리 카페 심플 > 프로그램 > 피리 카페 관리 > 내 카페 > 게시판 > 게시글 목록 불러오기
=====================================================================

*/

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

  $sop = strtolower($sop);

  IF ( $sop != 'and' && $sop != 'or' )
  {

    $sop = 'and';

  }

  $stx = trim($stx);

  $is_search_bbs = false;

  IF ( $sca || $stx || $stx === '0' )
  {

    $is_search_bbs = true;

    $sql_search = get_piree_cafe_sql_search($sfl, $stx, $sop);

    $sql_select  = "SELECT MIN(`wr_num`) AS `min_wr_num` FROM `". $piree['cafe_article'] ."` ";
    $sql_select .= "WHERE ". $sql_article_category_s;
    $row = sql_fetch($sql_select);
    $min_spt = (int)$row['min_wr_num'];

    IF ( !$spt )
    {
      $spt = $min_spt;
    }

    $sql_search .= " AND (`wr_num` BETWEEN ". $spt." AND ". ($spt+$config['cf_search_part']) .") ";

    // 코드로 대체 http://.kr/g5_bug/2922
    $sql_select  = "SELECT COUNT(DISTINCT `wr_parent`) AS `cnt` FROM `". $piree['cafe_article'] ."` ";
    $sql_select .= "WHERE ". $sql_article_category_s ." AND ". $board_cate_list_a[$board_cate_n]['article_range_where_0_s'] . $sql_search;
    $row = sql_fetch($sql_select);
    $total_count = $row['cnt'];

    /*
    $sql = " select distinct wr_parent from `". $piree['cafe_article'] ."` where {$sql_search} ";
    $result = sql_query($sql);
    $total_count = sql_num_rows($result);
    */

  }
  ELSE
  {

    $sql_search = "";

    $total_count = $board_cate_list_a[$board_cate_n]['bca_count_write'];

  }

  IF ( G5_IS_MOBILE )
  {

    $page_rows = $board_cate_list_a[$board_cate_n]['bca_mo_page_rows'];

    $list_page_rows = $board_cate_list_a[$board_cate_n]['bca_mo_page_rows'];

  }
  ELSE
  {

    $page_rows = $board_cate_list_a[$board_cate_n]['bca_pc_page_rows'];

    $list_page_rows = $board_cate_list_a[$board_cate_n]['bca_pc_page_rows'];

  }

  IF ( $page < 1 )
  {
    $page = 1;
  }

  $today2 = G5_TIME_YMD;

  $list = array();

  $article_list_n = 0;

  $notice_count = 0;

  $notice_list_a = array();

  IF ( !$is_search_bbs )
  {

    $arr_notice = explode(',', trim($board_cate_list_a[$board_cate_n]['bca_notice']));

    $from_notice_idx = ($page - 1) * $page_rows;

    IF ( $from_notice_idx < 0 )
    {

      $from_notice_idx = 0;

    }

    $board_notice_t = count($arr_notice);

    FOR ( $loop_n=0; $loop_n<$board_notice_t; $loop_n++)
    {

      IF ( trim($arr_notice[$loop_n]) == '' )
      {

        continue;

      }

      $sql_select = "SELECT * FROM `". $piree['cafe_article'] ."` ";
      $sql_select .= "WHERE ". $sql_article_category_s ." AND ". $board_cate_list_a[$board_cate_n]['article_range_where_0_s'] . " `wr_id` = '". $arr_notice[$loop_n] ."'";
      $row = sql_fetch($sql_select);

      IF ( !isset($row['wr_id']) || !$row['wr_id'] )
      {

        continue;

      }

      $notice_list_a[] = $row['wr_id'];

      IF ( $loop_n < $from_notice_idx )
      {

        continue;

      }

      $list[$article_list_n] = get_list($row, $board, $board_skin_url, G5_IS_MOBILE ? $board_cate_list_a[$board_cate_n]['bca_mobile_subject_len'] : $board_cate_list_a[$board_cate_n]['bca_subject_len']);

      $list[$article_list_n]['is_notice'] = true;

      $list[$article_list_n]['num'] = 0;

      $article_list_n++;

      $notice_count++;

      IF ( $notice_count >= $list_page_rows )
      {

        continue;

      }

    }

  }

  $total_page  = ceil($total_count / $page_rows);

  $from_record = ($page - 1) * $page_rows;

  IF ( !empty($notice_list_a) )
  {

    $from_record -= count($notice_list_a);

    IF ( $from_record < 0 )
    {

      $from_record = 0;

    }

    IF ( $notice_count > 0 )
    {

      $page_rows -= $notice_count;

    }

    IF ( $page_rows < 0 )
    {
      $page_rows = $list_page_rows;
    }

  }

  $qstr2 = $board_cate_list_a[$board_cate_n]['piree_cafe_board_query'] .'&amp;sop='. $sop;

  $bo_gallery_cols = $board_cate_list_a[$board_cate_n]['bca_gallery_cols'] ? $board_cate_list_a[$board_cate_n]['bca_gallery_cols'] : 1;

  $td_width = (int)(100 / $bo_gallery_cols);

  // 아니면 정렬에 사용하지 않음
  // (!$sst || ($sst && !(strstr($sst, 'wr_id') || strstr($sst, "wr_datetime")))) {
  IF ( !$sst )
  {

    IF ($board_cate_list_a[$board_cate_n]['bca_sort_field'])
    {

      $sst = $board_cate_list_a[$board_cate_n]['bca_sort_field'];

    }
    ELSE
    {

      $sst  = "`wr_num`, `wr_reply`";

      $sod = "";

    }

  }
  ELSE
  {

    $board_sort_fields = get_piree_cafe_board_sort_fields($board_cate_a, 1);

    IF ( !$sod && array_key_exists($sst, $board_sort_fields) )
    {

      $sst = $board_sort_fields[$sst];

    }
    ELSE
    {

      // 정렬 대상 필드가 아니라면 공백으로 (nasca 님 09.06.16)
      // 필드로 정렬을 하려면 아래의 코드에 해당 필드를 추가하세요.
      //$sst = preg_match("/^(wr_subject|wr_datetime|wr_hit|wr_good|wr_nogood)$/i", $sst) ? $sst : "";
      $sst = preg_match("/^(wr_datetime|wr_hit|wr_good|wr_nogood)$/i", $sst) ? $sst : "";

    }

  }

  IF ( !$sst )
  {
    $sst = "`wr_num`, `wr_reply`";
  }

  IF ( $sst )
  {
    $sql_order = " ORDER BY ". $sst ." ". $sod ." ";
  }

  IF ( $is_search_bbs )
  {

    $sql_select  = "SELECT DISTINCT `wr_parent` FROM `". $piree['cafe_article'] ."` ";
    $sql_select .= "WHERE ". $sql_article_category_s ." AND ". $board_cate_list_a[$board_cate_n]['article_range_where_0_s'] . $sql_search ." ";
    $sql_select .= $sql_order ." LIMIT ". $from_record .", ". $page_rows;

  }
  ELSE
  {

    $sql_select  = "SELECT *  FROM `". $piree['cafe_article'] ."` ";
    $sql_select .= "WHERE ". $sql_article_category_s ." AND ". $board_cate_list_a[$board_cate_n]['article_range_where_0_s'] . " `wr_is_comment` = '0' ";

    IF ( !empty($notice_array) )
    {
      $sql_select .= " AND `wr_id` NOT IN (". implode(', ', $notice_array) .") ";
    }

    IF ( $board_cate_n > 0 )
    {
      $sql_select .= " AND (". get_piree_cafe_article_count($board_cate_n, $board_cate_list_a[$board_cate_n]['bca_depth_n'], 'wr_pi_board_cate_n') .")";
    }

    $sql_select .= " ". $sql_order ." LIMIT ". $from_record .", ". $page_rows;

  }

  IF ( $page_rows > 0 )
  {
    $result = sql_query($sql_select);
  }

//$loop_n = 0;

// ( $row = sql_fetch_array($result) )
  FOR ( $loop_n=0; $row = sql_fetch_array($result); $loop_n++ )
  {

    IF ( $is_search_bbs )
    {

      $sql_select  = "SELECT * FROM `". $piree['cafe_article'] ."` ";
      $sql_select .= "WHERE ". $sql_article_category_s ." AND `wr_id` = '". $row['wr_parent'] ."'";
      $row = sql_fetch($sql_select);

    }

    $list[$article_list_n] = get_list($row, $board_cate_a, $board_skin_url, G5_IS_MOBILE ? $board_cate_list_a[$board_cate_n]['bca_mo_subject_len'] : $board_cate_list_a[$board_cate_n]['bca_pc_subject_len']);

    IF ( strstr($sfl, 'subject') )
    {

      $list[$article_list_n]['subject'] = search_font($stx, $list[$article_list_n]['subject']);

    }

    $list[$article_list_n]['is_notice'] = false;

    $list_num = $total_count - ($page - 1) * $list_page_rows - $notice_count;

    $list[$article_list_n]['num'] = $list_num - $loop_n;

    $loop_cate_path_s = $row['wr_parent'];
    $list[$article_list_n]['is_notice'] = false;

//$loop_n++;

    $article_list_n++;

  }

  g5_latest_cache_data($board_cate_list_a[$board_cate_n]['bca_table'], $list);

  $write_pages = get_paging(G5_IS_MOBILE ? $config['cf_mobile_pages'] : $config['cf_write_pages'], $page, $total_page, go_piree_cafe_board('', $piree_cafe_n, $board_cate_n) .'&amp;page=');

  $list_href = '';

  $prev_part_href = '';

  $next_part_href = '';

  IF ( $is_search_bbs )
  {

    $list_href = go_piree_cafe_board('', $piree_cafe_n, $board_cate_n);

    $patterns = array('#&amp;page=[0-9]*#', '#&amp;spt=[0-9\-]*#');

// ($prev_spt >= $min_spt)
    $prev_spt = $spt - $config['cf_search_part'];

    IF ( isset($min_spt) && $prev_spt >= $min_spt )
    {

      $qstr1 = preg_replace($patterns, '', $qstr);
      $prev_part_href = go_piree_cafe_board('', $piree_cafe_n, $board_cate_n) . $qstr1 .'&amp;spt='. $prev_spt.'&amp;page=1';
      $write_pages = page_insertbefore($write_pages, '<a href="'. $prev_part_href.'" class="pg_page pg_search pg_prev">이전검색</a>');

    }

    $next_spt = $spt + $config['cf_search_part'];

    IF ( $next_spt < 0 )
    {
      $qstr1 = preg_replace($patterns, '', $qstr);
      $next_part_href = go_piree_cafe_board('', $piree_cafe_n, $board_cate_n) . $qstr1 .'&amp;spt='. $next_spt.'&amp;page=1';
      $write_pages = page_insertafter($write_pages, '<a href="'. $next_part_href.'" class="pg_page pg_search pg_next">다음검색</a>');
    }

  }

  $write_href = '';

  IF ( $cafe_member_a['cafe_mem_level_n'] >= $board_cate_list_a[$board_cate_n]['bca_write_level'] )
  {

    $write_href = go_piree_cafe_board('piree.cafe.board.write.php', $piree_cafe_n, $board_cate_n);

  }

  $nobr_begin = $nobr_end = "";

  IF ( preg_match("/gecko|firefox/i", $_SERVER['HTTP_USER_AGENT']) )
  {
    $nobr_begin = '<nobr>';
    $nobr_end   = '</nobr>';
  }

  $rss_href = '';

  IF ( $board_cate_list_a[$board_cate_n]['bca_use_rss_view'] )
  {
    $rss_href = G5_BBS_URL .'/rss.php?bo_table='. $piree['cafe_board_code'];

  }

  $stx = get_text(stripslashes($stx));

  IF ( piree_cafe_auth_get() )
  {

    include_once( piree__path($piree_prog_a[770000]['num'], 'form.lib.php') );

    $piree_cafe_board_list_action_u = https_url_get( piree__url($piree_prog_a[770099]['num'], 'piree.cafe.board.list_update.php') );

  }

?>
    <script>

      var piree_cafe_url = "<?php echo piree__url($piree_prog_a[770099]['num'], ''); ?>";

    </script>
<?php

  include_once( $board_cate_list_a[$board_cate_n]['board_skin_p'] .'/piree.cafe.list.skin.php' );

?>