-
Notifications
You must be signed in to change notification settings - Fork 593
/
Action.php
60 lines (54 loc) · 2.17 KB
/
Action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
class Zan_Action extends Typecho_Widget implements Widget_Interface_Do {
protected $db;
public function __construct($request, $response, $params = NULL) {
parent::__construct($request, $response, $params);
$this->db = Typecho_Db::get();
}
public function showZan($cid){
$html = '<a href="javascript:;" class="post-zan" data-cid="';
$html .= $cid;
$html .= '">赞 (<span>';
$html .= self::getZan($cid);
$html .= '</span>)</a>';
echo $html;
}
private function getZan($cid){
$exist = $this->db->fetchRow($this->db->select('int_value')->from('table.fields')->where('cid = ? AND name = ?', $cid, 'zan'));
if (empty($exist)) {
return 0;
}else{
return $exist['int_value'];
}
}
private function addZan($cid){
$exist = $this->db->fetchRow($this->db->select('int_value')->from('table.fields')->where('cid = ? AND name = ?', $cid, 'zan'));
$zan = array('cid'=>$cid, 'name'=>'zan', 'type'=>'int', 'str_value'=>NULL, 'int_value'=>1, 'float_value'=>0);
$result = null;
if (empty($exist)) {
$result = $this->db->query($this->db->insert('table.fields')->rows($zan));
} else {
$zan['int_value'] = $exist['int_value'] + 1;
$result = $this->db->query($this->db->update('table.fields')->rows($zan)->where('cid = ? AND name = ?', $cid, 'zan'));
}
if($result){
$cookie = Typecho_Cookie::get("__zan_cids");
$cids = null;
if($cookie){
$cids = Json::decode($cookie, true);
$cids[$cid] = isset($cids[$cid]) ? $cids[$cid] + 1 : 0;
}else{
$cids = array($cid=>0);
}
Typecho_Cookie::set("__zan_cids", Json::encode($cids));
}
}
public function action() {
//if($this->request->isGet() && $this->request->is('cid')){
if($this->request->isPost() && $this->request->is('cid')){
$this->addZan($this->request->get('cid'));
$this->response->throwJson(array('result' => 1, 'message' => _t('感谢您的赞!')));
}
}
}
?>