<?php
function pr($array=array())
{
	echo '<pre>';
	print_r($array);
	die;
}

function chkReadPermission($slug=null,$id=null)
{
    $CI =& get_instance();
    $CI->db->select('read_access');
    $CI->db->from('user_permissions');
    $CI->db->join('modules','modules.id = user_permissions.module_id');
    $CI->db->where('modules.slug',$slug);
    $CI->db->where('user_permissions.user_type_id',$id);
    $query=$CI->db->get();
    $res=$query->row_array();
    if(!empty($res))
    {
        return $res['read_access'];
    }
    elseif($id==1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

function chkWritePermission($slug=null,$id=null)
{
    $CI =& get_instance();
    $CI->db->select('write_access');
    $CI->db->from('user_permissions');
    $CI->db->join('modules','modules.id = user_permissions.module_id');
    $CI->db->where('modules.slug',$slug);
    $CI->db->where('user_permissions.user_type_id',$id);
    $query=$CI->db->get();
    $res=$query->row_array();
    if(!empty($res))
    {
        return $res['write_access'];
    }
    elseif($id==1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

function getFiles($file_id=null)
{
    $data = array();
    $CI =& get_instance();
    $CI->db->select('*');
    $CI->db->from('sys_files');
    $CI->db->where('id',$file_id);
    $CI->db->where('is_image',1);
    $get = $CI->db->get();
    return $get->row_array();
}
function getValue($table='',$where=null)
{
    $data = array();
    $CI =& get_instance();
    $CI->db->select('*');
    $CI->db->from($table);
    $CI->db->where($where);
    $get = $CI->db->get();
    return $get->row_array();
}
function getValues($where=null)
{
    $data = array();
    $CI =& get_instance();
    $CI->load->model('common_model');
    $getUserData = $CI->common_model->getAllRow('sys_files',$where,array('ugal_images','ugal_images.file_id = sys_files.id'));
    
    return $getUserData;
}

function getLang($key='')
{
    if(get_cookie('lang'))
    { 
        $lang = get_cookie('lang'); 
    } 
    else 
    { 
        $lang = 2; 
    } 
    $data = array();
    $CI =& get_instance();
    $CI->db->select('*');
    $CI->db->from('language_values');
    $CI->db->where('key',$key);
    $CI->db->where('lang_id',$lang);
    $get = $CI->db->get();
    $res = $get->row_array();
    if(!empty($res))
    {
        return trim($res['value']);
    }
    else
    {
        return $key;
    } 
}
function zodiac($dob)
{
    $zodiac = "";

    if(!empty($dob)){
        $day = (int)date("j", $dob);
        $month = (int)date("n", $dob);

        if     ( ( $month == 3 && $day > 20 ) || ( $month == 4 && $day < 20 ) ) { $zodiac = "Aries"; }
        elseif ( ( $month == 4 && $day > 19 ) || ( $month == 5 && $day < 21 ) ) { $zodiac = "Taurus"; }
        elseif ( ( $month == 5 && $day > 20 ) || ( $month == 6 && $day < 21 ) ) { $zodiac = "Gemini"; }
        elseif ( ( $month == 6 && $day > 20 ) || ( $month == 7 && $day < 23 ) ) { $zodiac = "Cancer"; }
        elseif ( ( $month == 7 && $day > 22 ) || ( $month == 8 && $day < 23 ) ) { $zodiac = "Leo"; }
        elseif ( ( $month == 8 && $day > 22 ) || ( $month == 9 && $day < 23 ) ) { $zodiac = "Virgo"; }
        elseif ( ( $month == 9 && $day > 22 ) || ( $month == 10 && $day < 23 ) ) { $zodiac = "Libra"; }
        elseif ( ( $month == 10 && $day > 22 ) || ( $month == 11 && $day < 22 ) ) { $zodiac = "Scorpio"; }
        elseif ( ( $month == 11 && $day > 21 ) || ( $month == 12 && $day < 22 ) ) { $zodiac = "Sagittarius"; }
        elseif ( ( $month == 12 && $day > 21 ) || ( $month == 1 && $day < 20 ) ) { $zodiac = "Capricorn"; }
        elseif ( ( $month == 1 && $day > 19 ) || ( $month == 2 && $day < 19 ) ) { $zodiac = "Aquarius"; }
        elseif ( ( $month == 2 && $day > 18 ) || ( $month == 3 && $day < 21 ) ) { $zodiac = "Pisces"; }
        else { $zodiac = ""; }
    }

    return $zodiac;
}
function delete_cache($uri_string=null)
{
    $CI =& get_instance();
    $path = $CI->config->item('cache_path');
    $path = rtrim($path, DIRECTORY_SEPARATOR);

    $cache_path = ($path == '') ? APPPATH.'cache/' : $path;

    $uri =  $CI->config->item('base_url').
            $CI->config->item('index_page').
            $uri_string;

    $cache_path .= md5($uri);
    return unlink($cache_path);
}
function conversation($from_id = '', $to_id = '')
{
    $CI =& get_instance();
    $res = $CI->db->query('select msg_messages.created, msg_conversations_users.user_id,msg_messages.text,msg_conversations.id as id from msg_conversations left join msg_messages on msg_conversations.id = msg_messages.conversation_id left join msg_conversations_users on msg_conversations_users.conversation_id = msg_messages.conversation_id where (from_user_id = '.$from_id.' and user_id = '.$to_id.') or (from_user_id = '.$to_id.' and user_id = '.$from_id.') order by msg_conversations.id DESC');
    $result = $res->row_array();
    return $result; 
}
function checkStep()
{
    $CI =& get_instance();
    if($CI->session->userdata('user_id'))
    {
        $CI->db->select('*');
        $CI->db->from('feu_user_steps');
        $CI->db->where('user_id',$CI->session->userdata('user_id'));
        $res = $CI->db->get();
        if(!empty($res->row_array()))
        {
            return 1;
        }
        else
        {
            return 2;
        }
    }
    else
    {
        return 3;
    }
}
function avatarCron()
{
    $CI =& get_instance();
    $CI->load->model('common_model');
    $getFileData = $CI->common_model->getAll('sys_files',array('temp_name !='=>''));
    if(!empty($getFileData))
    {
        foreach ($getFileData as $value) {
            if($value['created'] < time()-8*60*60)
            {
                $CI->common_model->Updatedata('sys_files',array('filename'=>$value['temp_name'],'temp_name'=>''),array('id'=>$value['id']));
            }
        }
    }
}
function get_time_ago( $time )
{
    $time_difference = time() - $time;

    if( $time_difference < 1 ) { return 'less than 1 second ago'; }
    $condition = array( 12 * 30 * 24 * 60 * 60 =>  'year',
                30 * 24 * 60 * 60       =>  'month',
                24 * 60 * 60            =>  'day',
                60 * 60                 =>  'hour',
                60                      =>  'minute',
                1                       =>  'second'
    );

    foreach( $condition as $secs => $str )
    {
        $d = $time_difference / $secs;

        if( $d >= 1 )
        {
            $t = round( $d );
            return 'about ' . $t . ' ' . $str . ( $t > 1 ? 's' : '' ) . ' ago';
        }
    }
}
function get_time_to_go( $time )
{
    $time_difference = $time - time();

    if( $time_difference < 1 ) { return 'less than 1 second ago'; }
    $condition = array( 12 * 30 * 24 * 60 * 60 =>  'year',
                30 * 24 * 60 * 60       =>  'month',
                24 * 60 * 60            =>  'day',
                60 * 60                 =>  'hour',
                60                      =>  'minute',
                1                       =>  'second'
    );

    foreach( $condition as $secs => $str )
    {
        $d = $time_difference / $secs;

        if( $d >= 1 )
        {
            $t = round( $d );
            return  $t . ' ' . $str . ( $t > 1 ? 's' : '' ) . ' to go';
        }
    }
}
function profile_percent($userID='')
{
    $CI =& get_instance();
    $CI->load->model('common_model');
    $getUserData = $CI->common_model->getAllRow('feu_userdetails',array('user_id'=>$userID));
    $getFileData = $CI->common_model->getAllRowMultipleJoin('sys_files',array('feu_userdetails.user_id'=>$userID,'avatar_file_id >'=>0,'filename !='=>''),array(array('feu_userdetails','feu_userdetails.avatar_file_id = sys_files.id')),'sys_files.created DESC','','');
    $complete = 60;
    if(!empty($getUserData))//avatar step
    {
        if(!empty($getFileData))
        {
            $complete = 60;
        }
        if($getUserData['about']!='')//profile text step
        {
            $complete = $complete + 10;
        }
        if($getUserData['he_height']> 0 && $getUserData['he_weight']> 0)//height & weight step
        {
            $complete = $complete + 10;
        }
        $getExtraFileData = $CI->common_model->getAllMultipleJoin('sys_files',array('ugal_images.user_id'=>$userID,'filename !='=>''),array(array('ugal_images','ugal_images.file_id = sys_files.id')),'sys_files.created DESC','','');
        if(count($getExtraFileData)>=3)//profile pic step
        {
            $complete = $complete + 10;
        }
    }
    return $complete;
}
function newMessage($userID)
{
    $CI =& get_instance();
    $CI->load->model('common_model');
    $userdata = $CI->common_model->getAllRow('usr_users',array('id'=>$userID));
    $getData = array();
    if(!empty($userdata) && $userdata['last_login']!='')
    {
        $getData = $CI->common_model->getAll('notification',array('to_id'=>$userID,'type'=>4,'read_unread'=>0,'created >'=>$userdata['last_login']));
    }
    //echo $CI->db->last_query(); 
    return count($getData);
}
function newMembers($userID)
{
    $CI =& get_instance();
    $CI->load->model('common_model');
    $userdata = $CI->common_model->getAllRow('usr_users',array('id'=>$userID));
    $getData = array();
    if(!empty($userdata) && $userdata['last_login']!='')
    {
        $where = "to_id =1 and type = 11 and read_unread = 0 and created >".$userdata['last_login'];
        $getData = $CI->common_model->getAll('notification',$where);
    }   
    return count($getData);
}
function newContacts($userID)
{
    $CI =& get_instance();
    $CI->load->model('common_model');
    $userdata = $CI->common_model->getAllRow('usr_users',array('id'=>$userID));
    $getData = array();
    if(!empty($userdata) && $userdata['last_login']!='')
    {
        $where = "to_id =".$userID." and type in ('1','8','9','10') and read_unread = 0 and created >".$userdata['last_login'];
        $getData = $CI->common_model->getAll('notification',$where);
    }    
    //echo $CI->db->last_query(); 
    return count($getData);
}
function partyDetails($userID)
{
    $CI =& get_instance();
    $CI->load->model('common_model');
    $userdata = $CI->common_model->getAllRow('feu_userdetails',array('user_id'=>$userID));
    $eventdata = array();
    if(!empty($userdata))
    {
        $eventdata = $CI->common_model->getAllMultipleJoin('event_guest',array('username'=>$userdata['username'],'payment'=>1,'time_from >='=>time()),array(array('evt_events','evt_events.id = event_guest.event_id')));
    }    
    //echo $CI->db->last_query(); 
    return count($eventdata);
}
function resizeImage($url, $width = 150, $height = true,$default=0) {
    //return file_get_contents($url); die;
    if($default==1)
    {
        list($width, $height) = getimagesize($url);
    }
    $image = ImageCreateFromString(file_get_contents($url));
    //return $image; die;
    $height = $height === true ? (ImageSY($image) * $width / ImageSX($image)) : $height;
    $output = ImageCreateTrueColor($width, $height);
    ImageCopyResampled($output, $image, 0, 0, 0, 0, $width, $height, ImageSX($image), ImageSY($image));
    ob_start();
    imagejpeg($output);
    $imgRawData = ob_get_clean(); 
    $imgData = base64_encode($imgRawData);
    $src = 'data:image/png;base64,'.$imgData;

    /**************SAVE IMAGE**************/
    if($default!=1)
    {
        $folderpath = FCPATH.'assets/userfiles/'.$width;
        if (!file_exists($folderpath)) {
            mkdir($folderpath, 0777, true);
        }
        $filenames = explode('/', $url);
        $filename = end($filenames);
        $filepath = FCPATH.'assets/userfiles/'.$width.'/'.$filename;
        if (!file_exists($filepath)) {
            $ifp = fopen($filepath, "wb"); 
            $data = explode(',', $src);
            fwrite($ifp, base64_decode($data[1])); 
            //fclose($ifp);
        }
    }    
    /**************SAVE IMAGE**************/
    return $src;
}
function blurImage($url, $width = 150, $height = true,$default=0) {
    //return file_get_contents($url); die;
    if($default==1)
    {
        list($width, $height) = getimagesize($url);
    }
    $image = ImageCreateFromString(file_get_contents($url));
    //return $image; die;
    $height = $height === true ? (ImageSY($image) * $width / ImageSX($image)) : $height;
    $output = ImageCreateTrueColor($width, $height);
    ImageCopyResampled($output, $image, 0, 0, 0, 0, $width, $height, ImageSX($image), ImageSY($image));
    ob_start();
    imagejpeg($output);
    $imgRawData = ob_get_clean(); 
    //$imgData = base64_encode($imgRawData);
    //$src = 'data:image/png;base64,'.$imgData;
    return $output;
}

