<?php
class ChannelAction extends HomeBaseAction {
	private $_data;
	private $_m;
	
	/**
	 * 网站引导页
	 */
	public function welcome(){
		header("Content-Type:text/html; charset=utf-8");
		$this->display();
	}
	
	//频道首页
    public function index(){
        header("Content-Type:text/html; charset=utf-8");
        $this-> _getChannel();
        $id = $this->_data['ChannelID'];
        if( !is_numeric( $id ) )  return;
		
        switch ($this->_data['ChannelModelID']) {
        	case 32:  $this->_single(); break; //单页频道
        	case 33:    //内部转向链接（非网络协议如：http://开头）
        		switch( $id ){
        			case 1: $this->_home(); break;
        			case 2: $this->_home(); break;
        			
        			case 6: $this->_order(); break; //订购中文版
        			case 7: $this->_order(); break;  //订购英文版
        			
        			case 10: $this->_resume(); break; //投简历中文版
        			case 11: $this->_resume(); break;  //投简历英文版
        			
        			case 4: $this->_job(); break;
        			case 8: $this->_job(); break;
        			
        			case 5: $this->_guestbook(); break;  //在线留言英文版
        			case 9: $this->_guestbook(); break;  //在线留言中文版
        		}
        		break;
        	default:  $this->_info(); break;   //Info模型
        }
        $SitePath = $this->_getSitePath( $id, 0,  $this->_data['Parent'], $this->_data['ChannelName'], $this->_data['ChannelUrl']);
        $this->assign('SitePath', $SitePath);
        
        $IndexTemplate = str_ireplace('.html', '', trim($this->_data['IndexTemplate']) );
        $this->display($IndexTemplate);
    }
    
    private function _home(){

    }
    
    private function _info(){
    	$keywords = isset($_REQUEST['keywords']) ? YdInput::checkKeyword( $_REQUEST['keywords'] ) : '';
    	$labelid = isset($_REQUEST['labelid']) ? YdInput::checkCommaNum( $_REQUEST['labelid'] ) : '';
    	$specialid = isset($_REQUEST['specialid']) ? YdInput::checkNum( $_REQUEST['specialid'] ) : 0;
 
    	import("ORG.Util.Page");
    	$m = D('Admin/Info');
    	$Total = $m->getCount( $this->_data['ChannelID'], 1, 1, $keywords, -1, $specialid, $labelid, 1, -1, array('Time'=>1) );  //总记录数
    	$PageSize = $this->_data['PageSize'] > 0 ? $this->_data['PageSize'] : 20 ;
    	$Page = new Page($Total, $PageSize);
    	
    	$Page->parameter = '';
    	if( !empty($keywords) ) {
    		$Page->parameter .= "&keywords=$keywords"; 
    	}
    	if( !empty($labelid) ) {
    		$Page->parameter .= "&labelid=$labelid";
    	}
    	if( !empty($specialid) ) {
    		$Page->parameter .= "&specialid=$specialid";
    	}
    	
    	$Page->rollPage = 10;
    	$ShowPage = $Page->show();
    	$this->assign('NowPage', $Page->getNowPage()); //分页条
    	$this->assign('Page', $ShowPage);   //分页条
    }
    
    private function _single(){
    	
    }
    
    private function _job(){
    	import("ORG.Util.Page");
    	$m = D('Admin/Job');
    	$Total = $m->getCount("IsEnable=1");  //总记录数
    	$PageSize = $this->_data['PageSize'] > 0 ? $this->_data['PageSize'] : 20 ;
    	$Page = new Page($Total, $PageSize);
    	$Page->rollPage = 10;
    	$ShowPage = $Page->show();
    	$this->assign('NowPage', $Page->getNowPage()); //分页条
    	$this->assign('Page', $ShowPage); //分页条
    }
    
