<?php
class Group extends Model
{
  function Group()
  {
    parent::Model();
  }


// Get all groups //
 

function get_all_groups($name = FALSE)
{

if($name !=FALSE)
{
$this->db->like('name',$name);
}

$query = $this->db->get('groups');

return $query;

}


//Create
  function Create($name,$type,$owner_id,$sImage)
  {
  
     $data = array(
               'name' => $name,
               'type' =>  $type,
			   'pending' =>  $type,
               'owner_uid' => $owner_id,
               'logo' => $sImage,
			   'date_time' => date('Y-m-d h:i:s')      
			   );
			   
			//   print_r($data);
			//   exit;

 $query = $this->db->insert('groups', $data); 
  
 $query1= $this->db->get_where('groups',array('date_time'=>$data['date_time'],'owner_uid'=> $owner_id,'pending' => $type,'name'=>$name));
  
      $join = array(
               'group_id' => $query1->row('id'),
               'member_uid' =>  $owner_id,
			   'pending1' => '0'
			   );
   $this->db->insert('members',$join); 
   
   return true;
  
  }
 
//Joined Group List

  function JoinedList($user_id)
  {
  
   $query =  $this->db->get_where('members', array('member_uid' => $user_id , 'pending1' => 0 ));
   
   if($query->num_rows() > 0 )
   {

    return $query;
   }
   else
   {
   return false;
   }
   
   
  }
  
// Check Join  
   
  function CheckJoin($member_uid,$group_id)
  {
  
  
   $query =  $this->db->get_where('members', array('member_uid' => $member_uid,'group_id'=> $group_id,'pending1'=> 0) );
   
   if($query->num_rows() > 0 )
   {

    return true;
   }
   else
   {
   
    return false;
   }
  
  
  }
 //Get group inforomation
 
  function GroupInfo($id)
  {  
  
  $query =  $this->db->get_where('groups', array('id' => $id));
  
  if($query->num_rows() > 0 )
   {
    
	$group_name = $query->row('name');
    
	return $group_name;
   
   }
   else
   {
   return false;
   }
  
  } 
//Check Group Exits or not //

  function getDetails($id)
  {
 
  $query =  $this->db->get_where('groups', array('id' => $id));
    if($query->num_rows() > 0 )
   {
   return true;
   }
   else
   {
   return false;
   }
  
  }
//Destory

  function Destory($group_id,$owner_id)
  {
  
  $query = $this->db->get_where('groups',array('id' => $group_id,'owner_uid'=> $owner_id) );
 if($query->num_rows() > 0 )
   {
    $this->db->delete('groups',array('id' => $group_id));
	$this->db->delete('messages',array('group_id' => $group_id));
	$this->db->delete('members',array('group_id' => $group_id));
	
     
	 return true;
   }
  else
   {
    
	return false;
   }
   
     
  }
  
  
 //Check If Group Is Public 
 
  function CheckIfGroupIsPublic($id,$email)
  {
 
  $query = $this->db->get_where('groups',array('id' =>$id ,'type'=>'0','pending'=>'0') );
   
   if($query->num_rows() > 0 )
   {
    
	return true;
   }
   else
   {
 	 $query = $this->db->get_where('invites',array('group_id' => $id ,'email'=> $email));
     
 if($query->num_rows() > 0 )
  {
	 	return true;
   }
   else
   {
 
   return false;
   
   }	
  
  }
  
  }
  
// Join

  function Join($group_id,$email,$pending1)
  {
  
  
 $query = $this->db->get_where('groups',array('id' => $group_id ,'type'=>'0') );

 if($query->num_rows() > 0 )
   {
   		 $query = $this->db->get_where('users',array('email' => $email));
	     $ABT = $this->input->post('about1234');       
		 $data = array(
               'group_id' => $group_id,
               'member_uid' =>  $query->row('id'),
			   'pending1' => $pending1,
			   'about' => $ABT
			   );

        $this->db->insert('members', $data); 
		return true;
	 
	 
	 
   }
   else
   {
   

	  $query = $this->db->get_where('users',array('email' => $email));
	   $ABT = $this->input->post('about1234');       
		 $data = array(
               'group_id' => $group_id,
               'member_uid' =>  $query->row('id'),
			   'pending1' => '1',
			   'about' => $ABT
			   );

  			$this->db->insert('members', $data); 
  
      return true;
	 
   }

  
  }

 function Join1($group_id,$email)
  {
  
  
 $query = $this->db->get_where('groups',array('id' => $group_id ,'type'=>'0') );
 
 
 
 if($query->num_rows() > 0 )
   {
   
   
    $query = $this->db->get_where('users',array('email' => $email));
	

         
		 $data = array(
               'group_id' => $group_id,
               'member_uid' =>  $query->row('id'),
			   'pending1' => '1'
			   );

  $this->db->insert('members', $data); 
  
	 return true;
	 
	 
	 
   }
   

  
  }

//Leave
  function Leave($group_id,$member_id)
  {

   $this->db->delete('members',array('group_id' => $group_id,'member_uid' => $member_id));
   $this->db->delete('messages',array('group_id' => $group_id,'member_uid' => $member_id));
  
   return true;
  }
  
  
  
  
  
  
  
 //Check If Owner 
    
	
	function IsOwner($group_id,$owner_uid)
	{
	
	
	$query  =$this->db->get_where('groups',array('id'=>$group_id,'owner_uid'=>$owner_uid));
	
	if($query->num_rows() > 0) 
	{
	
	return true;
	
	}
	else
	{
	
	return false;
	
	}
	
	
	
	
	}
  
 
  function GroupName($id)
  {
  
  $q = $this->db->get_where('groups',array('id' => $id));
  
  $name = $q->row('name');
  
  return $name;
 
 } 

 function ChangeGroupName($id,$name)
 {
 
 $data = array(
            'name' => $name
            );
      $this->db->where('id =', $id);
$q = $this->db->update('groups', $data); 
 
   return true;
 }
 

//Admin Destory

  function AdminDestory($group_id)
  {
  
  $query = $this->db->get_where('groups',array('id' => $group_id) );
 if($query->num_rows() > 0 )
   {
    $this->db->delete('groups',array('id' => $group_id));
	$this->db->delete('messages',array('group_id' => $group_id));
	$this->db->delete('members',array('group_id' => $group_id));
	
     
	 return true;
   }
  else
   {
    
	return false;
   }
   
     
  }

function getMyDefGroup($iUserId)
{
    $q = $this->db->get_where('groups',array('owner_uid' => $iUserId,'is_def'=>1));
    $id = $q->row('id');
    if(!$id)
    {
        $q = $this->db->get_where('groups',array('owner_uid' => $iUserId));
        $id = $q->row('id');
    }
    return $id;
} 

function canCreateGroup()
{
    $userID = $this->session->userdata('id');
    
    $sqlConf = mysql_query("SELECT * FROM usertype_config WHERE id IN (SELECT membertype FROM users WHERE id='$userID')") or die(mysql_error());
	$aConf=mysql_fetch_array($sqlConf,MYSQL_ASSOC);
    
	$sqlGroupCount = mysql_query("SELECT count(*) as cnt FROM groups WHERE owner_uid='$userID'") or die(mysql_error());
	$aCount=mysql_fetch_array($sqlGroupCount,MYSQL_ASSOC);
    
	//print_r($aCount);exit();
    
	if($aConf['can_create_count_groups']>$aCount['cnt'])
    {
	   return true;
    }
    else 
    {
        return false;
    }
} 



}
?>
