<?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('usr_modules');
    $CI->db->join('modules','modules.id = usr_modules.module_id');
    $CI->db->where('modules.slug',$slug);
    $CI->db->where('usr_modules.user_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('usr_modules');
    $CI->db->join('modules','modules.id = usr_modules.module_id');
    $CI->db->where('modules.slug',$slug);
    $CI->db->where('usr_modules.user_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 getAllValue($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->result_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='')
{
    $CI =& get_instance();
    $lang = 2; 
    if($CI->session->userdata('user_id')) {
        $userdetails = $CI->common_model->getAllRow('feu_userdetails',array('user_id'=>$CI->session->userdata('user_id')));
        if($userdetails){
            if($userdetails['userlanguage'] > 0){
                $lang = $userdetails['userlanguage'];
            }
        }
    } elseif(get_cookie('lang')) { 
        $lang = get_cookie('lang'); 
    } else { 
        $lang = 2; 
    } 
    $data = array();
    $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 getUserLang($key='', $user_id='')
{
    $CI =& get_instance();
    $lang = 2; 
    if($user_id > 0) {
        $userdetails = $CI->common_model->getAllRow('feu_userdetails',array('user_id'=>$user_id));
        if($userdetails){
            if($userdetails['userlanguage'] > 0){
                $lang = $userdetails['userlanguage'];
            }
        }
    } elseif($CI->session->userdata('user_id')) {
        $userdetails = $CI->common_model->getAllRow('feu_userdetails',array('user_id'=>$CI->session->userdata('user_id')));
        if($userdetails){
            if($userdetails['userlanguage'] > 0){
                $lang = $userdetails['userlanguage'];
            }
        }
    } elseif(get_cookie('lang')) { 
        $lang = get_cookie('lang'); 
    } else { 
        $lang = 2; 
    } 
    $data = array();
    $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 getAdminLang($key='',$lang='')
{
    if($lang=='') 
    { 
        $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']));
            }
        }
    }

    $videoSql = $CI->db->query("SELECT * FROM sys_videos where accepted = 0 AND created_at < DATE_SUB(NOW(), INTERVAL 8 HOUR)");
    $todaysVideos = $videoSql->result_array();
    if(!empty($todaysVideos))
    {
        foreach ($todaysVideos as $video) {
            $CI->common_model->UpdateData('sys_videos',array('accepted'=>1, 'created_at'=>date('Y-m-d H:i:s',time()-8*60*60)),array('id'=>$video['id']));
            $video_name = explode('.', $video['filename']);
            if(count($video_name)>1){
                @unlink(FCPATH.'assets/videos/uploads/'.$video_name[0].'.mp4');
                @unlink(FCPATH.'assets/videos/uploads/'.$video_name[0].'.webm');
            }
        }
    }
}
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
        {
            if($getUserData['accounttype']=='gentleman')
            {
                $complete = $complete + 20;  
            }
            else
            {
                $complete = $complete + 20;
            }
        }
        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','','');
        //print_r($getExtraFileData); die;
        if(count($getExtraFileData)>=3)//profile pic step
        {
            $complete = $complete + 10;
        }
        $userParticipation = $CI->common_model->getAllMultipleJoin('shop_orders',array('shop_carts.user_id'=>$userID,'payed'=>1,'shop_orders.is_booking'=>1,'shop_booking_details.event_id >'=>0),array(array('shop_carts','shop_carts.id = shop_orders.cart_id'),array('shop_booking_details','shop_booking_details.cart_id = shop_carts.id'),array('evt_events','evt_events.id = shop_booking_details.event_id'),array('evt_language_values','evt_events.id = evt_language_values.event_id')),'shop_carts.created DESC','*,evt_events.alias','shop_booking_details.cart_id');
        //$userParticipation = $CI->common_model->getAllMultipleJoin('event_guest',array('username'=>$getUserData['username'],'event_guest.payment'=>1),array(array('evt_events','evt_events.id = event_guest.event_id'),array('evt_language_values','evt_events.id = evt_language_values.event_id')),'event_guest.confirmed','','event_guest.event_id');
        //return $CI->db->last_query(); die;
        $userParticipationOld = $CI->common_model->getAllMultipleJoin('shop_orders',array('shop_carts.user_id'=>$userID,'payed'=>1,'shop_articles_carts.event_id >'=>0),array(array('shop_carts','shop_carts.id = shop_orders.cart_id'),array('shop_articles_carts','shop_articles_carts.cart_id = shop_carts.id'),array('evt_events','evt_events.id = shop_articles_carts.event_id'),array('evt_language_values','evt_events.id = evt_language_values.event_id')),'shop_carts.created DESC','','shop_articles_carts.event_id');
        
        if((count($userParticipation)>0 || count($userParticipationOld)>0 || $getUserData['verified']==1 || $getUserData['reality_authenticated']==1) && $getUserData['accounttype']!='gentleman')//attend party pic step
        {
            $complete = $complete + 10;
        }
    }
    if($complete > 100){
        $complete = 100;
    }
    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));
    }
    //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(),'payed'=>1),array(array('evt_events','evt_events.id = event_guest.event_id'),array('shop_orders','shop_orders.order_id = event_guest.order_id')));
    }    
    //echo $CI->db->last_query(); 
    return count($eventdata);
}
function participation_new($userID)
{
    $langId = 2;
    if(get_cookie('lang'))
    {
        $langId = get_cookie('lang');
    }
    $CI =& get_instance();
    $CI->load->model('common_model');
    $userDetials = $CI->common_model->getAllRow('feu_userdetails',array('user_id'=>$userID));
    
    $userParticipation = $CI->common_model->getAllRowMultipleJoin('shop_orders',array('shop_carts.user_id'=>$CI->session->userdata('user_id'),'payed'=>1,'shop_orders.is_booking'=>1,'shop_booking_details.event_id >'=>0,'time_from <'=>time()),array(array('shop_carts','shop_carts.id = shop_orders.cart_id'),array('shop_booking_details','shop_booking_details.cart_id = shop_carts.id'),array('evt_events','evt_events.id = shop_booking_details.event_id'),array('evt_language_values','evt_events.id = evt_language_values.event_id'),array('evt_event_files','evt_event_files.event_id = evt_events.id'),array('sys_files','sys_files.id = evt_event_files.file_id')),'shop_carts.created DESC','evt_events.time_from,evt_language_values.title,sys_files.path,sys_files.filename,sys_files.extension','shop_booking_details.event_id');
    //echo $CI->db->last_query();
    $arr = array();
    if($userParticipation)
    {
        return $userParticipation;
    }
    else
    {
        return $arr;
    }
}  
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;
}
function eventDetails($eventId='')
{
    $CI =& get_instance();
    if(get_cookie('lang'))
    { 
        $lang = get_cookie('lang'); 
    } 
    else 
    { 
        $lang = 2; 
    } 
    $CI->load->model('common_model');
    $eventdata = $CI->common_model->getAllRowMultipleJoin('loc_events_locations',array('event_id'=>$eventId,'lang_id'=>$lang),array(array('loc_locations','loc_locations.id = loc_events_locations.location_id'),array('loc_location_lang_values','loc_location_lang_values.location_id = loc_locations.id')));
    
    if(!empty($eventdata))
    {
        return $eventdata['name'];
    }
    else
    {
        $eventsdata = $CI->common_model->getAllRowMultipleJoin('loc_events_locations',array('event_id'=>$eventId),array(array('loc_locations','loc_locations.id = loc_events_locations.location_id')));
        //return $CI->db->last_query(); die;
        if(!empty($eventsdata))
        {
            return $eventsdata['name'];
        }
        else
        {
            return '';
        }
    }
}
function checkCardUpdated()
{
    $CI =& get_instance();
    $CI->load->model('common_model');
    $expiry = $CI->common_model->getAllRow('usr_users',array('id'=>$CI->session->userdata('user_id'))) ;
    $steps = $CI->common_model->getAllRow('feu_user_steps',array('user_id'=>$CI->session->userdata('user_id'))) ;
    $exp = $expiry['saferpay_exp']; 
    $expDate = sprintf("%04s", $exp);
    $expdate = "31-".substr($expDate, 0,2).'-20'.substr($expDate, 2,2);
    $date = date('Y-m-d');
    $effectiveDate = date('d-m-Y',strtotime(date("Y-m-d", strtotime($date)) . " +3 month")); 
    /*if(empty($steps)){    
        if($expiry['customer_id']!='') { 
            if(strtotime($expdate)>strtotime($effectiveDate)){ 
                return 1; 
            } else { 
                return 2; 
            } 
        } 
        else{ 
            return 1; 
        } 
    }
    else{
        return 1;
    }*/
    return 1;
}
function checkMobileVerified($userID='')
{
    $CI =& get_instance();
    $CI->load->model('common_model');
    $getUserData = $CI->common_model->getAllRow('feu_userdetails',array('user_id'=>$userID));
    $userParticipation = $CI->common_model->getAllMultipleJoin('shop_orders',array('shop_carts.user_id'=>$userID,'payed'=>1,'shop_orders.is_booking'=>1,'shop_booking_details.event_id >'=>0),array(array('shop_carts','shop_carts.id = shop_orders.cart_id'),array('shop_booking_details','shop_booking_details.cart_id = shop_carts.id'),array('evt_events','evt_events.id = shop_booking_details.event_id'),array('evt_language_values','evt_events.id = evt_language_values.event_id')),'shop_carts.created DESC','*,evt_events.alias','shop_booking_details.cart_id');
    
    $userParticipationOld = $CI->common_model->getAllMultipleJoin('shop_orders',array('shop_carts.user_id'=>$userID,'payed'=>1,'shop_articles_carts.event_id >'=>0),array(array('shop_carts','shop_carts.id = shop_orders.cart_id'),array('shop_articles_carts','shop_articles_carts.cart_id = shop_carts.id'),array('evt_events','evt_events.id = shop_articles_carts.event_id'),array('evt_language_values','evt_events.id = evt_language_values.event_id')),'shop_carts.created DESC','','shop_articles_carts.event_id');
    //return $this->db->last_query(); die;
    if(count($userParticipation)>0 || count($userParticipationOld)>0)
    {
        if($getUserData['mobile_number']>0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 1;
    }
}
function combine_my_files($array_files, $destination_dir, $dest_file_name){

    if(!is_file($destination_dir . $dest_file_name)){ //continue only if file doesn't exist
        $content = "";
        foreach ($array_files as $file){ //loop through array list
            $content .= file_get_contents($file); //read each file
        }

        //You can use some sort of minifier here 
        //minify_my_js($content);

        $new_file = fopen($destination_dir . $dest_file_name, "w" ); //open file for writing
        fwrite($new_file , $content); //write to destination
        fclose($new_file); 
        return '<script src="'. $destination_dir . $dest_file_name.'"></script>'; //output combined file
    }else{ 
        //use stored file
        return '<script src="'. $destination_dir . $dest_file_name.'"></script>'; //output combine file
    }
}
function getonline()
{
    date_default_timezone_set("Europe/Zurich");
    $CI =& get_instance();
    $CI->load->model('common_model');
    $getUserData = $CI->common_model->getAll('feu_userdetails',array('online_status'=>1));
    $online = 0;
    if(date("H")>22 && date("H")<24)
    {
        if(count($getUserData)>50)
        {
            $online =  intval(count($getUserData)-50);
        }
        else
        {
            $online =  intval(count($getUserData)-(count($getUserData)*0.75));
        }
    }
    elseif(date("H")>1 && date("H")<10)
    {
        if(count($getUserData)>20)
        {
            $online =  intval(count($getUserData)-20);
        }
        else
        {
            $online =  intval(count($getUserData)-(count($getUserData)*0.25));
        }
    }
    else
    {
        $online =  intval(count($getUserData)-(count($getUserData)*rand(10,90)/100));
    }
    if($online>0)
    {
        return $online;
    }
    else
    {
        return count($getUserData);
    }
}
function GetPartyName($cartId='')
{
    $CI =& get_instance();
    $CI->load->model('common_model');
    $userParticipationOld = $CI->common_model->getAllRowMultipleJoin('shop_booking_details',array('cart_id'=>$cartId),array(array('evt_events','evt_events.id = shop_booking_details.event_id')));
    return $userParticipationOld;
}
function FriendUnfriend($loginId='',$userId='')
{
    $CI =& get_instance();
    $CI->load->model('common_model');
    $friend = $CI->common_model->getAllRowMultipleJoin('feu_relationships',array('user_id_1'=>$loginId,'user_id_2'=>$userId,'type'=>'friend'));
    $friendGuest = $CI->common_model->getAllRowMultipleJoin('feu_relationships',array('user_id_1'=>$loginId,'user_id_2'=>$userId,'type'=>'guest'));
    $unfriend = $CI->common_model->getAllRowMultipleJoin('feu_relationships',array('user_id_2'=>$loginId,'user_id_1'=>$userId,'type'=>'friend'));
    $unfriendGuest = $CI->common_model->getAllRowMultipleJoin('feu_relationships',array('user_id_2'=>$loginId,'user_id_1'=>$userId,'type'=>'guest'));
    if(count($friend)>0 || count($unfriend)>0 || count($friendGuest)>0 || count($unfriendGuest)>0)
    {
        if($friend['status']=='accepted' || $unfriend['status']=='accepted' || $friendGuest['status']=='guest' || $unfriendGuest['status']=='guest')
        {    
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }
}
function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
    $output = NULL;
    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
        $ip = $_SERVER["REMOTE_ADDR"];
        if ($deep_detect) {
            if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
    }
    $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
    $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
    $continents = array(
        "AF" => "Africa",
        "AN" => "Antarctica",
        "AS" => "Asia",
        "EU" => "Europe",
        "OC" => "Australia (Oceania)",
        "NA" => "North America",
        "SA" => "South America"
    );
    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
        $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
        if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
            switch ($purpose) {
                case "location":
                    $output = array(
                        "city"           => @$ipdat->geoplugin_city,
                        "state"          => @$ipdat->geoplugin_regionName,
                        "country"        => @$ipdat->geoplugin_countryName,
                        "country_code"   => @$ipdat->geoplugin_countryCode,
                        "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                        "continent_code" => @$ipdat->geoplugin_continentCode
                    );
                    break;
                case "address":
                    $address = array($ipdat->geoplugin_countryName);
                    if (@strlen($ipdat->geoplugin_regionName) >= 1)
                        $address[] = $ipdat->geoplugin_regionName;
                    if (@strlen($ipdat->geoplugin_city) >= 1)
                        $address[] = $ipdat->geoplugin_city;
                    $output = implode(", ", array_reverse($address));
                    break;
                case "city":
                    $output = @$ipdat->geoplugin_city;
                    break;
                case "state":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "region":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "country":
                    $output = @$ipdat->geoplugin_countryName;
                    break;
                case "countrycode":
                    $output = @$ipdat->geoplugin_countryCode;
                    break;
            }
        }
    }
    return $output;
}
function getLocationInfoByIp(){
    $ip = $_SERVER['REMOTE_ADDR'];
    $details = json_decode(file_get_contents("http://api.ipapi.com/".$ip."?access_key=d4fd2b06ecb17c9f314b715ada399988"));
    $result = $details->country_code;//print_r($details);
    return $result;
}
function uerLocation($userId)
{
    $CI =& get_instance();
    $CI->load->model('common_model');
    $uerLocation = $CI->common_model->getAllRowMultipleJoin('feu_userdetails',array('user_id'=>$userId),array(array('feu_countries','feu_countries.country_code = feu_userdetails.country')));
    if($uerLocation)
    {
        return $uerLocation['country_name'];
    }
    else
    {
        return '';
    }
}
function getNights($table='',$where=null)
{
    $data = array();
    $CI =& get_instance();
    $CI->db->select('*');
    $CI->db->from($table);
    $CI->db->where($where);
    $CI->db->order_by('date','DESC');
    $get = $CI->db->get();
    return $get->row_array();
}

function getNightsNew($table='',$where=null)
{
    $data = array();
    $CI =& get_instance();
    $CI->db->select('*');
    $CI->db->from($table);
    $CI->db->where($where);
    $get = $CI->db->get();
    $event_output = $get->row_array();

    $get1 = $CI->db->query("SELECT * FROM evt_events where id = ".$event_output['event_id']);
    $event_details = $get1->row_array();

    $get2 = $CI->db->query("SELECT * FROM event_features ORDER BY ABS(".$event_details['time_from']." - date) LIMIT 1");
    return $get2->row_array();
}

function getOpenBookingInvoices()
{
    $data = array();
    $CI =& get_instance();
    $main_time = (time()-(10*86400));
    /*$getOpenBookingInvoices = $CI->common_model->getAllMultipleJoin('shop_orders',array('shop_orders.order_id >'=>0, 'payed'=>0,'is_booking'=>1, 'time_from >'=>time(),'shop_carts.user_id'=>$CI->session->userdata('user_id'),'shop_orders.created <'=>$main_time, 'cancelled'=>0),array(array('shop_carts','shop_carts.id = shop_orders.cart_id'),array('usr_users','usr_users.id = shop_carts.user_id'),array('shop_booking_details','shop_booking_details.cart_id = shop_carts.id'),array('evt_events','evt_events.id = shop_booking_details.event_id')));
    //echo $CI->db->last_query();die;
    return count($getOpenBookingInvoices);*/

    $get = $CI->db->query("SELECT * FROM (SELECT order_id,DATE(FROM_UNIXTIME(`shop_orders`.`created`)) as order_date,event_id,payed FROM `shop_orders` LEFT JOIN `shop_carts` ON `shop_carts`.`id` = `shop_orders`.`cart_id` LEFT JOIN `usr_users` ON `usr_users`.`id` = `shop_carts`.`user_id` LEFT JOIN `shop_booking_details` ON `shop_booking_details`.`cart_id` = `shop_carts`.`id` LEFT JOIN `evt_events` ON `evt_events`.`id` = `shop_booking_details`.`event_id` WHERE `shop_orders`.`order_id` >0 AND `is_booking` = 1 AND `time_from` > ".time()." AND `shop_carts`.`user_id` = ".$CI->session->userdata('user_id')." AND shop_orders.created < ".$main_time." AND `cancelled` =0 ORDER BY payed DESC) a GROUP BY a.order_date, a.event_id");
    $getOpenBookingInvoices = $get->result_array();
    $main_count = 0;
    if(count($getOpenBookingInvoices)>0){
        foreach ($getOpenBookingInvoices as $key => $value) {
            if($value['payed']==0){
                $main_count = 1;
            }
        }
    }
    //echo $CI->db->last_query();die;
    return $main_count;
}
function getMembershipPrice($code)
{
    $CI =& get_instance();
    if($code=='basic_half_price'){
        $details = $CI->common_model->getAllRow('shop_new_articles',array('id'=>1));
    }
    if($code=='basic_full_price'){
        $details = $CI->common_model->getAllRow('shop_articles',array('id'=>5));
    }
    if($code=='premium_half_price'){
        $details = $CI->common_model->getAllRow('shop_new_articles',array('id'=>2));
    }
    if($code=='premium_full_price'){
        $details = $CI->common_model->getAllRow('shop_articles',array('id'=>4));
    }
    if($code=='platinum_full_price'){
        $details = $CI->common_model->getAllRow('shop_articles',array('id'=>3));
    }
    return $details['base_price']/100;
}

function getUserDataByEmail($email)
{
    $data = array();
    $CI =& get_instance();
    $get = $CI->common_model->getAllRow('deleted_users',array('email'=>$email));
    $username = '';
    if($get){
        $username = $get['username'];
    }
    return $username;
}

function getAllCount($table='',$where=null)
{
    $data = array();
    $CI =& get_instance();
    $CI->db->select('*');
    $CI->db->from($table);
    $CI->db->where($where);
    $get = $CI->db->count_all_results();
    return $get;
}