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

253 lines
10KB

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