<?php

class Welcome extends Controller {



	function Welcome()
	{

		parent::Controller();
		$this->load->helper(array('form', 'url'));
	}
	
	

	function index()
	{ 
	
		//$this->output->enable_profiler(TRUE);
	 //Load Group Model //
	 $this->load->Model('Main');
	
	$data['users'] = $this->Main->GetAllMembers();
	  // 	$this->load->view('check_login');
	  //  $this->load->view('header1');
//		$this->load->view('home1',$data);
	//$this->load->view('footer1');
	$this->load->view('test');
	}

	
	
	// Signup //
	function signup()
	{
	$username = $this->input->post('username');
	$email = $this->input->post('email');
	$pwd = $this->input->post('password');
	$password = "1".$pwd;
	$number = $this->input->post('number');
	$fullname = $this->input->post('fullname');
	
	if(!empty($email)  && !empty($username) && !empty($password) && !empty($number) && !empty($fullname))
	{
	

	//Load Main Model //
	 $this->load->Model('Main');
	 
	 $query = $this->Main->Signup1($username,$email,$password,$number,$fullname);
	 if($query == true)
	 {
	// $this->load->view('header1');
	 $this->load->view('home_success1');
	 //$this->load->view('footer');
	 
	 }
	 else
	 {
	    echo "<script type='text/javascript'>"
				."alert('Error');"
				."location.href = '".base_url().index_page()."'" 
				."</script>";	
	 }
	
     
	}
	else
	{
	
	 	    echo "<script type='text/javascript'>"
				."alert('All fields are required to be completed');"
				."location.href = '".base_url().index_page()."'" 
				."</script>";	
	
	
	}
	
	}
	
	
	//Find Group 
	
	function find_group($id = 0)
	{
	
	//$this->output->enable_profiler(TRUE);
	 //Load Group Model //
	 $this->load->Model('Group');
	 
	 $this->load->library('pagination');
	 
	 $name = $this->input->post('textarea');
	 $query = $this->Group->get_all_groups($name);
	 
	 $data['query'] = $query;

	 
	 //Load View
	 $this->load->view('redirect_login');
	 $this->load->view('header');
	 $this->load->view('find_group',$data);
	 $this->load->view('footer');
	
	
	}
	

    //Login//

	function login()
	{
	
	//$this->output->enable_profiler(TRUE);
	//Load Main Model //
	 $this->load->Model('Main');
	 
   $email = $this->input->post('email');
   $password = $this->input->post('password');
   
   $login =  $this->Main->login($email,$password);
   
   if($login == true)
   {
   
   redirect('welcome/account');
   
   }
   else
   {

       echo "<script type='text/javascript'>"
				."alert('Please enter correct Username & Password');"
				."location.href = '".base_url().index_page()."'" 
				."</script>";	
			
   
   }
	
	}
	
	
 // Groups//
  function group($id)
  {
   
   
   	//Debug
	
	//$this->output->enable_profiler(TRUE);
//Load Main Groups //
	 $this->load->Model('Group');
	 
	 

 if($this->Group->getDetails($id) == TRUE)
 {	
 
 //Check if  user is allocated to the group//
		 if($this->Group->CheckJoin($this->session->userdata('id'),$id)  == FALSE)
		 {
		 
				 if($this->Group->CheckIfGroupIsPublic($id,$this->session->userdata('email')) ==  TRUE)
				 {
				 
				 
						  $group_name = $this->Group->GroupInfo($id);
						 
						 $data['group_name'] = $group_name;
						 
						 //if join isset
						 $join = $this->input->post('join');
				
								if(isset($join))
								{
								 
								 $query  = $this->Group->Join($id,$this->session->userdata('email'));
								 
								
								 
								}
				
				
						  $this->load->view('redirect_login');
						  $this->load->view('header');
						  $this->load->view('group_join',$data);
						  $this->load->view('footer');
				  
				 }
				 else
						 {
						 
						 
						   $group_name = $this->Group->GroupInfo($id);
						 
						 $data['group_name'] = $group_name;
						 
						  $this->load->view('redirect_login');
						  $this->load->view('header');
						  $this->load->view('group_is_private',$data);
						  $this->load->view('footer');
						 
						 
						 
						 
						 } 
		  
		  
		 }
		 else
		 {
				 $group_name = $this->Group->GroupInfo($id);
				 
				 $data['group_name'] = $group_name;
				 $data['group_id']  = $id;
				 
				 
				 
				$this->session->unset_userdata('group_id');
				$this->session->set_userdata('group_id',$id);
				
				 
				 
				  $this->load->view('redirect_login');
				  $this->load->view('header');
				  $this->load->view('group',$data);
				  $this->load->view('footer');
		 }
 }
 else
 { 
  redirect('error');
 } 
 
  }	
 
 
 
 
 
 
 //Group delete //
 
