<?php
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
* PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：admin_poll.php
 * 摘　　要：投票
 * 适用范围：PHPWMS 1.0
 * 版　　本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS'))exit('access die');
if (!getAdminPri('8106'))exit(NO_AUTH_LOGIN);
$do = trim($_GET['do']);
$tpl->assign('do', $do);
$post = $_POST;
$url = '?action=poll&do=list';
switch($do){
	case 'poll_add':
	case 'poll_edit':
		$data['poll_title'] = $post['poll_name'];
		if(empty($data['poll_title'])){
			$sp->bgmsg('评论的标题不能为空', $url);
		}
		$data['poll_desc'] = $post['poll_desc'];
		$data['poll_type'] = $post['poll_type'];
		if($do == 'edit'){
			$id = abs(intval($post['id']));
			$msg = '修改成功';
			$db->update($data, 'user_poll', array('poll_id', $id));
		}else{
			$msg = '添加成功';
			$db->insert($data,'user_poll');
		}
		$sp->bgmsg($msg, $url);
		break;
	case 'poll_delete':
		$id = abs(intval($_GET['id']));
		if(empty($id)){
			$sp->bgmsg('请您正确选择您要删除的项', $url);
		}
		$sql = "DELETE FROM ##__user_option WHERE `option_poll_id` = '$id'";
		$db->query($sql);
		$sql = "DELETE FROM ##__user_poll WHERE `poll_id` = '$id'";
		$db->query($sql);
		$sp->bgmsg('删除成功', $url);
		break;
	case 'detail':
		$id = abs(intval($_GET['id']));
		if(empty($id)){
			$sp->bgmsg('请您正确选择投票详情', $url);
		}
		$sql = "SELECT poll_title, poll_type FROM ##__user_poll WHERE poll_id = '$id'";
		$data = $db->fetch($sql, true);
		if(empty($data['poll_title'])){
			$sp -> bgmsg('没有此投票内容', $url);
		}
		$result = get_poll($id, $data['poll_title']);
		$result['poll_type'] = $data['poll_type'];
		$result['poll_id'] = $id;
		$tpl->assign('result', $result);
		$pn = '投票详情';
		break;
	case 'option_add':
	case 'option_edit':
		$poll_id = abs(intval($post['poll_id']));
		if(empty($poll_id)){
			$sp -> bgmsg('操作有误', '?action=poll&do=list');
		}
		$url = '?action=poll&do=detail&id=' . $poll_id;
		$data['option_name'] = $post['option_name'];
		if($do == 'option_edit'){
			$option_id = $post['option_id'];
			if(empty($option_id)){
				$sp -> bgmsg('请您选择要修改的选项', '?action=poll&do=list');
			}
			$msg = $db->update($data, 'user_option', array('option_id'=>$option_id)) ? '修改成功' : '修改失败';
		}else{
			$data['option_poll_id'] = $poll_id;
			$msg  = $db->insert($data, 'user_option') ?  '添加成功' : '添加失败';
		}
		$sp->bgmsg($msg, $url);
		break;
	case 'option_delete':
		$id = $_REQUEST['id'];
		if(is_array($id)){
			$opid = '';
			$id = array_filter($id);
			foreach ($id as $v){
				$opid .= abs(intval($v)) . ',';
			}
			$opid = substr($opid, 0, -1);
		}else{
			$opid = abs(intval($id));
		}
		if(empty($opid)){
			$sp -> bgmsg('操作有误', '?action=poll&do=list');
		}
		$sql = "DELETE FROM ##__user_option WHERE option_id IN ($opid)";
		$msg = $db->query($sql) ? '删除成功 ' : '删除失败';
		$sp->bgmsg($msg,'?action=poll&do=detail&id=' . $_GET['poll_id']);
		break;
	default:
		$pn = '投票列表';
		$sql = "SELECT COUNT(*) AS count FROM ##__user_poll";
		$count = $db->fetch($sql, true);
		$sql = "SELECT poll_id, poll_title, poll_desc, poll_type, SUM(option_ticket) AS ticke_total, COUNT(option_id) AS `count` FROM ##__user_poll LEFT JOIN ##__user_option ON poll_id = option_poll_id GROUP BY poll_id";
		$data = page_for_db_extend($count['count'], $sql, '?action=poll', 15);
		$tpl->assign('result', $data);
		break;
}
$tpl -> assign("admin_nav",shownav($pn));
$tpl -> display('poll.tpl');
?>