<?php

class EmailLogic {
	
	private $email_task;
	
	function __construct() {
		Doo::loadModel ( 'm/email_task' );
		
		$this->email_task = new EmailTask ();
	}
	
	/**
	 * 获取邮件群发任务
	 */
	function get_email_task() {
		$result = $this->email_task->get_email_task ();
		
		return $result;
	}
	
	/**
	 * 添加邮件群发任务
	 * @param unknown_type $data
	 */
	function add_email_task($data = array()) {
		
		Doo::loadModel ( 'users' );
		
		$user = new Users ();
		
		$t = $user->getCountz ();
		
		$this->email_task->email_title = $data ['email_title'];
		
		$this->email_task->email_content = $data ['email_content'];
		
		$this->email_task->status = 1;
		
		$this->email_task->send_count = $user->getCountz ();
		
		$this->email_task->time = time ();
		
		$id = $this->db ()->insert ( $this->email_task );
		
		return $id;
	}
	
	/**
	 * 获取邮件任务
	 * @param unknown_type $tid
	 */
	function get_email_task_by_tid($tid = 0) {
		
		$result = $this->email_task->get_email_task_by_tid ( $tid );
		
		Doo::loadModel ( 'users' );
		
		$user = new Users ();
		
		$limit = 30;
		
		if ($result ['is_send_count'] >= $result ['send_count'])
			return array ();
		else {
			
			if ($result ['send_count'] < $limit)
				$limit = $result ['send_count'];
			
			$in = $result ['send_count'] - $result ['is_send_count'];
			
			if ($in < $limit)
				$limit = $in;
			
			$result ['user_list'] = $user->getAll ( $result ['is_send_count'] . "," . $limit );
			
			return $result;
		}
	}
	
	/**
	 * 获取发送失败邮件
	 * @param unknown_type $tid
	 */
	function get_faild_email_task_by_tid($tid = 0) {
		
		$result = $this->email_task->get_email_task_by_tid ( $tid );
		
		return $result;
	}
	
	/**
	 * 发送邮件
	 * @param unknown_type $email
	 */
	function send_email($email = "", $title = "", $content = "", $host = "smtp.exmail.qq.com", $port = "", $username = "websupport@smartcost.com.cn", $password = "smartcost2010", $form = "websupport@smartcost.com.cn") {
		
		require_once './protected/class/class.pop3.php';
		
		require_once './protected/class/class.smtp.php';
		
		require_once './protected/class/class.phpmailer.php';
		
		$mail = new PHPMailer ();
		
		$body = stripcslashes ( $content );
		
		$mail->IsSMTP (); // telling the class to use SMTP
		

		$mail->SMTPAuth = true; // enable SMTP authentication
		//$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
		//$mail->Host       = "smtp.exmail.qq.com";      // sets GMAIL as the SMTP server
		$mail->Host = $host;
		
		if (! empty ( $port ))
			$mail->Port = $port; // set the SMTP port for the GMAIL server
		

		//$mail->CharSet = "GBK";
		

		$mail->Username = $username; // GMAIL username
		$mail->Password = $password; // GMAIL password
		

		$mail->From = $username;
		
		$mail->FromName = "=?UTF-8?B?" . base64_encode ( '纵横知道·问答' ) . "?=";
		
		$mail->Subject = $title;
		
		$mail->MsgHTML ( $body );
		
		$mail->AddAddress ( $email );
		
		if (! $mail->Send ()) {
			//echo $mail->ErrorInfo;
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * 删除邮件任务
	 * @param unknown_type $tid
	 */
	function delete_email_task_by_tid($tid = 0) {
		Doo::loadModel ( 'm/email_task' );
		
		$task = new EmailTask ();
		
		$task->tid = $tid;
		
		$this->db ()->delete ( $task );
	}
	
	/**
	 * 邮件发送完成-更新状态
	 * @param unknown_type $tid
	 */
	function update_email_task_by_faild_email($tid) {
		
		Doo::loadModel ( 'm/email_task' );
		
		$task = new EmailTask ();
		
		$email = $task->get_email_task_by_tid ( $tid );
		
		$task->tid = $tid;
		
		$task->status = 3;
		
		if (empty ( $email ['faild_uid'] ))
			$task->status = 4;
		
		$this->db ()->update ( $task );
	}
	
	/**
	 * 更新群发任务进度
	 * @param unknown_type $tid
	 * @param unknown_type $is_send_count
	 */
	function update_email_task_by_tid($tid = 0, $is_send_count = 0, $faild_email = "") {
		
		$task = $this->email_task->get_email_task_by_tid ( $tid );
		
		$this->email_task->tid = $tid;
		
		$this->email_task->is_send_count = $is_send_count;
		
		$this->email_task->status = 2;
		
		$this->email_task->faild_uid = $faild_email . $task ['faild_uid'];
		
		$this->db ()->update ( $this->email_task );
	}
	
	/**
	 * 更新邮件失败状态
	 * @param unknown_type $tid
	 * @param unknown_type $faild_email
	 */
	function update_email_task_status_by_tid($tid = 0, $status = 4) {
		Doo::loadModel ( 'm/email_task' );
		
		$task = new EmailTask ();
		
		$task->tid = $tid;
		
		$task->status = $status;
		
		$this->db ()->update ( $task );
	}
	
	/**
	 * 更新失败邮件
	 * @param unknown_type $tid
	 * @param unknown_type $faild_email
	 */
	function update_faild_email_task_by_tid($tid = 0, $faild_email = "") {
		
		Doo::loadModel ( 'm/email_task' );
		
		$task = new EmailTask ();
		
		//$task_faild = $this->email_task->get_email_task_by_tid ( $tid );
		
		$task->tid = $tid;
		
		if (empty ( $faild_email ))
			$task->status = 4;
		
		$task->faild_uid = $faild_email;
		
		$this->db ()->update ( $task );
	}
	
	/**
	 * 更新失败邮件
	 * @param unknown_type $tid
	 * @param unknown_type $faild_email
	 */
	function de_faild_email_task_by_tid($tid = 0, $faild_email = "") {
		
		Doo::loadModel ( 'm/email_task' );
		
		$task = new EmailTask ();
		
		//$task_faild = $this->email_task->get_email_task_by_tid ( $tid );
		
		$task->tid = $tid;
		
		$task->de_email = $faild_email;
		
		$this->db ()->update ( $task );
	}
	
	/**
	 * 获得分页数据
	 * @param unknown_type $table
	 * @param unknown_type $condition
	 * @param unknown_type $on_page
	 * @param unknown_type $page_size
	 */
	function get_page($table = "", $condition = "", $on_page = 1, $page_size = 20, $action = "", $get = "", $other = "page") {
		$page_c = "";
		
		$page ['previous'] = get_previous ( $on_page );
		
		$page ['on_page'] = $on_page;
		
		$total_count = $this->get_table_count ( $table, $condition );
		
		$total = intval ( $total_count / $page_size );
		
		$page ['total_page'] = ($total_count % $page_size) == 0 ? $total : $total + 1;
		
		$page ['total_data'] = $total_count;
		
		$page ['next'] = $on_page == $page ['total_page'] ? $page ['total_page'] : $on_page + 1;
		
		$i = 1;
		
		$page_max = 1;
		
		if ($on_page > 10) {
			$page_max = intval ( $on_page / 10 ) + 1;
			$i = intval ( $on_page / 10 ) * 10 - 1;
		}
		
		for(; $i <= $page ['total_page']; $i ++) {
			
			if ($i == $on_page) {
				if ($other == "page")
					$page_c .= '<a href="javascript:void(0);" class="current">' . $i . '</a>';
				else
					$page_c .= '&nbsp;<span class="current">' . $i . '</span>&nbsp;';
			} else if ($other == "page")
				$page_c .= '<a href="' . $action . $i . $get . '" class="paginate">' . $i . '</a>';
			else
				$page_c .= '&nbsp;<a href="' . $action . $i . $get . '" class="paginate">' . $i . '</a>&nbsp;';
			if ($i == (10 * $page_max))
				break;
		}
		
		$page ['page'] = $page_c;
		
		$page ['lower'] = (-- $on_page) * $page_size;
		
		return $page;
	}
	
	/**
	 * 获取总页数
	 * @param unknown_type $table
	 * @param unknown_type $condition
	 */
	public function get_table_count($table = "", $condition = "") {
		$sql = "select count(*) as count from " . $table . " where 1 " . $condition;
		
		$query = Doo::db ()->query ( $sql );
		
		$result = $query->fetch ();
		
		return $result ['count'];
	}
	
	/**
	 * 获取数据
	 * @param unknown_type $data 一般选择条件
	 * @param unknown_type $limit 
	 * @param unknown_type $condition
	 * @param unknown_type $table
	 */
	function get_list($data = array(), $condition = "", $limit = "", $table = "") {
		
		foreach ( $data as $key => $value ) {
			if (is_numeric ( $value ))
				$condition .= " and " . $key . " = " . $value;
			else
				$condition .= " and " . $key . " like '%" . $value . "%' ";
		}
		
		$condition = ' where 1 ' . $condition . $limit;
		
		$sql = "select * from " . $table . $condition;
		
		$query = Doo::db ()->query ( $sql );
		
		$result = $query->fetchAll ();
		
		return $result;
	}
	
	function db() {
		return Doo::db ();
	}
}

?>