    private function _guestbook(){
    	import("ORG.Util.Page");
    	$m = D('Admin/Guestbook');
    	$Total = $m->getCount("IsCheck=1");  //总记录数
    	$PageSize = $this->_data['PageSize'] > 0 ? $this->_data['PageSize'] : 20 ;
    	$Page = new Page($Total, $PageSize);
    	$Page->rollPage = 10;
    	$ShowPage = $Page->show();
    	
    	$this->assign('NowPage', $Page->getNowPage()); //分页条
    	$this->assign('Page', $ShowPage); //分页条
    	
    	$tip = L('ChangeVerifycode');
    	
    	//验证码
    	$GuestbookCode = "<script type='text/javascript'>\n";
    	$GuestbookCode .= "function ChangeGuestbookCode(){\n";
    	$GuestbookCode .= "    var timenow = new Date().getTime();\n";
    	$GuestbookCode .= "    var obj = document.getElementById('GuestbookCode'); \n";
    	$GuestbookCode .= "    if( obj ) obj.src = '".__GROUP__."/public/guestbookCode/'+timenow;\n";
    	$GuestbookCode .= "}\n";
    	$GuestbookCode .= "</script>\n";
    	$GuestbookCode .= '<img  src="'.__GROUP__.'/public/guestbookCode/"  onclick="ChangeGuestbookCode()"';
    	$GuestbookCode .= ' style="cursor:pointer;" id="GuestbookCode"  align="absMiddle"  alt="'.$tip.'"  title="'.$tip.'" />';
    	$this->assign('GuestbookCode', $GuestbookCode); //分页条

    	$this->assign('GuestbookAction', GuestbookAddAction()); 
    }
    
    public function guestbookAdd(){
    	header("Content-Type:text/html; charset=utf-8");
    	foreach ($_POST as $k=>$v){
    		if( is_array($v) ){
    			$_POST[$k] = implode(',', $v);
    		}
    	}
    	$c = YdCache::readGuestbook();
    	$_POST = YdInput::checkTextbox($_POST);
    	
    	//留言权限
    	switch( $c['GUEST_BOOK_ALLOW'] ){
    		case 0:  //禁止留言
    			$this->ajaxReturn(null, L('GuestbookForbidden') , 0);
    			break;
    		case 1: //允许匿名留言
    			break;
    		case 2: //允许会员留言
    			if( !$this->MemberIsLogin() ){
    				$this->ajaxReturn(null, L('GuestbookLevel') , 0);
    			}
    			break;
    	}
    
    	if( $c['GUEST_BOOK_VERIFYCODE'] == 1){ //启用验证码
    		$verifycode = $_POST['verifycode'];
    		if( empty($verifycode) ){
    			session('guestbookcode', rand(1000, 9999) );
    			$this->ajaxReturn(null, L('VerifyCodeRequired') , 0);
    		}
    		if( md5($verifycode) != session('guestbookcode')  ){
    			session('guestbookcode', rand(1000, 9999) );
    			$this->ajaxReturn(null, L('VerifyCodeError')  , 0);
    		}
    	}
    	$m = D('Admin/Guestbook');
    	if( $m->create() ){
    		if( $this->MemberIsLogin() ){
    			$m->GuestID = session('MemberID');
    			$m->GuestName = session('MemberName');
    			$_POST['GuestName'] = $m->GuestName;
    		}else{
    			$m->GuestID = 0;
    		}
    		$m->IsCheck = ($c['GUEST_BOOK_CHECK'] == 1) ? 0 : 1;
    		$MessageTime = date('Y-m-d H:i:s');
    		$m->MessageTime = $MessageTime;
    		$m->GuestIP = get_client_ip();

    		if($m->add()){
    			$success = ($c['GUEST_BOOK_CHECK'] == 1) ? L('GuestbookSuccess') : L('GuestbookSuccessNoCheck');
    			if( $c['GUEST_BOOK_SENDEMAIL'] == 1){
    				//获取留言内容===============================================================
    				$m = D('Admin/Attribute');
    				$Attribute = $m->getAttribute(6);
    				foreach ($Attribute as $a){
    					$name = explode(',', $a['DisplayName']);
    					$value = $_POST[ $a['FieldName'] ];
    					if(  $value != '' ){
    						$emailbody .= $name[0].':'.$value.'<br/>';
    					}
    				}
    				$emailbody .= '时间:'.$MessageTime;
    				$emailtitle = $c['GUEST_BOOK_EMAIL_TITLE'];
    				$emailto = empty($c['GUEST_BOOK_EMAIL']) ? YdCache::readContact('EMAIL') : $c['GUEST_BOOK_EMAIL'];
    				//=======================================================＝===============
    				//提交留言时自动发送电子邮件
    				$b = sendwebmail($emailto, $emailtitle, $emailbody);
    			}
    			$this->ajaxReturn(null, $success , 1);
    		}else{
    			session('guestbookcode', rand(1000, 9999) );
    			$this->ajaxReturn(null, L('GuestbookFail') , 0);
    		}
    	}else{
    		session('guestbookcode', rand(1000, 9999) );
    		$this->ajaxReturn(null, $m->getError() , 0);
    	}
    }
    
    private function _order(){
    	$InfoID = $_GET['infoid'];  //信息ID
    	if( !is_numeric($InfoID) ) {
    		$this->_empty('orderAdd');
    		exit();
    	}
    	
    	import("ORG.Util.Page");
    	$m = D('Admin/Order');
    	$Total = $m->getCount();  //总记录数
    	$PageSize = $this->_data['PageSize'] > 0 ? $this->_data['PageSize'] : 20 ;
    	$Page = new Page($Total, $PageSize);
    	$Page->rollPage = 10;
    	$ShowPage = $Page->show();
    	$this->assign('NowPage', $Page->getNowPage()); //分页条
    	$this->assign('Page', $ShowPage); //分页条
    	 
    	$tip = L('ChangeVerifycode');
    	
    	//验证码
    	$OrderCode = "<script type='text/javascript'>
    	function ChangeOrderCode(){
    	    var timenow = new Date().getTime();
    	    var obj = document.getElementById('OrderCode'); 
    	    if( obj ) obj.src = '__GROUP__/public/orderCode/'+timenow;
    	}
    	</script>
    	<img  src='__GROUP__/public/orderCode/'  onclick='ChangeOrderCode()'
    	style='cursor:pointer;' id='OrderCode'  align='absMiddle'  alt='$tip' title='$tip' />";
    	
    	$this->assign('OrderCode', $OrderCode); 
    
    	$m1 = D('Admin/Info');
    	$InfoTitle = $m1->where("InfoID=$InfoID")->getField('InfoTitle');
    	$this->assign('InfoTitle', $InfoTitle);
    	$this->assign('InfoID', $InfoID);
    	
    	if( session('?MemberID') ){
    		$this->assign('MemberID', session('MemberID'));
    		$this->assign('MemberName', session('MemberName'));
    	}else{
    		$this->assign('MemberID', '0');
    		$this->assign('MemberName', '');
    	}
    	$this->assign('OrderAction', __URL__.'/OrderAdd/l/'.LANG_SET);
    }
    
