初步完成数据收藏功能

This commit is contained in:
Li Jianxuan 2013-07-19 10:18:07 +00:00
parent 57990bcd4a
commit a0c8be3a73
2 changed files with 133 additions and 1 deletions

View File

@ -0,0 +1,94 @@
<?php
namespace data;
class Favorite extends Zend_Controller_Plugin_Abstract
{
public $db; //传入PDO对象.
private $auth = NULL; //Zend_Auth 对象
//使用到的公共变量
public $tbl_metadata = "metadata"; //元数据
public $tbl_favorite = "favorite"; //用户收藏表
public $fav_max;
function __construct($db)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
$this->fav_max = empty($this->config->favorite->limit) ? 50 : $this->config->favorite->limit ;
}
function like($uuid,$uid = 0)
{
if(empty($uid))
{
$uid = \view::User('id');
}
if(!is_numeric($uid))
{
$uid = \view::User('id');
}
if(!preg_match("/^[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{12}$/",$uuid))
{
return "参数错误";
}
if($this->checkUserFavNum($uid))
{
if($this->checkAlreadyLike($uuid,$uid))
{
$sql = "INSERT INTO {$this->tbl_favorite} (uid,uuid) VALUES ($uid,'$uuid')";
if($this->db->exec($sql))
{
return true;
}else{
return "收藏失败,请重试";
}
}else{
return "您已经收藏过此数据";
}
}else{
return "您的收藏量已达到系统允许的上限";
}
}
//检查是否达到允许的收藏上限
public function checkUserFavNum($uid)
{
$sql = "SELECT count(id) as c FROM {$this->tbl_favorite} WHERE uid=$uid";
$rs = $this->db->query($sql);
$row = $rs->fetch();
if($row['c'] >= $this->fav_max)
{
return false;
}else{
return true;
}
}
//检查是否已经搜藏过
public function checkAlreadyLike($uuid,$uid)
{
$sql = "SELECT * FROM {$this->tbl_favorite} WHERE uid=$uid AND uuid='$uuid'";
$rs = $this->db->query($sql);
$row = $rs->fetch();
if(isset($row['uuid']) && !empty($row['uuid']))
{
return false;
}else{
return true;
}
}
}

View File

@ -1,5 +1,43 @@
var method = {};
//收藏数据
method.like = function(uuid,btn){
text = $(btn).text();
current_event = $(btn).attr('onclick');
$.ajax({
type:"GET",
url:"/data/like/",
data:'uuid=' + uuid,
success:function(data){
if (typeof(data)=='object')
{
if(typeof(data.error) !== 'undefined')
{
Alert(data.error);return false;
}else{
Alert("收藏成功!");
}
}
else{
Alert('出现错误,请稍后再试');
}
},
beforeSend:function(){
$(btn).removeAttr('onclick');
$(btn).html('<img src="/images/ajax-load-small.gif" />');
},
timeout: 15000,
error: function(){
Alert('处理中出现错误,请刷新页面后重试');
},
complete:function(){
$(btn).attr('onclick',current_event);
$(btn).html(text);
}
});
}
//文件列表
method.filelist = {
get : function(uuid){
html ='<div id="window-outter">'