Created
June 13, 2011 15:20
-
-
Save summic/1022971 to your computer and use it in GitHub Desktop.
commentModel
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 commentModel{ | |
function __construct(){ | |
global $db,$user,$IN; | |
$this->db = $db; | |
$this->user = $user; | |
$this->input = $IN; | |
$error_msg = ''; | |
} | |
//添加评论的时候,全局应该有spammer的数据库,并可以学习,被加入到spammer次数达到一定阀值,就认为是垃圾。 | |
public function add($id,$type,$uid){ | |
$data = array( | |
'uid' => $uid, | |
'id' => $id, | |
'type' => $type, | |
'author_id' => $this->user->uid, | |
'author_name' => $this->user->uid ? $this->user->session['username'] : $this->input->post('author'), | |
'author_email' => $this->user->uid ? $user->session['email'] : $this->input->post('email'), | |
'author_url' => $this->user->uid ? $user->session['domain'] : $this->input->post('url'), | |
'author_ip' => $this->input->ip_address(), | |
'status' => $this->user->uid ? 1 : 1, //TODO 非站内用户的评论需审核 | |
'dateline' => time(), | |
'comment' => nl2br($this->input->post('comment')), | |
); | |
//未登录、无名 | |
if(!$this->user->uid && empty($data['author_name'])){ | |
$this->set_error('name','好汉留名!'); | |
} | |
//未登录,无email | |
if(!$this->user->uid && empty($data['author_email'])){ | |
$this->set_error('email','邮件不写怎么联系你呢?'); | |
} | |
//无内容 | |
if(empty($data['comment'])){ | |
$this->set_error('comment','什么也不写就提交?那是无效的!'); | |
} | |
//禁止包含链接 | |
if(strpos($data['comment'],'http://')) { | |
$this->set_error('comment','评论中不允许包含链接!'); | |
} | |
//如果有错退出 | |
if($this->error_msg) { | |
//给出临时数据 | |
$this->setTemp($_POST); | |
return false; | |
} | |
//检查数据库中是否已存在相同数据,防止重复提交 | |
$query = $this->db->query("SELECT cid FROM ".tname('comment')." WHERE id ='".$data['id']."' AND author_ip='".$data['author_ip']."' AND comment = '".$data['comment']."'"); | |
//写入cookie | |
setcookie('author', $data['author_name'], 2145916800, '/'); | |
setcookie('email', $data['author_email'], 2145916800, '/'); | |
setcookie('url', $data['author_url'], 2145916800, '/'); | |
if($this->db->num_rows($query)) { | |
$this->set_error('say','相同的评论您已经提交过一次了!'); | |
return false; | |
}else{ | |
$cid = inserttable('comment', $data, 1); | |
if($data['status'] == 1) { | |
$this->syncCommentsNum($id,$type); | |
}else{ | |
$this->set_error('say','您的评论需要审核后才能显示!'); | |
} | |
return $cid; | |
} | |
return false; | |
} | |
public function edit($blog_id){ | |
$blogData = array(); | |
$blogData['user_id'] = $this->user->uid; | |
$blogData['subject'] = $this->input->post('subject', 1); | |
$blogData['message'] = nl2br($this->input->post('content', 1)); | |
$blogData['blog_privacy'] = $this->input->post('privacy'); | |
$blogData['blog_commenter'] = $this->input->post('commenter'); | |
$blogData['blog_tags'] = $this->formatTags($this->input->post('tags'), str); | |
$blogData['blog_upddt'] = now(); | |
//检查标题是否为空 | |
if(empty($blogData['subject'])){ | |
$this->set_error('标题必须填写'); | |
return FALSE; | |
} | |
//检查内容是否为空 | |
if(empty($blogData['blog_content'])){ | |
$this->set_error('内容必须填写'); | |
return FALSE; | |
} | |
$query = $this->db->query(" | |
UPDATE ".tname('blog')." | |
SET subject = '" . $blogData['subject'] ."', | |
message = '" . $blogData['message']."', | |
blog_privacy = '" . $blogData['blog_privacy'] ."', | |
blog_commenter = '" . $blogData['blog_commenter'] . "', | |
blog_exclude = '" . $blogData['blog_exclude'] ."', | |
blog_tags = '" . $blogData['blog_tags'] ."', | |
blog_upddt = '" . $blogData['blog_upddt'] ."' | |
WHERE blogid = '".$blogData['blog_id']."' | |
AND uid = '".$blogData['user_id']."' | |
LIMIT 1 | |
"); | |
if($query){ | |
//更新tags是个比较麻烦的问题 | |
//$this->addTags($blogData['blog_tags'], $blog_id); | |
return $blog_id; | |
}else{ | |
$this->set_error('修改日记失败,您没有权限修改这篇日记'); | |
return FALSE; | |
} | |
return FALSE; | |
} | |
public function operation($opt, $id){ | |
switch($opt) { | |
case 'delete': | |
$query = $this->db->query("DELETE FROM ".tname('comment')." WHERE cid = '".$id."' AND uid = '".$this->user->uid."'", 'UNBUFFERED'); | |
break; | |
case 'approve': | |
$query = $this->db->query("UPDATE ".tname('comment')." SET `status` = '1' WHERE cid ='".$id."' AND uid = '".$this->user->uid."' LIMIT 1 ;"); | |
break; | |
case 'unapprove': | |
$query = $this->db->query("UPDATE ".tname('comment')." SET `status` = '0' WHERE cid ='".$id."' AND uid = '".$this->user->uid."' LIMIT 1 ;"); | |
break; | |
case 'spam': | |
$query = $this->db->query("UPDATE ".tname('comment')." SET `status` = '2' WHERE cid ='".$id."' AND uid = '".$this->user->uid."' LIMIT 1 ;"); | |
break; | |
} | |
if($query){ | |
Return TRUE; | |
}else{ | |
$this->set_error('操作失败'); | |
return FALSE; | |
} | |
} | |
//id 目标的id,$type 类型,1=log,2=set,3=pic, $uid = 所属用户id,$perpage 每页数目 | |
function get($id,$type,$uid, $perpage = 30, $targetpage = ''){ | |
//分页处理 | |
$page = max(1, intval($this->input->get('p'))); | |
$offset = ($page - 1) * $perpage; | |
$data = $this->db->fetch_first("SELECT COUNT(*) as num FROM ".tname('comment')." WHERE id ='".$id."' AND type = $type AND status = 1"); | |
$total = $data['num']; | |
unset($data); | |
if($targetpage != 'inline') { | |
self::setPagination($page, $total, $perpage, $targetpage); | |
} | |
//查询并预处理数据 | |
$query = $this->db->query("SELECT * FROM ".tname('comment')." WHERE id ='".$id."' AND type = $type AND status = 1 ORDER BY dateline ASC LIMIT $offset,$perpage"); | |
$comments = array(); | |
while($data = $this->db->fetch_array($query)){ | |
if($data['author_id']){ | |
$data['avatar'] = 'http://www.denglin.com/static/s1/p/1001_n.jpg'; | |
}else{ | |
$data['avatar'] = 'http://www.gravatar.com/avatar/'.md5($data['author_email']).'?s=32&d=monsterid'; | |
} | |
$data['dateline'] = strElapsed($data['dateline']); | |
$comments[] = $data; | |
} | |
return $comments; | |
} | |
//后台读取所有自己收到的的评论 | |
function getAll($uid,$status='all', $perpage = 15, $targetpage = ''){ | |
$cond = "uid ='$uid'"; | |
switch($status) { | |
case 'unapproved': | |
$cond .= " AND status =0"; | |
break; | |
case 'approved': | |
$cond .= " AND status =1"; | |
break; | |
case 'spam': | |
$cond .= " AND status =2"; | |
break; | |
case 'all': | |
$cond .= " AND status !=2"; | |
} | |
//分页处理 | |
$page = max(1, intval($this->input->get('p'))); | |
$offset = ($page - 1) * $perpage; | |
$data = $this->db->fetch_first("SELECT COUNT(*) as num FROM ".tname('comment')." WHERE $cond"); | |
$total = $data['num']; | |
unset($data); | |
if($targetpage != 'inline') { | |
self::setPagination($page, $total, $perpage, $targetpage); | |
} | |
//查询并预处理数据 | |
$query = $this->db->query("SELECT * FROM ".tname('comment')." WHERE $cond ORDER BY dateline DESC LIMIT $offset,$perpage"); | |
$comments = array(); | |
while($data = $this->db->fetch_array($query)){ | |
$data['author_avatar'] = 'http://www.gravatar.com/avatar/'.md5($data['author_email']).'?s=32'; | |
$data['dateline'] = strElapsed($data['dateline']); | |
$comments[] = $data; | |
} | |
return $comments; | |
} | |
public function getMine($uid, $perpage = 30) { | |
$cond = "author_id ='$uid'"; | |
//分页处理 | |
$page = max(1, intval($this->input->get('p'))); | |
$offset = ($page - 1) * $perpage; | |
$data = $this->db->fetch_first("SELECT COUNT(*) as num FROM ".tname('comment')." WHERE $cond"); | |
$total = $data['num']; | |
unset($data); | |
if($targetpage != 'inline') { | |
self::setPagination($page, $total, $perpage, $targetpage); | |
} | |
//查询并预处理数据 | |
$query = $this->db->query("SELECT * FROM ".tname('comment')." WHERE $cond ORDER BY dateline DESC LIMIT $offset,$perpage"); | |
$comments = array(); | |
while($data = $this->db->fetch_array($query)){ | |
$data['author_avatar'] = 'http://www.gravatar.com/avatar/'.md5($data['author_email']).'?s=32'; | |
$data['dateline'] = strElapsed($data['dateline']); | |
$comments[] = $data; | |
} | |
return $comments; | |
} | |
//同步评论数 | |
function syncCommentsNum($id,$type,$op='plus') { | |
switch($type) { | |
case 1: | |
$table = 'blog'; | |
$id_name = 'blogid'; | |
break; | |
case 2: | |
$table = 'album'; | |
$id_name = 'albumid'; | |
break; | |
case 3: | |
$table = 'pic'; | |
$id_name = 'picid'; | |
break; | |
} | |
$do = $op == 'plus' ? '+1':'-1'; | |
$this->db->query("UPDATE LOW_PRIORITY ".tname($table)." SET comments=comments".$do." WHERE $id_name='$id'"); | |
} | |
// | |
private function setPagination($page, $num, $offset,$targetpage) { | |
$this->pagination = '';//清除上次的数据(如果一个页面有两个分页的话) | |
$this->pagination = pagination($page, $num, $offset,$targetpage); | |
} | |
public function getPagination() { | |
return $this->pagination; | |
} | |
// 记录错误信息 | |
private function set_error($name,$msg) | |
{ | |
$this->error_msg[$name] = $msg; | |
} | |
// 输出错误信息 | |
public function display_errors($open = '<span class="error">', $close = '</span>') | |
{ | |
if(empty($this->error_msg)) return; | |
if(!is_array($this->error_msg) && !empty($this->error_msg)){ | |
return $open.$this->error_msg.$close; | |
} | |
foreach ($this->error_msg as $key => $val) | |
{ | |
$str[$key] = $open.$val.$close; | |
} | |
return $str; | |
} | |
private function setTemp($data) { | |
$this->tmpData = $data; | |
} | |
public function getTemp() { | |
return $this->tmpData; | |
} | |
}; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment