<?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
+--------------------------------------------------------------------------
*/

define('IN_AJAX', TRUE);


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

class ajax extends ASK_CONTROLLER
{
	var $per_page = 10;
	
	function setup()
	{
		HTTP::no_cache_header();
	}
	
	/**
	 * Listed in the list of invited
	 */
	function invitation_list_action()
	{		
		$page = intval($_GET['page']);
		
		$limit = $page * $this->per_page . ', ' . $this->per_page;
		
		if ($invitation_list = $this->model('invitation')->get_invitation_list($this->user_id, $limit))
		{
			$user_ids = array();

			foreach ($invitation_list as $key => $val)
			{
				if ($val['active_status'] == '1')
				{
					$user_ids[] = $val['active_uid'];
				}
			}

			if($user_ids = array_unique($user_ids))
			{
				if($user_infos = $this->model('account')->get_user_info_by_uids($user_ids))
				{
					foreach ($invitation_list as $key => $val)
					{
						if ($val['active_status'] == '1')
						{
							$invitation_list[$key]['user_info'] = $user_infos[$val['active_uid']];
						}
					}
				}
			}
		}
		
		TPL::assign('invitation_list', $invitation_list);
		
		TPL::output("invitation/ajax/invitation_list");
	}
	
	//Invite action execution
	function invite_action()
	{
		$email = trim($_POST['email']);
		
		if (! H::valid_email($email))
		{
			H::ajax_json_output(ASK_APP::RSM(null, -1, "Please enter a correct Email address"));
		}
		
		//Determine the amount of the current user if there is to invite
		if (! $this->user_info['invitation_available'])
		{
			H::ajax_json_output(ASK_APP::RSM(null, -1, "Have invited the maximum number of people"));
		}
		
		//Search E-mail has been for the site users, there is a prompt the user already exists, return to the display.
		if ($uid = $this->model('account')->check_email($email))
		{
			if ($uid == $this->user_id)
			{
				H::ajax_json_output(ASK_APP::RSM(null, -1, "You can not invite yourself"));
			}
			
			H::ajax_json_output(ASK_APP::RSM(null, -1, "The Email address has already been registered"));
		}
		
		//If you have been invited again to fill in the mailbox, and then send the message again invite
		if ($invitation_info = $this->model('invitation')->get_invitation_by_email($email))
		{
			if ($invitation_info['uid'] == $this->user_id)
			{				
				if ($this->model('invitation')->send_invitation_email($invitation_info['invitation_id']))
				{
					H::ajax_json_output(ASK_APP::RSM(null, 1, "The invitation has been resent"));
				}
				else
				{
					H::ajax_json_output(ASK_APP::RSM(null, -1, "Failed to send invitation, please contact your administrator"));
				}
			}
			else
			{
				H::ajax_json_output(ASK_APP::RSM(null, -1, "The Email has already received invitation before'"));
			}
		}
		
		//Invitation code generation
		$invitation_code = $this->model('invitation')->get_unique_invitation_code();
		
		if ($invitation_id = $this->model('invitation')->add_invitation($this->user_id, $invitation_code, $email, time(), ip2long($_SERVER['REMOTE_ADDR'])))
		{
			$this->model('account')->edit_invitation_available($this->user_id, -1);
			
			if($this->model('invitation')->send_invitation_email($invitation_id))
			{
				H::ajax_json_output(ASK_APP::RSM(null, 1, "The invitation has been sent"));
			}
			else
			{
				H::ajax_json_output(ASK_APP::RSM(null, -1, "Failed to send invitation"));
			}
		}
	}

	/**
	 * Resend invitation
	 */
	function invite_resend_action()
	{
		if ($this->model('invitation')->send_invitation_email($_GET['invitation_id']))
		{
			H::ajax_json_output(ASK_APP::RSM(null, 1, null));
		}
		else
		{
			H::ajax_json_output(ASK_APP::RSM(null, -1, "Failed to send invitation, please contact your administrator"));
		}
	}

	function invite_cancel_action()
	{
		if (! intval($_GET['invitation_id']))
		{
			H::ajax_json_output(ASK_APP::RSM(null, -1, "Invite ID does not exist"));
		}
		
		if (! $this->model('invitation')->get_invitation_by_id(intval($_GET['invitation_id'])))
		{
			H::ajax_json_output(ASK_APP::RSM(null, -1, "The invitation message does not exist"));
		}
		
		//Update Status
		$this->model('invitation')->update_invitation_fields(array(
			"active_status" => '-1'
		), intval($_GET['invitation_id']));
		
		if ($this->model('account')->edit_invitation_available($this->user_id, 1))
		{
			H::ajax_json_output(ASK_APP::RSM(null, 1, null));
		}
		else
		{
			H::ajax_json_output(ASK_APP::RSM(null, 1, "Operation failed, please contact your administrator"));
		}
	}
}