dedevote.class.php
10.2 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php if(!defined('DEDEINC')) exit("Request Error!");
/**
* 投票类
*
* @version $Id: dedevote.class.php 1 10:31 2010年7月6日Z tianya $
* @package DedeCMS.Libraries
* @copyright Copyright (c) 2007 - 2010, DesDev, Inc.
* @license http://help.dedecms.com/usersguide/license.html
* @link http://www.dedecms.com
*/
require_once(DEDEINC."/dedetag.class.php");
/**
* 投票类
*
* @package DedeVote
* @subpackage DedeCMS.Libraries
* @link http://www.dedecms.com
*/
class DedeVote
{
var $VoteInfos;
var $VoteNotes;
var $VoteCount;
var $VoteID;
var $dsql;
//php5构造函数
function __construct($aid)
{
$this->dsql = $GLOBALS['dsql'];
$this->VoteInfos = $this->dsql->GetOne("SELECT * FROM `#@__vote` WHERE aid='$aid'");
$this->VoteNotes = Array();
$this->VoteCount = 0;
$this->VoteID = $aid;
if(!is_array($this->VoteInfos))
{
return;
}
$dtp = new DedeTagParse();
$dtp->SetNameSpace("v", "<", ">");
$dtp->LoadSource($this->VoteInfos['votenote']);
if(is_array($dtp->CTags))
{
foreach($dtp->CTags as $ctag)
{
$this->VoteNotes[$ctag->GetAtt('id')]['count'] = $ctag->GetAtt('count');
$this->VoteNotes[$ctag->GetAtt('id')]['name'] = trim($ctag->GetInnerText());
$this->VoteCount++;
}
}
$dtp->Clear();
}
//兼容php4的构造函数
function DedeVote($aid)
{
$this->__construct($aid);
}
function Close()
{
}
/**
* 获得投票项目总投票次数
*
* @access public
* @return int
*/
function GetTotalCount()
{
if(!empty($this->VoteInfos["totalcount"]))
{
return $this->VoteInfos["totalcount"];
}
else
{
return 0;
}
}
/**
* 增加指定的投票节点的票数
*
* @access public
* @param int $aid 投票ID
* @return string
*/
function AddVoteCount($aid)
{
if(isset($this->VoteNotes[$aid]))
{
$this->VoteNotes[$aid]['count']++;
}
}
/**
* 获得项目的投票表单
*
* @access public
* @param int $lineheight 行高
* @param string $tablewidth 表格宽度
* @param string $titlebgcolor 标题颜色
* @param string $titlebackgroup 标题背景
* @param string $tablebg 表格背景
* @param string $itembgcolor 项目背景
* @return string
*/
function GetVoteForm($lineheight=30,$tablewidth="100%",$titlebgcolor="#EDEDE2",$titlebackgroup="",$tablebg="#FFFFFF",$itembgcolor="#FFFFFF")
{
//省略参数
if($lineheight=="")
{
$lineheight=24;
}
if($tablewidth=="")
{
$tablewidth="100%";
}
if($titlebgcolor=="")
{
$titlebgcolor="#98C6EF";
}
if($titlebackgroup!="")
{
$titlebackgroup="background='$titlebackgroup'";
}
if($tablebg=="")
{
$tablebg="#FFFFFF";
}
if($itembgcolor=="")
{
$itembgcolor="#FFFFFF";
}
$items = "<table width='$tablewidth' border='0' cellspacing='1' cellpadding='1' id='voteitem'>\r\n";
$items .= "<form name='voteform' method='post' action='".$GLOBALS['cfg_phpurl']."/vote.php' target='_blank'>\r\n";
$items .= "<input type='hidden' name='dopost' value='send' />\r\n";
$items .= "<input type='hidden' name='aid' value='".$this->VoteID."' />\r\n";
$items .= "<input type='hidden' name='ismore' value='".$this->VoteInfos['ismore']."' />\r\n";
$items.="<tr align='center'><td height='$lineheight' id='votetitle' style='border-bottom:1px dashed #999999;color:#3F7652' $titlebackgroup><strong>".$this->VoteInfos['votename']."</strong></td></tr>\r\n";
if($this->VoteCount > 0)
{
foreach($this->VoteNotes as $k=>$arr)
{
if($this->VoteInfos['ismore']==0)
{
$items.="<tr><td height=$lineheight bgcolor=$itembgcolor style='color:#666666'><input type='radio' name='voteitem' value='$k' />".$arr['name']."</td></tr>\r\n";
}
else
{
$items.="<tr><td height=$lineheight bgcolor=$itembgcolor style='color:#666666'><input type=checkbox name='voteitem[]' value='$k' />".$arr['name']."</td></tr>\r\n";
}
}
$items .= "<tr><td height='$lineheight'>\r\n";
$items .= "<input type='submit' class='btn-1' name='vbt1' value='投票' />\r\n";
$items .= "<input type='button' class='btn-1' name='vbt2' ";
$items .= "value='查看结果' onClick=window.open('".$GLOBALS['cfg_phpurl']."/vote.php?dopost=view&aid=".$this->VoteID."'); /></td></tr>\r\n";
}
$items.="</form>\r\n</table>\r\n";
return $items;
}
/**
* 保存投票数据
* 请不要在输出任何内容之前使用SaveVote()方法!
*
* @access public
* @param string $voteitem 投票项目
* @return string
*/
function SaveVote($voteitem)
{
global $ENV_GOBACK_URL,$file,$memberID,$row,$content;
if(empty($voteitem))
{
return '你没选中任何项目!';
}
$items = '';
//检查投票是否已过期
$nowtime = time();
if($nowtime > $this->VoteInfos['endtime'])
{
ShowMsg('投票已经过期!',$ENV_GOBACK_URL);
exit();
}
if($nowtime < $this->VoteInfos['starttime'])
{
ShowMsg('投票还没有开始!',$ENV_GOBACK_URL);
exit();
}
//检测游客是否已投过票
if(isset($_COOKIE['VOTE_MEMBER_IP']))
{
if($_COOKIE['VOTE_MEMBER_IP'] == $_SERVER['REMOTE_ADDR'])
{
ShowMsg('您已投过票',$ENV_GOBACK_URL);
exit();
} else {
setcookie('VOTE_MEMBER_IP',$_SERVER['REMOTE_ADDR'],time()*$row['spec']*3600,'/');
}
} else {
setcookie('VOTE_MEMBER_IP',$_SERVER['REMOTE_ADDR'],time()*$row['spec']*3600,'/');
}
//检查用户是否已投过票
$nowtime = time();
$VoteMem = $this->dsql->GetOne("SELECT * FROM #@__vote_member WHERE voteid = '$this->VoteID' and userid='$memberID'");
if(!empty($memberID))
{
if(isset($VoteMem['id']))
{
$voteday = date("Y-m-d",$VoteMem['uptime']);
$day = strtotime("-".$row['spec']." day");
$day = date("Y-m-d",$day);
if($day < $voteday)
{
ShowMsg('在'.$row['spec'].'天内不能重复投票',$ENV_GOBACK_URL);
exit();
}else{
$query = "UPDATE #@__vote_member SET uptime='$nowtime' WHERE voteid='$this->VoteID' AND userid='$memberID'";
if($this->dsql->ExecuteNoneQuery($query) == false)
{
ShowMsg('插入数据过程中出现错误',$ENV_GOBACK_URL);
exit();
}
}
}else{
$query = "INSERT INTO #@__vote_member(id,voteid,userid,uptime) VALUES('','$this->VoteID','$memberID','$nowtime')";
if($this->dsql->ExecuteNoneQuery($query) == false)
{
ShowMsg('插入数据过程中出现错误',$ENV_GOBACK_URL);
exit();
}
}
}
//必须存在投票项目
if($this->VoteCount > 0)
{
foreach($this->VoteNotes as $k=>$v)
{
if($this->VoteInfos['ismore']==0)
{
//单选项
if($voteitem == $k)
{
$this->VoteNotes[$k]['count']++; break;
}
}
else
{
//多选项
if(is_array($voteitem) && in_array($k,$voteitem))
{
$this->VoteNotes[$k]['count']++;
}
}
}
foreach($this->VoteNotes as $k=>$arr)
{
$items .= "<v:note id='$k' count='".$arr['count']."'>".$arr['name']."</v:note>\r\n";
}
}
$this->dsql->ExecuteNoneQuery("UPDATE `#@__vote` SET totalcount='".($this->VoteInfos['totalcount']+1)."',votenote='".addslashes($items)."' WHERE aid='".$this->VoteID."'");
return "投票成功!";
}
/**
* 获得项目的投票结果
*
* @access public
* @param string $tablewidth 表格宽度
* @param string $lineheight 行高
* @param string $tablesplit 表格分隔
* @return string
*/
function GetVoteResult($tablewidth="600", $lineheight="24", $tablesplit="40%")
{
$totalcount = $this->VoteInfos['totalcount'];
if($totalcount==0)
{
$totalcount=1;
}
$res = "<table width='$tablewidth' border='0' cellspacing='1' cellpadding='1'>\r\n";
$res .= "<tr height='8'><td width='$tablesplit'></td><td></td></tr>\r\n";
$i=1;
foreach($this->VoteNotes as $k=>$arr)
{
$res .= "<tr height='$lineheight'><td style='border-bottom:1px solid'>".$i."、".$arr['name']."</td>";
$c = $arr['count'];
$res .= "<td style='border-bottom:1px solid'>
<table border='0' cellspacing='0' cellpadding='2' width='".(($c/$totalcount)*100)."%'><tr><td height='16' background='img/votebg.gif' style='border:1px solid #666666;font-size:9pt;line-height:110%'>".$arr['count']."</td></tr></table>
</td></tr>\r\n";
$i++;
}
$res .= "<tr><td></td><td></td></tr>\r\n";
$res .= "</table>\r\n";
return $res;
}
}//End Class