Created
December 13, 2012 09:47
-
-
Save kevan/4275358 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Comment | |
{ | |
public static function gets($node_id) | |
{ | |
$comments = Db::select() | |
->from('comments') | |
->where('node_id', '=', $node_id) | |
->and_where('status', '=', 1) | |
->order_by(Db::expr('SUBSTRING(thread, 1, (LENGTH(thread) - 1))'), 'asc') | |
->execute() | |
->as_array() | |
; | |
return $comments; | |
} | |
public static function add($node_id) | |
{ | |
$user = user::get(); | |
$data = array('content' => $_POST['content']); | |
if (empty($_POST['parent_id'])) | |
{ | |
$data['thread'] = self::max_thread($node_id); | |
$data['parent_id'] = 0; | |
} | |
else | |
{ | |
$data['thread'] = self::max_sub_thread($_POST['parent_id']); | |
$data['parent_id'] = $_POST['parent_id']; | |
} | |
$data['node_id'] = $node_id; | |
$data['node_type'] = $_POST['node_type']; | |
$data['user_id'] = $user['id']; | |
$data['nickname'] = $user['nickname']; | |
$data['username'] = $user['username']; | |
$data['created'] = time(); | |
$data['status'] = 1; | |
list($cmt_id,) = Db::insert('comments', array_keys($data)) | |
->values(array_values($data)) | |
->execute() | |
; | |
Db::update('nodes') | |
->set(array('cmt_num' => Db::expr('cmt_num + 1'))) | |
->where('id', '=', $node_id) | |
->execute() | |
; | |
feed::comment($data+array('id' => $cmt_id), node::get($node_id)); | |
return $cmt_id; | |
} | |
public static function delete($comment) | |
{ | |
$expr = '(SUBSTRING(thread, 1, '.(strlen($comment['thread'])-1).') = "'.substr($comment['thread'], 0, -1).'" OR id = '.$comment['id'].')'; | |
$affcted = Db::update('comments') | |
->set(array('status' => 0)) | |
->where('node_id', '=', $comment['node_id']) | |
->and_where(Db::expr($expr), '', Db::expr('')) | |
->execute() | |
; | |
Db::update('nodes') | |
->set(array('cmt_num' => Db::expr('cmt_num - '.$affcted))) | |
->where('id', '=', $comment['node_id']) | |
->execute() | |
; | |
} | |
public static function max_thread($node_id) | |
{ | |
$comment = Db::select('thread') | |
->from('comments') | |
->where('node_id', '=', $node_id) | |
->order_by('thread', 'desc') | |
->execute() | |
->current() | |
; | |
if (empty($comment['thread'])) | |
return '01/'; | |
else | |
return self::add_thd($comment['thread']); | |
} | |
public static function max_sub_thread($parent_id) | |
{ | |
$thread = Db::select('thread') | |
->from('comments') | |
->where('parent_id', '=', $parent_id) | |
->order_by(Db::expr('SUBSTRING(thread, 1, (LENGTH(thread) - 1))'), 'desc') | |
->execute() | |
->get('thread') | |
; | |
if (empty($thread)) | |
{ | |
$thread = Db::select('thread') | |
->from('comments') | |
->where('id', '=', $parent_id) | |
->execute() | |
->get('thread') | |
; | |
return substr($thread, 0, -1).'.01/'; | |
} | |
return self::add_thd($thread); | |
} | |
public static function add_thd($thread) | |
{ | |
$prefix = ''; | |
$num = substr($thread, 0, -1); | |
if (strpos($thread, '.') !== FALSE) | |
{ | |
$prefix = substr($thread, 0, -3); | |
$num = substr($thread, -3, 3); | |
} | |
//48-57 -> 0-9 | |
$num1 = ord($num[1]); | |
if ($num1 < 57) | |
return $prefix.$num[0].chr($num1+1).'/'; | |
if ($num1 == 57) | |
return $prefix.$num[0].'a'.'/'; | |
//97-122 -> a-z | |
if ($num1 < 122) | |
return $prefix.$num[0].chr($num1+1).'/'; | |
if ($num1 == 122) | |
{ | |
$num0 = ord($num[0]); | |
if ($num0 < 57) | |
return $prefix.chr($num0+1).'0/'; | |
if ($num0 == 57) | |
return $prefix.'a0/'; | |
if ($num0 < 122) | |
return $prefix.chr($num0+1).'0/'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment