<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Message extends Controller {

    public function action_index(){
        
        if(Auth::instance()->get_user()){
			
	    #Allow only users to see page
	    
	    $this->action_inbox();
	}
	else
	    Request::instance()->redirect("");
    }
    
    public function action_inbox(){
	
        if(Auth::instance()->get_user()){
			
	    #Allow only users to see page
	    
	    $message_model 	= Model::factory('message');
	    $messages 		= $message_model->getInboxMessages();
	    
	    $data		= array('group_type' => 'i');
	    
	    $view = timation::pagedMessages($data, $messages);
	    $this->request->response = $view;
	}
	else
	    Request::instance()->redirect("");
    }
    
    public function action_sent(){
	
        if(Auth::instance()->get_user()){
			
	    #Allow only users to see page
	    
	    $message_model 	= Model::factory('message');
	    $messages 		= $message_model->getSentMessages();
	    
	    $data		= array('group_type'		=> 's');
	    
	    $view = timation::pagedMessages($data, $messages);
	    $this->request->response = $view;
	}
	else
	    Request::instance()->redirect("");
    }
    
    public function action_read($group_id,$group_type){
	if(Auth::instance()->get_user()){
	    
	    #Allow only users to see page
	    $message_model 	= Model::factory('message');
	    
	    if($group_type == 'i'){
		$messages 	= $message_model->getInboxGroupMessages($group_id);
		$message_model->getNumberOfNewMessages();
	    }
	    else{
		$messages 	= $message_model->getSentGroupMessages($group_id);
	    }
	    $getter 		= $message_model->getIdToSendMessage($group_id);
	    
	    $data		= array('group_id'		=> $group_id,
					'message_group' 	=> $messages,
					'group_type'		=> $group_type,
					'getter_id'		=> $getter);
	    
	    $view   = View::factory("readmsg", $data);
	    $this->request->response = $view;
	}
	else
	    Request::instance()->redirect("");
    }
    
    public function action_sendmsg(){
	
       if(Auth::instance()->get_user()){
	    
	    #Allow only users to see page
	    if($_POST){
	    # Send message
		$group_id	= $_POST['group_id'];
		$getter_id 	= $_POST['getter_id'];
		$post		= $_POST['message'];
		$sale_id	= $_POST['sale_id'];
		
		$message_model 	= Model::factory('message');
		$message_model->sendANewMessage($sale_id, $group_id, $getter_id, $post);
		
		if($_POST['group_type'] == 'i')
		    $messages 	= $message_model->getInboxGroupMessages($group_id);
		else
		    $messages 	= $message_model->getSentGroupMessages($group_id);
		    
		$data		= array('group_id'		=> $group_id,
					'message_group' 	=> $messages,
					'group_type'		=> $_POST['group_type'],
					'getter_id'		=> $getter_id);
		
		$view   = View::factory("readmsg", $data);
		$this->request->response = $view;
	    }
	}
	else
	    Request::instance()->redirect("");
    }
    
    public function action_deletemsg($group_id,$group_type){
	
       if(Auth::instance()->get_user()){
	    
	    #Allow only users to see page
	    $message_model = Model::factory('message');
	    if($group_type == 'i'){
		$message_model->deleteInboxMessage($group_id);
		$messages 		= $message_model->getInboxMessages();
	    }
	    else{
		$message_model->deleteSentMessage($group_id);
		$messages 		= $message_model->getSentMessages();
	    }
	    
	    $data		= array('group_type'		=> $group_type);
	    
	    $view = timation::pagedMessages($data, $messages);
	    $this->request->response = $view;
	}
	else
	    Request::instance()->redirect("");
    }
    
    public function action_deletegroup($group_type){
	
       if(Auth::instance()->get_user()){
	    
	    #Allow only users to see page
	    if($_POST){
		
		if(isset($_POST["messgroups"])){
		    $message_model = Model::factory('message');
		    if($group_type == 'i'){
			foreach($_POST['messgroups'] as $group_id){
			    $message_model->deleteInboxMessage($group_id);
			}
			$messages 		= $message_model->getInboxMessages();
		    }
		    else{
			foreach($_POST['messgroups'] as $group_id){
			    $message_model->deleteSentMessage($group_id);
			}
			$messages 		= $message_model->getSentMessages();
		    }
		    
		    $data		= array('group_type'		=> $group_type);
		    
		    $view = timation::pagedMessages($data, $messages);
		    $this->request->response = $view;
		}
		else
		    Request::instance()->redirect("");
	    }
	}
	else
	    Request::instance()->redirect("");
    }
    

}