国内流行的内容管理系统(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.

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