    public function orderAdd(){
    	header("Content-Type:text/html; charset=utf-8");
    	$c = YdCache::readOrder();
    	//留言权限
    	switch( $c['ORDER_ALLOW'] ){
    		case 0:  //禁止提交订单
    			$this->ajaxReturn(null, L('OrderForbidden') , 0);
    			break;
    		case 1: //允许匿名提交订单
    			break;
    		case 2: //允许会员提交订单
    			if( !$this->MemberIsLogin() ){
    				$this->ajaxReturn(null, L('OrderLevel') , 0);
    			}
    			break;
    	}
  
    	if( $c['ORDER_VERIFYCODE'] == 1){
	    	$verifycode = $_POST['verifycode'];   	
	    	if( empty($verifycode) ){
	    		session('ordercode', rand(1000, 9999) );
	    		$this->ajaxReturn(null, L('VerifyCodeRequired') , 0);
	    	}
	
	    	if( md5($verifycode) != session('ordercode')  ){
	    		session('ordercode', rand(1000, 9999) );
	    		$this->ajaxReturn(null, L('VerifyCodeError')  , 0);
	    	}
    	}
    	
    	$_POST = YdInput::checkTextbox( $_POST ); //xss过滤
    
    	$m = D('Admin/Order');
    	if( $m->create() ){
    		if( $this->MemberIsLogin() ){
    			$m->GuestID = session('MemberID');
    			$m->GuestName = session('MemberName');
    		}else{
    			$m->GuestID = 0;
    		}
    		$OrderTime = date('Y-m-d H:i:s');
    		$m->OrderTime = $OrderTime;
      
    		if($m->add()){
    			if( $c['ORDER_EMAIL'] == 1){ //订单邮件
    				//获取留言内容===============================================================
    				$m = D('Admin/Attribute');
    				$Attribute = $m->getAttribute(26);
    				foreach ($Attribute as $a){
    					$name = explode(',', $a['DisplayName']);
    					$value = $_POST[ $a['FieldName'] ];
    					if(  $value != '' ){
    						$body .= $name[0].':'.$value.'<br/>';
    					}
    				}
    				$body .= '时间:'.$OrderTime;
    				$title = $c['ORDER_EMAIL_TITLE'];
    				$to = empty($c['ORDER_EMAIL_TO']) ? YdCache::readContact('EMAIL') : $c['ORDER_EMAIL_TO'];
    				//=======================================================＝===============
    				$b = sendwebmail($to, $title, $body);
    			}
    			$this->ajaxReturn(null, L('OrderSuccess') , 1);
    		}else{
    			session('ordercode', rand(1000, 9999) );
    			$this->ajaxReturn(null, L('OrderFail') , 0);
    		}
    	}else{
    		session('ordercode', rand(1000, 9999) );
    		$this->ajaxReturn(null, $m->getError() , 0);
    	}
    }
    
    private function _resume(){
    	$JobID = $_GET['jobid'];  //信息ID
    	if( !is_numeric($JobID) ) {
    		$this->_empty('ResumeAdd');
    		exit();
    	}
    	$tip = L('ChangeVerifycode');
    	//验证码
    	$ResumeCode = "<script type='text/javascript'>
    	function ChangeResumeCode(){
    	    var timenow = new Date().getTime();
    	    var obj = document.getElementById('ResumeCode');
    	    if( obj ) obj.src = '__GROUP__/public/ResumeCode/'+timenow;
        }
        </script>
	    <img  src='__GROUP__/public/ResumeCode/'  onclick='ChangeResumeCode()'
	    style='cursor:pointer;' id='ResumeCode'  align='absMiddle'  alt='$tip' title='$tip' />";
    	 
    	$this->assign('ResumeCode', $ResumeCode); 
    	$this->assign('ResumeAction', __URL__.'/ResumeAdd/l/'.LANG_SET);
    	
    	$m1 = D('Admin/Job');
    	$JobName = $m1->where("JobID=$JobID")->getField('JobName');
    	$this->assign('JobName', $JobName);
    	$this->assign('JobID', $JobID);
    	 
    	if( session('?MemberID') ){
    		$this->assign('MemberID', session('MemberID'));
    		$this->assign('MemberName', session('MemberName'));
    	}else{
    		$this->assign('MemberID', '0');
    		$this->assign('MemberName', '');
    	}
    }
    
    public function resumeAdd(){
    	header("Content-Type:text/html; charset=utf-8");
    	$_POST = YdInput::checkTextbox( $_POST ); //xss过滤
    	if( empty( $_POST['GuestName'] ) ){
    		$this->ajaxReturn(null, L('JobPeopleRequired') , 0);
    	}
    	
    	if( empty( $_POST['Telephone'] ) ){
    		$this->ajaxReturn(null, L('TelephoneRequired') , 0);
    	}
    	
    	$verifycode = $_POST['verifycode'];
    	if( empty($verifycode) ){
    		session('resumecode', rand(1000, 9999) );
    		$this->ajaxReturn(null, L('VerifyCodeRequired') , 0);
    	}
    
    	if( md5($verifycode) != session('resumecode')  ){
    		session('resumecode', rand(1000, 9999) );
    		$this->ajaxReturn(null, L('VerifyCodeError')  , 0);
    	}
    	
    	$m = D('Admin/Resume');
    	if( $m->create() ){
    		if( $this->MemberIsLogin() ){
    			$m->GuestID = session('MemberID');
    		}else{
    			$m->GuestID = 0;
    		}
    		$m->Time = date('Y-m-d H:i:s');
    
    		if($m->add()){
    			$this->ajaxReturn(null, L('ResumeSuccess') , 1);
    		}else{
    			session('resumecode', rand(1000, 9999) );
    			$this->ajaxReturn(null, L('ResumeFail') , 0);
    		}
    	}else{
    		session('resumecode', rand(1000, 9999) );
    		$this->ajaxReturn(null, $m->getError() , 0);
    	}
    }
    
    //频道信息检索
    public function search(){
    	header("Content-Type:text/html; charset=utf-8");
    	$ChannelID = isset($_REQUEST['id']) ? YdInput::checkNum( $_REQUEST['id'] ) : 0;
    	$Keywords = isset($_REQUEST['Keywords']) ? YdInput::checkKeyword( $_REQUEST['Keywords'] ) : '';
    	
    	import("ORG.Util.Page");
    	$m = D('Admin/Info');
    	$Total = $m->getCount( $ChannelID, 1, 1, $Keywords);  //总记录数
    	$PageSize = PageSize($ChannelID);
    	$PageSize = ( $PageSize > 0 ) ? $PageSize : 20;
    	$Page = new Page($Total, $PageSize);
    	$Page->parameter = "&Keywords=$Keywords&id=$ChannelID";
    	$Page->rollPage = 10;
    	$ShowPage = $Page->show();
    	
    	$SitePath = $this->_getSitePath( 'search' );
    	$this->assign('SitePath', $SitePath);
    	
    	$this->assign('NowPage', $Page->getNowPage()); //分页条
    	$this->assign('Page', $ShowPage); //分页条
    	 
    	$this->assign('SearchWord', $Keywords);
    	$this->assign('SearchWord', $Keywords);
    	$this->assign('Searchwords', $Keywords);
    	$this->assign('ChannelID', $ChannelID);
    	$this->display();
    }
    
    //用户发表评论
    public function commentAdd(){
    	header("Content-Type:text/html; charset=utf-8");
    	$c = YdCache::readComment();
    	 
    	//评论权限
    	if( $c['COMMENT_ENABLE'] == 0){
    		$this->ajaxReturn(null, L('CommentForbidden') , 0);
    	}
    
    	if( $c['COMMENT_VERIFYCODE'] == 1){ //启用验证码
    		$verifycode = $_POST['verifycode'];
    		if( empty($verifycode) ){
    			session('commentcode', rand(1000, 9999) );
    			$this->ajaxReturn(null, L('VerifyCodeRequired') , 0);
    		}
    		if( md5($verifycode) != session('commentcode')  ){
    			session('commentcode', rand(1000, 9999) );
    			$this->ajaxReturn(null, L('VerifyCodeError')  , 0);
    		}
    	}
    	$_POST = YdInput::checkTextbox( $_POST ); //xss过滤
    	$m = D('Admin/Comment');
    	if( $m->create() ){
    		if( $this->MemberIsLogin() ){
    			$m->GuestID = session('MemberID');
    			$m->GuestName = session('MemberName');
    		}else{
    			$m->GuestID = 0;
    		}
    		$m->IsCheck = ($c['COMMENT_CHECK'] == 1) ? 0 : 1;
    		$m->CommentTime = date('Y-m-d H:i:s');
    		$m->GuestIP = get_client_ip();
    
    		if($m->add()){
    			$success = ($c['COMMENT_CHECK'] == 1) ? L('CommentSuccess') : L('CommentSuccessNoCheck');
    			$this->ajaxReturn(null, $success , 1);
    		}else{
    			session('commentcode', rand(1000, 9999) );
    			$this->ajaxReturn(null, L('CommentFail') , 0);
    		}
    	}else{
    		session('commentcode', rand(1000, 9999) );
    		$this->ajaxReturn(null, $m->getError() , 0);
    	}
    }
    
