_get_search_cache($sca, $sfl, $stx, $spt, $sco); if ($sca || ($sfl && $stx)) { $this->db->distinct(); $this->db->select('wr_parent'); } else { $this->db->select($wr_field); $this->db->where('wr_is_comment', '0'); } if ($sst && $sod) $this->db->order_by($sst, $sod); $qry = $this->db->get(TBL_WRITE.'_'.$table_name, $limit, $offset); $result = $qry->result_array(); if ($sca || ($sfl && $stx)) { foreach($result as $row) $wr_ids[] = $row['wr_parent']; if (isset($wr_ids)) { if ($sst && $sod) $this->db->order_by($sst, $sod); return $this->M_basic->get_write( $table_name, $wr_ids, $wr_field); } } return $result; } function list_count($table_name, $spt, $sca, $sfl, $stx, $sco) { // 인기검색어 if (trim($stx)) $this->db->simple_query(" insert into ".LUCA_POPULAR." set pp_word = '".$stx."', pp_date = '".TIME_YMD."', pp_ip = '".$this->input->server('REMOTE_ADDR')."' "); // 원글만 얻는다. (코멘트의 내용도 검색하기 위함) $this->db->select(' COUNT(DISTINCT '.$this->db->protect_identifiers('wr_parent').') as '.$this->db->protect_identifiers('wr_count')); $this->_get_search_cache($sca, $sfl, $stx, $spt, $sco); $qry = $this->db->get(TBL_WRITE.'_'.$table_name); $row = $qry->row(); return $row->wr_count; } function list_count_org($table_name) { // 원글만 얻는다. (코멘트의 내용도 검색하기 위함) $this->db->select(' COUNT(DISTINCT '.$this->db->protect_identifiers('wr_parent').') as '.$this->db->protect_identifiers('wr_count')); $qry = $this->db->get(TBL_WRITE.'_'.$table_name); $row = $qry->row(); return $row->wr_count; } // 공지사항 function list_notice($table_name, $notice, $wr_field) { $key = count($notice)-1; if ($notice[$key] == '') unset($notice[$key]); $this->db->select($wr_field); $this->db->where_in('wr_id', $notice); $qry = $this->db->get(TBL_WRITE.'_'.$table_name); return $qry->result_array(); } // 검색 구문을 얻는다. function _get_search_cache($search_ca_code, $search_field, $search_text, $spt=false, $sco = '') { if ($sco) $this->db->where('site_code', $sco); if ($search_ca_code) { $code_exp = explode('.', $search_ca_code); if (!isset($code_exp[1])) $limit_code = $search_ca_code + 1; else { $code_ori = substr($code_exp[1], 0, -3); $code_num = substr($code_exp[1], -3) + 1; $code_plus = repeater('0', 3-strlen($code_num)).$code_num; $limit_code = $code_exp[0].'.'.$code_ori.$code_plus; } $this->db->where(array( 'ca_code >=' => (float)$search_ca_code, 'ca_code <' => (float)$limit_code )); } if (!$search_field || !$search_text) return false; // 검색어를 구분자로 나눈다. 여기서는 공백 $s = array(); $s = explode(' ', $search_text); // 검색필드를 구분자로 나눈다. $tmp = array(); $tmp = explode('-', trim($search_field)); $field = explode('.', $tmp[0]); $opt = ''; $where = ' ( '; foreach ($s as $sval) { // 검색어 $search_str = trim($sval); if ($search_str == '') continue; $opt2 = ''; $where .= $opt.'('; foreach ($field as $fval) { // 필드의 수만큼 다중 필드 검색 (필드1+필드2...) $where .= $opt2; switch ($fval) { case 'mb_id' : case 'wr_name' : $where .= $this->db->protect_identifiers($fval).' = '.$this->db->escape($search_str); break; // LIKE 보다 INSTR 속도가 빠름 (누가 그래?) default : if (preg_match('/[a-zA-Z]/', $search_str)) $where .= 'INSTR(LOWER('.$this->db->protect_identifiers($fval).'), LOWER('.$this->db->escape($search_str).'))'; else $where .= 'INSTR('.$this->db->protect_identifiers($fval).', '.$this->db->escape($search_str).')'; break; } $opt2 = ' or '; } $where .= ')'; $opt = ' and '; } $where .= ' ) '; $this->db->where($where, null, false); if (isset($tmp[1])) $this->db->where('wr_is_comment', '0'); if ($spt) $this->db->where("wr_num between '".$spt."' and '".($spt + 10000)."'"); } // 원본글 작성자 인가. 비밀글시. function is_owner($table_name, $wr_num) { $this->db->select('mb_id'); $qry = $this->db->get_where(TBL_WRITE.'_'.$table_name, array( 'wr_is_comment' => '0', 'wr_num' => $wr_num, 'wr_reply' => '' )); return $qry->row_array(); } // 조회수 증가 function hit_update($table_name, $wr_id) { $this->db->set('wr_hit', 'wr_hit + 1', false); $this->db->update(TBL_WRITE.'_'.$table_name, null, array('wr_id' => $wr_id)); } // 다음, 이전 글 링크 function prev_next_link($table_name, $wr_num, $wr_reply, $sca, $sfl, $stx) { $this->db->start_cache(); // S $this->db->select('wr_id, wr_subject'); $this->db->where(array('wr_is_comment' => '0')); $this->db->stop_cache(); // E // 이전글 답변글 $this->db->where(array('wr_num' => $wr_num, 'wr_reply <' => $wr_reply)); $this->_get_search_cache($sca, $sfl, $stx); $this->db->order_by('wr_num desc, wr_reply desc'); $qry = $this->db->get(TBL_WRITE.'_'.$table_name, 1); $prev = $qry->row_array(); // 이전 답변글이 없다면 이전글 if (!isset($prev['wr_id'])) { $this->db->where('wr_num <', $wr_num); $this->_get_search_cache($sca, $sfl, $stx); $this->db->order_by('wr_num desc, wr_reply desc'); $qry = $this->db->get(TBL_WRITE.'_'.$table_name, 1); $prev = $qry->row_array(); } // 다음글 답변글 $this->db->where(array('wr_num' => $wr_num, 'wr_reply >' => $wr_reply)); $this->_get_search_cache($sca, $sfl, $stx); $this->db->order_by('wr_num desc, wr_reply desc'); $qry = $this->db->get(TBL_WRITE.'_'.$table_name, 1); $next = $qry->row_array(); // 다음 답변글이 없다면 다음글 if (!isset($next['wr_id'])) { $this->db->where('wr_num >', $wr_num); $this->_get_search_cache($sca, $sfl, $stx); $this->db->order_by('wr_num, wr_reply'); $qry = $this->db->get(TBL_WRITE.'_'.$table_name, 1); $next = $qry->row_array(); } $this->db->flush_cache(); $result['prev'] = ($prev) ? $prev : false; $result['next'] = ($next) ? $next : false; return $result; } // 관련 답변 존재 여부 function is_reply($table_name, $wr_id, $wr_num, $wr_reply) { $len = strlen($wr_reply); $len = ($len < 0) ? 0 : $len; $reply = substr($wr_reply, 0, $len); $this->db->like('wr_reply', $reply, 'after'); $this->db->where(array( 'wr_id <>' => $wr_id, 'wr_num' => $wr_num, 'wr_is_comment' => 0 )); if ($this->db->count_all_results(TBL_WRITE.'_'.$table_name) > 0) return true; return false; } // 관련 코멘트 존재 여부 function is_comment($table_name, $wr_id, $mb_id) { $where = array( 'wr_parent' => $wr_id ); if ($mb_id) $where['mb_id <>'] = $mb_id; $where['wr_is_comment'] = 1; $this->db->where($where); return $this->db->count_all_results(TBL_WRITE.'_'.$table_name); } // 답변 단계 얻기 function get_reply_step($table_name, $wr_num, $bo_reply_order, $wr_reply) { $reply_len = strlen($wr_reply) + 1; if ($bo_reply_order) { $begin_reply_char = 'A'; $end_reply_char = 'Z'; $reply_number = +1; $this->db->select_max(' SUBSTRING(wr_reply, '.$reply_len.', 1) ', 'reply'); } else { $begin_reply_char = 'Z'; $end_reply_char = 'A'; $reply_number = -1; $this->db->select_min(' SUBSTRING(wr_reply, '.$reply_len.', 1) ', 'reply'); } $this->db->where(array( 'wr_num' => $wr_num, 'SUBSTRING(wr_reply, '.$reply_len.', 1) <>' => '' )); if ($wr_reply) $this->db->like('wr_reply', $wr_reply, 'after'); $qry = $this->db->get(TBL_WRITE.'_'.$table_name); $row = $qry->row_array(); if (!isset($row['reply'])) $reply_char = $begin_reply_char; else if ($row['reply'] == $end_reply_char) // A~Z은 26 입니다. alert("더 이상 답변하실 수 없습니다.\\n\\n답변은 26개 까지만 가능합니다."); else $reply_char = chr(ord($row['reply']) + $reply_number); return $wr_reply.$reply_char; } // 코멘트 리스트 function list_comment($table_name, $wr_id) { $this->db->select('wr_id, mb_id, wr_option, wr_content, wr_name, wr_datetime, wr_ip, wr_comment_reply'); $this->db->order_by('wr_comment, wr_comment_reply'); $qry = $this->db->get_where(TBL_WRITE.'_'.$table_name, array( 'wr_parent' => $wr_id, 'wr_is_comment' => '1' )); return $qry->result_array(); } // 동일내용 연속 등록 불가 function same_write($table_name) { $this->db->select('MD5(CONCAT(wr_ip, wr_subject, wr_content)) as prev_md5', false); $this->db->order_by('wr_id', 'desc'); $qry = $this->db->get_where(TBL_WRITE.'_'.$table_name, array('wr_is_comment' => '0'), 1); return $qry->row_array(); } // 게시판의 최소 wr_num을 얻는다. function get_min_num($table_name) { // 가장 작은 번호를 얻어 $this->db->select_min('wr_num', 'min_wr_num'); $qry = $this->db->get(TBL_WRITE.'_'.$table_name); $row = $qry->row_array(); return $row['min_wr_num']; } function write_insert($table_name, $wr_content, $wr_num, $wr_reply, $mb, $bo_notice) { if ( defined('STORE_CODE') ) $site_code = STORE_CODE; else $site_code = ''; $sql = array( 'table_name' => $table_name, 'wr_num' => $wr_num, 'wr_reply' => $wr_reply, 'wr_comment' => '0', 'site_code' => $site_code, 'ca_code' => ($this->input->post('ca_code')) ? str_replace('-', '.', $this->input->post('ca_code')) : '', 'wr_option' => $this->input->post('editor').','.$this->input->post('secret').','.$this->input->post('mail').','.$this->input->post('nocomt'), 'wr_subject' => $this->input->post('wr_subject'), 'wr_content' => $wr_content, 'wr_hit' => '0', 'mb_id' => $mb['mb_id'], 'wr_password' => $mb['wr_password'], 'wr_name' => $mb['wr_name'], 'wr_email' => $mb['wr_email'], 'wr_datetime' => TIME_YMDHIS, 'wr_last' => TIME_YMDHIS, 'wr_ip' => $this->input->server('REMOTE_ADDR') ); $this->db->insert(TBL_WRITE.'_'.$table_name, $sql); $wr_id = $this->db->insert_id(); // 부모 아이디에 UPDATE $this->db->update(TBL_WRITE.'_'.$table_name, array('wr_parent' => $wr_id), array('wr_id' => $wr_id)); // 게시글 1 증가 $this->db->set('bo_count_write', 'bo_count_write + 1', false); $sql_u = array(); if ($this->input->post('w') == '') { if ($this->input->post('notice')) $sql_u['bo_notice'] = trim($wr_id.','.$bo_notice); $sql_u['xx_bo_min_wr_num'] = $wr_num; } $this->db->update(TBL_BOARD, $sql_u, array('table_name' => $table_name)); return $wr_id; } function write_update($table_name, $wr_content, $wr_id, $mb, $bo_notice) { $ca_code = ($this->input->post('ca_code')) ? str_replace('-', '.', $this->input->post('ca_code')) : ''; $sql = array( 'ca_code' => $ca_code, 'wr_option' => $this->input->post('editor').','.$this->input->post('secret').','.$this->input->post('mail').','.$this->input->post('nocomt'), 'wr_subject' => $this->input->post('wr_subject'), 'wr_content' => $wr_content, 'mb_id' => $mb['mb_id'], 'wr_name' => $mb['wr_name'], 'wr_email' => $mb['wr_email'] ); if ($this->input->post('wr_password')) { $this->load->library('encrypt'); $sql['wr_password'] = $this->encrypt->encode($this->input->post('wr_password')); } if (!IS_ADMIN) $sql['wr_ip'] = $this->input->server('REMOTE_ADDR'); $this->db->update(TBL_WRITE.'_'.$table_name, $sql, array('wr_id' => $wr_id)); // 분류가 수정되는 경우 해당되는 코멘트의 분류명도 모두 수정함 // 코멘트의 분류를 수정하지 않으면 검색이 제대로 되지 않는다. if ($ca_code) $this->db->update(TBL_WRITE.'_'.$table_name, array('ca_code' => $ca_code), array('wr_parent' => $wr_id)); // 공지사항 if (IS_ADMIN) $this->db->update(TBL_BOARD, array('bo_notice' => trim($bo_notice)), array('table_name' => $table_name)); } function write_delete($table_name, $wr_ids, $bo_notice, $bo_min_wr_num) { if (!$wr_ids) return false; // 게시물 파일 삭제 $this->db->select('bf_file'); $this->db->where('table_name', $table_name); $this->db->where_in('wr_id', $wr_ids); $qry = $this->db->get(TBL_BOARD_FILE); $result = $qry->result_array(); foreach ($result as $row) { @unlink(DATA_PATH.'/file/'.$table_name.'/'.$row['bf_file']); } // 파일테이블 삭제 $this->db->where('table_name', $table_name); $this->db->where_in('wr_id', $wr_ids); $this->db->delete(array(TBL_BOARD_FILE,TBL_BOARD_EXTRA)); // 게시물/코멘트 삭제 $this->db->where_in('wr_parent', $wr_ids); $this->db->delete(TBL_WRITE.'_'.$table_name); $count_write = count($wr_ids); $count_comment = $this->db->affected_rows() - $count_write; // 게시물 삭제개수에서 원글을 빼면 코멘트 개수 // 글숫자 감소 if ($count_write > 0 || $count_comment > 0) { $this->db->set('bo_count_write', 'bo_count_write - '.$count_write, false); $this->db->set('bo_count_comment', 'bo_count_comment - '.$count_comment, false); } $sql = array(); if (IS_ADMIN) $sql['bo_notice'] = trim($bo_notice); // min_wr_num 업데이트 $min_wr_num = $this->get_min_num($table_name); if ($min_wr_num != $bo_min_wr_num) $sql['xx_bo_min_wr_num'] = $min_wr_num; $this->db->update(TBL_BOARD, $sql); } function content_update($table_name, $wr_id, $content) { $this->db->update(TBL_WRITE.'_'.$table_name, array('wr_content' => $content), array('wr_id' => $wr_id)); } function get_extra($table_name, $wr_id, $bo_extra) { $select = 'ex_'.implode(', ex_', $bo_extra); $where['table_name'] = $table_name; if (is_array($wr_id)) { $select = 'wr_id, '.$select; $this->db->where_in('wr_id', $wr_id); } else $where['wr_id'] = $wr_id; $this->db->select($select); $qry = $this->db->get_where(TBL_BOARD_EXTRA, $where); return $qry->result_array(); } function extra_update($w, $table_name, $wr_id, $exSql) { if ($w == '') { $exSql['table_name'] = $table_name; $exSql['wr_id'] = $wr_id; $this->db->insert(TBL_BOARD_EXTRA, $exSql); } else { $this->db->update(TBL_BOARD_EXTRA, $exSql, array( 'table_name' => $table_name, 'wr_id' => $wr_id )); } } } ?>