<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin_m extends CI_Model {
	public function __construct(){
		parent::__construct();
		$this->db->query("SET sql_mode = ''");
	}

	/**
	  ** check username or email while login
	**/
  	public function login_info_check($email,$password)
  	{
        $this->db->select('u.*');
        $this->db->from('users u');
        $this->db->where("(u.email = '$email' OR u.username = '$email')");
        $this->db->where('u.password', md5($password));
        $this->db->limit(1);
        $query = $this->db->get();
        if($query->num_rows() ==1){
            return $query->result();
        }else{
            return false;
        }

    }

    public function check_customer_login($email,$password)
  	{
        $this->db->select('u.*');
        $this->db->from('customer_list u');
        $this->db->where("(u.phone = '$email' OR u.email = '$email')");
        $this->db->where('u.password', md5($password));
        $this->db->limit(1);
        $query = $this->db->get();
        if($query->num_rows() ==1){
            return $query->result();
        }else{
            return false;
        }

    }

    /**
	  ** check valid email for recovery password
	**/
  	public function check_valid_email($email)
  	{
        $this->db->select('u.*');
        $this->db->from('users u');
        $this->db->where('u.email', $email);
        $this->db->limit(1);
        $query = $this->db->get();
        if($query->num_rows() ==1){
            return $query->row_array();
        }else{
            return false;
        }

    }

    /**
      ** check username  while Registration
    **/
    public function check_username($username)
  	{
        $this->db->select('u.username');
        $this->db->from('users u');
        $this->db->where("u.username",$username);
        $this->db->limit(1);
        $query = $this->db->get();
        if($query->num_rows() ==1){
            return 1;
        }else{
            return 0;
        }

    }

    public function get_id_by_package_slug($slug)
	{
		$this->db->select('ft.*');
		$this->db->from('features_type as ft');
		$this->db->where('ft.slug',$slug);
		$this->db->where('ft.status',1);
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}

  // get user for custom helper
 	public function get_user_info()
 	{
		$this->db->select('u.*');
		$this->db->select('(SELECT count(users.id) FROM users 
    	 	WHERE (users.is_active = 1 AND users.is_verify = 1  AND users.user_type != 1)) 
    		AS reg_user',TRUE);
		$this->db->select('(SELECT count(users.id) FROM users) AS total_user',TRUE);
		$this->db->from('users as u');
		$this->db->where('u.id',auth('id'));
		$query = $this->db->get();
		$query = $query->row();
		return $query;
	}

	public function check_update(){
    	return ['check'=>0];
    }


    function get_data($url) {
		$ch = curl_init();
		$timeout = 5;
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;

	}
	

	// get_user_info
	public function get_all_user($limit=0,$per_page,$offset,$total)
	{
		$this->db->select('u.*,ft.type_name,ft.type ');
		$this->db->from('users as u');
		$this->db->join('features_type as ft ','ft.id = u.account_type','LEFT');
		$this->db->where('u.user_type !=',1);
		if($limit !=0){
			$this->db->limit($limit);
		}
		$this->db->order_by('u.id','DESC');

		if(isset($_GET) && !empty($_GET)):
            if(!empty($_GET['name'])){
                $this->db->where('u.username',$_GET['name']);
            }

            if(!empty($_GET['package'])){
                $this->db->where('f.slug',$_GET['package']);
            }
        endif;

        if($total==1){
            $query = $this->db->get();
            $query = $query->num_rows();
        }else{
            $query = $this->db->get('',$per_page,$offset);
            $query = $query->result_array();
        }

		return $query;
	}

	/**
	  ** Update with sesion id
	**/
	public function update_profile($data,$table)
	{
		$this->db->where('id',auth('id'));
		$this->db->update($table,$data);
		return auth('id');
	}

	/**
	  ** Update with sesion id
	**/
	public function update_user($data,$id,$table)
	{
		$this->db->where('user_id',$id);
		$this->db->update($table,$data);
		return $id;
	}

	public function select_all_by_user_id($id,$table)
    {
        $this->db->select();
        $this->db->from($table);
        $this->db->where('user_id',$id);
        $this->db->order_by('id','ASC');
        $query = $this->db->get();
        $query = $query->result_array();
        return $query;
    }


    public function count_all_by_table($table,$status=null)
    {
        $this->db->select();
        $this->db->from($table);
        if($status != null){
       		$this->db->where('status',$status);
        }
        $query = $this->db->get();
        $query = $query->num_rows();
        return $query;
    }

     public function get_users_active_features($id,$user_id)
    {
        $this->db->select('us.*');
        $this->db->from('users_features us');
        $this->db->where('us.feature_id',$id);
        $this->db->where('us.user_id',$user_id);
        $this->db->order_by('id','ASC');
        $query = $this->db->get();
        $query = $query->row_array();
        return $query;
    }


	public function update_by_username($data,$id,$table)
	{
		$this->db->where('username',$id);
		$this->db->update($table,$data);
		return $id;
	}

	/**
	  ** Update with sesion id
	**/
	public function update_setting($data,$id,$table)
	{
		$this->db->where('site_id',$id);
		$this->db->update($table,$data);
		return $id;
	}


	public function update_feature($data,$id,$table)
	{
		$this->db->where('feature_id',$id);
		$this->db->where('user_id',auth('id'));
		$this->db->update($table,$data);
		return $id;
	}

	function item_delete($id,$table)
	{
		$this->db->where('user_id',auth('id'));
		$this->db->delete($table,array('id'=>$id));
		return $id;
	}

	// check password for change password 
  	public function check_pass($pass)
  	{
        $this->db->select('u.*');
        $this->db->from('users as u');
        $this->db->where('u.password', md5($pass));
        $this->db->where('u.id', $this->session->userdata('id'));
        $this->db->limit(1);
        $query = $this->db->get();
        if($query->num_rows() ==1){
            return $query->result();
        }else{
            return false;
        }

    }
 	
    /**
      *** Get setting 
    **/ 
	public function get_setting()
	{
		$this->db->select();
		$this->db->from('setting');
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	} 


	public function get_user_info_by_id($id)
	{
		$this->db->select('u.*');
		$this->db->from('users u');
		$this->db->where('u.id',$id);
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}

	

	public function insert($data,$table)
	{
		
		$this->db->insert($table,$data);
		return $this->db->insert_id();
	}

	public function select($table)
	{
		$this->db->select();
		$this->db->from($table);
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function active_select($table)
	{
		$this->db->select();
		$this->db->from($table);
		$this->db->where('status',1);
		$this->db->where('user_id',auth('id'));
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function select_by_user($table)
	{
		$this->db->select();
		$this->db->from($table);
		$this->db->where('user_id',auth('id'));
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function select_desc($table)
	{
		$this->db->select();
		$this->db->from($table);
		$this->db->order_by('id','DESC');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function select_active_all($table)
	{
		$this->db->select();
		$this->db->from($table);
		$this->db->where('status',1);
		$this->db->order_by('id','ASC');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function select_all_by_user($table)
	{
		$this->db->select();
		$this->db->from($table);
		$this->db->where('user_id',auth('id'));
		$this->db->order_by('id','ASC');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	// select function
	function select_with_status($table)
	{
	    $this->db->select();
	    $this->db->from($table);
	    $this->db->where('status',1);
	    $this->db->order_by('id','ASC');
	    $query = $this->db->get();
	    $query = $query->result_array();  
	    return $query;
	}

	// select function
	function get_social_icon($type)
	{
	    $this->db->select();
	    $this->db->from('social_sites');
	    $this->db->where('status',1);
	    if($type == 1):
		    $this->db->where('is_profile',$type);
		endif;
	    $this->db->order_by('id','ASC');
	    $query = $this->db->get();
	    $query = $query->result_array();  
	    return $query;
	}
	// select function
		function select_all_by_status($table)
		{
		    $this->db->select();
		    $this->db->from($table);
		    $this->db->where('status',1);
		    $this->db->order_by('id','ASC');
		    $query = $this->db->get();
		    $query = $query->result_array();  
		    return $query;
		}

	/**
	  ** Get user resume by auth id
	**/
	public function get_resume_by_user()
	{
		$this->db->select('r.*,rt.type_name,rt.icon');
		$this->db->from('resume r');
		$this->db->join('resume_type rt','rt.id = r.type_id','LEFT');
		$this->db->where('r.user_id',auth('id'));
		$this->db->order_by('r.id','ASC');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	/**
	  ** Get all social sites
	**/
	public function get_social_sites()
	{
		$this->db->select('ss.*');
		$this->db->from('social_sites ss');
		$this->db->where('ss.status',1);
		$this->db->order_by('id','ASC');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}


	/**
	  ** Get all user social sites
	**/
	public function get_user_social_sites($id)
	{
		$this->db->select('us.*');
        $this->db->from('user_social_sites us');
        $this->db->where('us.site_id',$id);
        $this->db->where('us.status',1);
		$this->db->where('us.user_id',auth('id'));
        $this->db->order_by('id','ASC');
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}

	/**
	  ** user home content
	**/

	public function get_home()
	{
		$this->db->select('h.*');
        $this->db->from('home h');
        $this->db->where('h.status',1);
		$this->db->where('h.user_id',auth('id'));
		$this->db->limit(1);
		$query = $this->db->get();
		$query = $query->result_array();
		foreach ($query as $key => $value) {
			$this->db->select('us.*,ss.*, ss.id as site_id,us.id as user_site_id,us.status as user_site_status');
	        $this->db->from('user_social_sites us');
	        $this->db->where('us.user_id',$value['user_id']);
			$this->db->join('social_sites ss','ss.id = us.site_id','RIGHT');
	        $this->db->order_by('ss.id','ASC');
	        $query2 = $this->db->get();
	        $query2 = $query2->result_array();
	        $query[$key]['my_sites'] = $query2;
    	}
		return $query;
	}

	/**
	  ** Get Pricing
	**/
	public function get_pricing()
	{
		$this->db->select('ft.*');
        $this->db->from('features_type ft');
		$query = $this->db->get();
		$query = $query->result_array();
		foreach ($query as $key => $value) {
			$this->db->select('p.*,p.id as pricing_id,f.*,f.id as feature_id');
	        $this->db->from('pricing p');
			$this->db->join('features f','f.id = p.feature_id','RIGHT');
	        $this->db->where('p.type_id',$value['id']);
	        $this->db->order_by('f.id','ASC');
	        $query2 = $this->db->get();
	        $query2 = $query2->result_array();
	        $query[$key]['features'] = $query2;
    	}
		return $query;
	}

	public function get_subscripe_pricing()
	{
		$this->db->select('ft.*');
        $this->db->from('features_type ft');
        $this->db->where('ft.type !=','free');
        $this->db->where('ft.type !=','trial');
		$query = $this->db->get();
		$query = $query->result_array();
		foreach ($query as $key => $value) {
			$this->db->select('p.*,p.id as pricing_id,f.*,f.id as feature_id');
	        $this->db->from('pricing p');
			$this->db->join('features f','f.id = p.feature_id','LEFT');
	        $this->db->where('p.type_id',$value['id']);
	        $this->db->order_by('f.id','ASC');
	        $query2 = $this->db->get();
	        $query2 = $query2->result_array();
	        $query[$key]['features'] = $query2;
    	}
		return $query;
	}

	public function get_active_package()
    {
        $this->db->select('p.*,u.id as user_id,u.account_type,u.end_date');
        $this->db->from('features_type p');
        $this->db->join('users u','u.account_type = p.id','LEFT');
        $this->db->where('u.id',auth('id'));
        $this->db->where('u.is_expired',0);
        $this->db->where('p.status',1);
        $type = array('free','trial');
        $this->db->where_in('p.type',$type);
        $query = $this->db->get();
        $query = $query->row_array();
        return $query;
    }


	/**
	  ** get features from pricing table by feature id
	**/
	public function get_feature_id($id)
	{
		$this->db->select('p.*');
        $this->db->from('pricing p');
        $this->db->where('p.feature_id',$id);
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}


	/**
	  ** get package info
	**/
	public function get_package_info_by_id($id)
	{
		$this->db->select('f.*');
        $this->db->from('features_type f');
        $this->db->where('f.id',$id);
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}

	/**
	  ** get pricing with feature type id and by feature id
	**/
	public function get_price_feature_id($id,$type_id)
	{
		$this->db->select('p.*');
        $this->db->from('pricing p');
        $this->db->where('p.feature_id',$id);
        $this->db->where('p.type_id',$type_id);
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}

	 public function get_active_package_features($id,$type_id)
    {

        $this->db->select('p.*');
        $this->db->from('pricing p');
        $this->db->where('p.feature_id',$id);
        $this->db->where('p.type_id',$type_id);
        $this->db->limit(1);
        $query = $this->db->get();
        if($query->num_rows() ==1):
            return 1;
        else:
            return 0;
        endif;
    }

	function delete_pricing($id,$table)
	{
		$this->db->delete($table,array('type_id'=>$id));
		return $id;
	}

	public function get_about($id)
	{
		$this->db->select('a.*');
		$this->db->from('about a');
		$this->db->where('user_id',auth('id'));
		if($id !=0){
			$this->db->where('id',$id);
		}
		$this->db->order_by('id','ASC');
		$this->db->limit(1);
		$query = $this->db->get();
		$query = $query->result_array();
		foreach ($query as $key => $value) {
			$this->db->select('ac.*');
	        $this->db->from('about_content ac');
	        $this->db->where('ac.about_id',$value['id']);
	        $this->db->order_by('id','ASC');
	        $query2 = $this->db->get();
	        $query2 = $query2->result_array();
	        $query[$key]['about_content'] = $query2;
    	}
		return $query;
	}

	public function verify_mail($data,$username,$table)
	{
		$this->db->where('username',$username);
		$this->db->update($table,$data);
		return 1;
	}

	/**
	  ** Get Pricing for user profile
	**/
	public function get_user_pricing_by_id()
	{
		$this->db->select('ft.*,u.account_type');
        $this->db->from('features_type ft');
        $this->db->join('users u','u.account_type=ft.id','LEFT');
	    $this->db->where('u.id',auth('id'));
		$query = $this->db->get();
		$query = $query->result_array();
		foreach ($query as $key => $value) {
			$this->db->select('p.*,p.id as pricing_id,f.*,f.id as feature_id');
	        $this->db->from('pricing p');
			$this->db->join('features f','f.id = p.feature_id','RIGHT');
	        $this->db->where('p.type_id',$value['id']);
	        $this->db->order_by('f.id','ASC');
	        $query2 = $this->db->get();
	        $query2 = $query2->result_array();
	        $query[$key]['features'] = $query2;
    	}
		return $query;
	}

	 public function get_new_users($limit=0)
    {
    	$date = get_interval('-',7,'days');
        $this->db->select('u.*,p.type_name,p.type ');
        $this->db->from('users as u');
        $this->db->join('features_type as p ','p.id = u.account_type','LEFT');
		$this->db->where('u.post_time >=',$date);
        $this->db->where('u.user_type !=',1);
        if($limit !=0){
            $this->db->limit($limit);
        }
        $this->db->order_by('u.id','DESC');
        $query = $this->db->get();
        $query = $query->result_array();
        return $query;
    }

	
	/**
	  ** Get all users for expired
	**/
	public function get_all_users($date)
	{
		$this->db->select('u.*');
		$this->db->from('users u');
		$this->db->where('u.is_active',1);
		$this->db->where('u.is_verify',1);
		$this->db->where('u.is_expired',0);
		$this->db->where('u.user_type',0);
		$this->db->where('u.end_date !=','0000-00-00 00:00:00');
		$this->db->where('u.end_date <',$date);
		$this->db->order_by('u.id','DESC');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function get_total_expired_users($date)
	{
		$this->db->select('u.*');
		$this->db->from('users u');
		$this->db->where('u.is_active',1);
		$this->db->where('u.is_verify',1);
		$this->db->where('u.is_expired',0);
		$this->db->where('u.user_type',0);
		$this->db->where('u.end_date !=','0000-00-00 00:00:00');
		$this->db->where('u.end_date <',$date);
		$this->db->order_by('u.id','DESC');
		$query = $this->db->get();
		$query = $query->num_rows();
		return $query;
	}

	public function get_all_active_users()
	{
		$this->db->select('u.*');
		$this->db->from('users u');
		$this->db->where('u.is_active',1);
		$this->db->where('u.is_verify',1);
		$this->db->where('u.is_expired',0);
		$this->db->where('u.user_type',0);
		$this->db->where('u.end_date !=','0000-00-00 00:00:00');
		$this->db->order_by('u.id','DESC');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	/**
	  ***  get total income
	**/ 

	public function get_total_income()
	{
		$this->db->select('pi.*');
		$this->db->select('COUNT(pi.id) as total_transaction');
		$this->db->select_sum('pi.price','total_price');
		$this->db->from('payment_info pi');
		$this->db->group_by('SUBSTRING(created_at,1,7)');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function get_total_user_by_account_type()
	{
		$this->db->select('u.id,u.account_type,ft.type_name,ft.id as type_id');
		$this->db->select('COUNT(u.id) as total_user');
		$this->db->from('users u');
		$this->db->join('features_type ft','ft.id = u.account_type','LEFT');
		$this->db->where('u.account_type !=',0);
		$this->db->order_by('account_type','DESC');
		$this->db->group_by('account_type');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function get_total_income_by_month($month,$type=0)
	{
		$this->db->select('pi.*');
		$this->db->select('COUNT(pi.id) as total_transaction');
		$this->db->select_sum('pi.price','total_price');
		$this->db->from('payment_info pi');
		$this->db->where('SUBSTRING(pi.created_at,1,7)', date('Y').'-'.$month);
		$query = $this->db->get();
		$query = $query->row_array();
		if($type==0):
			return !empty($query['total_price'])?$query['total_price']:0;
		else:
			return !empty($query['total_transaction'])?$query['total_transaction']:0;
		endif;
	}
	/**
	  ** single_appoinment
	**/
	public function single_appoinment($id)
	{
		$this->db->select();
        $this->db->from('appointments');
        $this->db->where('days',$id);
        $this->db->where('user_id',auth('id'));
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}

	public function check_setting_value($data_type)
	{
		$this->db->select();
        $this->db->from('setting');
		$query = $this->db->get();
		$query = $query->row_array();
		return $query[$data_type];
	}





	/**
	  ** single_appoinment
	**/
	public function check_layouts($type,$value)
	{
		$this->db->select();
        $this->db->from('users');
        $this->db->where($type,$value);
        $this->db->where('id',auth('id'));
		$query = $this->db->get();
		if($query->num_rows() ==1){
            return 1;
        }else{
            return 0;
        }

	}


	/**
	  ** Get Features name by type
	**/
	public function get_features_name($slug)
	{
		$this->db->select();
        $this->db->from('features');
        $this->db->where('slug',$slug);
		$query = $this->db->get();
		$query = $query->row_array();
		return $query['features'];

	}

	public function get_banner_img($img)
	{
		$this->db->select();
        $this->db->from('features_images');
        $this->db->where('user_id',auth('id'));
		$query = $this->db->get();
		$query = $query->row_array();
		return $query[$img];

	}
	 public function get_languages_by_slug($slug)
    {
        $this->db->select();
        $this->db->from('languages');
        $this->db->where('slug',$slug);
        $query = $this->db->get();
        $query = $query->row_array();
        return $query;
    }
    

	public function get_language_data($type,$status)
    {
        $this->db->select('l.*');
        $this->db->from('languages l');
        if($status != '*'){
            $this->db->where('l.status',$status);
        }
        $query = $this->db->get();
        $query = $query->result_array();
        foreach ($query as $key => $value) {
            $this->db->select('ld.*');
            $this->db->from('language_data ld');
            $this->db->where('ld.type',$type);
            $query2 = $this->db->get();
            $query2 = $query2->result_array();
            $query[$key]['lang_data'] = $query2;
        }
        return $query;
    }

    public function get_language($status)
    {
        $this->db->select('l.*');
        $this->db->from('languages l');
        if($status != '*'){
            $this->db->where('l.status',$status);
        }
        $query = $this->db->get();
        $query = $query->result_array();
        return $query;
    }


    public function get_all_language_data($per_page,$offset,$total)
    {
      
        $this->db->select('ld.*');
        $this->db->from('language_data ld');
        if(isset($_GET['q']) && !empty($_GET['q'])){
            $this->db->like('english',$_GET['q']);
            $this->db->or_like('keyword',$_GET['q']);
        }
        $this->db->order_by('ld.id','DESC');
        if($total==1):
            $query = $this->db->get();
            $query = $query->num_rows();
        else:
            $query = $this->db->get('',$per_page,$offset);;
            $query = $query->result_array();
        endif;
        
        return $query;
    }

    public function get_cvs_data()
    {
    	$today = today();
    	$this->db->select('c.*');
    	$this->db->from('language_data as c');
    	$query = $this->db->get();
    	$query = $query->result_array();
    	return $query;
    } 


	/**
	  ** Delete appointment
	**/

	function delete_appointment($table)
	{
		$this->db->delete($table,array('user_id'=>auth('id')));
		return 1;
	}

	public function single_select_by_user($id,$table)
	{
		$this->db->select();
		$this->db->from($table);
		$this->db->where('user_id',$id);
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}

	public function single_select_by_id_md5($id,$table)
	{
		$this->db->select();
		$this->db->from($table);
		$this->db->where('md5(id)',$id);
		$query = $this->db->get();
		$query = $query->row();
		return $query;
	}


	public function get_my_vcard($id)
	{
		$this->db->select();
		$this->db->from('users');
		$this->db->where('md5(id)',$id);
		// $this->db->where('ref_id',auth('id'));
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}

	public function get_my_all_vcard($id)
	{
		$this->db->select();
		$this->db->from('users');
		$this->db->where('ref_id',$id);
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}


	public function single_select($table)
	{
		$this->db->select();
		$this->db->from($table);
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}

	
	public function single_select_by_id($id,$table)
	{
		$this->db->select();
		$this->db->from($table);
		$this->db->where('id',$id);
		$query = $this->db->get();
		return $query->row_array();
	}

	public function update($data,$id,$table)
	{
		$this->db->where('id',$id);
		$this->db->update($table,$data);
		return $id;
	}

	public function update_language($data,$id,$table){
		$this->db->where('keyword',$id);
		$this->db->update($table,$data);
		return 1;
	}

	public function api_update($data,$id,$table){
		$this->db->where('active_code',$id);
		$this->db->update($table,$data);
		return $id;
	}


	
	function delete($id,$table)
	{
		$this->db->delete($table,array('id'=>$id));
		return $id;
	}
	
	public function get_empty_admin()
	{
		$this->db->select();
        $this->db->from('users');
        $this->db->where('user_type',1);
		$query = $this->db->get();
		if($query->num_rows() > 0){
            return 1;
        }else{
            return 0;
        }

	}

	public function get_all_users_features_by_id($id)
	{
		$this->db->select('ft.*,u.account_type');
        $this->db->from('features_type ft');
        $this->db->join('users u','u.account_type=ft.id','LEFT');
	    $this->db->where('u.id',$id);
		$query = $this->db->get();
		$query = $query->result_array();
		foreach ($query as $key => $value) {
			$this->db->select('p.*,p.id as pricing_id,f.*,f.id as feature_id');
	        $this->db->from('pricing p');
			$this->db->join('features f','f.id = p.feature_id','RIGHT');
	        $this->db->where('p.type_id',$value['id']);
	        $this->db->order_by('f.id','ASC');
	        $query2 = $this->db->get();
	        $query2 = $query2->result_array();
	        $query[$key]['features'] = $query2;
    	}
		return $query;
	}

	public function check_files($dir)
	{
		$it = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
	    $it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
	    foreach($it as $file) {
	        if ($file->isDir()) rmdir($file->getPathname());
	        else unlink($file->getPathname());
	    }
	    rmdir($dir);
	    return true;
	}


	public function get_users_features(){
		$this->db->select('uf.*,f.features as name,f.slug');
        $this->db->from('users_features uf');
        $this->db->join('features f','f.id=uf.feature_id','LEFT');
	    $this->db->where('f.status',1);
	    $this->db->where('uf.user_id',auth('id'));
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function get_customer_order_list($id){
		$this->db->select('sr.*,s.title,s.price');
        $this->db->from('service_order sr');
        $this->db->join('services s','s.id=sr.service_id','LEFT');
	    $this->db->where('sr.customer_id',$id);
	    $this->db->order_by('sr.id','DESC');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function get_my_customer_order_list(){
		$this->db->select('sr.*,s.title,s.price,c.name,c.phone,c.country_id');
        $this->db->from('service_order sr');
        $this->db->join('services s','s.id=sr.service_id','LEFT');
        $this->db->join('customer_list c','c.id=sr.customer_id','LEFT');
	    $this->db->where('sr.user_id',auth('id'));
	    $this->db->order_by('sr.id','DESC');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function get_my_vcard_info($user_id){
		$this->db->select('sr.*,sr.id as vsite_id,s.*,s.id as site_id');
        $this->db->from('vcard_social_sites sr');
        $this->db->join('social_sites s','s.id=sr.site_id','LEFT');
	    $this->db->where('sr.user_id',$user_id);
	    $this->db->order_by('sr.orders','ASC');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function get_vcard_social_sites($id,$user_id)
	{
		$this->db->select('us.*');
        $this->db->from('vcard_social_sites us');
        $this->db->where('us.site_id',$id);
		$this->db->where('us.user_id',$user_id);
        $this->db->where('us.status',1);
        $this->db->order_by('id','ASC');
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}

	public function get_price_type_by_id($id){
		$this->db->select('ft.*');
        $this->db->from('features_type ft');
	    $this->db->where('ft.id',$id);
	    $this->db->where('ft.status',1);
		$query = $this->db->get();
		$query = $query->row_array();
		return $query;
	}

	public function check_active_features($id,$slug){
		$this->db->select('uf.*,f.features as name,f.slug');
        $this->db->from('users_features uf');
        $this->db->join('features f','f.id=uf.feature_id','LEFT');
	    $this->db->where('f.status',1);
	    $this->db->where('uf.user_id',$id);
	    $this->db->where('f.slug',$slug);
	    $this->db->where('uf.status',1);
		$query = $this->db->get();
		if($query->num_rows() > 0){
		    return ['check'=>1];
		}else{
			return ['check'=>0];
		}
	}

	public function check_available_features($id)
  	{
        $this->db->select('uf.*');
        $this->db->from('users_features uf');
        $this->db->where("uf.user_id",$id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return 1;
        }else{
            return 0;
        }

    }

    /**
      *** check vaild mail for rating
    **/ 
    public function check_my_rating()
    {
        $this->db->select('ur.*');
        $this->db->from('users_rating ur');
        $this->db->where("ur.action_id",auth('id'));
        $this->db->limit(1);
        $query = $this->db->get();
        if($query->num_rows() ==1){
            return 1;
        }else{
            return 0;
        }

    }
	

	public function count_total_user_by_package_id($id)
    {
        $this->db->select('u.*');
        $this->db->from('users as u');
        $this->db->where('u.account_type ',$id);
        $query = $this->db->get();
        $query = $query->num_rows();
        return $query;
    }


    public function count_table_by_user_id($table)
    {
        $this->db->select();
        $this->db->from($table);
        $this->db->where('user_id',auth('id'));
        $query = $this->db->get();
        $query = $query->num_rows();
        return $query;
    }
    public function get_country_info($code)
    {
        $this->db->select();
        $this->db->from('countries');
        $this->db->where('code',$code);
        $query = $this->db->get();
        $query = $query->row();
        return $query;
    }

    public function get_currencies($code)
    {
        $this->db->select('c.*, c.currency_symbol as icon');
        $this->db->from('countries c');
        $this->db->where('c.id',$code);
        $query = $this->db->get();
        $query = $query->row_array();
        return $query;
    }


    public function get_package_info_by_slug($slug)
    {
    	$this->db->select('p.*');
    	$this->db->from('features_type as p');
    	$this->db->where('p.slug',$slug);
    	$this->db->where('p.status',1);
    	$query = $this->db->get();
    	$query = $query->row_array();
    	return $query;
    }

    public function check_coupon_code($code)
    {
    	$today = today();
    	$this->db->select('c.*');
    	$this->db->from('coupon_list as c');
    	$this->db->where('c.start_date <=', $today);
		$this->db->where('c.end_date >=', $today);
    	$this->db->where('c.coupon_code',$code);
    	$this->db->where('c.status',1);
    	$query = $this->db->get();
    	$query = $query->row_array();
    	return $query;
    }   

    public function update_discount($id)
    {
    	 $this->db->set('total_used', 'total_used+1', FALSE);
         $this->db->where('id',$id);
         $this->db->update('coupon_list');
         $this->session->unset_userdata('discount_ss');
         return $id;
    } 


    public function user_info_by_id($id)
	{
		$this->db->select('u.*');
		$this->db->from('users u');
		$this->db->where('u.id',$id);
		$query = $this->db->get();
		$query = $query->row();
		return $query;
	} 

	 public function count_by_vcard($id)
	{
		$this->db->select('u.*');
		$this->db->from('users u');
		$this->db->where('u.ref_id',$id);
		$query = $this->db->get();
		$query = $query->num_rows();
		return $query;
	} 

}