<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/


if (!defined('IN_TANYACMS'))
{
	die;
}

class email_class extends ASK_MODEL
{
	public $user_id;
	
	const TYPE_FOLLOW_ME = 11; //Noticed my
	const TYPE_QUESTION_INVITE = 13; //Someone invited me to reply to questions
	const TYPE_NEW_ANSWER = 14; //Followed questions have new replies
	const TYPE_NEW_MESSAGE = 15; //When someone messages me
	const TYPE_VALID_EMAIL = 101; //Verify the Email
	const TYPE_INVITE_REG = 102; //Invite Register
	const TYPE_FIND_PASSWORD = 103; //Find your password
	const TYPE_QUESTION_SHARE = 104; //Share Question
	const TYPE_ANSWER_SHARE = 105; //Share Reply
	const TYPE_INVITE_QUESTION = 106; //Outside the website to invite
	const TYPE_TOPIC_SHARE = 107; //Share Topic
	const TYPE_QUESTION_MOD = 108; //Question is modified
	const TYPE_QUESTION_DEL = 109; //Question is delete

	
	public function setup()
	{
		$this->user_id = USER::get_client_uid();
	}

	/**
	 * Action Mail
	 */
	function action_email($action = 0, $uid, $link, $data = array())
	{		
		if (!H::valid_email($uid))
		{
			if (! $this->check_email_setting($uid, $action))
			{
				return false;
			}
		}
		
		$email_message = (array)ASK_APP::config()->get('email_message');
		
		foreach ($email_message[$action] as $key => $val)
		{
			$$key = str_replace('[#user_name#]', $data['user_name'], $val);
			$$key = str_replace('[#site_name#]', get_setting('site_name'), $$key);
			$$key = str_replace('[#question_title#]', $data['question_title'], $$key);
			
			if (preg_match('/question_detail/i', $$key))
			{
				$$key = str_replace('[#question_detail#]', $data['question_detail'], $$key);
			}
		}
		
		return $this->send($uid, $subject, $message, $link, $link_title);
	}

	/**
     * Send mail
     */
	function send($uid, $subject, $message, $link = null, $link_title = null)
	{
		if (H::valid_email($uid))
		{
			$email = $uid;
		}
		else if (is_numeric($uid))
		{			
			if (! $user_info = $this->model('account')->get_user_info_by_uid($uid))
			{
				return false;
			}
			
			$username = $user_info['user_name'];
			$email = $user_info['email'];
		}
		else
		{
			return false;
		}
		
		TPL::assign('username', $username);
		TPL::assign('subject', $subject);
		TPL::assign('message', $message);
		TPL::assign('link', $link);
		TPL::assign('link_title', $link_title);
		
		return load_class('core_mail')->send_mail(null, get_setting('site_name'), $email, $username, $subject, TPL::output('global/email_template', false));
	}

	/**
     * Get user email settings record
     * @param unknown_type $uid
     */
	function get_email_setting($uid)
	{
		if (!$uid)
		{
			return false;
		}
		
		return $this->fetch_row('users_email_setting', 'uid = ' . intval($uid));
	}

	/**
     * Determine the user email settings
     * @param unknown_type $uid
     * @param unknown_type $type
     */
	function check_email_setting($uid, $type)
	{
		if (in_array($type, array(
			11, 
			12, 
			13, 
			14, 
			15
		)))
		{
			if ($uid == $this->user_id)
			{
				return false;
			}
			
			$user_setting = $this->get_email_setting($uid);
			
			if ($user_setting['sender_' . $type] == 1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		return true;
	}
}