175 lines
5.4 KiB
PHP
175 lines
5.4 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
/**
|
|
* Comment_write
|
|
*/
|
|
|
|
|
|
class Comment_write extends CI_Controller
|
|
{
|
|
|
|
private $CI;
|
|
|
|
function __construct()
|
|
{
|
|
$this->CI = & get_instance();
|
|
|
|
}
|
|
|
|
function main()
|
|
{
|
|
|
|
if ( ! $this->CI->input->post_get('token')) {
|
|
$this->CI->apilib->make_error("토큰 값이 넘어오지 않았습니다");
|
|
}
|
|
$token = $this->CI->input->post_get('token');
|
|
|
|
|
|
$app_id = (int) trim($this->CI->input->post_get('app_id'));
|
|
if (empty($app_id)) {
|
|
$this->CI->apilib->make_error("어플 아이디가 넘어오지 않았습니다");
|
|
}
|
|
|
|
$con_id = (int) trim($this->CI->input->post('con_id'));
|
|
if (empty($con_id)) {
|
|
$this->CI->apilib->make_error("데이터 고유 아이디가 넘어오지 않았습니다");
|
|
}
|
|
$com_id = (int) trim($this->CI->input->post('com_id'));
|
|
|
|
|
|
$this->CI->load->model(array('Contents_model', 'Contents_comment_model'));
|
|
|
|
$sessionuser = $this->CI->User_model->get_by_token($app_id, $token);
|
|
if ( ! element('user_id', $sessionuser)) {
|
|
$this->CI->apilib->make_error("로그인 한 회원만 댓글을 입력할 수 있습니다");
|
|
}
|
|
|
|
$contents = $this->CI->Contents_model->get_one($con_id);
|
|
if ( ! element('con_id', $contents)) {
|
|
$this->CI->apilib->make_error("존재하지 않는 컨텐츠입니다.");
|
|
}
|
|
|
|
$reply_com_id = (int) trim($this->CI->input->post('reply_com_id'));
|
|
$com_content = trim($this->CI->input->post('com_content'));
|
|
|
|
if (empty($com_content)) {
|
|
$this->CI->apilib->make_error("댓글 내용이 입력되지 않았습니다");
|
|
}
|
|
|
|
// mode : new, modify, reply
|
|
$mode = 'new';
|
|
if ($com_id) {
|
|
$mode = 'modify';
|
|
}
|
|
else if ($reply_com_id) {
|
|
$mode = 'reply';
|
|
}
|
|
|
|
$comment = '';
|
|
if ($mode == 'modify') {
|
|
$comment = $this->CI->Contents_comment_model->get_one($com_id);
|
|
if ( ! element('com_id', $comment)) {
|
|
$this->CI->apilib->make_error("존재하지 않는 댓글입니다.");
|
|
}
|
|
if (element('user_id', $comment) != element('user_id', $sessionuser)) {
|
|
$this->CI->apilib->make_error("본인의 댓글만 수정이 가능합니다.");
|
|
}
|
|
}
|
|
|
|
$com_reply = '';
|
|
if ($mode == 'reply') {
|
|
|
|
$origin = $this->CI->Contents_comment_model->get_one($reply_com_id);
|
|
|
|
if ( ! element('com_id', $origin)) {
|
|
$this->CI->apilib->make_error("존재하지 않는 댓글에 답변을 달려고 시도하고 있습니다");
|
|
}
|
|
|
|
//if (element('com_reply', $origin)) {
|
|
// $this->CI->apilib->make_error("답변댓글에는 더 이상 답변댓글을 입력하실 수가 없습니다");
|
|
//}
|
|
|
|
$reply_len = strlen(element('com_reply', $origin)) + 1;
|
|
$begin_reply_char = 'A';
|
|
$end_reply_char = 'Z';
|
|
$reply_number = +1;
|
|
$this->CI->db->select('MAX(SUBSTRING(com_reply, ' . $reply_len . ', 1)) as reply', false);
|
|
$this->CI->db->where('com_num', element('com_num', $origin));
|
|
$this->CI->db->where('SUBSTRING(com_reply, ' . $reply_len . ', 1) <>', '');
|
|
if (element('com_id', $origin)) {
|
|
$this->CI->db->like('com_reply', element('com_reply', $origin), 'after');
|
|
}
|
|
$result = $this->CI->db->get('contents_comment');
|
|
$row = $result->row_array();
|
|
|
|
if ( ! element('reply', $row)) {
|
|
$reply_char = $begin_reply_char;
|
|
} elseif (element('reply', $row) === $end_reply_char) { // A~Z은 26 입니다.
|
|
$this->CI->apilib->make_error("더 이상 답변하실 수 없습니다.\\n답변은 26개 까지만 가능합니다");
|
|
} else {
|
|
$reply_char = chr(ord(element('reply', $row)) + $reply_number);
|
|
}
|
|
$com_reply = element('com_reply', $origin) . $reply_char;
|
|
|
|
$com_reply_user_id = element('user_id', $origin);
|
|
}
|
|
|
|
if ($mode == 'new') {
|
|
|
|
$com_num = $this->CI->Contents_comment_model->next_comment_num();
|
|
$insertdata = array(
|
|
'con_id' => $con_id,
|
|
'app_id' => $app_id,
|
|
'con_user_id' => element('user_id', $contents),
|
|
'user_id' => element('user_id', $sessionuser),
|
|
'com_num' => $com_num,
|
|
'com_reply' => '',
|
|
'com_content' => $com_content,
|
|
'com_created_at' => cdate('Y-m-d H:i:s'),
|
|
'com_updated_at' => cdate('Y-m-d H:i:s'),
|
|
);
|
|
$com_id = $this->CI->Contents_comment_model->insert($insertdata);
|
|
}
|
|
else if ($mode == 'reply') {
|
|
|
|
$com_num = element('com_num', $origin);
|
|
$insertdata = array(
|
|
'con_id' => $con_id,
|
|
'app_id' => $app_id,
|
|
'con_user_id' => element('user_id', $contents),
|
|
'user_id' => element('user_id', $sessionuser),
|
|
'com_num' => $com_num,
|
|
'com_reply' => $com_reply,
|
|
'com_reply_user_id' => $com_reply_user_id,
|
|
'com_content' => $com_content,
|
|
'com_created_at' => cdate('Y-m-d H:i:s'),
|
|
'com_updated_at' => cdate('Y-m-d H:i:s'),
|
|
);
|
|
$com_id = $this->CI->Contents_comment_model->insert($insertdata);
|
|
|
|
}
|
|
else if ($mode == 'modify') {
|
|
|
|
$updatedata = array(
|
|
'com_content' => $com_content,
|
|
'com_updated_at' => cdate('Y-m-d H:i:s'),
|
|
);
|
|
$this->CI->Contents_comment_model->update($com_id, $updatedata);
|
|
|
|
}
|
|
|
|
$arr = array(
|
|
'result' => 'ok',
|
|
'token' => $token,
|
|
'app_id' => $app_id,
|
|
'con_id' => $con_id,
|
|
'com_id' => $com_id,
|
|
);
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
|
}
|