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

265 lines
10KB

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