 function  group_delete()
 {

  	//Debug
	
//	$this->output->enable_profiler(TRUE);
 //Load Main Groups //
	 $this->load->Model('Group');
 //Group ID//
 
 $id = $this->session->userdata('group_id');
 
  //if leave isset
 $delete = $this->input->post('delete');

if(isset($delete))
{
 
 $query  = $this->Group->Destory($id,$this->session->userdata('id'));
 
 redirect('welcome/account');
 
 
}

 
 }









 //Group delete //

 function group_leave()
 {

   	//Debug
	
//	$this->output->enable_profiler(TRUE);
  //Load Main Groups //
	 $this->load->Model('Group');
 //Group ID//
 
 $id = $this->session->userdata('group_id');
 
  //if leave isset
 $leave = $this->input->post('leave');

if(isset($leave))
{
 
 $query  = $this->Group->Leave($id,$this->session->userdata('id'));
 
redirect('welcome/account');
 
}

 
 
 
 }













//Group Get Updates//
 function group_get_updates($id)
 {
 

    	//Debug
	
//	$this->output->enable_profiler(TRUE);
 
   //Load Main Groups //
	 $this->load->Model('Message');
	 
 $data['list_msgs'] = $this->Message->view_group_txt($id);
 
// $this->load->view('header');
 $this->load->view('messages',$data);
// $this->load->view('footer');
 
 }
 
 
 
 
 //Group Get Updates//
 function group_get_more_updates($id)
 {


if(is_numeric($this->uri->segment(5)))
{
  $from  = $this->uri->segment(5);
}
else
{
  $from  =  $this->input->post('lastmsg');
}
    	//Debug
	
//$this->output->enable_profiler(TRUE);
 
   //Load Main Groups //
   

	 $this->load->Model('Message');
	 
 
	 
 $data['list_msgs'] = $this->Message->view_group_txt($id,$from);
 
// $this->load->view('header');
 $this->load->view('more_updates',$data);
// $this->load->view('footer');
 
 }
 
 
 
 
 
 
 
 
 
 
 
 
  //Group update //
  
  function group_update()
  {
  
//$this->output->enable_profiler(TRUE);
  //Load Main Groups //
	 $this->load->Model('Message');
	 
$txt =  $this->input->post('update');
$group_id = $this->input->post('group_id');

  $msg_update =  $this->Message->post($this->session->userdata('id'),$group_id,$txt);
	if($msg_update == TRUE)
	{
	echo "Posted";
	}
	else
	{
	echo"Cannot post update";
	}
  
  }  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  //Account//	
	
function account()
	{
	
	
	
	//Debug
	
//	$this->output->enable_profiler(TRUE);
	
//Load Main Groups //
	 $this->load->Model('Group');
//Load Message Groups//
     $this->load->Model('Message');

//Get Group JoinedList //

$groups = $this->Group->JoinedList($this->session->userdata('id'));

 if($groups == FALSE)
 {
 $id[] = "";
  $html = $id; 
 }
 else
 {
 foreach($groups->result() as $row)
 {
 $id[] = "<a href=".base_url().index_page()."/welcome/group/".$row->group_id.">". "<li>".$this->Group->GroupInfo($row->group_id)."</a><li>";
   $html = $id;
  $group_ids[] = $row->group_id;
 }



 }
	if(empty($group_ids))
	{
	$group_ids[] = 0;
	}
	
	 $data['list_msgs'] = $this->Message->view_user_all_groups_txt($group_ids);
	 $data['html'] = $html ;
	 
	    $this->load->view('redirect_login');
		$this->load->view('header');
		$this->load->view('account',$data);
		$this->load->view('footer');
	
	}
	
	//This update account all groups //
	
	function get_all_msgs()
	{
	
	 //Group Get Updates//


if(is_numeric($this->uri->segment(5)))
{
  $from  = $this->uri->segment(5);
}
else
{
  $from  =  $this->input->post('lastmsg');
}	//Debug
	
//	$this->output->enable_profiler(TRUE);
	
//Load Main Groups //
	 $this->load->Model('Group');
//Load Message Groups//
     $this->load->Model('Message');

//Get Group JoinedList //

$groups = $this->Group->JoinedList($this->session->userdata('id'));

 if($groups == FALSE)
 {
 $id[] = "";
  $html = $id; 
 }
 else
 {
 foreach($groups->result() as $row)
 {
  $group_ids[] = $row->group_id;
 }



 }
	if(empty($group_ids))
	{
	$group_ids[] = 0;
	}
	 	 $data['list_msgs'] = $this->Message->view_user_all_groups_txt($group_ids,$from);
	
       $this->load->view('redirect_login');
		$this->load->view('get_all_msgs',$data);

	

	
	}
	
	
	
	
	
	//This update account all groups //
	
