index.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. class index {
  4. function __construct() {
  5. pc_base::load_app_func('global');
  6. $this->vote = pc_base::load_model('vote_subject_model');//投票标题
  7. $this->vote_option = pc_base::load_model('vote_option_model');//投票选项
  8. $this->vote_data = pc_base::load_model('vote_data_model'); //投票统计的数据模型
  9. $this->username = param::get_cookie('_username');
  10. $this->userid = param::get_cookie('_userid');
  11. $this->groupid = param::get_cookie('_groupid');
  12. $this->ip = ip();
  13. $siteid = isset($_GET['siteid']) ? intval($_GET['siteid']) : get_siteid();
  14. define("SITEID",$siteid);
  15. }
  16. public function init() {
  17. $siteid = SITEID;
  18. $page = intval($_GET['page']);
  19. if($page<=0){
  20. $page = 1;
  21. }
  22. include template('vote', 'list_new');
  23. }
  24. /**
  25. * 投票列表页
  26. */
  27. public function lists() {
  28. $siteid = SITEID;
  29. $page = intval($_GET['page']);
  30. if($page<=0){
  31. $page = 1;
  32. }
  33. include template('vote', 'list_new');
  34. }
  35. /**
  36. * 投票显示页
  37. */
  38. public function show(){
  39. $type = intval($_GET['type']);//调用方式ID
  40. $subjectid = abs(intval($_GET['subjectid']));
  41. if(!$subjectid) showmessage(L('vote_novote'),'blank');
  42. //取出投票标题
  43. $subject_arr = $this->vote->get_subject($subjectid);
  44. $siteid = $subject_arr['siteid'];
  45. //增加判断,防止模板调用不存在投票时js报错 wangtiecheng
  46. if(!is_array($subject_arr)) {
  47. if(isset($_GET['action']) && $_GET['action'] == 'js') {
  48. exit;
  49. } else {
  50. showmessage(L('vote_novote'),'blank');
  51. }
  52. }
  53. extract($subject_arr);
  54. //显示模版
  55. $template = $template ? $template: 'vote_tp';
  56. //获取投票选项
  57. $options = $this->vote_option->get_options($subjectid);
  58. //新建一数组用来存新组合数据
  59. $total = 0;
  60. $vote_data =array();
  61. $vote_data['total'] = 0 ;//所有投票选项总数
  62. $vote_data['votes'] = 0 ;//投票人数
  63. //获取投票结果信息
  64. $infos = $this->vote_data->select(array('subjectid'=>$subjectid),'data');
  65. //循环每个会员的投票记录
  66. foreach($infos as $subjectid_arr) {
  67. extract($subjectid_arr);
  68. $arr = string2array($data);
  69. foreach($arr as $key => $values){
  70. $vote_data[$key]+=1;
  71. }
  72. $total += array_sum($arr);
  73. $vote_data['votes']++ ;
  74. }
  75. $vote_data['total'] = $total ;
  76. //取出投票时间,如果当前时间不在投票时间范围内,则选项变灰不可选
  77. if(date("Y-m-d",SYS_TIME)>$todate || date("Y-m-d",SYS_TIME)<$fromdate){
  78. $check_status = 'disabled';
  79. $display = 'display:none;';
  80. }else {
  81. $check_status = '';
  82. }
  83. //JS调用
  84. if($_GET['action']=='js'){
  85. if(!function_exists('ob_gzhandler')) ob_clean();
  86. ob_start();
  87. //$template = 'submit';
  88. $template = $subject_arr['template'];
  89. //根据TYPE值,判断调用模版
  90. switch ($type){
  91. case 3://首页、栏目页调用
  92. $true_template = 'vote_tp_3';
  93. break;
  94. case 2://内容页调用
  95. $true_template = 'vote_tp_2';
  96. break;
  97. default:
  98. $true_template = $template;
  99. }
  100. include template('vote',$true_template);
  101. $data=ob_get_contents();
  102. ob_clean();
  103. exit(format_js($data));
  104. }
  105. //SEO设置
  106. $SEO = seo(SITEID, '', $subject, $description, $subject);
  107. //前台投票列表调用默认页面,以免页面样式错乱.
  108. if($_GET['show_type']==1){
  109. include template('vote', 'vote_tp');
  110. }else {
  111. include template('vote', $template);
  112. }
  113. }
  114. /**
  115. * 处理投票
  116. */
  117. public function post(){
  118. $subjectid = intval($_POST['subjectid']);
  119. if(!$subjectid) showmessage(L('vote_novote'),'blank');
  120. //当前站点
  121. $siteid = SITEID;
  122. //判断是否已投过票,或者尚未到第二次投票期
  123. $return = $this->check($subjectid);
  124. switch ($return) {
  125. case 0:
  126. showmessage(L('vote_voteyes'),"?m=vote&c=index&a=result&subjectid=$subjectid&siteid=$siteid");
  127. break;
  128. case -1:
  129. showmessage(L('vote_voteyes'),"?m=vote&c=index&a=result&subjectid=$subjectid&siteid=$siteid");
  130. break;
  131. }
  132. if(!is_array($_POST['radio'])) showmessage(L('vote_nooption'),'blank');
  133. $time = SYS_TIME;
  134. $data_arr = array();
  135. foreach($_POST['radio'] as $radio){
  136. $radio = intval($radio);
  137. $data_arr[$radio]='1';
  138. }
  139. $new_data = array2string($data_arr);//转成字符串存入数据库中
  140. //添加到数据库
  141. $this->vote_data->insert(array('userid'=>$this->userid,'username'=>$this->username,'subjectid'=>$subjectid,'time'=>$time,'ip'=>$this->ip,'data'=>$new_data));
  142. //查询投票奖励点数,并更新会员点数
  143. $vote_arr = $this->vote->get_one(array('subjectid'=>$subjectid));
  144. pc_base::load_app_class('receipts','pay',0);
  145. receipts::point($vote_arr['credit'],$this->userid, $this->username, '','selfincome',L('vote_post_point'));
  146. //更新投票人数
  147. $this->vote->update(array('votenumber'=>'+=1'),array('subjectid'=>$subjectid));
  148. showmessage(L('vote_votesucceed'), "?m=vote&c=index&a=result&subjectid=$subjectid&siteid=$siteid");
  149. }
  150. /**
  151. *
  152. * 投票结果显示
  153. */
  154. public function result(){
  155. $siteid = SITEID;
  156. $subjectid = abs(intval($_GET['subjectid']));
  157. if(!$subjectid) showmessage(L('vote_novote'),'blank');
  158. //取出投票标题
  159. $subject_arr = $this->vote->get_subject($subjectid);
  160. if(!is_array($subject_arr)) showmessage(L('vote_novote'),'blank');
  161. extract($subject_arr);
  162. //获取投票选项
  163. $options = $this->vote_option->get_options($subjectid);
  164. //新建一数组用来存新组合数据
  165. $total = 0;
  166. $vote_data =array();
  167. $vote_data['total'] = 0 ;//所有投票选项总数
  168. $vote_data['votes'] = 0 ;//投票人数
  169. //获取投票结果信息
  170. $infos = $this->vote_data->select(array('subjectid'=>$subjectid),'data');
  171. //循环每个会员的投票记录
  172. foreach($infos as $subjectid_arr) {
  173. extract($subjectid_arr);
  174. $arr = string2array($data);
  175. foreach($arr as $key => $values){
  176. $vote_data[$key]+=1;
  177. }
  178. $total += array_sum($arr);
  179. $vote_data['votes']++ ;
  180. }
  181. $vote_data['total'] = $total ;
  182. //SEO设置
  183. $SEO = seo(SITEID, '', $subject, $description, $subject);
  184. include template('vote','vote_result');
  185. }
  186. /**
  187. *
  188. * 投票前检测
  189. * @param $subjectid 投票ID
  190. * @return 返回值 (1:可投票 0: 多投,时间段内不可投票 -1:单投,已投票,不可重复投票)
  191. */
  192. public function check($subjectid){
  193. //查询本投票配置
  194. $siteid = SITEID;
  195. $subject_arr = $this->vote->get_subject($subjectid);
  196. if($subject_arr['enabled']==0){
  197. showmessage(L('vote_votelocked'),"?m=vote&c=index&a=result&subjectid=$subjectid&siteid=$siteid");
  198. }
  199. if(date("Y-m-d",SYS_TIME)>$subject_arr['todate']){
  200. showmessage(L('vote_votepassed'),"?m=vote&c=index&a=result&subjectid=$subjectid&siteid=$siteid");
  201. }
  202. //游客是否可以投票
  203. if($subject_arr['allowguest']==0 ){
  204. if(!$this->username){
  205. showmessage(L('vote_votenoguest'),"?m=vote&c=index&a=result&subjectid=$subjectid&siteid=$siteid");
  206. }elseif($this->groupid == '7'){
  207. showmessage('对不起,不允许邮件待验证用户投票!',"?m=vote&c=index&a=result&subjectid=$subjectid&siteid=$siteid");
  208. }
  209. }
  210. //是否有投票记录
  211. $user_info = $this->vote_data->select(array('subjectid'=>$subjectid,'ip'=>$this->ip,'username'=>$this->username),'*','1',' time DESC');
  212. if(!$user_info){
  213. return 1;
  214. } else {
  215. if($subject_arr['interval']==0){
  216. return -1;
  217. }
  218. if($subject_arr['interval']>0){
  219. $condition = (SYS_TIME - $user_info[0]['time'])/(24*3600)> $subject_arr['interval'] ? 1 : 0;
  220. return $condition;
  221. }
  222. }
  223. }
  224. }
  225. ?>