<?php
class Message extends Model
{
  function Message()
  {
    parent::Model();
  }
 
 //Post Message Function //
  function post($member_uid,$group_id,$txt)
  {
   $query = $this->db->get_where('members',array('member_uid' => $member_uid,'group_id' => $group_id));
   
if($query->num_rows() >0)
{
   $data = array(
               'member_uid' => $member_uid,
               'group_id' =>  $group_id,
               'txt' => $txt,
			   'time_stamp' => time()
			   );

  $this->db->insert('messages', $data); 
  //List All Users From group //
  
   $query1 = $this->db->get_where('members',array('group_id' => $group_id));
   
   if($query1->num_rows() >0)
{
 foreach($query1->result()  as $row)
 {			 
 
 $query2 = $this->db->get_where('users',array('id' => $row->member_uid));	
 
 if($query2->row('number') == '0')
 {
 
  // nothing
 }
 else
 {
 file_get_contents ("http://24.10.39.134:8080/send?password=1111&recipient=".$query2->row('number')."&text=".urlencode($txt)."'"); 			
 }
 //fopen("http://24.10.39.134:8080/send?password=1111&recipient='".$query->row('number')."'&text='".urlencode($txt)."'", "r");
  
  }
  
 } 
   
   
  return true;
  
  }
  else
  {
  return false;
  }

  }

// View  group messages  function //
  
  function view_group_txt($group_id,$from = FALSE)
  {


  $this->db->order_by('id desc');
  $this->db->limit(5);
  
  if(is_numeric($from))
  {
  $this->db->where('id <',$from);
  }
  
  
 $query = $this->db->get_where('messages',array('group_id' => $group_id));
  

  return $query;

 
 
   }


// View user all group messages  function //
  
  function view_user_all_groups_txt($group_ids,$from = FALSE)
  {


  $this->db->order_by('id desc');
  $this->db->limit(5);
  $this->db->where_in('group_id',$group_ids);
  
  if(is_numeric($from))
  {
  $this->db->where('id <',$from);
  }
  
  
 $query = $this->db->get('messages');
  

  return $query;

 
 
   }










 
  function usernameByUID($id)
  {
  
  $query = $this->db->get_where('users',array('id' => $id));
  
  if($query->num_rows() > 0)
  {
  
  $username = $query->row('username');
  
  return $username;
  }
  else
  {
  return false;
  }
  
  
  
  
  }
  
  
/* Works out the time since the entry post, takes a an argument in unix time (seconds)
*/
 function Timesince($original) {
    // array of time period chunks
    $chunks = array(
	array(60 * 60 * 24 * 365 , 'year'),
	array(60 * 60 * 24 * 30 , 'month'),
	array(60 * 60 * 24 * 7, 'week'),
	array(60 * 60 * 24 , 'day'),
	array(60 * 60 , 'hour'),
	array(60 , 'min'),
	array(1 , 'sec'),
    );

    $today = time(); /* Current unix time  */
    $since = $today - $original;

    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {

	$seconds = $chunks[$i][0];
	$name = $chunks[$i][1];

	// finding the biggest chunk (if the chunk fits, break)
	if (($count = floor($since / $seconds)) != 0) {
	    break;
	}
    }

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";

    if ($i + 1 < $j) {
	// now getting the second item
	$seconds2 = $chunks[$i + 1][0];
	$name2 = $chunks[$i + 1][1];

	// add second item if its greater than 0
	if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
	    $print .= ($count2 == 1) ? ', 1 '.$name2 : " $count2 {$name2}s";
	}
    }
    return $print;
}

  
  
  
function getPhoto($id)
{

  $query = $this->db->get_where('users',array('id' => $id));
  
  if($query->num_rows() > 0)
  {
  
  $photo = $query->row('photo');

  return $photo;

  }
  else
  {
   return false;
  }
  
  

}






function get_all_messages($name = FALSE)
{

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


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



  return $query;
  
 } 

function MessageById($id)
{

$query = $this->db->get_where('messages',array('id'=>$id));

$name = $query->row('txt');

return $name;


}



 function ChangeMessage($id,$name)
 {
 
 $data = array(
            'txt' => $name
            );
      $this->db->where('id =', $id);
$q = $this->db->update('messages', $data); 
 
   return true;
 }
 
 
 function DestoryMessage($id)
 {
 
 $this->db->where('id =',$id);
 $this->db->delete('messages'); 
 
  return true;
 
 
 
 }
  



}
?>