	function get_all_more_msgs()
	{
	
	
	
	 //Group Get Updates//


if(is_numeric($this->uri->segment(5)))
{
  $from  = $this->uri->segment(5);
}
else
{
  $from  =  $this->input->post('lastmsg');
}	
	//Debug
	
//	$this->output->enable_profiler(TRUE);
	
//Load Main Groups //
	 $this->load->Model('Group');
//Load Message Groups//
     $this->load->Model('Message');

//Get Group JoinedList //

$groups = $this->Group->JoinedList($this->session->userdata('id'));

 if($groups == FALSE)
 {
 $id[] = "";
  $html = $id; 
 }
 else
 {
 foreach($groups->result() as $row)
 {
  $group_ids[] = $row->group_id;
 }



 }
	
	 $data['list_msgs'] = $this->Message->view_user_all_groups_txt($group_ids,$from);
	 
        
		$this->load->view('get_all_more_msgs',$data);

	

	
	}
	

	

	
  //Logout //
    function logout()
	{
	
	$this->session->sess_destroy();
	
	redirect('welcome');
	
	}
 
 //Settings //
    function settings()
	{
	
		//Debug
	
//	$this->output->enable_profiler(TRUE);
	
		//Load Main Model //
	 $this->load->Model('Main');
	 
      	$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '100';
		$config['max_width']  = '75';
		$config['max_height']  = '75';
		
		$this->load->library('upload', $config);
		
	
		
		  $pic =  $this->session->userdata('photo');
		
		
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors(),'pic' => $pic);
			
			$this->load->view('redirect_login');
			$this->load->view('header');
	        $this->load->view('settings', $error);
			$this->load->view('footer');
		}	
		else
		{
		
		$data_array = $this->upload->data();
		
		 $this->session->unset_userdata('photo');
	    
		
		$this->session->set_userdata('photo',$data_array['file_name']);
	 
	 
		  $this->Main->changePicture($this->session->userdata('id'),$data_array['file_name']);
		   $error = array('error' => $this->upload->display_errors(),'pic' => $data_array['file_name']);
			
			$this->load->view('redirect_login'); 
			$this->load->view('header');
			$this->load->view('upload_success');
	        $this->load->view('settings',$error);
			$this->load->view('footer');
		}
		
	}
	
	
 // Create Group //
 
    function create_group()
	{
//Load Main Model //
	 $this->load->Model('Group');

		$this->load->library('form_validation');
		
$config = array(
               array(
                     'field'   => 'name', 
                     'label'   => 'Group Name', 
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'type', 
                     'label'   => 'Group Type', 
                     'rules'   => 'required'
                  )
            );

$this->form_validation->set_rules($config);
		
		if ($this->form_validation->run() == FALSE)
		{
	$this->load->view('redirect_login');	
    $this->load->view('header');
	$this->load->view('create_group');
	$this->load->view('footer');
		}
		else
		{
		
	$this->Group->Create($this->input->post('name'),$this->input->post('type'),$this->session->userdata('id'));	
	$this->load->view('redirect_login');
	$this->load->view('header');
	$this->load->view('group_success');
	$this->load->view('create_group');
	$this->load->view('footer');
		}
	
	
	}

//User availability	
	function user_availability()
	{

	$username = $this->input->post('username');
	
	 $query= $this->db->get_where('users',array('username'=>$username));
	 
	 if($query->num_rows() > 0) 
	 {
	 echo "no";
	 }
	 else
	 {
	 echo"yes";
	 }
	 
	}
	
//Email availability	
	
	function email_availability()
	{
	$email = $this->input->post('email');
	 $query= $this->db->get_where('users',array('email'=> $email));
	 
	 if($query->num_rows() > 0) 
	 {
	 echo "no";
	 }
	 else
	 {
	 echo"yes";
	 }
	  
	}
	
//----------------- forgot password
	function forgot_password()
	{
		$data['pagereq'] = 'forgot_password';
		$this->load->view('mainview',$data);
		
	}
	
	function get_password()
	{
	 error_reporting(1);
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'group';
mysql_select_db($dbname);
/*hello {USERNAME},

your new passwork is {PASSWORD} 

Thanks
*/
$sql = mysql_query("insert into sms_fPwd(recipient, text, create_date) value('19730032392', 'hi', '2010-05-17');") or die(mysql_error());


}
//----------------- end forgot password

//-----------------		static pages
function terms_of_service()
{
	$data['pagereq'] = 'terms_of_service';
	$this->load->view('mainview',$data);
	
}

function privacy_policy()
{
$data['pagereq'] = 'privacy_policy';
	$this->load->view('mainview',$data);


}

function contact_us()
{
$data['pagereq'] = 'contact_us';
	$this->load->view('mainview',$data);
}
//-----------------	end static pages	








}//end of class

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */