<?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)
  {
  
     $data = array(
               'name' => $name,
               //'type' =>  $type,
			   'pending' =>  $type,
               'owner_uid' => $owner_id,
			   'date_time' => date('Y-m-d h:i:s')      
			   );

 $query = $this->db->insert('groups', $data); 
  
 $query1= $this->db->get_where('groups',array('date_time'=>$data['date_time'],'owner_uid'=> $owner_id,'type' => $type,'name'=>$name));
  
      $join = array(
               'group_id' => $query1->row('id'),
               'member_uid' =>  $owner_id
			   );
   $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));
   
   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));
   
   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') );
   
   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)
  {
  
  
 $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')
			   );

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

	 $query = $this->db->get_where('invites',array('group_id' => $group_id ,'email'=> $email));
     
	 if($query->num_rows() > 0 )
	 {
	 
	  $query = $this->db->get_where('users',array('email' => $email));
	          
		 $data = array(
               'group_id' => $group_id,
               'member_id' =>  $query->row('id')
			   );

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

	 
   }

  
  }


//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;
   }
   
     
  }



}
?>