    //获取频道信息
    private function _getChannel(){
    	$html = $_GET['id'];  //频道静态文件名称，不带扩展名
    	$this->_m = D('Admin/Channel');
    	$this->_data = $this->_m->findChannelByHtml( $html );
    	if( empty( $this->_data ) ) { //若频道不存在，则转向404页面
    		$this->_empty('channel');
    		exit();
    	}
    	$id = $this->_data['ChannelID'];
    	$Parent = $this->_data['Parent'];
    	
    	//计算特殊字段值start==================================================
    	//是否有阅读权限
    	$ReadLevel = $this->_data['ReadLevel'];
    	$ReadLevel= ( !empty($ReadLevel) || $Parent==0 ) ? $ReadLevel : get_read_level( $Parent );
    	$this->_data['HasReadLevel'] = has_read_level( $ReadLevel ) ? 1 : 0;
    	
    	//详细内容特殊标签解析
    	$this->_data['ChannelContent'] = ParseTag( $this->_data['ChannelContent'] );
    	tag('channel_content', $this->_data['ChannelContent']);
    	
    	//搜索引擎优化字段
    	$seo = YdCache::readSeo();
    	if( empty($this->_data['Title']) ) {
    		$this->_data['Title'] = ($Parent==0) ? $seo['TITLE'] : get_title( $Parent );
    	}
    	$this->_data['Title'] = YdInput::checkSeoString( $this->_data['Title'] );
    	
    	if( empty($this->_data['Keywords']) ){
    		$this->_data['Keywords'] = ($Parent==0) ? $seo['KEYWORDS'] : get_keywords( $Parent );
    	}
    	$this->_data['Keywords'] = YdInput::checkSeoString( $this->_data['Keywords'] );
    	
    	if( empty($this->_data['Description']) ){
    		$this->_data['Description'] = ($Parent==0) ? $seo['DESCRIPTION'] : get_description( $Parent );
    	}
    	$this->_data['Description'] = YdInput::checkSeoString( $this->_data['Description'] );
    	
    	//其它
    	$this->_data['HasParent'] = ($Parent > 0 ) ? 1 : 0 ;  //是否有父频道
    	$this->_data['TopChannelID'] = ($Parent == 0) ? $id : $this->_m->getTopChannel( $id ); //顶级频道ID
    	$this->_data['TopHasChild'] = ( $this->_data['HasChild'] == 1 ||  $Parent != 0 ) ? 1 : 0;
    	$this->_data['ChannelUrl'] = ChannelUrl($id, $this->_data['Html'], $this->_data['LinkUrl']);
    	//计算特殊字段值end==================================================
    	$this->assign($this->_data);
    }
    
    
    //反馈提交
    public function feedbackAdd(){
    	header("Content-Type:text/html; charset=utf-8");
    	//处理checkbox的情况====================
    	foreach ($_POST as $k=>$v){
    		if( is_array($v) ){
    			$_POST[$k] = implode(',', $v);
    		}
    	}
    	//===================================
    	$c = YdCache::readGuestbook();
    	if( $c['FEEDBACK_VERIFYCODE'] == 1){ //启用验证码
    		$verifycode = $_POST['verifycode'];
    		if( empty($verifycode) ){
    			session('feedbackcode', rand(1000, 9999) );
    			$this->ajaxReturn(null, L('VerifyCodeRequired') , 0);
    		}
    		if( md5($verifycode) != session('feedbackcode')  ){
    			session('feedbackcode', rand(1000, 9999) );
    			$this->ajaxReturn(null, L('VerifyCodeError')  , 0);
    		}
    	}
    	$_POST = YdInput::checkTextbox( $_POST ); //xss过滤
    	$m = D('Admin/Info');
    	if( $m->create() ){
    		$m->MemberID =$this->MemberIsLogin() ? session('MemberID') : 0;
    		$m->IsCheck = ($c['FEEDBACK_CHECK'] == 1) ? 0 : 1;
    		$m->IsEnable = 1;
    		$InfoTime = date('Y-m-d H:i:s');
    		$m->InfoTime = $InfoTime;
    		if($m->add()){
    			if( $c['FEEDBACK_SENDEMAIL'] == 1){
    				//获取留言内容===============================================================
    				$m = D('Admin/Attribute');
    				$Attribute = $m->getAttribute(37);
    				$Group = $m->getGroup(37);
    				foreach($Group as $g){
    					foreach ($Attribute as $a){
    						if( $g['AttributeID'] == $a['GroupID']){
	    						$name = explode(',', $a['DisplayName']);
	    						$value = $_POST[ $a['FieldName'] ];
	    						if(  $value != '' ){
	    							$emailbody .= $name[0].':'.$value.'<br/>';
	    						}
    						}
    					}
    				}
    				$emailbody .= '时间:'.$InfoTime;
    				$emailtitle = $c['FEEDBACK_EMAIL_TITLE'];
    				$emailto = empty($c['FEEDBACK_EMAIL']) ? YdCache::readContact('EMAIL') : $c['FEEDBACK_EMAIL'];
    				//=======================================================＝===============
    				//提交留言时自动发送电子邮件
    				$b = sendwebmail($emailto, $emailtitle, $emailbody);
    			}
    			$this->ajaxReturn(null, L('FeedbackSuccess') , 1);
    		}else{
    			session('feedbackcode', rand(1000, 9999) );
    			$this->ajaxReturn(null, L('FeedbackFail') , 0);
    		}
    	}else{
    		session('feedbackcode', rand(1000, 9999) );
    		$this->ajaxReturn(null, $m->getError() , 0);
    	}
    }
    
    //邮件订阅
    public function subscibe(){
    	header("Content-Type:text/html; charset=utf-8");
    	if( !isset($_POST['MailClassID'])){
    		$c = D('Admin/MailClass');
    		$_POST['MailClassID'] = $c->getFirstClassID(); //如果不存在，则添加到第一个分类 
    	}
    	$_POST = YdInput::checkTextbox( $_POST ); //xss过滤
    	$m = D('Admin/Mail');
    	if( $m->create() ){
    		$m->AddTime = date('Y-m-d H:i:s');
    		if( $m->add() ){
    			$this->ajaxReturn(null, L('SubscibeSuccess') , 1);
    		}else{
    			$this->ajaxReturn(null, L('SubscibeFail') , 0);
    		}
    	}else{
    		$this->ajaxReturn(null, $m->getError() , 0);
    	}
    }
    
    public function test(){
    	header("Content-Type:text/html; charset=utf-8");
    	$this->display();
    }
    
    //保存用户投票
    public function voteAdd(){
    	header("Content-Type:text/html; charset=utf-8");
    	$item = $_REQUEST['item'];
    	$appid = $_REQUEST['appid'];
    	$fromUser = !empty($this->_fromUser) ? $this->_fromUser : get_client_ip();
    	$_REQUEST = YdInput::checkTextbox( $_REQUEST );
    	$m = D('Admin/WxVote');
    	if($m->hasVoted($appid, $fromUser) ){
    		$this->ajaxReturn(null, '', 2);
    	}
    	
    	if(false === $m->submitVote($appid, $item, $fromUser)){
    		$this->ajaxReturn(null, '', 0);
    	}else{
    		$this->ajaxReturn(null, '', 1);
    	}
    }
}
?>