国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
11KB

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